PROJECT REPORT-Moving Object Tracking
PROJECT REPORT-Moving Object Tracking
CONTENTS
I Abstract 1
1. CHAPTER-1 INTRODUCTION
1.1 Motivation 2
1.2 Objective and Scope 2
1.3 Problem Definition and Challenges 6
1.3.1 Assumptions 7
1.4 Contribution of the Thesis 8
1.4.1 Saliency Detection based on Low-level Perceptual cues 8
1.4.2 Salient Object Segmentation in Natural Images 10
2. CHAPTER-2 SEGMENTATION
2.1 Introduction 12
2.2 Segmentation using discontinuities 14
2.3 Edge Detection 15
2.4 Segmentation using thresholding 27
Currently, both the market and the academic communities have required
applications based on image and video processing with several real-time
constraints. On the other hand, detection of moving objects is a very
important task in mobile robotics and surveillance applications. This
project proposes an efficient technique for moving object detection and
it’s velocity determination based on background subtraction using
dynamic threshold and morphological process. These methods are used
effectively for object detection and most of the previous methods depend
on the assumption that the background is static over short time periods. In
dynamic threshold based object detection, morphological process and
filtering also used effectively for unwanted pixel removal from the
background.
-1-
CHAPTER 1
INTRODUCTION
1.1 Motivation
The motivation behind this project is to develop software for tracking, the
major application in security, surveillance and vision analysis. The
developed software must be capable of tracking any single object moving
in the frame and to simulate on software. This system might be useful for
extending in real-time surveillance or object classification.
1.2 Objective
-2-
1.1 Motivation:
What is Saliency?
-3-
Figure 1.1: The top row shows an example of saliency map generated
from the image (left) and the bottom row depicts an ideal segmentation
of the object in the image (left)
We propose two deferent methods for the task. First is based on low-
level perceptual features. The second combines the low-level saliency
with generic object specific cues in a graphical model-based approach.
-4-
-5-
The problem we address in the thesis is: Given a natural scene, detect
one or more regions of interest (ROI) which contain the salient objects
in a scene. The method must be unsupervised with no training sample
for classes of objects available. Parameters of an optimization function
may be learned using a part of another dataset. Although the problem is
similar to unsupervised foreground segmentation, it differs in the
context of features which is mostly inspired by biological motivation.
Some examples of finding objects of interest are presented in Figure
1.1.
-6-
1.3.1 Assumptions
1. The images are indoor or outdoor natural scenes, captured using the
optical camera (not X-ray or infrared etc.).
2. The object of interest is not just visible in few pixels on the boundary
of the image.
-7-
-8-
The first two terms named above are based on the rarity of feature and
the third terms correspond to boundary prior. The idea of boundary
prior is that the boundary of an image mostly contains background
image elements or superpixels and background superpixels are spatially
connected among themselves, but not with foreground ones.
On the other hand, spatial compactness takes into account that a colour
belonging to a salient object would be grouped at a spatial location and
thus the spatial variance of the colour would be low. Whereas,
background colours are generally distributed over the whole image and
score low on spatial compactness.
-9-
CHAPTER 2
SEGMENTATION
Fig 2.1
-12-
-13-
Several techniques for detecting the three basic gray level discontinuities
in a digital image are points, lines and edges. The most common way to
look for discontinuities is by spatial filtering methods.
w1=w2=w3=w4=w6=w7=w8=w9 =-1, w5 = 8.
Based on the response calculated from the above equation we can find out
the points desired.
Line detection is next level of complexity to point detection and the lines
could be vertical, horizontal or at +/- 45 degree angle.
Fig 2.3
-14-
Responses are calculated for each of the mask above and based on the
value we can detect if the lines and their orientation.
The edges of an image hold much information in that image. The edges
tell where objects are, their shape and size, and something about their
texture. An edge is where the intensity of an image moves from a low
value to a high value or vice versa.
There are numerous applications for edge detection, which is often used
for various special effects. Digital artists use it to create dazzling image
outlines. The output of an edge detector can be added back to an original
image to enhance the edges.
-15-
The user may then edit the color of the sky without worrying about
altering the color of the mountains or whatever else may be in the image.
Edge detection is also used in image registration. Image registration
aligns two images that may have been acquired at separate times or from
different sensors.
The simplest and quickest edge detectors determine the maximum value
from a series of pixel subtractions. The homogeneity operator subtracts
each 8 surrounding pixels from the center pixel of a 3 x 3 window as in
Figure 2.5. The output of the operator is the maximum of the absolute
value of each difference.
-16-
Figure 2.5 How the homogeneity operator works.
-17-
new pixel = maximum{½ 11-11½ , ½ 13-12½ , ½ 15-16½ , ½ 11-16½ } =
5
Likewise
–1 1
-18-
It is also to divide the Y_difference by the X_difference and identify a
gradient direction (the angle of the edge between the regions)
In real image, the lines are rarely so well defined, more often the change
between regions is gradual and noisy.
-19-
Sobel edge detection
The Sobel operator is more sensitive to diagonal edges than vertical and
horizontal edges. The Sobel 3 x 3 templates are normally given as
X-direction
Y-direction
Original image
absA + absB
-20-
Threshold at 12
The Roberts operator has a smaller effective area than the other mask,
making it more susceptible to noise.
-21-
pixels wide, it is difficult to locate the centers of the edges. It becomes
necessary to employ a process called thinning to reduce the edge width to
one pixel. Second order derivative edge detectors provide better edge
localization.
The basic Sobel vertical edge operator (as described above) will yield a
value right across the image. For example if
Implementing the same template on this "all eight image" would yield
3x-2.
-22-
Images often come with a gray level "trend" on them, i.e. one side of a
regions is lighter than the other, but there is no "edge" to be discovered in
the region, the shading is even, indicating a light source that is stronger at
one end, or a gradual colour change over the surface.
or
Due to its shape, the function is also called the Mexican hat filter. Figure
2.7 shows the cross section of the LoG edge operator with different
values of s. The wider the function, the wider the edge that will be
detected. A narrow function will detect sharp edges and more detail.
-24-
The greater the value of s, the wider the convolution mask necessary. The
Most edge detector masks are seldom greater than 7 x 7. Due to the shape
of the LoG operator, it requires much larger mask sizes. The initial work
in developing the LoG operator was done with a mask size of 35 x 35.
One advantage of the DoG is the ability to specify the width of edges to
detect by varying the values of s1 and s2. Here are a couple of sample
masks. The 9 x 9 mask will detect wider edges than the 7x7 mask.
Where global thresholding is considering the function for the entire image
and local thresholding involving only a certain region. In addition to the
above mentioned techniques that if the thresholding function T depends
on the spatial coordinates then it is known as the dynamic or adaptive
thresholding.
-27-
-28-
2) Segment the image using T. This will produce two groups of pixels.
G1 consisting of all pixels with gray level values >T and G2 consisting of
pixels with values <=T.
3) Compute the average gray level values mean1 and mean2 for the pixels
in regions G1 and G2.
CHAPTER 3
RELATED WORK
3.1 Pre-processing
Example of Median
The median filter sorts the elements in a given matrix and median value is
assigned to the central pixel. Sorted elements 1, 2, 3, 4, 5, 6, 7, 8, 9 and
median 5 will assign to the central element. Similar box scan is
-30-
performed over the whole image and reduces noise. Execution time is
more compared to mean filter,as the algorithm involves with sorting
techniques. But it removes the small pixel noise.
The output of the change detection module is the binary image that
contains only two labels, i.e., „0‟ and „255‟, representing as
„background‟ and „foreground‟ pixels respectively, with some noise. The
goal of the connected component analysis is to detect the large sized
connected foreground region or object. This is one of the important
operations in motion detection. The pixels that are collectively connected
can be clustered into moving objects by analysing their connectivity.
-31-
-32-
-33-
-34-
-35-
model, but since values of pixel models are replaced according to a first-
in first-out update policy.
In some cases, distance of the moving object also forms a basis for it to
be considered a background, e.g. if in a scene one person is close to the
camera while there is a person far away in background, in this case the
nearby person is considered as foreground while the person far away is
ignored due to its small size and the lack of information that it provides.
Identifying moving objects from a video sequence is a fundamental and
critical task in many computer-vision applications. A common approach
is to perform background subtraction, which identifies moving objects
from the portion of video frame that differs from the background model.
Fig.3.2 Flow Chart for background subtraction algorithm.
-37-
In this a set of frames (previous frames) are taken and the calculation is
done for separation. The separation is performed by calculating the mean
and variance at each pixel position. If we take N frames with pixel value
P and intensity I.
Mean, µ= (1)
Variance,
σ= (2)
-38-
-39-
objects in the frame. Edges are formed where there is a sharp change in
the intensity of images. If there is an object, the pixel positions of the
object boundary are stored and in the next sequence of frames this
position is verified. Corner based algorithm uses the pixel position of
edges for defining and tracking of objects.
Images are often only interested in certain parts. These parts are often
referred to as goals or foreground (as other parts of the background). In
order to identify and analyse the target in the image, we need to isolate
-40-
them from the image. The Image segmentation refers to the image is
divided into regions,each with characteristics and to extract the target of
interest in processes. The image segmentation used in this paper is
threshold segmentation. To put it simply, the threshold of the gray-scale
image segmentation is to identify a range in the image of the gray-scale
threshold, and then all image pixels gray values are compared with the
threshold and according to the results to the corresponding pixel is
divided into two categories: the foreground of, or background. The
simplest case, the image after the single-threshold segmentation can be
defined as: Threshold segmentation has two main steps:
l) Determine the threshold T.
2) Pixel value will be compared with the threshold value T.
In the above steps to determine the threshold value is the most critical
step in partition. In the threshold selection, there is a best threshold based
on different goals of image segmentation.
If we can determine an appropriate threshold, we can correct the image
for segmentation.
-41-
CHAPTER 4
4.3 Tracking
-42-
4.4 Distance
4.5 Velocity
-43-
Fig.4.1Flow chart of object velocity determination
Object tracking, the main application for security, surveillance and vision
analysis. In this, a video is recorded using digital camera. The recorded
video frames are converted into individual frames. Noise is removed for
the imported images using median filter. The filtered images are used as
input for the frame difference for the separation of foreground and
background objects. A rectangular bounding box is plotted around the
foreground objects produced from frame difference and frame
subtraction.
-44-
Input video dividing into frames Noise removal Background subtraction
algorithm Object detection Object bounding box Calculation of distance
travelled Velocity Determination Input video Frame difference Frame
separation Dynamic Threshold Object detection Figure 4.2 shows the step
by step process of tracking moving object detection. From this figureit is
clear that the inputs and the output produced at each step.
-45-
CHAPTER 5
CODING
clc;
close all;
clear all;
%video loading
[f,p]=uigetfile('.avi');
T=strcat(p,f);
Vid=VideoReader(T);
%number of frames
n=Vid.NumberOfFrames;
disp('number of frames...');
disp(n);
H=Vid.Height;
W=Vid.Width;
disp(H);
disp(W);
Framerate=Vid.FrameRate;
disp(Framerate);
-46-
%region of interest points%
p2 = [70,600];
p3= [70,25];
q3 = [250,300];
q2 = [250,10];
alpha=0.1;
f=read(Vid,1);
reader = vision.VideoFileReader(T);
'MinimumBlobArea',400);
ii = 1;
while ~isDone(reader)
frame =reader.step();
fgMask = detector.step(frame);
-47-
mask = imfill(mask, 'holes');
[~,centroid,bbox] = blob.step(fgMask);
arr = [];
arr = bbox;
a1{ii,1} = arr;
ii = ii+1;
[~,centroids,bboxs] = blob.step(mask);
out1 = insertShape(double(mask),'Rectangle',bboxs,'Color','green');
step(vid_frame, out);
step(vid_mask, out1);
% % Background Subtraction
end
temp=0;
%%%%%%%%%%%%%%%
for i = 1:n
frame=read(Vid,i);
F1=((1-alpha).*f)+(alpha.*frame);% BS formula
FF=frame-F1;
temp=(temp+FF)./i;
-48-
bw=im2bw(FF,0.1);
out=bwareaopen(bw,1000);
out=bwlabel(out,8);
figure(1),imshow(out)
V(:,:,:,i)=out;
end
T1=V(:,:,:,1);
for i=2:n
T2=V(:,:,:,i);
DD(i)=dist(T1(:)',T2(:));
end
D1=mean(DD);
vl=(mean(DD)/Framerate);
-49-
RESULTS:
Before Filtering
-50-
After Filtering
Object Tracking
-51-
Object Tracking
-52-
CHAPTER 6
-53-
REFERENCES
[1] Lingfei Meng, Student Member, IEEE, and John P. Kerekes, Senior
Member, IEEE,” Object Tracking Using High Resolution Satellite
Imagery”. IEEE Journal of selected topics in applied earth observations
and remote sensing,2012 vol.5.
-54-
[8] S. Zhang, H. Yao, S. Liu, X. Chen, and W. Gao, “A covariance-based
method for dynamic background subtraction,” in Proc. IEEE Int. Conf.
Pattern Recognition (ICPR), Dec. 2008.
-55-
APPENDIX
MATLAB
INTRODUCTION
-56-
A.2 Basic Building Blocks of MATLAB
MATLAB Window
The command window is where the user types MATLAB commands and
expressions at the prompt (>>) and where the output of those commands
is displayed. It is opened when the application program is launched. All
commands including user-written programs are typed in this window at
MATLAB prompt for execution.
MATLAB defines the workspace as the set of variables that the user
creates in a work session. The workspace browser shows these variables
and some information about them. Double clicking on a variable in the
workspace browser launches the Array Editor, which can be used to
obtain information.
The current Directory tab shows the contents of the current directory,
whose path is shown in the current directory window. For example, in the
windows operating system the path might be as follows:
-57-
C:\MATLAB\Work, indicating that directory “work” is a subdirectory of
the main directory “MATLAB”; which is installed in drive C. Clicking
on the arrow in the current directory window shows a list of recently used
paths. MATLAB uses a search path to find M-files and other MATLAB
related files. Any file run in MATLAB must reside in the current
directory or in a directory that is on search path.
The MATLAB editor is both a text editor specialized for creating M-files
and a graphical MATLAB debugger. The editor can appear in a window
by itself, or it can be a sub window in the desktop. In this window one
can write, edit, create and save programs in files called M-files.
MATLAB editor window has numerous pull-down menus for tasks such
as saving, viewing, and debugging files. Because it performs some simple
checks and also uses color to differentiate between various elements of
code, this text editor is recommended as the tool of choice for writing and
editing M-functions.
-58-
A.2.1.6 Graphics or Figure Window
MATLAB provides online help for all it’s built in functions and
programming language constructs. The principal way to get help online is
to use the MATLAB help browser, opened as a separate window either
by clicking on the question mark symbol (?) on the desktop toolbar, or by
typing help browser at the prompt in the command window. The help
Browser is a web browser integrated into the MATLAB desktop that
displays a Hypertext Markup Language (HTML) documents. The Help
Browser consists of two panes, the help navigator pane, used to find
information, and the display pane, used to view the information. Self-
explanatory tabs other than navigator pane are used to perform a search.
MATLAB has three types of files for storing information. They are: M-
files and MAT-files.
A.3.1 M-Files
These are standard ASCII text file with ‘m’ extension to the file name
and creating own matrices using M-files, which are text files containing
MATLAB code. MATLAB editor or another text editor is used to create
a file containing the same statements which are typed at the MATLAB
command line and save the file under a name that ends in .m.
There are two types of M-files:
-59-
1. Script Files
2. Function Files
A function file is also an M-file except that the variables in a function file
are all local. This type of files begins with a function definition line.
A.3.2 MAT-Files
These are binary data files with .mat extension to the file that are created
by MATLAB when the data is saved. The data written in a special format
that only MATLAB can read. These are located into MATLAB with
‘load’ command.
This is the set of tools and facilities that help you use MATLAB
functions and files. Many of these tools are graphical user interfaces. It
includes the MATLAB desktop and Command Window, a command
history, an editor and debugger, and browsers for viewing help, the
workspace, files, and the search path.
-60-
elementary functions like sum, sine, cosine, and complex arithmetic, to
more sophisticated functions like matrix inverse, matrix eigen values,
Bessel functions, and fast Fourier transforms.
A.4.4 Graphics:
This is a library that allows you to write C and FORTRAN programs that
interact with MATLAB. It includes facilities for calling routines from
MATLAB (dynamic linking), calling MATLAB as a computational
engine, and for reading and writing MAT-files.
-61-
A.5 SOME BASIC COMMANDS:
hold on holds the current plot and all axis properties so that subsequent
graphing
hold off sets the next plot property of the current axes to "replace"
statement is:
-62-
for n=1:cc/c;
magn(n,1)=NaNmean(a((n-1)*c+1:n*c,1));
end
save saves all the matrices defined in the current session into the file,
save filename x y z saves the matrices x, y and z into the file titled
filename.mat
save filename x y z /ascii save the matrices x, y and z into the file titled
filename.dat
-63-
load filename.dat loads the contents of filename.dat into the
variable filename
plot
Kinds of plots:
polar(theta,r) creates a polar plot of the vectors r & theta where theta is in
radians
-64-
bar(x, y) creates a bar-graph of the elements of the vector y,
locating the bars
Plot description:
Scalar Calculations:
+ Addition
- Subtraction
* Multiplication
-65-
\ left division (a\b means b ÷ a)
^ Exponentiation
-66-
The command window is where the user types MATLAB commands and
expressions at the prompt (>>) and where the output of those commands
is displayed. MATLAB defines the workspace as the set of variables that
the user creates in a work session.
The current Directory tab above the workspace tab shows the contents of
the current directory, whose path is shown in the current directory
window. For example, in the windows operating system the path might be
as follows: C:\MATLAB\Work, indicating that directory “work” is a
subdirectory of the main directory “MATLAB”; WHICH IS
INSTALLED IN DRIVE C. clicking on the arrow in the current directory
window shows a list of recently used paths. Clicking on the button to the
right of the window allows the user to change the current directory.
MATLAB uses a search path to find M-files and other MATLAB related
files, which are organize in directories in the computer file system. Any
file run in MATLAB must reside in the current directory or in a directory
that is on search path. By default, the files supplied with MATLAB and
math works toolboxes are included in the search path. The easiest way
to see which directories are soon the search path, or to add or modify a
search path, is to select set path from the File menu the desktop, and then
use the set path dialog box. It is good practice to add any commonly used
directories to the search path to avoid repeatedly having the change the
current director.
-67-
The Command History Window contains a record of the commands a
user has entered in the command window, including both current and
previous MATLAB sessions. Previously entered MATLAB commands
can be selected and re-executed from the command history window by
right clickingon a command or sequence of commands.
The MATLAB editor is both a text editor specialized for creating M-files
and a graphical MATLAB debugger. The editor can appear in a window
by itself, or it can be a sub window in the desktop. M-files are denoted by
the extension .m, as in pixelup.m.
The MATLAB editor window has numerous pull-down menus for tasks
such as saving, viewing, and debugging files. Because it performs some
simple checks and also uses color to differentiate between various
elements of code, this text editor is recommended as the tool of choice for
writing and editing M-functions.
To open the editor , type edit at the prompt opens the M-file filename.m
in an editor window, ready for editing. As noted earlier, the file must be
in the current directory, or in a directory in the search path
-68-
A.8.3 Getting Help:
The principal way to get help online is to use the MATLAB help
browser, opened as a separate window either by clicking on the question
mark symbol (?) on the desktop toolbar, or by typing help browser at the
prompt in the command window. The help Browser is a web browser
integrated into the MATLAB desktop that displays a Hypertext Markup
Language(HTML) documents. The Help Browser consists of two panes,
the help navigator pane, used to find information, and the display pane,
used to view the information. Self-explanatory tabs other than navigator
pane are used to perform a search.
-69-
Appendix B
What is DIP?
-70-
processing at one end to complete vision at the other. However, one
useful paradigm is to consider three types of computerized processes in
this continuum: low-, mid-, & high-level processes. Low-level process
involves primitive operations such as image processing to reduce noise,
contrast enhancement & image sharpening. A low- level process is
characterized by the fact that both its inputs & outputs are images. Mid-
level process on images involves tasks such as segmentation, description
of that object to reduce them to a form suitable for computer processing
& classification of individual objects. A mid-level process is
characterized by the fact that its inputs generally are images but its
outputs are attributes extracted from those images. Finally higher- level
processing involves “Making sense” of an ensemble of recognized
objects, as in image analysis & at the far end of the continuum
performing the cognitive functions normally associated with human
vision.
I(x, y) is the intensity of the image at the point (x, y) on the image plane.
-71-
I (xylem)takes non-negative values assume the image is bounded by
arectangle[0, a] [0, b]I: [0, a] [0, b] [0, info)
Color image:
-72-
The coordinate convention used in the toolbox to denote arrays is
different from the preceding paragraph in two minor ways. First, instead
of using (xylem) the toolbox uses the notation (race) to indicate rows and
columns. Note, however, that the order of coordinates is the same as the
order discussed in the previous paragraph, in the sense that the first
element of a coordinate topples, (alb), refers to a row and the second to a
column. The other difference is that the origin of the coordinate system is
at (r, c) = (1, 1); thus, r ranges from 1 to M and c from 1 to N in integer
increments. IPT documentation refers to the coordinates. Less frequently
the toolbox also employs another coordinate convention called spatial
coordinates which uses x to refer to columns and y to refers to rows. This
is the opposite of our use of variables x and y.
f (xylem) = . . .
. . .
f (M-1, 0) f (M-1, 1) ………… f (M-1, N-1)
The right side of this equation is a digital image by definition. Each
element of this array is called an image element, picture element, pixel or
pel. The terms image and pixel are used throughout the rest of our
discussions to denote a digital image and its elements.
-73-
A digital image can be represented naturally as a MATLAB matrix:
f (1, 1) f (1, 2) ……. f (1, N)
f (2, 1) f (2, 2) …….. f (2, N)
. . .
f = . . .
f (M, 1) f (M, 2) …….f (M, N)
Where f (1, 1) = f (0, 0) (note the use of a monoscope font to
denote MATLAB quantities). Clearly the two representations are
identical, except for the shift in origin. The notation f (p, q) denotes the
element located in row p and the column q. For example f (6, 2) is the
element in the sixth row and second column of the matrix f. typically we
use the letters M and N respectively to denote the number of rows and
columns in a matrix. A 1xN matrix is called a row vector whereas an
Mx1 matrix is called a column vector. A 1x1 matrix is a scalar.
Matrices in MATLAB are stored in variables with names such as A,
a, RGB, real array and so on. Variables must begin with a letter and
contain only letters, numerals and underscores. As noted in the previous
paragraph, all MATLAB quantities are written using monoscope
characters. We use conventional Roman, italic notation such as f(x, y),
for mathematical expressions
A.B.4 Reading Images:
Images are read into the MATLAB environment using function
imread whose syntax is
imread(‘filename’)
-74-
JPEG Joint Photograph Experts Group .jpg, .jpeg
GIF Graphics Interchange Format .gif
BMP Windows Bitmap .bmp
PNG Portable Network Graphics .png
XWD X Window Dump .xwd
-75-
MATLAB desktop toolbar displays MATLAB’s current working
directory and provides a simple, manual way to change it. Above table
lists some of the most of the popular image/graphics formats supported
by imread and imwrite.
Function size gives the row and column dimensions of an image:
>> size (f)
ans = 1024 * 1024
This function is particularly useful in programming when used in the
following form to determine automatically the size of an image:
>>[M,N]=size(f);
This syntax returns the number of rows(M) and columns(N) in the
image.
The whole function displays additional information about an array.
For instance ,the statement
>>whos f
gives
Name size Bytes Class
F 1024*1024 1048576 unit8 array
Grand total is 1048576 elements using 1048576 bytes
The unit8 entry shown refers to one of several MATLAB data
classes. A semicolon at the end of a whose line has no effect ,so normally
one is not used.
A.B.5 Displaying Images:
Images are displayed on the MATLAB desktop using function
imshow, which has the basic syntax:
imshow(f,g)
Where f is an image array, and g is the number of intensity levels
-76-
used to display it. If g is omitted ,it defaults to 256 levels .using the
syntax
Imshow (f, {low high})
Displays as black all values less than or equal to low and as white
all values greater than or equal to high. The values in between are
displayed as intermediate intensity values using the default number of
levels .Finally the syntax
Imshow(f,[ ])
Sets variable low to the minimum value of array f and high to its
maximum value. This form of imshow is useful for displaying images
that have a low dynamic range or that have positive and negative values.
Function pixval is used frequently to display the intensity values of
individual pixels interactively. This function displays a cursor overlaid on
an image. As the cursor is moved over the image with the mouse the
coordinates of the cursor position and the corresponding intensity values
are shown on a display that appears below the figure window .When
working with color images, the coordinates as well as the red, green and
blue components are displayed. If the left button on the mouse is clicked
and then held pressed, pixval displays the Euclidean distance between the
initial and current cursor locations.
The syntax form of interest here is Pixval which shows the cursor
on the last image displayed. Clicking the X button on the cursor window
turns it off.
The following statements read from disk an image called
rose_512.tif extract basic information about the image and display it
using imshow :
>>f=imread(‘rose_512.tif’);
-77-
Name Size Bytes Class
F 512*512 262144 unit8 array
Grand total is 262144 elements using 262144 bytes
>>imshow(f)
A semicolon at the end of an imshow line has no effect, so normally
one is not used. If another image,g, is displayed using imshow,
MATLAB replaces the image in the screen with the new image. To keep
the first image and output a second image, we use function figure as
follows:
>>figure ,imshow(g)
Using the statement
>>imshow(f),figure ,imshow(g) displays both images.
Note that more than one command can be written on a line ,as long
as different commands are properly delimited by commas or semicolons.
As mentioned earlier, a semicolon is used whenever it is desired to
suppress screen outputs from a command line.
Suppose that we have just read an image h and find that using
imshow produces the image. It is clear that this image has a low dynamic
range, which can be remedied for display purposes by using the
statement.
>>imshow(h,[ ])
A.B.6 WRITING IMAGES:
Images are written to disk using function imwrite, which has the
following basic syntax:
Imwrite (f,’filename’)
With this syntax, the string contained in filename must include a
-78-
recognized file format extension .Alternatively, the desired format can be
specified explicitly with a third input argument.
>>imwrite(f,’patient10_run1’,’tif’)
Or alternatively
For example the following command writes f to a TIFF file named
patient10_run1:
>>imwrite(f,’patient10_run1.tif’)
If filename contains no path information, then imwrite saves the file
in the current working directory.
The imwrite function can have other parameters depending on e file
format selected. Most of the work in the following deals either with JPEG
or TIFF images ,so we focus attention here on these two formats.
-79-
For example,
>>imfinfo bubbles25.jpg
outputs the following information(note that some fields contain no
information in this case):
Filename: ‘bubbles25.jpg’
FileModDate: ’04-jan-2003 12:31:26’
FileSize: 13849
Format: ‘jpg’
Format Version: ‘ ‘
Width: 714
Height: 682
Bit Depth: 8
Color Depth: ‘grayscale’
Format Signature: ‘ ‘
Comment: { }
Where file size is in bytes. The number of bytes in the original
image is corrupted simply by multiplying width by height by bit depth
and dividing the result by 8. The result is 486948.Dividing this file size
gives the compression ratio:(486948/13849)=35.16.This compression
ratio was achieved. While maintaining image quality consistent with the
requirements of the appearance. In addition to the obvious advantages in
storage space, this reduction allows the transmission of approximately 35
times the amount of un compressed data per unit time.
The information fields displayed by imfinfo can be captured in to a
so called structure variable that can be for subsequent computations.
Using the receding an example and assigning the name K to the structure
variable.
-80-
We use the syntax >>K=imfinfo(‘bubbles25.jpg’);
To store in to variable K all the information generated by command
imfinfo, the information generated by imfinfo is appended to the structure
variable by means of fields, separated from K by a dot. For example, the
image height and width are now stored in structure fields K. Height and
K. width.
As an illustration, consider the following use of structure variable K to
commute the compression ratio for bubbles25.jpg:
>> K=imfinfo(‘bubbles25.jpg’);
>> image_ bytes =K.Width* K.Height* K.Bit Depth /8;
>> Compressed_ bytes = K.FilesSize;
>> Compression_ ratio=35.162
Note that iminfo was used in two different ways. The first was t
type imfinfobubbles25.jpg at the prompt, which resulted in the
information being displayed on the screen. The second was to type
K=imfinfo (‘bubbles25.jpg’),which resulted in the information generated
by imfinfo being stored in K. These two different ways of calling imfinfo
are an example of command_ function duality, an important concept that
is explained in more detail in the MATLAB online documentation.
More general imwrite syntax applicable only to tifimages has the
form
Imwrite(g,’filename.tif’,’compression’,’parameter’,….’resloution’,[colres
rowers] )
-81-
ccitt compression. (the default for binary images).The 1*2 array [colres
rowers]
Contains two integers that give the column resolution and row resolution
in dot per_ unit (the default values). For example, if the image
dimensions are in inches, colres is in the number of dots(pixels)per inch
(dpi) in the vertical direction and similarly for rowers in the horizontal
direction. Specifying the resolution by single scalar, res is equivalent to
writing [res res].
>>imwrite(f,’sf.tif’,’compression’,’none’,’resolution’,……………..[300
300])
>> res=round(200*2.25/1.5);
>>imwrite(f,’sf.tif’,’compression’,’none’,’resolution’,res)
-82-
of a figure window can be exported to disk in two ways. The first is to
use the file pull-down menu is in the figure window and then choose
export. With this option the user can select a location, filename, and
format. More control over export parameters is obtained by using print
command:
Print-fno-dfileformat-rresno filename
Where no refers to the figure number in the figure window interest, file
format refers one of the file formats in table above. ‘resno’ is the
resolution in dpi, and filename is the name we wish to assign the file.
If we simply type print at the prompt, MATLAB prints (to the default
printer) the contents of the last figure window displayed. It is possible
also to specify other options with print, such as specific printing device.
-83-