Image Processing
Name
Institution
Edge Detection Using function “edge”
It is documented that, MATLAB is supported by the function “edge” detection of an image in addition, and different methods develop for edge detection can also be used (Fisher, Walker & Wolfart, 2003). Therefore, one can opt for the best method depending on the application. In this case, MATLAB edge detection method is applied to study the behavior of three edge detection methods namely Sobel, LOG, and Canny edge detection. The code that used for implementing the edge detection methods using MALTLAB edge function and the results achieved are as indicated below.
%% Edge Detection using different Methods supported by MATLAB edge function
%Reading Input Image
I = imread (‘img06.tif’);
%applying Sobel edge detection and recording the value of threshold applied
%by MATLAB
[sobel_edge, th_sob] = edge (I,’sobel’);
%applying log (Laplacian of Gaussian) edge detection and recording the value
%of threshold applied by MATLAB
[log_edge,th_log] = edge (I,’log’);
%Applying canny edge detection and recording the value of threshold applied
%by MATLAB
[canny_edge,th_cany] = edge(I,’ canny’);
%Displaying the Original image and result of edge detection using three
% different methods.
Figure (‘Name’,’Edge Detection’);
Subplot 121;
imshow (I);title(‘Original Image’);
Subplot 122;
imshow (sobel_edge);title(‘Sobel Edge Detection’);
Figure;
Subplot 121;
imshow (log edge);title(‘Laplacian of Gaussian (LOG) Edge Detection’);
subplot 122;
imshow (canny edge);title(‘Canny Edge Detection’);
 1. Give the algorithms of these three edge methods
In brief, we shall discuss three edge detection methods supported by MATLAB in addition, discuss the advantages and disadvantages of working algorithm. The three edge detections are:
 Sobel Edge Detection
 LOG (Laplacian of Gaussian ) Edge Detection
 Canny Edge Detection
Sobel Edge Detection
Basically, this method calculates the gradient of image pixel that eventually gives direction of any largest increment from lighter darker regions. Additionally, it gives rate changes of direction. Therefore, this indicates how rapidly or smoothly images pixel values changes and how likely a may result to as edge. Mathematically, the gradient of a point in two dimension signal like image is a vector of the two components; vertical and horizontal calculated by derivates in horizontal and vertical directions. At any point, the gradient vector points toward the direction of an increase of maximum intensity. With this in mind, the gradient with regions with the constant pixel or intensity image is zero, while for any point at the edge the gradient vector point across the edge from darker to brighter region (Canny, 2002).
To calculate the gradient at any point, the horizontal and vertical components are calculated. When calculating horizontal and vertical components of the gradient field the following and are used respectively as indicated below.
Fundamentally, Gx and Gy bestow horizontal and vertical components of the gradient of the image. The gradient magnitude can be calculated as follows.
The gradient direction at any point is calculated as follows.
LOG (Laplacian of Gaussian) Edge Detection
As the name suggests, LOG (Laplacian of Gaussian) edge detection is the combination of two filters; Laplican and Gaussian. In brief, when discussing the algorithm of LOG edge detection it important to note that, the Laplacian of an image I ( x, y) is given by:
Given that the input image is represented as the set of discrete pixels, one has to find the pixel convolution kernel to derive to the definition of the Laplican Operator. The discrete Laplacian operators can alternatively be defined by the arithmetic signs (Rafael & Richard, 2002).
Steps for LOG edge detection
The following steps need to be followed for the detection using LOG method (Raman & Dr. Himanshu, 2003).
 Generate LOG mask
 Apply mask in the input image
 Scanning each row by looking at zerocrossing to detect edges.
 Scanning each column by looking at zerocrossing to detect edges
Canny Edge Detection
Canny Edge Detection is said to be the most complex and optimal algorithm to detect edges of an image. First, a smoothing filter is applied to the input image to filter noise in the image. Mostly, a Gaussian filter is convolved at the image to filter noise. Once the image is filtered from noise, some gradient operators are applied on the smoothed image to find the gradient of the image. For example, a Sobel operator is convolved by the smoothed image to give absolute value for each pixel gradient location. Thereafter, gradient direction is computed by the gradient in the x and y direction given by the formula.
In this case, Gy and Gx are gradients of Y and x direction respectively. Whether the edge is horizontal or vertical, positive or negative diagonal, the direction of the gradient is determined upon theta values of 0, 45, 90, and 135 degrees ( ).
 Compare the performance, advantages and disadvantages
Method  Advantages  Disadvantages 
Sobel  It is the simplest in terms of complexity, time, computation, and orientations
There is no Y junction problem 
Lack of accuracy in term of edge detection
Very sensitive to noise 
LOG  Accurate in finding places for edges
Wider area of testing pixel Lack of Y junction ;problem

Fails to find orientation of edge. 
Canny  Have better detection
Precise in noise suppression Use two thresholds thus, making it more flexible to edge detection Helpful in removing ineffective information

Time consuming
Problems in Y junction Intensive computationally. 
 Determinate the suitable thresholds for “img06” to bring out the principal features and reduce irrelevant details.
To eliminate unwanted details and preserve the main edges, thresholds needs are changed. Through default, MATLAB chooses threshold (Raman & Dr. Himanshu, 2003). With this regard, the value for threshold edge detection for every method is another output variable that is introduced and as a result, MATLAB stores value of the threshold to variable and black and white detected edge images. Though the threshold leaves some irreverent information, the threshold used by MATLAB is 0.0738. With the new threshold of 0.1 the following results were obtained as shown below. It is important to note that, increasing threshold by a certain range does not only removes unwanted information but, affects main edges of input image in addition; inexact threshold can slide in the small range. In this context, a 0.0002 threshold has been found to be the the LOG operator for calculating edges of MATLAB with the new threshold being 0.0055. With this in mind, the results are shown in the following figures. One can articulate that, a lower threshold of 0.0188 and the upper threshold of 0.0469 is applied by MATLAB canny edge detector. In this context, the new upper threshold is 0.18 whereby, the MATLAB opt for lower threshold of the input upper threshold given by the following equation; Lower threshold= Upper threshold *0.4. The results of the new threshold are given below:
The code that generate the above images is:
%% Edge Detection With new thresholds
%Applying Sobel edge detection with new threshod
sobel_edge_new = edge(I,’sobel’, 0.1);
%Applying log (Laplacian of Gaussian) edge detection with new threshod
log_edge_new = edge(I,’log’,0.0055);
%Applying canny edge detection with new threshod
canny_edge_new = edge(I,’canny’,0.18);
%New Figure Window and Displaying the MATLAB thresholded Sobel image and the
%one with new threshold
figure (‘Name’,’Sobel vs Sobel’);
subplot (1,2,1);imshow(sobel_edge_new);title(‘After New Threshold Applied’);
subplot (1,2,2);imshow(sobel_edge);title(‘With MATLAB calculated Threshold’);
%New Figure Window and Displaying the MATLAB thresholded LOG image and the
%one with new threshold
figure (‘Name’,’LoG vs LoG’);
subplot (1,2,1);imshow(log_edge_new);title(‘After New Threshold Applied’);
subplot (1,2,2);imshow(log_edge);title(‘With MATLAB calculated Threshold’);
%New Figure Window and Displaying the MATLAB thresholded Canny image and the
%one with new threshold
figure (‘Name’,’Canny vs Canny’);
subplot (1,2,1);imshow(canny_edge_new);title(‘After New Threshold Applied’);
subplot (1,2,2);imshow(canny_edge);title(‘With MATLAB calculated Threshold’);
d) Without using any MATLAB filtering functions, implement a sobel edge detector.
%% Edge Detection With new thresholds
%Applying Sobel edge detection with new threshod
sobel_edge_new = edge(I,’sobel’, 0.1);
%Applying log (Laplacian of Gaussian) edge detection with new threshod
log_edge_new = edge(I,’log’,0.0055);
%Applying canny edge detection with new threshod
canny_edge_new = edge(I,’canny’,0.18);
%New Figure Window and Displaying the MATLAB thresholded Sobel image and the
%one with new threshold
figure (‘Name’,’Sobel vs Sobel’);
subplot (1,2,1);imshow(sobel_edge_new);title(‘After New Threshold Applied’);
subplot (1,2,2);imshow(sobel_edge);title(‘With MATLAB calculated Threshold’);
%New Figure Window and Displaying the MATLAB thresholded LOG image and the
%one with new threshold
figure (‘Name’,’LoG vs LoG’);
subplot (1,2,1);imshow(log_edge_new);title(‘After New Threshold Applied’);
subplot (1,2,2);imshow(log_edge);title(‘With MATLAB calculated Threshold’);
%New Figure Window and Displaying the MATLAB thresholded Canny image and the
%one with new threshold
figure (‘Name’,’Canny vs Canny’);
subplot (1,2,1);imshow(canny_edge_new);title(‘After New Threshold Applied’);
subplot (1,2,2);imshow(canny_edge);title(‘With MATLAB calculated Threshold’);
d) Without using any MATLAB filtering functions, implement a Sobel edge detector.
It is borne in mind; the new code of the Sobel edge detection is given below. From this argument, I have developed my personal convolution function to perform convolution with the new code of Sobel filtering and convolution filters as follows.
%% OWN Developed Sobel Edge Detection
%Converting the image matrix to double precision data type
I = double(I);
% The Horizontal Sobel Operator
Hx = [1,2,1;0,0,0;1,2,1];
%The Vertical Sobel Operator which is the Transpose of Horizontal Operator
Hy = Hx’;
%Convolving Image and Horizontal Sobel Operator using my own convolution
%function
Ix = my_conv(I,Hx);
%Convolving Image and Vertical Sobel Operator using my own convolution
%function
Iy = my_conv(I,Hy);
%Final Result of Sobel Operation is obtained by combining the results of
%above two convolutions.
S = sqrt(Ix.^2 + Iy.^2);
%Converting the result to uint8 data class.
S = uint8(S);
%Applying Threshold to obtain the Black and White image after Sobel
%Operation
Sbw = im2bw(S,0.6);
%Creating New Figure
figure(‘Name’,’Sobel Edge Detection’);
%Displaying Image Obtained from MATLAB sobel Edge Detection
subplot 121; imshow(sobel_edge); title(‘Sobel Edge MATLAB’);
%Displaying Image Obtained from my own developed method of sobel of Sobel
%Edge Detection
subplot 122; imshow(Sbw); title(‘Own Method after thresholding’);
The convolution function being used in above code is given below. It is vital to note that, the code for the convolution is only applicable for 3*3 filter mask.
function Y = my_conv(X,W)
%This function convolves the input matrix X with the input mask W where X
%can be of any size while W is of size 3×3
%Padding Zeros along all four boundaries of the image X inorder to get it
%convolved by mask of 3×3. So two rows (above and below) and two columns
%(left and right) consisting of zeros have been padded with the matrix X
X = padd_zero(X);
%Calculate the new size of matrix X
[m,n]=size(X);
%The Output matrix X should have two rows and two columns less than the
%new size of matrix X, that’s why creating matrix of zeros of this size.
Y = zeros(m2,n2);
%Start of For Loop for rows
for i = 2:m1
for j = 2:n1%Start of For Loop for columns
%Multiplying X(i,j) with the Mask W(n)
tmp=[W(1)*X(i1,j1),W(2)*X(i,j1),W(3)*X(i+1,j1),W(4)*X(i1,j),W(5)*X(i,j),W(6)*X(i+1,j),W(7)*X(i1,j+1),W(8)*X(i,j+1),W(9)*X(i+1,j+1)];
Y(i1,j1)=sum(tmp);%Calculating Sum and hence the result of convolution at this point
end %End of Loop Columns
end %End of Loop Rows
The results obtained are shown in the figure below comparing with MATLAB generated Sobel edge detected image.
e) Compare the Sobel filtered image from d) and that of the image from a). Are they the same? If not, why?
Generally, the above code for implementing segmentation was written in MATLAB. In this case, the initial threshold that was equal to the average of the minimum and maximum values of the input image was taken. Eventually, the initial threshold was set to be 127.5 as the midpoint of max and the min. The code for calculating appropriate threshold value T becomes equal to 0; T0=0. The loop image was segmented in two portions whereby, one had a greater and equal pixels and threshold respectively while the other consisted of pixels less that the threshold. Thereafter, the mean intensities of the region were calculated to find the new threshold. This was used to check the difference of the previous and the new threshold set of calculation in loop. The following results were observed:
 The loop took three iterations when the differences between the new and threshold became zero.
 The initial threshold selected was 127.50
 The final threshold calculated was 125.4018
With this regard, the new threshold calculated is used for segmentation of the image “img07.jpg” using MATLAB Function “im2bw”. It is vital to note that, the new threshold calculated should be first normalized since, the valid input range for thresholds range from 0 to 1 obtained by dividing the threshold value by the maximum pixel intensity 255. The results achieved by threshold and the code are as shown below.
Image segmentation
As the name suggests, image segmentation divides the image into two or more than two groups or segments. Image segmentation ensures that, the resulted segmented image is easy to understand. In the second question in this assignment, we will discuss technique of image segmentation and show the applicability of the resultant image. Specifically, we will discuss how threshold acts as basic building block for image segmentation process.
Initial Threshold
Initial Threshold is used to perform binary segmentation that occur when pixels in an image is white or pure black. In cases when the image is grey, the pixel values vary from 0 to 255. In order to divide an image into segments, two extreme values 0 and 255 are put into consideration. In cases of gray scales, the pixel value is 128 though it is not a good threshold initialization. Hence, the best way to initialize the threshold is calculating accurate minimum and maximum gray sale image. In this case, the values are obtained using max (max IM) and Min (Min (Im)) operators that turns to be 29 and 224; minimum and maximum respectively. Therefore, the arithmetic mean of the two values and the initial threshold results to 136.5.
Grouping
By using the initial threshold, the image is divided into two groups G1 and G2. G1 is divided by having pixel values above the threshold while G2 by lower pixel values and equal threshold values. The average intensity values of U1 and U2 for groups G1 and G2 is calculated by:
T=0.5* (u1 +u2) where T is the new threshold. Thereafter, threshold and grouping is performed by the new value of threshold until a predefined constant is obtained.
Applying the Threshold
When the iterative process stops, threshold is used to perform binary segmentation that entails assigning pure black values below and pure white pixel values above. The following are the input image and the binary segmented image. At this point, we can see how the segmentation has made image crisp and separated finger prints from the background.
%% Image Segmentation
%Reading input image
I = double(imread(‘img07.jpg’));
%Calculating initial estimated threshold
T = (max(I(:))+min(I(:)))/2;
i = 0; %Just to check the number of iterations it takes ‘T0’ to become zero
%Assigning any random value other than zero
T0 = T;
%Start of While LoopShould continue if T0 is not zero
while (~(T0==0))
tmp = I>=T;%Logical matrix: tmp(x,y) will be 1 if I(x,y)>= T else zero
u1 = mean(I(tmp));%Taking Average of all the values of I(x,y)>=T
tmp = not(tmp);%Previously tmp(x,y) was 1 for I(x,y)>=T, a logical not will change the inequality and so tmp(x,y) will be 1 for I(x,y)<T
u2 = mean(I(tmp));%Taking Average of all the values of I(x,y)<T
T_new = (u1+u2)/2;%Finding new approximate value for threshold
T0 = abs(T_newT);%Difference of new and old threshold
T = T_new;%Assigning new threshold value
i = i+1;%Just counting the number of iterations which is found to be 3
end
%End of While Loop
%Threshold to be used in the calculation for im2bw as the value for the
%threshold to be input to the “im2bw” function should lie between 0 and 1
t = T/255;
%Converting the image to uint8 class of data
I = uint8(I);
%Converting image I to black and white image using threshold ‘t’
B = im2bw(I,t);
%New figure
figure(‘Name’,’Threshold and Segmentation’);
%Displaying the original image and the thresholded black and white image.
imshow(I);title(‘Original Image’);
figure;
imshow(B);title(‘Black & White Image with calculated threshold’);
References
Canny, L.G. (2002). Edge Detection:
http://www.cs.ucf.edu/courses/cap6411/cap5415/spring02/Lecture7h.pdf
Fisher, S. R., Walker, A., & Wolfart, E. (2003). Spatial Filters. Laplician/ Laplacian of Gaussian: http://homepages.inf.ed.ac.uk/rbf/HIPR2/log.htm
Rafael C. G, & Richard E. W. (2002). Digital Image Processing, Second Edition. New York: Prentice Hall
Raman, M., & Dr. Himanshu. A. (2003).Study and Comparison of Various Image Edge Detection Techniques. International Journal of Image Processing (IJIP) 3, 1
Image Processing
Name
Institution
Edge Detection Using function “edge”
It is documented that, MATLAB is supported by the function “edge” detection of an image in addition, and different methods develop for edge detection can also be used (Fisher, Walker & Wolfart, 2003). Therefore, one can opt for the best method depending on the application. In this case, MATLAB edge detection method is applied to study the behavior of three edge detection methods namely Sobel, LOG, and Canny edge detection. The code that used for implementing the edge detection methods using MALTLAB edge function and the results achieved are as indicated below.
%% Edge Detection using different Methods supported by MATLAB edge function
%Reading Input Image
I = imread (‘img06.tif’);
%applying Sobel edge detection and recording the value of threshold applied
%by MATLAB
[sobel_edge, th_sob] = edge (I,’sobel’);
%applying log (Laplacian of Gaussian) edge detection and recording the value
%of threshold applied by MATLAB
[log_edge,th_log] = edge (I,’log’);
%Applying canny edge detection and recording the value of threshold applied
%by MATLAB
[canny_edge,th_cany] = edge(I,’ canny’);
%Displaying the Original image and result of edge detection using three
% different methods.
Figure (‘Name’,’Edge Detection’);
Subplot 121;
imshow (I);title(‘Original Image’);
Subplot 122;
imshow (sobel_edge);title(‘Sobel Edge Detection’);
Figure;
Subplot 121;
imshow (log edge);title(‘Laplacian of Gaussian (LOG) Edge Detection’);
subplot 122;
imshow (canny edge);title(‘Canny Edge Detection’);
1. Give the algorithms of these three edge methods
In brief, we shall discuss three edge detection methods supported by MATLAB in addition, discuss the advantages and disadvantages of working algorithm. The three edge detections are:
1. Sobel Edge Detection
2. LOG (Laplacian of Gaussian ) Edge Detection
3. Canny Edge Detection
Sobel Edge Detection
Basically, this method calculates the gradient of image pixel that eventually gives direction of any largest increment from lighter darker regions. Additionally, it gives rate changes of direction. Therefore, this indicates how rapidly or smoothly images pixel values changes and how likely a may result to as edge. Mathematically, the gradient of a point in two dimension signal like image is a vector of the two components; vertical and horizontal calculated by derivates in horizontal and vertical directions. At any point, the gradient vector points toward the direction of an increase of maximum intensity. With this in mind, the gradient with regions with the constant pixel or intensity image is zero, while for any point at the edge the gradient vector point across the edge from darker to brighter region (Canny, 2002).
To calculate the gradient at any point, the horizontal and vertical components are calculated. When calculating horizontal and vertical components of the gradient field the following and are used respectively as indicated below.
Fundamentally, Gx and Gy bestow horizontal and vertical components of the gradient of the image. The gradient magnitude can be calculated as follows.
The gradient direction at any point is calculated as follows.
LOG (Laplacian of Gaussian) Edge Detection
As the name suggests, LOG (Laplacian of Gaussian) edge detection is the combination of two filters; Laplican and Gaussian. In brief, when discussing the algorithm of LOG edge detection it important to note that, the Laplacian of an image I ( x, y) is given by:
Given that the input image is represented as the set of discrete pixels, one has to find the pixel convolution kernel to derive to the definition of the Laplican Operator. The discrete Laplacian operators can alternatively be defined by the arithmetic signs (Rafael & Richard, 2002).
Steps for LOG edge detection
The following steps need to be followed for the detection using LOG method (Raman & Dr. Himanshu, 2003).
1. Generate LOG mask
2. Apply mask in the input image
3. Scanning each row by looking at zerocrossing to detect edges.
4. Scanning each column by looking at zerocrossing to detect edges
Canny Edge Detection
Canny Edge Detection is said to be the most complex and optimal algorithm to detect edges of an image. First, a smoothing filter is applied to the input image to filter noise in the image. Mostly, a Gaussian filter is convolved at the image to filter noise. Once the image is filtered from noise, some gradient operators are applied on the smoothed image to find the gradient of the image. For example, a Sobel operator is convolved by the smoothed image to give absolute value for each pixel gradient location. Thereafter, gradient direction is computed by the gradient in the x and y direction given by the formula.
In this case, Gy and Gx are gradients of Y and x direction respectively. Whether the edge is horizontal or vertical, positive or negative diagonal, the direction of the gradient is determined upon theta values of 0, 45, 90, and 135 degrees ( ).
2. Compare the performance, advantages and disadvantages
Method 
Advantages 
Disadvantages 
Sobel 
It is the simplest in terms of complexity, time, computation, and orientations There is no Y junction problem 
Lack of accuracy in term of edge detection Very sensitive to noise 
LOG 
Accurate in finding places for edges Wider area of testing pixel Lack of Y junction ;problem

Fails to find orientation of edge. 
Canny 
Have better detection Precise in noise suppression Use two thresholds thus, making it more flexible to edge detection Helpful in removing ineffective information

Time consuming Problems in Y junction Intensive computationally. 
3. Determinate the suitable thresholds for “img06” to bring out the principal features and reduce irrelevant details.
To eliminate unwanted details and preserve the main edges, thresholds needs are changed. Through default, MATLAB chooses threshold (Raman & Dr. Himanshu, 2003). With this regard, the value for threshold edge detection for every method is another output variable that is introduced and as a result, MATLAB stores value of the threshold to variable and black and white detected edge images. Though the threshold leaves some irreverent information, the threshold used by MATLAB is 0.0738. With the new threshold of 0.1 the following results were obtained as shown below. It is important to note that, increasing threshold by a certain range does not only removes unwanted information but, affects main edges of input image in addition; inexact threshold can slide in the small range. In this context, a 0.0002 threshold has been found to be the the LOG operator for calculating edges of MATLAB with the new threshold being 0.0055. With this in mind, the results are shown in the following figures. One can articulate that, a lower threshold of 0.0188 and the upper threshold of 0.0469 is applied by MATLAB canny edge detector. In this context, the new upper threshold is 0.18 whereby, the MATLAB opt for lower threshold of the input upper threshold given by the following equation; Lower threshold= Upper threshold *0.4. The results of the new threshold are given below:
The code that generate the above images is:
%% Edge Detection With new thresholds
%Applying Sobel edge detection with new threshod
sobel_edge_new = edge(I,’sobel’, 0.1);
%Applying log (Laplacian of Gaussian) edge detection with new threshod
log_edge_new = edge(I,’log’,0.0055);
%Applying canny edge detection with new threshod
canny_edge_new = edge(I,’canny’,0.18);
%New Figure Window and Displaying the MATLAB thresholded Sobel image and the
%one with new threshold
figure (‘Name’,’Sobel vs Sobel’);
subplot (1,2,1);imshow(sobel_edge_new);title(‘After New Threshold Applied’);
subplot (1,2,2);imshow(sobel_edge);title(‘With MATLAB calculated Threshold’);
%New Figure Window and Displaying the MATLAB thresholded LOG image and the
%one with new threshold
figure (‘Name’,’LoG vs LoG’);
subplot (1,2,1);imshow(log_edge_new);title(‘After New Threshold Applied’);
subplot (1,2,2);imshow(log_edge);title(‘With MATLAB calculated Threshold’);
%New Figure Window and Displaying the MATLAB thresholded Canny image and the
%one with new threshold
figure (‘Name’,’Canny vs Canny’);
subplot (1,2,1);imshow(canny_edge_new);title(‘After New Threshold Applied’);
subplot (1,2,2);imshow(canny_edge);title(‘With MATLAB calculated Threshold’);
d) Without using any MATLAB filtering functions, implement a sobel edge detector.
%% Edge Detection With new thresholds
%Applying Sobel edge detection with new threshod
sobel_edge_new = edge(I,’sobel’, 0.1);
%Applying log (Laplacian of Gaussian) edge detection with new threshod
log_edge_new = edge(I,’log’,0.0055);
%Applying canny edge detection with new threshod
canny_edge_new = edge(I,’canny’,0.18);
%New Figure Window and Displaying the MATLAB thresholded Sobel image and the
%one with new threshold
figure (‘Name’,’Sobel vs Sobel’);
subplot (1,2,1);imshow(sobel_edge_new);title(‘After New Threshold Applied’);
subplot (1,2,2);imshow(sobel_edge);title(‘With MATLAB calculated Threshold’);
%New Figure Window and Displaying the MATLAB thresholded LOG image and the
%one with new threshold
figure (‘Name’,’LoG vs LoG’);
subplot (1,2,1);imshow(log_edge_new);title(‘After New Threshold Applied’);
subplot (1,2,2);imshow(log_edge);title(‘With MATLAB calculated Threshold’);
%New Figure Window and Displaying the MATLAB thresholded Canny image and the
%one with new threshold
figure (‘Name’,’Canny vs Canny’);
subplot (1,2,1);imshow(canny_edge_new);title(‘After New Threshold Applied’);
subplot (1,2,2);imshow(canny_edge);title(‘With MATLAB calculated Threshold’);
d) Without using any MATLAB filtering functions, implement a Sobel edge detector.
It is borne in mind; the new code of the Sobel edge detection is given below. From this argument, I have developed my personal convolution function to perform convolution with the new code of Sobel filtering and convolution filters as follows.
%% OWN Developed Sobel Edge Detection
%Converting the image matrix to double precision data type
I = double(I);
% The Horizontal Sobel Operator
Hx = [1,2,1;0,0,0;1,2,1];
%The Vertical Sobel Operator which is the Transpose of Horizontal Operator
Hy = Hx’;
%Convolving Image and Horizontal Sobel Operator using my own convolution
%function
Ix = my_conv(I,Hx);
%Convolving Image and Vertical Sobel Operator using my own convolution
%function
Iy = my_conv(I,Hy);
%Final Result of Sobel Operation is obtained by combining the results of
%above two convolutions.
S = sqrt(Ix.^2 + Iy.^2);
%Converting the result to uint8 data class.
S = uint8(S);
%Applying Threshold to obtain the Black and White image after Sobel
%Operation
Sbw = im2bw(S,0.6);
%Creating New Figure
figure(‘Name’,’Sobel Edge Detection’);
%Displaying Image Obtained from MATLAB sobel Edge Detection
subplot 121; imshow(sobel_edge); title(‘Sobel Edge MATLAB’);
%Displaying Image Obtained from my own developed method of sobel of Sobel
%Edge Detection
subplot 122; imshow(Sbw); title(‘Own Method after thresholding’);
The convolution function being used in above code is given below. It is vital to note that, the code for the convolution is only applicable for 3*3 filter mask.
function Y = my_conv(X,W)
%This function convolves the input matrix X with the input mask W where X
%can be of any size while W is of size 3×3
%Padding Zeros along all four boundaries of the image X inorder to get it
%convolved by mask of 3×3. So two rows (above and below) and two columns
%(left and right) consisting of zeros have been padded with the matrix X
X = padd_zero(X);
%Calculate the new size of matrix X
[m,n]=size(X);
%The Output matrix X should have two rows and two columns less than the
%new size of matrix X, that’s why creating matrix of zeros of this size.
Y = zeros(m2,n2);
%Start of For Loop for rows
for i = 2:m1
for j = 2:n1%Start of For Loop for columns
%Multiplying X(i,j) with the Mask W(n)
tmp=[W(1)*X(i1,j1),W(2)*X(i,j1),W(3)*X(i+1,j1),W(4)*X(i1,j),W(5)*X(i,j),W(6)*X(i+1,j),W(7)*X(i1,j+1),W(8)*X(i,j+1),W(9)*X(i+1,j+1)];
Y(i1,j1)=sum(tmp);%Calculating Sum and hence the result of convolution at this point
end %End of Loop Columns
end %End of Loop Rows
The results obtained are shown in the figure below comparing with MATLAB generated Sobel edge detected image.
e) Compare the Sobel filtered image from d) and that of the image from a). Are they the same? If not, why?
Generally, the above code for implementing segmentation was written in MATLAB. In this case, the initial threshold that was equal to the average of the minimum and maximum values of the input image was taken. Eventually, the initial threshold was set to be 127.5 as the midpoint of max and the min. The code for calculating appropriate threshold value T becomes equal to 0; T0=0. The loop image was segmented in two portions whereby, one had a greater and equal pixels and threshold respectively while the other consisted of pixels less that the threshold. Thereafter, the mean intensities of the region were calculated to find the new threshold. This was used to check the difference of the previous and the new threshold set of calculation in loop. The following results were observed:
1. The loop took three iterations when the differences between the new and threshold became zero.
2. The initial threshold selected was 127.50
3. The final threshold calculated was 125.4018
With this regard, the new threshold calculated is used for segmentation of the image “img07.jpg” using MATLAB Function “im2bw”. It is vital to note that, the new threshold calculated should be first normalized since, the valid input range for thresholds range from 0 to 1 obtained by dividing the threshold value by the maximum pixel intensity 255. The results achieved by threshold and the code are as shown below.
Image segmentation
As the name suggests, image segmentation divides the image into two or more than two groups or segments. Image segmentation ensures that, the resulted segmented image is easy to understand. In the second question in this assignment, we will discuss technique of image segmentation and show the applicability of the resultant image. Specifically, we will discuss how threshold acts as basic building block for image segmentation process.
Initial Threshold
Initial Threshold is used to perform binary segmentation that occur when pixels in an image is white or pure black. In cases when the image is grey, the pixel values vary from 0 to 255. In order to divide an image into segments, two extreme values 0 and 255 are put into consideration. In cases of gray scales, the pixel value is 128 though it is not a good threshold initialization. Hence, the best way to initialize the threshold is calculating accurate minimum and maximum gray sale image. In this case, the values are obtained using max (max IM) and Min (Min (Im)) operators that turns to be 29 and 224; minimum and maximum respectively. Therefore, the arithmetic mean of the two values and the initial threshold results to 136.5.
Grouping
By using the initial threshold, the image is divided into two groups G1 and G2. G1 is divided by having pixel values above the threshold while G2 by lower pixel values and equal threshold values. The average intensity values of U1 and U2 for groups G1 and G2 is calculated by:
T=0.5* (u1 +u2) where T is the new threshold. Thereafter, threshold and grouping is performed by the new value of threshold until a predefined constant is obtained.
Applying the Threshold
When the iterative process stops, threshold is used to perform binary segmentation that entails assigning pure black values below and pure white pixel values above. The following are the input image and the binary segmented image. At this point, we can see how the segmentation has made image crisp and separated finger prints from the background.
%% Image Segmentation
%Reading input image
I = double(imread(‘img07.jpg’));
%Calculating initial estimated threshold
T = (max(I(:))+min(I(:)))/2;
i = 0; %Just to check the number of iterations it takes ‘T0’ to become zero
%Assigning any random value other than zero
T0 = T;
%Start of While LoopShould continue if T0 is not zero
while (~(T0==0))
tmp = I>=T;%Logical matrix: tmp(x,y) will be 1 if I(x,y)>= T else zero
u1 = mean(I(tmp));%Taking Average of all the values of I(x,y)>=T
tmp = not(tmp);%Previously tmp(x,y) was 1 for I(x,y)>=T, a logical not will change the inequality and so tmp(x,y) will be 1 for I(x,y)<T
u2 = mean(I(tmp));%Taking Average of all the values of I(x,y)<T
T_new = (u1+u2)/2;%Finding new approximate value for threshold
T0 = abs(T_newT);%Difference of new and old threshold
T = T_new;%Assigning new threshold value
i = i+1;%Just counting the number of iterations which is found to be 3
end
%End of While Loop
%Threshold to be used in the calculation for im2bw as the value for the
%threshold to be input to the “im2bw” function should lie between 0 and 1
t = T/255;
%Converting the image to uint8 class of data
I = uint8(I);
%Converting image I to black and white image using threshold ‘t’
B = im2bw(I,t);
%New figure
figure(‘Name’,’Threshold and Segmentation’);
%Displaying the original image and the thresholded black and white image.
imshow(I);title(‘Original Image’);
figure;
imshow(B);title(‘Black & White Image with calculated threshold’);
References
Canny, L.G. (2002). Edge Detection:
http://www.cs.ucf.edu/courses/cap6411/cap5415/spring02/Lecture7h.pdf
Fisher, S. R., Walker, A., & Wolfart, E. (2003). Spatial Filters. Laplician/ Laplacian of Gaussian: http://homepages.inf.ed.ac.uk/rbf/HIPR2/log.htm
Rafael C. G, & Richard E. W. (2002). Digital Image Processing, Second Edition. New York: Prentice Hall
Raman, M., & Dr. Himanshu. A. (2003).Study and Comparison of Various Image Edge Detection Techniques. International Journal of Image Processing (IJIP) 3, 1
Use the order calculator below and get started! Contact our live support team for any assistance or inquiry.
[order_calculator]