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

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:


A=imread('peppers.png');
B=rgb2gray(A);

C=double(B);


for i=1:size(C,1)-2
    for j=1:size(C,2)-2
        %Sobel mask for x-direction:
        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)));
        %Sobel mask for y-direction:
        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)));
     
        %The gradient of the image
        %B(i,j)=abs(Gx)+abs(Gy);
        B(i,j)=sqrt(Gx.^2+Gy.^2);
     
    end
end
figure,imshow(B); title('Sobel gradient');
Sobel Gradient

%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:

Also Check  Sobel Edge Detection - Part 2







like button Like "IMAGE PROCESSING" page

Adaptive filtering-local noise filter


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. 






























A = imread('saturn.png');
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
C = padarray(B,[floor(M/2),floor(N/2)]);


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 =  temp(:);
             %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 button Like "IMAGE PROCESSING" page

Add salt and pepper noise to image

How to add salt and pepper noise to an image


          To obtain an image with ‘speckle’ or ‘salt and pepper’ noise we need to add white and black pixels randomly in the image matrix.



First convert the RGB image into grayscale image.
Then generate random values for the size of the matrix.
Here I used MATLAB function ‘randint’.





This function will generate random values for the given matrix size within the specified range.
For instance, consider an image matrix of size 4X3


Imgmatrix =
   237   107   166
   234    95   162
   239   116   169
   56   126    89


Generate random values for a 4X3 matrix with range 0 to 10.
randint(4,3,[0,10])

ans =
     4    10     4
     7     8     4
    10     0     5
     3    10     9

    

Now we can replace with pixel value zero (black) in the image matrix if there is ‘0’ value in the random matrix.

Now the image matrix will have black pixels.

Imgmatrix =
   237   107   166
   234    95   162
   239   0   169
   56   126    89


 
Similarly, replace the image matrix pixel value with ‘255’ if there is value ‘10’ in the random matrix.
The white pixels are now added.

Imgmatrix =
   237   255   166
   234    95   162
   255   0   169
   56   126    89

Black=0 white=255

 

MATLAB CODE:


A=imread('taj.jpg');
B=rgb2gray(A);


black=3;
white=253;
%Adjust the values in 'black' and 'white' to increase the noise.

NoiseImg = B;
    Rmatrix = randint(size(B,1),size(B,2),[0,255]);
    NoiseImg(Rmatrix <= black) = 0;
    NoiseImg(Rmatrix >=white) = 255;
    RImg=medfilt2(NoiseImg);
    figure,subplot(1,2,1),imshow(NoiseImg),title('Add ''Salt and Pepper'' Noise');
    subplot(1,2,2),imshow(RImg),title('After Noise Removal');






 I used the MATLAB function 'medfilt2' to remove noise. 


like button Like "IMAGE PROCESSING" page

I can't sleep Illusion


In this illusion, I used slanted lines in two different directions  to highlight the foreground from the background.

First, the background is formed with slanting lines in one direction.

Steps to form background with slanting lines:
1.       Get the size of the foreground image. For instance, MXN =5X8
2.       Initialize a matrix of size 1XN=1X8.
3.       Generate values for the matrix.
Eg. [10  10 250 250 10 10 250 250]ie [BBWWBBWW] B-Black, W-White
4.       Initialize another matrix of size MXN. In the first row copy the random values.
5.       For the second row perform circshift. By circshift the pixel at the first column will be moved to the last column and the remaining pixels are shifted to left by one.Eg [10 250 250 10 10 250 250 10].
      Now the colors will be [BWWBBWWB].
6.       Continue to perform circshift  for all the rows.  Now the slanting lines are formed.
   10    10   250   250    10    10   250   250
   10   250   250    10    10   250   250    10
   250   250    10    10   250   250    10    10
   250    10    10   250   250    10    10   250
    10    10   250   250    10    10   250   250


MATLAB CODE:
%Read the Image
TImg=imread('sleep.jpg');

m=size(TImg,1);
n=size(TImg,2);

if(mod(n,2)~=0)
    n=n-mod(n,2);
end   
if(mod(n,4)~=0)
        n=n-mod(n,4);
end
BImg=uint8(zeros([1 n 3]));
flag=0;
%Values for first column (bbwwbbwwbbww)
for i=1:2:n
    if(flag==1)
            BImg(1,i:i+1,:)=10;
            flag=0;
    else
            BImg(1,i:i+1,:)=240;
            flag=1;
    end
end
%Initialization
A=uint8(zeros([m size(BImg,2) 3]));
Arot=uint8(zeros([m size(BImg,2) 3]));
SImg=uint8(zeros([m size(BImg,2) 3]));
FImg=uint8(zeros([m size(BImg,2) 3]));
A(1,:,:)=BImg(1,:,:);

%Slanting lines
for i=2:m
    BImg=circshift(BImg,[1,-1]);
    A(i,:,:)=BImg(1,:,:);
end



Steps to perform on foreground Image:
1.       Rotate the background image from left to right.
2.       Convert the foreground image into binary image and negate it. Then perform image multiplication with the rotated background image.

MATLAB CODE:
%Flip the image from left to right
Arot(:,:,1)=fliplr(A(:,:,1));
Arot(:,:,2)=fliplr(A(:,:,2));
Arot(:,:,3)=fliplr(A(:,:,3));
%Foreground
BImg=uint8(~im2bw(TImg));
BImg=imresize(BImg,[size(Arot,1) size(Arot,2)]);
SImg(:,:,1)=BImg.*Arot(:,:,1);
SImg(:,:,2)=BImg.*Arot(:,:,2);
SImg(:,:,3)=BImg.*Arot(:,:,3);
SImg(SImg==0)=1;

Image Mask:
1.       Multiply the background image with the binary foreground image to obtain the mask.
MATLAB CODE:
%Background Image mask
BImg1=uint8(im2bw(TImg));
BImg1=imresize(BImg1,[size(Arot,1) size(Arot,2)]);
FImg(:,:,1)=BImg1.*A(:,:,1);
FImg(:,:,2)=BImg1.*A(:,:,2);
FImg(:,:,3)=BImg1.*A(:,:,3);
FImg(FImg==0)=1;







Combine Foreground Image and the background Image mask.

MATLAB CODE:
%Combine Background mask with foreground.
Zig=FImg.*SImg;
figure,imshow(Zig);

            




GUESS THE IMAGE: 
If you can read this then give your reactions...


I used square font to make this illusion. You can download the font at:http://fontzone.net/font-download/SF+Square+Head+Bold/

Check this link for more optical Illusion Images: Images from Blog

like button Like "IMAGE PROCESSING" page

Black and White-Optical illusion

Today, Some of my friends in Facebook posted black and white optical illusion photo saying 'ITS FREAKING AWESOME'.
After seeing the image, I found it's actually black and white illusion. Yes, convert the image to binary and do logical not of the image. When you look or concentrate at one point on the image ,maybe in center you can see a logical not of the image (i.e) White pixels will be black and black pixels will be white.

The most interesting thing is, you can try with your own photo.

MATLAB CODE:


A=imread('illusion.jpg');


B=uint8(zeros(size(A)));
A=~im2bw(A);
Logical Not on Binary Image

for i=1:size(A,1)
    for j=1:size(A,2)
        if(A(i,j)==1)
            B(i,j,:)=255;
        end
    end
end

Stare at the dots on image for 20 to 30 secs. Close your eyes or look up towards the ceiling.
You can see a binary image. i.e im2bw(A);

To draw the dots on the image:

C=uint8(ones(4 ,4, 3));
C(:,:,1)=C(:,:,1)*200;
C(:,:,2)=C(:,:,2)*0;
C(:,:,3)=C(:,:,3)*250;
%Find the mid point of the image and draw 3 dots.


midx=round(size(B,1)/2);
midy=round(size(B,2)/2)+30;
for i=1:size(C,1)
    for j=1:size(C,2)
B(midx+i+5,midy+j,:)=C(i,j,:);
B(midx+i-5,midy+j,:)=C(i,j,:);
B(midx+i+15,midy+j,:)=C(i,j,:);
    end
end

















Check this Illusion also:I cant sleep Illusion
Check this link for more optical Illusion Images: Images from Blog


like button Like "IMAGE PROCESSING" page

Darker Edges


Captured Image

Last week my friend was in need of her signature in digital format. Fortunately, it was not for any official purpose. So we captured the signature using the camera and used it.

The image that we acquired using the camera was not that much good. So we processed the image and then included in the document.

After this thing happened, I thought of trying the image with MATLAB.
First I adjusted the image intensity using the imadjust function . 
The syntax for the imadjust for rgb image is
imadjust(RGB,[Low_R Low_G Low_B; High_R High G High B],[]);
The Low values should always be lower than High values.
The range of these values between 0 to 1.
After Imadjust

Then I detected the edge of the image. Then to enlarge the image parts I dilated it. Use an appropriate structure element. Here I used 'disk'.
After that, using labeling method, I labeled all the connected components. Based on the size of the components the components where finally used and components whose size are less are ignored.
Edge detected Image

MATLAB CODE:



function my_edge
global filename A I ;
scz=get(0,'ScreenSize');
figure('Position',[round(scz(1,3)/4) round(scz(1,4)/8) 700 500],'MenuBar','None','NumberTitle','off','Name','Pencil sketch Application','Resize','off');
axes('Position',[0 0 .7 1],'xtick',[],'ytick',[]);
Low=uicontrol('Style','slider','Position',[500,280 200 20],'Max',0.99 ,'Min',0,'Value',0.0,'SliderStep',[0.05,0.1],'Callback',@draw);
High=uicontrol('Style','slider','Position',[500,370 200 20],'Max',1,'Min',0.1,'Value',.5,'SliderStep',[0.05,0.1],'Callback',@draw);
uicontrol('Style','pushbutton','String','Done','Position',[500 200 40 20],'Callback',@pushme);
directory=dir('*.jpg');
files={directory.name}';
uicontrol('style','text','Position',[500,310 100 20]','String','Low:');
uicontrol('style','text','Position',[500,395 100 20]','String','High :');
uicontrol('style','text','Position',[500,455 100 20]','String','Filename:');
uicontrol('Style','popupmenu','position',[500 420 160 30],'Value',1,'String',files,'Callback',@displayfile);
    function displayfile(obj,eve)
        ptr=get(obj,'value');
        filename=char(files(ptr));
        A=imread(filename);
        A=imresize(A,0.4);
        subplot('Position',[0.05 0.1 .6 .9]);imshow(A);
    end
    function draw(obj,eve)
        H=get(High,'Value');
        L=get(Low,'Value');
      uicontrol('Style','edit','position',[500 250 50 20],'String',L);
      uicontrol('Style','edit','position',[500 340 50 20],'String',H);
        if( L < H )
            %The intensity values for each R,G,B planes are adjusted.
            I=imadjust(A,[L L L; H H H],[]);
        end
           subplot('Position',[0.05 0.1 .6 .9]);imshow(I);
       end
                                                                                                         
                                                                                        
                                                                                         
                                                                                   
                                                                                    
    function pushme(obj,eve)
        myedges(I);
    end
    function myedges(A)
      
A1=rgb2gray(A);
B=(edge(A(:,:,1))|edge(A(:,:,2))|edge(A(:,:,3)));

Edge detected




SE=strel('disk',3);
C=~(imdilate(B,SE));
target=(ones([size(A1,1) size(A1,2)] ));
[BW,label]=bwlabel(~C,8);
for l=1:max(max(label))
[row, col] = find(BW==l);
if(size(row,1)>30)
for i=1:size(row,1)
    x=row(i,1);
    y=col(i,1);
    target(x,y)=C(x,y);
end
end
end


subplot('Position',[0 0.5 .8 .45]);imshow(A);title('Original Image');
subplot('Position',[0 0   .8 .45]);imshow(target);title('Final Image');
    end
end


Another example:

After Imadjust and edge detection (structuring element:disk Radius=1)


like button Like "IMAGE PROCESSING" page
Previous Post Next Post Home
Google ping Hypersmash.com