# IMAGE PROCESSING

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

## Recent Posts

### Sobel edge detection

The gradient of the image is calculated for each pixel position in the image.

The procedure and the MATLAB  code for sobel edge detection without using MATLAB built-in function:

MATLAB CODE:

B=rgb2gray(A);

C=double(B);

for i=1:size(C,1)-2
for j=1:size(C,2)-2
Gx=((2*C(i+2,j+1)+C(i+2,j)+C(i+2,j+2))-(2*C(i,j+1)+C(i,j)+C(i,j+2)));
Gy=((2*C(i+1,j+2)+C(i,j+2)+C(i+2,j+2))-(2*C(i+1,j)+C(i,j)+C(i+2,j)));

%B(i,j)=abs(Gx)+abs(Gy);
B(i,j)=sqrt(Gx.^2+Gy.^2);

end
end

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

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

 Edge detected Image

 Edge detected Image(Threshold value:35)
The edge detected image can be obtained from the sobel gradient by
using a threshold value.

• If the sobel gradient values are lesser than the threshold value then replace it with the threshold value.
if f < threshold value then
f = threshold value.

To avoid complex computation, the gradient can also be computed using the formula:

The Image obtained from computing X-direction derivative:

The Image obtained from computing Y-direction derivative:

### Check  Sobel Edge Detection - Part 2

Like "IMAGE PROCESSING" page

Adaptive filter is performed on the degraded image that contains original image and noise. The mean and variance are the two statistical measures that a local adaptive filter depends with a defined mxn window region.

B=rgb2gray(A);
sz=size(B,1)*size(B,2);

%Add gaussian noise with mean 0 and variance 0.005
B = imnoise(B,'gaussian',0,0.005);
figure,imshow(B); title('Image with gaussian noise');

B= double(B);

%Define the window size mxn
M=5;
N=5;

%Pad the matrix with zeros on all sides

lvar=zeros([size(B,1) size(B,2)]);
lmean=zeros([size(B,1) size(B,2)]);
temp=zeros([size(B,1) size(B,2)]);
NewImg=zeros([size(B,1) size(B,2)]);

for i=1:size(C,1)-M-1
for j=1:size(C,2)-N-1

temp=C(i:i+(M-1),j:j+(N-1));
tmp=reshape(temp,[1,(M*N)]);
%Find the local mean and local variance for the local region
lmean(i,j)=mean(tmp);
lvar(i,j)=mean(tmp.^2)-mean(tmp).^2;

end
end

%Noise variance and average of the local variance
nvar=sum(lvar(:))/sz;

%If noise_variance > local_variance then local_variance=noise_variance
lvar=max(lvar,nvar);

%Final_Image=B- (noise variance/local variance)*(B-local_mean);
NewImg=nvar./lvar;
NewImg=NewImg.*(B-lmean);
NewImg=B-NewImg;

%Convert the image to uint8 format.
NewImg=uint8(NewImg);
figure,imshow(NewImg);title('Restored Image using Adaptive Local filter');

Like "IMAGE PROCESSING" page