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
%Boundary Detection - Local Variance
%Read an image
I = imread('rice.png');
I = double(I);
A grayscale image is taken as input for edge detection. If the input image is RGB then convert it to gray scaleusing ‘rgb2gray’.
%Define the window size
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');
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.
%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');
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.