Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

Edge detection using Local Variance

Local statistics can determine the gradient of the image. The local variance can be used to generate an edge map.
Steps to be performed:
1.       Read a grayscale image
2.       Define a window size (For eg: 3x3,5x5,7x7)
3.       Find the local variance
4.       Find the global mean of the local variance
5.       Set local variance to zero if it is less than the global mean else set it to one


MATLAB code:
%Boundary Detection - Local Variance
%Read an image
I = imread('rice.png');
figure,imagesc(I);colormap(gray);

I = double(I);

Explanation:
A grayscale image is taken as input for edge detection. If the input image is RGB then convert it to gray scaleusing ‘rgb2gray’.







MATLAB code:

%Define the window size
sz=3;
window = ones(sz)/sz.^2;

%Find the local mean
mu = conv2(I,window,'same');

%Find the local Variance
II = conv2(I.^2,window,'same');
Lvar = II-mu.^2;

figure,imagesc(Lvar);colormap(gray);title('Local Variance of the image');

Explanation:
A window size of 3 by 3 is defined and local variance is computed. Check ‘local variance- matlab code’ to understand how local variance is estimated.

MATLAB CODE:
%Define a Threshold
meanL = mean(Lvar(:));

%Set the pixel values based on threshold
Boundary = zeros(size(Lvar));
Boundary(Lvar < meanL)=1;
Boundary(Lvar >= meanL) = 0;

figure,imagesc(Boundary);colormap(gray);title('Boundary Extracted Image');

Explanation:
 The mean of the local variance is obtained and using the mean value as threshold, the boundary is defined for the image.  The mean value of the given image is 239.3638.



The threshold value can also be set randomly by the user. For instance, set the threshold value to 500.



like button Like "IMAGE PROCESSING" page

Local Variance – MATLAB CODE

Variance Formula:
Let us understand how global variance works. First let us theoretically define variance in simple terms. In lay man terms, variance is defined as how divergent values are from the average value.

Consider a matrix A=[5 5 5;5 5 5]. The variance of the matrix A is zero.. In the given example, the average is 5 and all the elements in the matrix are equal to 5. So finding deviation in the above example is not possible.

Let us consider another matrix B =[3 7 1;5 4 2]. The variance of the matrix B is 3.8889.. Let us arrive at the result using theoretical formula as follows:
Variance of B = Mean of B^2 – (mean of B)^2
                        = 17.333 – 13.444
                        = 3.888
MATLAB CODE:
Var(B(:),1)
Local Variance
Instead of finding variance for the whole matrix, variance is computed based on a small sliding window.
Steps to Perform:

MATLAB CODE:
I = imread('cameraman.tif');
figure,imagesc(I);colormap(gray);title('Original Image');

I = double(I);
%Define the window size
sz = 5;
mn=floor(sz/2);
%Preallocate the matrix
Output = zeros(size(I));
%Pad the matrix with zeros
I = padarray(I,[mnmn]);

for i=1:size(I,1)-mn*2
for j=1:size(I,2)-mn*2
tmp = I(i:i+(sz-1),j:j+(sz-1));
mu = mean(tmp(:));
        tmp2 = mean(tmp(:).^2);
Output(i,j)=tmp2 - mu.^2;
end
end

figure,imagesc(Output);colormap(gray);



Quick Implementation:




like button Like "IMAGE PROCESSING" page

Sobel Edge Detection - Part 2

In Edge Detection- fundamentals, we have seen how the first and second order derivatives are used in finding the edge strength. Now lets see another version of sobel edge detection.

Basic Steps followed in Sobel Edge Detection:

1.       Obtain the gradient of the image.
2.       Find the magnitude
3.       Threshold the gradient image.


SOBEL EDGE DETECTION USING ‘edge’ FUNCTION:

%Input Image
A=imread('coins.png');

%Image obtained using MATLAB function 'edge'
[E,th]=edge(A,'sobel','nothinning');
figure,imshow(E);title('Image obtained using MATLAB function')




Edge Detection without using the 'edge' function: 

MATLAB CODE:

%Input Image
A=imread('coins.png');

%Preallocate the matrices with zeros
I=zeros(size(A));


%Filter Masks
F1=[-1 0 1;-2 0 2; -1 0 1];
F2=[-1 -2 -1;0 0 0; 1 2 1];

A=double(A);


for i=1:size(A,1)-2
    for j=1:size(A,2)-2
        %Gradient operations
        Gx=sum(sum(F1.*A(i:i+2,j:j+2)));
        Gy=sum(sum(F2.*A(i:i+2,j:j+2)));
               
        %Magnitude of vector
         I(i+1,j+1)=sqrt(Gx.^2+Gy.^2);
       
    end
end


I=uint8(I);
figure,imshow(I);title('Filtered Image');



%Define a threshold value
Thresh=210;
B=max(I,Thresh);
B(B==round(Thresh))=0;

B=im2bw(B);
figure,imshow(B);title('Edge detected Image');



EXPLANATION:

1.       Read the image
2.       Convert the image to double
3.       Use the mask F1 for x direction and F2 for y direction and obtain the gradient of the image.
4.       Find the magnitude of the vector.
5.       Since we need 3x3 image pixels, the border pixels are not considered, and so starting from the pixel (2, 2) the edge detection process starts.
       %Magnitude of vector
    I(i+1,j+1)=sqrt(Gx.^2+Gy.^2);
    When i=1 and j =1, then Image I pixel position will be I(2,2).Thus we are not considering the borders.
Example:

    In the for loop 2 is subtracted.
"
for i=1:size(A,1)-2
    for j=1:size(A,2)-2 "
     The filter mask is 3x3, so the last position to be processed in our example is I(3,3).And normally it will be I(size(A,1)-2,size(A,2)-2). Thus the borders are left.

Example :
               


6.       Threshold the image
7.       Display the logical image


Advantage:

1. Sobel masks perform better noise suppression.

2. Image smoothing


Disadvantage:

1.       Diagonal direction points are not preserved always.

like button Like "IMAGE PROCESSING" page

Image Sharpening using second order derivative –(Laplacian)


Prerequisite: Read EdgeDetection- fundamentals

The derivative operator Laplacian for an Image is defined as



For X-direction,

For Y-direction,


By substituting, Equations in Fig.B and Fig.C in Fig.A, we obtain the following equation







The equation  represented in terms of Mask:
0
1
0
1
-4
1
0
1
0

When the diagonals also considered then the equation becomes,



The Mask representation of the above equation,
1
1
1
1
-8
1
1
1
1

Now let’s discuss further how image sharpening is done using Laplacian.

Equation:
Where f(x,y)  is the input image
              g(x,y) is the sharpened image and
               c= -1 for the above mentioned filter masks.(fig.D and fig.E)

MATLAB CODE:

%Input Image
A=imread('coins.png');
figure,imshow(A);



%Preallocate the matrices with zeros
I1=A;
I=zeros(size(A));
I2=zeros(size(A));

%Filter Masks
F1=[0 1 0;1 -4 1; 0 1 0];
F2=[1 1 1;1 -8 1; 1 1 1];

%Padarray with zeros
A=padarray(A,[1,1]);
A=double(A);

%Implementation of the equation in Fig.D
for i=1:size(A,1)-2
    for j=1:size(A,2)-2
       
        I(i,j)=sum(sum(F1.*A(i:i+2,j:j+2)));
       
    end
end

I=uint8(I);
figure,imshow(I);title('Filtered Image');









The Laplacian derivative equation has produced grayish edge lines and other areas are made dark(background)

%Sharpenend Image
%Refer Equation in Fig.F
B=I1-I;
figure,imshow(B);title('Sharpened Image');




The Filter Image is combined with the Original input image thus the background is preserved and the sharpened image is obtained .



For a Filter Mask that includes Diagonal,


1
1
1
1
-8
1
1
1
1

Filtered Image:


Sharpened Image:



like button Like "IMAGE PROCESSING" page

Edge Detection-Fundamentals


The derivatives of a digital function are defined in terms of differences.
The above statement made me to analyze about derivatives and how it is used for edge detection.  The first time when I came across the edge detection operation [Example: edge(Image,’sobel’)], I wondered how it worked. 
Consider a single dimensional array,
A =
5
4
3
2
2
2
2
8
8
8
6
6
5
4
0

MATLAB CODE:

x=1:15;
y=[5 4 3 2 2 2 2 8 8 8 6 6 5 4 0];

figure,
plot(x,y,'-o','LineWidth',3,'MarkerEdgeColor','k','Color','y');
title('Input Array');




First-order Derivative for one dimensional function f(x):




MATLAB  CODE:

x1=1:14;
y1=diff(y,1);
figure,
plot(x1,y1,'-o','LineWidth',3,'MarkerEdgeColor','k','Color','r');


-1
-1
-1
0
0
0
6
0
0
-2
0
-1
-1
-4
































NOTE: The contiguous values are zero. Since the values are nonzero for non-contiguous values, the result will be thick edges.
The first-order derivative produces thicker edges.

Second-order Derivative for one dimensional function f(x):

















MATLAB CODE:

x2=1:13;
y2=diff(y,2);
figure,
plot(x2,y2,'-o','LineWidth',3,'MarkerEdgeColor','k','Color','g');


0
0
1
0
0
6
-6
0
-2
2
-1
0
-3































The Second-order derivative gives finer result compared to first-order derivative. It gives fine detailed thin lines and isolated points.  Let’s see how the second-order derivative used for Image sharpening (Laplacian) in my upcoming post.
like button Like "IMAGE PROCESSING" page
Next Post Home
Google ping Hypersmash.com