INDEX 1. Explore matlab libraries: a. Implement matrix simple operations. b. Image processing toolbox. 2. Read image and perform simple operations. a. subtract background image from the original image. b. Increase the image contrast c. Threshold the image d. Identify objects in the image e. Examine one object f. View all objects 3. Spatial transformation using inbuilt functions and matrix transformation. 4. Implement Filters: linear filter and predefined filter. 5. Transform: a. Fourier transform b. Discrete cosine transform c. Radon transform 6. Analyzing Images: a. Detecting Edges using the edge function. b. Detecting corners using corner function. c. Tracing object boundaries in an image. d. Detecting lines using the Hough transform. e. Analyzing the texture of an image. f. Adjusting pixel intensity values g. Removing noise from images. 7. Region of interest based processing: a. Specifying a region of interest (ROI). b. Filtering an ROI.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
INDEX
1. Explore matlab libraries: a. Implement matrix simple operations. b. Image processing toolbox.
2. Read image and perform simple operations. a. subtract background image from the original image. b. Increase the image contrast c. Threshold the image d. Identify objects in the image e. Examine one object f. View all objects
3. Spatial transformation using inbuilt functions and matrix transformation. 4. Implement Filters: linear filter and predefined filter. 5. Transform:
a. Fourier transform b. Discrete cosine transform c. Radon transform
6. Analyzing Images: a. Detecting Edges using the edge function. b. Detecting corners using corner function. c. Tracing object boundaries in an image. d. Detecting lines using the Hough transform. e. Analyzing the texture of an image. f. Adjusting pixel intensity values g. Removing noise from images.
7. Region of interest based processing: a. Specifying a region of interest (ROI). b. Filtering an ROI.
1. a) Basic matrix manipulation using MATLAB
MATRIX MULTIPLICATION
>> % Matrix multiplication example using full looping >> >> n = 100; A = rand(n); B=rand(n); C=zeros(n); >> for i=1:n, for j=1:n, for k=1:n C(i,j) = C(i,j) + (A(i,k)*B(k,j)); end end end ii. >> n = 100; A = rand(n); B=rand(n); C=zeros(n); >>C = A*B;
B. MATRIX ADDITION COMMUTATIVE PROPERTY
>> n = 100; a = [1 2; 3 4]; b = [5 6; 7 9]; >>B+A = A+B;
OUTPUT
Ans =1 1
1 1
MATRIX DISTRIBUTIVE PROPERTY
>> n = 100; a = [1 2; 3 4]; b = [5 6; 7 9];
c = [-1 -5; -3 9];
a*(b+c)=a*b+a*c;
OUTPUT
Ans =1 1
1 1
4. COMBINING MATRIX TO FORM NEW ONE
g = [a b c] h = [c' a' b']' OUTPUT g = 1 2 5 6 -1 -5 3 4 7 9 -3 9 h = -1 -5 -3 9 1 2 3 4 5 6 7 9
1. b) Explore the Image Processing Toolbox in MATLAB
Image Processing Toolbox™ provides a comprehensive set of reference-standard algorithms and
graphical tools for image processing, analysis, visualization, and algorithm development. You can perform
8. Read image and perform simple operations. a. subtract background image from the original image. b. Increase the image contrast c. Threshold the image d. Identify objects in the image e. Examine one object f. View all objects
Subtract the Background Image from the Original Image
# Read Image
I = imread('rice.png');
# Display Image
imshow(I);
Grayscale Image rice.png
# In this step, the example uses a morphological opening operation to estimate the background
# illumination. Morphological opening is an erosion followed by a dilation, using the same structuring
# element for both operations. The strel function to create a disk-shaped structuring element with a
# radius of 15. To remove the rice grains from the image,
background = imopen(I,strel('disk',15));
# subtract the background image, background, from the original image, I, and then view the image
I2 = I - background;
figure, imshow(I2)
Image with Uniform Background
Increase the Image Contrast
# Use imadjust to adjust the contrast of the image.
I3 = imadjust(I2);
figure, imshow(I3);
Image After Intensity Adjustment
Threshold the Image
# im2bw function to convert the grayscale image into a binary image by using thresholding.
# The function graythresh automatically computes an appropriate threshold to use to convert the
grayscale image to binary.
# Remove background noise with bwareaopen
level = graythresh(I3);
bw = im2bw(I3,level);
bw = bwareaopen(bw, 50);
figure, imshow(bw)
Binary Version of the Image
Identify Objects in the Image
# The function bwconncomp finds all the connected components (objects) in the binary image. The
# accuracy of your results depends on the size of the objects, the connectivity parameter (4, 8, or
# arbitrary), and whether or not any objects are touching (in which case they could be labeled as one # object).
cc = bwconncomp(bw, 4)
cc.NumObjects
You will receive the output shown below:
cc =
Connectivity: 4
ImageSize: [256 256]
NumObjects: 95
PixelIdxList: {1x95 cell}
ans =
95
Examine One Object
# Each distinct object is labeled with the same integer value. Show the grain that is the 50th # connected component:
grain = false(size(bw));
grain(cc.PixelIdxList{50}) = true;
figure, imshow(grain);
View All Objects
# Use labelmatrix to create a label matrix from the output of bwconncomp.
labeled = labelmatrix(cc);
whos labeled
Name Size Bytes Class Attributes
labeled 256x256 65536 uint8
# In the pseudo-color image, the label identifying each object in the label matrix maps to a different
# color in the associated colormap matrix. Uselabel2rgb to choose the colormap, the background
# color, and how objects in the label matrix map to colors in the colormap:
Filtering of images, either by correlation or convolution, can be performed using the toolbox
function imfilter. This example filters an image with a 5-by-5 filter containing equal weights. Such
a filter is often called an averaging filter.
I = imread('coins.png');
h = ones(5,5) / 25;
I2 = imfilter(I,h);
imshow(I), title('Original Image');
figure, imshow(I2), title('Filtered Image')
Predefined Filter :
The fspecial function produces several kinds of predefined filters, in the form of correlation kernels.
After creating a filter with fspecial, you can apply it directly to your image data using imfilter.
This example illustrates applying an unsharp masking filter to a grayscale image. The unsharp masking filter has the effect of making edges and fine detail in the image more crisp.
Consider data sampled at 1000 Hz. Form a signal containing a 50 Hz sinusoid of amplitude 0.7 and 120 Hz sinusoid of amplitude 1 and corrupt it with some zero-mean random noise:
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sample time
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector
% Sum of a 50 Hz sinusoid and a 120 Hz sinusoid
x = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
y = x + 2*randn(size(t)); % Sinusoids plus noise
NFFT = 2^nextpow2(L); % Next power of 2 from length of y
6) Analyzing Images Detecting Edges using the edge function. Detecting corners using corner function. Tracing object boundaries in an image. Detecting lines using the Hough transform. Analyzing the texture of an image. Adjusting pixel intensity values Removing noise from images.
I = imread('coins.png');
imshow(I)
Detecting Edges using the edge function.
BW1 = edge(I,'sobel');
BW2 = edge(I,'canny');
imshow(BW1)
figure, imshow(BW2)
Detecting corners using corner function.
The corner function identifies corners in an image. Two methods are available—the Harris corner
detection method (the default) and Shi and Tomasi's minimum eigenvalue method. Both methods use algorithms that depend on the eigenvalues of the summation of the squared difference matrix (SSD).
The eigenvalues of an SSD matrix represent the differences between the surroundings of a pixel and the surroundings of its neighbors. The larger the difference between the surroundings of a pixel and those of its neighbors, the larger the eigenvalues. The larger the eigenvalues, the more likely that a pixel appears at a corner. Create a checkerboard image, and find the corners.
I = checkerboard(40,2,2);
C = corner(I);
Display the corners when the maximum number of desired corners is the default setting of 200.
subplot(1,2,1);
imshow(I);
hold on
plot(C(:,1), C(:,2), '.', 'Color', 'g')
title('Maximum Corners = 200')
hold off
Display the corners when the maximum number of desired corners is 3.
Hough transform functions detect lines in an image. The following table lists the Hough transform functions in the order you use them to perform this task.
The following example shows how to use these functions to detect lines in an image.
1. Read an image into the MATLAB workspace. I = imread('circuit.tif');
2. For this example, rotate and crop the image using the imrotate function.
rotI = imrotate(I,33,'crop');
fig1 = imshow(rotI);
3. Find the edges in the image using the edge function.
BW = edge(rotI,'canny');
figure, imshow(BW);
4. Compute the Hough transform of the image using the hough function.
[H,theta,rho] = hough(BW);
5. Display the transform using the imshow function.
1. Filter the image with the rangefilt function and display the results. Note how range filtering
highlights the edges and surface contours of the coins.
2. K = rangefilt(I);
figure, imshow(K)
Adjusting pixel intensity values
Adjusting Pixel Intensity Values
I = imread('cameraman.tif');
J = imadjust(I,[0 0.2],[0.5 1]);
imshow(I)
figure, imshow(J)
Image After Remapping and Widening the Dynamic Range
Removing noise from images.
Using an averaging filter and medfilt2 to remove salt and pepper noise. This type of noise consists
of random pixels' being set to black or white (the extremes of the data range). In both cases the size of the neighborhood used for filtering is 3-by-3.
1. Read in the image and display it.
I = imread('eight.tif');
imshow(I)
2. Add noise to it.
J = imnoise(I,'salt & pepper',0.02);
figure, imshow(J)
3. Filter the noisy image with an averaging filter and display the results.
K = filter2(fspecial('average',3),J)/255;
figure, imshow(K)
4. Now use a median filter to filter the noisy image and display the results. Notice
that medfilt2 does a better job of removing noise, with less blurring of edges.
L = medfilt2(J,[3 3]);
figure, imshow(L)
9. Region of interest based processing: a. Specifying a region of interest (ROI). b. Filtering an ROI.
Using the createMask method of ROI objects you can create a binary mask that defines an ROI
associated with a particular image. To create a binary mask without having an associated image, use
the poly2mask function. Unlike the createMask method, poly2mask does not require an input
image. You specify the vertices of the ROI in two vectors and specify the size of the binary mask returned. For example, the following creates a binary mask that can be used to filter an ROI in
the pout.tif image.
c = [123 123 170 170];
r = [160 210 210 160];
m = 291; % height of pout image
n = 240; % width of pout image
BW = poly2mask(c,r,m,n);
figure, imshow(BW)
FILTERING OF ROI
This example continues the roipoly example, filtering the region of the image I specified by the
mask BW. The roifilt2 function returns the filtered image J, shown in the following figure.