Samstag, 26. April 2014

Image Zooming – Part 1


Let’s try to view an Image of size 1366 x 768.
To view the image:
I = imread('Vatican-City-Wallpaper_.jpg');
figure,imshow(I);
Reference:
The image is too big. I want to view the image part by part, so that I can view the details in the image more clearly.


Method one:
I use MATLAB function ‘imshow’ and the ‘figure’ command.
To view the image:
I = imread('Vatican-City-Wallpaper_jpg');
figure,imshow(I);

Now click the ‘zoom in’ option to enlarge the image.
ZOOM IN


Click the ‘pan’ option and drag the cursor to view the image part by part.
 
PAN OPTION

Method two:
We can do the image viewing using our own code instead of using ‘zoom in’ and ‘pan’ options.
We will define our own window and display the image alone.
%Read the Image
I=imread('Vatican-City-Wallpaper_.jpg');
Im=I;


%Get the Screen size and define the window
scrsz = get(0,'ScreenSize');
figure('Position',[300 50 scrsz(3)-600 scrsz(4)-200],'Menu','none','NumberTitle','off','Name','VIEW PHOTO','Resize','off');

%Size of the Image Portion
Len=round(size(I,1)/4);
Flag=1;
x=1;
wid=Len;
y=1;
ht=Len;

%Display the image
ax=axes('Position',[0 .1 1 .8],'xtick',[],'ytick',[]);
imshow(Im(x:wid,y:ht,:));

Num=50;
while(Flag==1)
try
    %User button click
    waitforbuttonpress;
    pt=get(ax,'CurrentPoint');
    y1=round(pt(1,1));
    x1=round(pt(1,2));
   
    %Calculate the Next part of the image based on the button press
    if( x1 < Len && x1 > 0 && y1 < Len && y1 > 0)
   
    x=x+x1;
    y=y+y1;
    if(x-Num>1)
      
        x=x-Num;
    end
   
    if(y-Num>1)
      
        y=y-Num;
    end
   
    if(x+wid>size(I,1))
      
        x=size(I,1)-wid;
    end
   
    if(y+ht>size(I,2))
      
        y=size(I,2)-ht;
    end
   %New Image Part
   imshow(Im(x:x+wid,y:y+ht,:));
  
   wid=Len;
   ht=Len;
  
    end
catch
    Flag=0;
end

end

 
Click on the bottom right part of the image to view the center part

Click to the right from the above mentioned part 

Freitag, 25. April 2014

Gaussian Filter without using the MATLAB built_in function

Gaussian Filter



Gaussian Filter is used to blur the image. It is used to reduce the noise and the image details.










          The Gaussian kernel's center part  ( Here 0.4421 )  has the highest value and  intensity of other pixels                 decrease as the distance from the center part increases.


               On convolution of the local region and the Gaussian kernel gives the highest intensity value to the center part of the local region(38.4624) and the remaining pixels have less intensity as the distance from the center increases.
               Sum up the result and store it in the current pixel location(Intensity = 94.9269) of the image.




MATLAB CODE:
%Gaussian filter using MATLAB built_in function
%Read an Image
Img = imread('coins.png');
A = imnoise(Img,'Gaussian',0.04,0.003);
%Image with noise
figure,imshow(A);

H = fspecial('Gaussian',[9 9],1.76);
GaussF = imfilter(A,H);
figure,imshow(GaussF);


MATLAB CODE for Gaussian blur WITHOUT built_in function:
%Read an Image
Img = imread('coins.png');
A = imnoise(Img,'Gaussian',0.04,0.003);
%Image with noise
figure,imshow(A);
 
Image with Noise
I = double(A);

%Design the Gaussian Kernel
%Standard Deviation
sigma = 1.76;
%Window size
sz = 4;
[x,y]=meshgrid(-sz:sz,-sz:sz);

M = size(x,1)-1;
N = size(y,1)-1;
Exp_comp = -(x.^2+y.^2)/(2*sigma*sigma);
Kernel= exp(Exp_comp)/(2*pi*sigma*sigma);
 
Gaussian Kernel 9x9 size with Standard Deviation =1.76
%Initialize
Output=zeros(size(I));
%Pad the vector with zeros
I = padarray(I,[sz sz]);

%Convolution
for i = 1:size(I,1)-M
    for j =1:size(I,2)-N
        Temp = I(i:i+M,j:j+M).*Kernel;
        Output(i,j)=sum(Temp(:));
    end
end
%Image without Noise after Gaussian blur
Output = uint8(Output);
figure,imshow(Output);
After Filtering