Binary images

advertisement
EE 454 Image Processing Project
E E E 4 5 4 : D i g i t a l F i l te r s
and Systems
Image Processing with
Ma t l a b
In this section you will learn



How to use Matlab and the Image Processing Toolbox to work with images.
Scilab and Scicoslab as open source alternatives to Matlab.
How to filter an image using these programs.
Installation
If you have access to Matlab, with the image processing toolbox, then use that. If you
don't, then you can download the latest Scilab from the website http://www.scilab.org/.
After installing scilab, install the vision and image processing toolbox, available from
the website: http://sivp.sourceforge.net/. Follow the instructions in the text file INSTALL,
and you should have the toolbox working. Next you need to quit Scilab and restart it. If
you have successfully installed the image and vision processing toolbox, you should see
it under the menu Toolboxes when you restart. Documentation about the toolbox and
about scilab commands is available from the help menu. There is also a file
http://sivp.sourceforge.net/doc.php, download SIVP Doc for Scilab Contest 2006 in
English.
Binary images
In matlab, binary images can be represented by an array of integers. Consider the
following code:
image1 = zeros(100,100);
Page 1
Sample Manual Template
apter 1: Sample Template
image1(1:10,:) = 1;
image1(21:30,:) = 1;
image1(41:50,:) = 1;
image1(61:70,:) = 1;
image1(81:90,:) = 1;
imshow(image1);
If you copy and paste this, you will see a black and white image.
Greyscale images
Now try the following code:
image1 = zeros(100,100);
image1(1:10,:) = 0.5;
image1(21:30,:) = 1;
image1(41:50,:) = 0.5;
image1(61:70,:) = 1;
image1(81:90,:) = 0.5;
imshow(image1);
You will see that the result is quite different, in greyscale images 1 corresponds to
white, 0 to black and 0.5 to a grey level between the two.
Reading images from file
You can read the following images from file (make sure you download them to your
working directory in Matlab).
binary = imread('Binary.png');
gray = imread('Gray.png');
rgb = imread('Rgb.png');
Now let's see what these images look like:
imshow(binary);
imshow(gray);
imshow(rgb);
To see more information about the variables, use the command whos
>> whos
Name
binary
gray
rgb
Size
372x316
400x316
426x480x3
Bytes
117552
126400
613440
Page 2
Sample Manual Template
Class
logical
uint8
uint8
Attributes
EE 454 Image Processing Project
The whos command will work on Scilab, too.
The variable binary will hold 1s and 0s, it is a 400 by 316 matrix, so its data is stored in
logical (0/1) format. Both the variables gray and rgb hold data in 8 bit unsigned integer
format, so they can produce values between 0 and 28-1=255. The elements of the
matrix gray reflect the image intensity at each point. The matrix rgb contains 3 pages
containing the red, green and blue intensity of the image at each pixel, its size is 426 by
480 by 3. Just for fun, we can plot each separately:
imshow([rgb(:,:,1),rgb(:,:,2),rgb(:,:,3)])
As you can see the image contains a lot of red but not much blue.
You can also visualize the intensities by plotting them in three dimensions:
surf(double(imresize(rgb(:,:,2),[50 50])))
In both Scilab and Matlab you can rotate this image using one of the buttons on the
figure window, try it.
Thresholding
One very easy image processing trick is thresholding. Try the following code:
[i1 i2] = find(gray < 60);
gray2 = gray;
gray2(i1+(i2-1)*400) = 0;
Now let us try to save this image to file:
imwrite(gray2, 'threshold.png');
You can see that the file is written.
Sometimes we want to reduce a figure to just two gray levels: one belonging to an
object in the image, the other to the background of the image. To do this, in Matlab
there is a function called graythresh.
It is easy to do this, suppose the threshold we choose T=60.
T = 60;
[i1 i2] = find(gray < T);
L1 = mean(mean(gray2< T));
L2 = mean(mean(gray2>= T));
gray2 = gray;
gray2 = L2;
gray2(i1+(i2-1)*400) = L1;
Page 3
Sample Manual Template
apter 1: Sample Template
But how do we find the best threshold which separates the background from the
object? Gonzalez and Woods suggested this method:
1. Choose a threshold arbitrarily.
2. Threshold the image using it.
3. Compute the mean grey value of the pixels with intensities above and below the
threshold, and then compute the average of these two values.
4. Use the new value to rethreshold the image.
5. Repeat steps 3 and 4 until the threshold changes by an insignificant amount.
Otsu found a "best" method (which maximizes the difference between the two
"classes"), Matlab implements this method.
>> T = graythresh(A);
Histogram
The histogram of an image measures the number of pixels with a given grey or colour
value. To draw the histogram of an image, we may need to reshape the matrix into a
very long vector first. The commands to do this are different in Matlab and in Scilab.
In Matlab you type the following
[x y z] = size(rgb);
rgb_vector = reshape(rgb, x*y,1);
hist(double(rgb_vector), 50);
whereas in Scilab, you will need to type
[x y z] = size(rgb);
rgb_vector = matrix(rgb, x*y,1);
histplot(50, double(rgb_vector))
Filtering
Filtering in 2-D images is multiplying and adding, just as with convolution for 1-D
signals. The main difference is that in image processing we mostly use symmetric filters,
so that the mirror flipping property of the convolution just doesn't happen.
This is how the convolution works:
Page 4
Sample Manual Template
EE 454 Image Processing Project
The figure is from the following:
http://www.s2.chalmers.se/undergraduate/courses0203/ess060/PDFdocuments/ForScre
en/Notes/Convolution.pdf
Some Special Filters
The Sobel Filter is given by the matrix
>> F=fspecial('sobel')
F
=
1.
0.
- 1.
2.
0.
- 2.
1.
0.
- 1.
You can see that this is a filter that would be good for horizontal edge detection: if the
result is 0 then the two ends of the filter are looking at a stretch of material the same
color, otherwise the difference tells you how different the two pixels are.
Try applying this filter to the gray image:
>> F=fspecial('sobel')
>> gray_edge = filter2(F, gray);
>> imshow(gray_edge)
Do you think this would work better if we low pass filtered or thresholded the image
before filtering?
More filters (also known as convolution kernels for images)
Page 5
Sample Manual Template
apter 1: Sample Template
Arithmetic mean filter (smoothing)
>>fspecial('average')
Laplacian (enhance edges)
>>fspecial('laplacian')
0.11
0.11
0.11
0.11
0.11
0.11
0.11
0.11
0.11
-0.17 -0.67 -0.17
-0.67 3.33 -0.67
-0.17 -0.67 -0.17
Sharpening filter
>>fspecial('unsharp')
-0.17 -0.67 -0.17
-0.67 4.33 -0.67
-0.17 -0.67 -0.17
Gaussian filter (smoothing)
>>fspecial('gaussian')
0.01 0.08 0.01
0.08 0.62 0.08
0.01 0.08 0.01
Sobel operators (edge detection in x and y 1
2
directions)
1
>>fspecial('sobel')
0
0
0
-1
-2
-1
1
0
-1
2
0
-2
1
0
-1
>>fspecial('sobel')’
Other special functions
In Scilab and Matlab:
Convert image to grayscale
>>Igray=rgb2gray(I);
Resize image
>>Ismall=imresize(I,[100 100], 'bilinear');
In Matlab only:
Rotate image
>>I90=imrotate(I,90);
Page 6
Sample Manual Template
EE 454 Image Processing Project
Reference:
[1] Michael Sears, Image Processing - Laboratory 1,
http://www.cs.wits.ac.za/~michael/Lab1.html
[2] Sumitha Balasuriya, Image Processing Using Matlab tutorial, retrieved from
http//www.dcs.gla.ac.uk/~sumitha/
[3] Dr. Tim Morris, Image Processing with MATLAB, Supporting Material for
COMP27112.
Page 7
Sample Manual Template
Download