Sunday, April 10, 2016

Matlab: Butterworth_LowPass_Filter

%page=235
%DFT and IDFT


clc;
clear all;
close all;

imageAa = imread('5.jpg');
imageA =rgb2gray(imageAa );

[M N]=size(imageA);


%Display images

subplot(2,2,1);
imshow(imageA);
title('Image A ');


imageA=double(imageA);
%-1 *

for x=1:M
    for y=1:N
        imageA(x,y)=imageA(x,y)*(-1)^(x+y);
    end
end

%Perform 2D FFTs

h=waitbar(0,'Calculating DFT...');
for u=1:M
    for v=1:N
        sum=0.0;
        for x=1:M
            for y=1:N
                sum=sum+double(imageA(x,y))*(exp(-j*2*pi*(u*x/M+v*y/N)));
            end
        end
        F(u,v)=sum;
    end
    waitbar(u/M);
end
close(h);





subplot(2,2,2);
imshow(abs(F),[24 100000])
title('Image A DFT Magnitude');


subplot(2,2,3);
imshow(angle((F)),[-pi pi])
title('Image A DFT Phase');


%Define Filter function h
h=zeros(M,N);
d0=input('Enter the value of D0=');
n=input('Enter the order of n=');
for u=1:M
    for v=1:N
        d=((u-M/2)^2+(v-N/2)^2)^(1/2);
        h(u,v)=1/(1+(d/d0)^(2*n));       
    end
end

%Step 3
for u=1:M
    for v=1:N
        glo(u,v)=F(u,v)*h(u,v);
    end
end


%Calculating IDFT...

h=waitbar(0,'Calculating IDFT...');
for u=1:M
    for v=1:N
        suml=0.0;
        for x=1:M
            for y=1:N
                suml=suml+double(glo(x,y))*(exp(j*2*pi*(u*x/M+v*y/N)));
            end
        end
        f(u,v)=suml/(M*N);
    end
    waitbar(u/M);
end
close(h);




%-1 *
for x=1:M
    for y=1:N
        f(x,y)=f(x,y)*(-1)^(x+y);
    end
end



%image show
subplot(2,2,4);
imshow(uint8(f));
title('Image A-IDFT');

Matlab: Butterworth_HighPass_Filter

%page=235
%DFT and IDFT


clc;
clear all;
close all;

imageAa = imread('5.jpg');
imageA =rgb2gray(imageAa );

[M N]=size(imageA);


%Display images

subplot(2,2,1);
imshow(imageA);
title('Image A ');


imageA=double(imageA);
%-1 *

for x=1:M
    for y=1:N
        imageA(x,y)=imageA(x,y)*(-1)^(x+y);
    end
end

%Perform 2D FFTs

h=waitbar(0,'Calculating DFT...');
for u=1:M
    for v=1:N
        sum=0.0;
        for x=1:M
            for y=1:N
                sum=sum+double(imageA(x,y))*(exp(-j*2*pi*(u*x/M+v*y/N)));
            end
        end
        F(u,v)=sum;
    end
    waitbar(u/M);
end
close(h);





subplot(2,2,2);
imshow(abs(F),[24 100000])
title('Image A DFT Magnitude');


subplot(2,2,3);
imshow(angle((F)),[-pi pi])
title('Image A DFT Phase');


%Define Filter function h
h=zeros(M,N);
d0=input('Enter the value of D0=');
n=input('Enter the order of n=');
for u=1:M
    for v=1:N
        d=((u-M/2)^2+(v-N/2)^2)^(1/2);
        h(u,v)=1/(1+(d0/d)^(2*n));       
    end
end

%Step 3
for u=1:M
    for v=1:N
        glo(u,v)=F(u,v)*h(u,v);
    end
end


%Calculating IDFT...

h=waitbar(0,'Calculating IDFT...');
for u=1:M
    for v=1:N
        suml=0.0;
        for x=1:M
            for y=1:N
                suml=suml+double(glo(x,y))*(exp(j*2*pi*(u*x/M+v*y/N)));
            end
        end
        f(u,v)=suml/(M*N);
    end
    waitbar(u/M);
end
close(h);




%-1 *
for x=1:M
    for y=1:N
        f(x,y)=f(x,y)*(-1)^(x+y);
    end
end



%image show
subplot(2,2,4);
imshow(uint8(f));
title('Image A-IDFT');

Matlab: image_gaussian_low_pass

%page=235
%image_gaussian_low_pass.m


clc;
clear all;
close all;

imageAa = imread('6.jpg');
imageA =rgb2gray(imageAa );

[M N]=size(imageA);


%Display images

subplot(4,2,1:2);
imshow(imageA);
title('Image A ');


F=dft_img(imageA);






%Define Filter function h
 t=3;
for kk=0:5:10
   
    h=zeros(M,N);
    %d0=input('Enter the value of D0=');
    d0=kk;
    for u=1:M
        for v=1:N
            d=((u-M/2)^2+(v-N/2)^2)^(1/2);
            h(u,v)=exp(-(d^2/(2*(d0^2))));     
        end
    end
   
    %Filter show
    subplot(4,2,t);
    t=t+1;
    imshow((h));
    title(['Filter H, D0= ',int2str(kk)]);
   
    %multiply
    for u=1:M
        for v=1:N
            outPut_img(u,v)=F(u,v)*h(u,v);
        end
    end

 %Calculating IDFT...

   f=idft_img(outPut_img);


     %image show
    subplot(4,2,t);
    t=t+1;
    imshow(uint8(f));
    title(['Image IDFT D0= ',int2str(kk)]);

end

Matlab: image_gaussian_high_pass

%page=235
%image_gaussian_high_pass


clc;
clear all;
close all;

imageAa = imread('5.jpg');
imageA =rgb2gray(imageAa );

[M N]=size(imageA);


%Display images

subplot(4,2,1:2);
imshow(imageA);
title('Image A ');


F=dft_img(imageA);


%Define Filter function h
 t=3;
for kk=0:2:4
    h=ones(M,N);
    %d0=input('Enter the value of D0=');
    d0=kk;
    for u=1:M
        for v=1:N
            d=((u-M/2)^2+(v-N/2)^2)^(1/2);
            h(u,v)=1-(exp(-(d^2/(2*(d0^2)))) );     
        end
    end
   
    %Filter show
    subplot(4,2,t);
    t=t+1;
    imshow((h));
    title(['Filter H, D0= ',int2str(kk)]);
   
    %multiply
    for u=1:M
        for v=1:N
            outPut_img(u,v)=F(u,v)*h(u,v);
        end
    end


     %Calculating IDFT...

   f=idft_img(outPut_img);



    %image show
    subplot(4,2,t);
    t=t+1;
    imshow(uint8(f));
    title(['Image IDFT D0= ',int2str(kk)]);
end

Matlab: DFT and IDFT image function

%page=235
%DFT and IDFT


clc;
clear all;
close all;

imageAa = imread('6.jpg');
imageA =rgb2gray(imageAa );


%Display images

subplot(2,2,1);
imshow(imageA);
title('Image A ');
[M N]=size(imageA)



shift_F=dft_img(imageA);



subplot(2,2,2);
imshow(abs(shift_F),[24 100000])
title('Image A DFT Magnitude');


subplot(2,2,3);
%imshow(angle((shift_F)),[-pi pi]);
F2 = log(abs(shift_F));
imshow(F2,[-1 5]);
title('Image A DFT Log');


 %Calculating IDFT...

 f=idft_img(shift_F);


%image show
subplot(2,2,4);
imshow(uint8(f));
title('Image A-IDFT');

Matlab: IDFT image function

function [f]=idft_img(shift_F)


[M N]=size(shift_F);


    h=waitbar(0,'Calculating IDFT...');
    for u=0:M-1
        for v=0:N-1
            suml=0.0;
            for x=0:M-1
                for y=0:N-1
                    suml=suml+double(shift_F(x+1,y+1))*(exp(j*2*pi*(u*x/M+v*y/N)));
                end
            end
            f(u+1,v+1)=suml/(M*N);
        end
        waitbar(u/M);
    end
    close(h);




%-1 *
for x=1:M
    for y=1:N
        f(x,y)=f(x,y)*(-1)^(x+y);
    end
end

Matlab: Ideal_Low_Pass_Filter

%page=235
%Ideal_Low_Pass_Filter.m


clc;
clear all;
close all;

imageAa = imread('5.jpg');
imageA =rgb2gray(imageAa );

[M N]=size(imageA);


%Display images

subplot(4,2,1:2);
imshow(imageA);
title('Input Image');



F=dft_img(imageA);



%Define Filter function h
 t=3;
for kk=3:3:9
   
    h=zeros(M,N);
    %d0=input('Enter the value of D0=');
    d0=kk;
    for u=1:M
        for v=1:N
            d=((u-M/2)^2+(v-N/2)^2)^(1/2);
            if(d<=d0)
                h(u,v)=1;
            end
        end
    end
    %Filter show
    subplot(4,2,t);
    t=t+1;
    imshow((h));
    title(['Filter H, D0= ',int2str(kk)]);
   
    %multiply
    for u=1:M
        for v=1:N
            outPut_img(u,v)=F(u,v)*h(u,v);
        end
    end


     %Calculating IDFT...

      f=idft_img(outPut_img);


  
    %image show
    subplot(4,2,t);
    t=t+1;
    imshow(uint8(f));
    title(['Image IDFT D0= ',int2str(kk)]);
end

Matlab: Ideal_High_Pass_Filter

%page=235
%Ideal_High_Pass_Filter.m


clc;
clear all;
close all;

imageAa = imread('5.jpg');
imageA =rgb2gray(imageAa );

[M N]=size(imageA);


%Display images

subplot(4,2,1:2);
imshow(imageA);
title('Image A ');


F=dft_img(imageA);





%Define Filter function h
 t=3;
for kk=0:2:4
    h=ones(M,N);
    %d0=input('Enter the value of D0=');
    d0=kk;
    for u=1:M
        for v=1:N
            d=((u-M/2)^2+(v-N/2)^2)^(1/2);
            if(d<=d0)
                h(u,v)=0;
            end
        end
    end
    %Filter show
    subplot(4,2,t);
    t=t+1;
    imshow((h));
    title(['Filter H, D0= ',int2str(kk)]);
   
    %multiply
    for u=1:M
        for v=1:N
            outPut_img(u,v)=F(u,v)*h(u,v);
        end
    end

 %Calculating IDFT...

    f=idft_img(outPut_img);


     %image show
    subplot(4,2,t);
    t=t+1;
    imshow(uint8(f));
    title(['Image IDFT D0= ',int2str(kk)]);
end

Matlab: DFT image function

function [F]=dft_img(imageA)


[M N]=size(imageA);


imageA=double(imageA);

%-1 *
for x=1:M
    for y=1:N
        imageA(x,y)=imageA(x,y)*(-1)^(x+y);
    end
end

%Perform 2D FFTs

h=waitbar(0,'Calculating DFT...');
for u=0:M-1
    for v=0:N-1
        sum=0.0;
        for x=0:M-1
            for y=0:N-1
                sum=sum+double(imageA(x+1,y+1))*(exp(-j*2*pi*(u*x/M+v*y/N)));
            end
        end
        F(u+1,v+1)=sum;
    end
    waitbar(u/M);
end
close(h);

Matlab: Butterworth_LowPass_Filter

%page=235
%Butterworth_LowPass_Filter.m


clc;
clear all;
close all;

imageAa = imread('5.jpg');
imageA =rgb2gray(imageAa );

[M N]=size(imageA);


%Display images

subplot(4,2,1:2);
imshow(imageA);
title('Image A ');


F=dft_img(imageA);





% subplot(2,2,2);
% imshow(abs(F),[24 100000])
% title('Image A DFT Magnitude');
%
%
% subplot(2,2,3);
% imshow(angle((F)),[-pi pi])
% title('Image A DFT Phase');


%Define Filter function h
t=3;
for kk=3:3:9
   
    h=zeros(M,N);
    %d0=input('Enter the value of D0=');
    %n=input('Enter the order of n=');
    d0=kk;
    n=kk;
   
    for u=1:M
        for v=1:N
            d=((u-M/2)^2+(v-N/2)^2)^(1/2);
            h(u,v)=1/(1+(d/d0)^(2*n));       
        end
    end
    %Filter show
    subplot(4,2,t);
    t=t+1;
    imshow((h));
    title(['Filter H, D0= ',int2str(kk)]);
   
    %multiply
    for u=1:M
        for v=1:N
            outPut_img(u,v)=F(u,v)*h(u,v);
        end
    end


     %Calculating IDFT...

    f=idft_img(outPut_img);



    %image show
    subplot(4,2,t);
    t=t+1;
    imshow(uint8(f));
    title(['Image IDFT D0= ',int2str(kk)]);
end

Matlab: Butterworth_HighPass_Filter

%page=235
%Butterworth_HighPass_Filter


clc;
clear all;
close all;

imageAa = imread('5.jpg');
imageA =rgb2gray(imageAa );

[M N]=size(imageA);


%Display images

subplot(4,2,1:2);
imshow(imageA);
title('Image A ');


F=dft_img(imageA);



% subplot(2,2,2);
% imshow(abs(F),[24 100000])
% title('Image A DFT Magnitude');


% subplot(2,2,3);
% imshow(angle((F)),[-pi pi])
% title('Image A DFT Phase');


%Define Filter function h
t=3;
for kk=2:2:6
    h=zeros(M,N);
    %d0=input('Enter the value of D0=');
    %n=input('Enter the order of n=');
    d0=kk;
    n=kk;
    for u=1:M
        for v=1:N
            d=((u-M/2)^2+(v-N/2)^2)^(1/2);
            h(u,v)=1/(1+(d0/d)^(2*n));       
        end
    end
    %Filter show
    subplot(4,2,t);
    t=t+1;
    imshow((h));
    title(['Filter H, D0= ',int2str(kk)]);
    %multiply
    for u=1:M
        for v=1:N
            outPut_img(u,v)=F(u,v)*h(u,v);
        end
    end


%Calculating IDFT...
   f=idft_img(outPut_img);

  
%image show
    subplot(4,2,t);
    t=t+1;
    imshow(uint8(f));
    title(['Image IDFT D0= ',int2str(kk)]);
end

Matlab: Gradient Using manually convolution 2d

%page=167
%Gradient Using manually convolution 2d


clc;
clear all;
close all;

A=imread('8.jpg');
A=rgb2gray(A);
A=imread('rice.tif');


subplot(2,2,1);
imshow(A);
title('Original Image');

mx=[-1 -2 -1; 0 0 0; 1 2 1];

my=[-1 0 1; -2 0 2; -1 0 1];


%H = conv2(double(A),double(mx));
%V = conv2(double(A),double(my));

H=conv_img2(double(mx),double(A)) ;
%H=corr_img(double(mx),double(A)) ;

V=conv_img2(double(my),double(A));


subplot(2,2,2);
imshow(uint8(H));
title('Horizontal delp/delx');

subplot(2,2,3);
imshow(uint8(V));
title('Vertical delp/dely');


subplot(2,2,4);
imshow(uint8(sqrt(H.^2 + V.^2)));
%imshow(uint8(H));
title('Gradient Image');




%---------function

%convolution of image
function [Output] = img_conv2( mask,Input)

v=flipdim(mask,2);
w = flipdim(v,1);

[x y] = size(Input);
[m n]=size(w);

k1=(m-1)/2;
k2=(n-1)/2;
tempInput=padarray(Input,[k1 k2]);

for i = 1 : x
    for j = 1 : y
         aValue = tempInput(i,j)*w(1,1)+tempInput(i,j+1)*w(1,2)+tempInput(i,j+2)*w(1,3)+tempInput(i+1,j)*w(2,1)+tempInput(i+1,j+1)*w(2,2)+tempInput(i+1,j+2)*w(2,3)+                  tempInput(i+2,j)*w(3,1)+tempInput(i+2,j+1)*w(3,2)+tempInput(i+2,j+2)*w(3,3);
         Output(i,j)= aValue;
    end
end

Matlab: Gradient Sobel operation

%page=167
%Gradient Sobel operation


clc;
clear all;
close all;

A=imread('8.jpg');
A=rgb2gray(A);

subplot(1,2,1);

imshow(A);
title('Original Image');

mx=[-1 -2 -1; 0 0 0; 1 2 1];
my=[-1 0 1; -2 0 2; -1 0 1];

[r,c]=size(A);

C=double(A);


for i=1:r-2
    for j=1:c-2
 
        %X -direction
        Gx1=mx(1,1)*C(i,j)+mx(1,2)*C(i,j+1)+mx(1,3)*C(i,j+2);
        Gx2=mx(2,1)*C(i+1,j)+mx(2,2)*C(i+1,j+1)+mx(2,3)*C(i+1,j+2);
        Gx3=mx(3,1)*C(i+2,j)+mx(3,2)*C(i+2,j+1)+mx(3,3)*C(i+2,j+2);
        Gx=Gx1+Gx2+Gx3;
       
        %Y -direction
        Gy1=my(1,1)*C(i,j)+my(1,2)*C(i,j+1)+my(1,3)*C(i,j+2);
        Gy2=my(2,1)*C(i+1,j)+my(2,2)*C(i+1,j+1)+my(2,3)*C(i+2,j+2);
        Gy3=my(3,1)*C(i+2,j)+my(3,2)*C(i+2,j+1)+my(3,3)*C(i+2,j+2);
        Gy=Gy1+Gy2+Gy3;
        B(i,j)=uint8(sqrt(Gx.^2+Gy.^2));
    end
end


subplot(1,2,2);
imshow(B);
title('Gradient Image');

Matlab: Gradient Robert cross difference

%page=167
%Gradient Robert cross difference


clc;
clear all;
close all;

A=imread('8.jpg');
A=rgb2gray(A);

subplot(1,2,1);

imshow(A);
title('Original Image');

operators_x=[-1 0; 0 1];

x_pad=padarray(operators_x,[1 1]);


operators_y=[0 -1; 1 0];

y_pad=padarray(operators_y,[1 1]);


% A =
%
%      1     2
%      3     4
% B = padarray(A,[1 1]);
% B =
%
%      0     0     0     0
%      0     1     2     0
%      0     3     4     0
%      0     0     0     0
[r,c]=size(A);

C=double(A);


for i=1:r-2
    for j=1:c-2
 
        %X -direction
      
        Gx=x_pad(3,3)*C(i+2,j+2)+x_pad(2,2)*C(i+1,j+1);
       
        %Y -direction
        Gy=y_pad(3,2)*C(i+2,j+1)+y_pad(2,3)*C(i+1,j+2);
       
        B(i,j)=uint8(sqrt(Gx.^2+Gy.^2));
    end
end


subplot(1,2,2);
imshow(B);
title('Gradient Image');

Matlab: Gradient Using Built in convolution 2d

%page=167
%Using Built in convolution 2d


clc;
clear all;
close all;

A=imread('8.jpg');
A=rgb2gray(A);
A=imread('rice.tif');
subplot(1,2,1);

imshow(A);
title('Original Image');

mx=[-1 -2 -1; 0 0 0; 1 2 1];
my=[-1 0 1; -2 0 2; -1 0 1];


H = conv2(double(A),double(mx));
V = conv2(double(A),double(my));


subplot(1,2,2);
imshow(uint8(sqrt(H.^2 + V.^2)));
title('Gradient Image');




Matlab: Unsharp masking, HighBoost Filtering Image, De emphasize Image

%Unsharp masking with k=1
%HighBoost Filtering Image with k=5 (k>1)
%De emphasize Image with k=-5 (k<1 br="">
clear all;
close all;
A=imread('8.jpg');

%img=imread('moon.tif');
A=rgb2gray(A);

subplot(2,3,1);
imshow(A);
title('Original Image');



%to blur image

%L=[1 2 1;2 4 2;1 2 1];

L=(1/9)*[1 1 1;1 1 1;1 1 1];

blur_img=uint8(conv_img2(L,A));
%end of blur image



subplot(2,3,2);
imshow(blur_img);
title('Blurred Image');


Gmask_img=A-blur_img;
subplot(2,3,3);
imshow(Gmask_img);
title('G mask Image');

k=1;
unsharp_img=A+k*Gmask_img;
subplot(2,3,4);
imshow(unsharp_img);
title('Unsharp Image');



k=5;
highboost_img=A+k*Gmask_img;
subplot(2,3,5);
imshow(highboost_img);
title('HighBoost Filtering Image');



k=-5;
de_emphsize_img=A+k*Gmask_img;
subplot(2,3,6);
imshow(de_emphsize_img);
title('De-emphasizes  Image');


%---function---------
%convolution of image
function [Output] = img_conv2( mask,Input)

v=flipdim(mask,2);
w = flipdim(v,1);

[x y] = size(Input);
[m n]=size(w);

k1=(m-1)/2;
k2=(n-1)/2;
tempInput=padarray(Input,[k1 k2]);

for i = 1 : x
    for j = 1 : y
         aValue = tempInput(i,j)*w(1,1)+tempInput(i,j+1)*w(1,2)+tempInput(i,j+2)*w(1,3)+tempInput(i+1,j)*w(2,1)+tempInput(i+1,j+1)*w(2,2)+tempInput(i+1,j+2)*w(2,3)+                  tempInput(i+2,j)*w(3,1)+tempInput(i+2,j+1)*w(3,2)+tempInput(i+2,j+2)*w(3,3);
         Output(i,j)= aValue;
    end
end



%---function---------

%correlation function of image

function [out_img]=conv_img(mask,input_img)

img=input_img;

%totals=sum(L(:));
total=9;


[r c]=size(img);


out_img=img;
 sum=0;
for i=2:(r-1)
    for j=2:(c-1)
        sum=0;
    
        s=1;
        t=1;
        for m=(i-1):(i+1)
            for n=(j-1):(j+1)
                sum=sum+double( ((img(m,n))*mask(s,t))  );
                t=t+1;
            end
            t=1;
            s=s+1;
        end
    %    sum=sum/total;     
    out_img(i,j)=uint8 (sum);
    end
end



Matlab: convolution of image

%convolution of image
function [Output] = img_conv2( mask,Input)

v=flipdim(mask,2);
w = flipdim(v,1);

[x y] = size(Input);
[m n]=size(w);


k1=(m-1)/2;
k2=(n-1)/2;
tempInput=padarray(Input,[k1 k2]);

for i = 1 : x
    for j = 1 : y
         aValue = tempInput(i,j)*w(1,1)+tempInput(i,j+1)*w(1,2)+tempInput(i,j+2)*w(1,3)+tempInput(i+1,j)*w(2,1)+tempInput(i+1,j+1)*w(2,2)+tempInput(i+1,j+2)*w(2,3)+ tempInput(i+2,j)*w(3,1)+tempInput(i+2,j+1)*w(3,2)+tempInput(i+2,j+2)*w(3,3);
         Output(i,j)= aValue;
    end
end

Matlab: correlation function of image function

%correlation function of image

function [out_img]=corr_img(mask,input_img)

img=input_img;

%totals=sum(L(:));
total=9;


[r c]=size(img);


out_img=img;
 sum=0;
for i=2:(r-1)
    for j=2:(c-1)
        sum=0;
    
        s=1;
        t=1;
        for m=(i-1):(i+1)
            for n=(j-1):(j+1)
                sum=sum+double( ((img(m,n))*mask(s,t))  );
                t=t+1;
            end
            t=1;
            s=s+1;
        end
    %    sum=sum/total;     
    out_img(i,j)=uint8 (sum);
    end
end

Matlab: Smoothing spatial filter using manual convulation V2

%Smoothing spatial filter using manual convulation
%page=145

clear all;
close all;
img=imread('8.jpg');

%img=imread('moon.tif');
img=rgb2gray(img);

subplot(1,2,1);
imshow(img);
title('Original Image');

%L=[1 2 1;2 4 2;1 2 1];
L=(1/9)*[1 1 1;1 1 1;1 1 1];

out_img=uint8(conv_img(L,img));

subplot(1,2,2);
imshow(out_img);
title('Smoothing Image');


%------function--------
%convolution of image
function [Output] = img_conv2( mask,Input)

v=flipdim(mask,2);
w = flipdim(v,1);

[x y] = size(Input);
[m n]=size(w);


k1=(m-1)/2;
k2=(n-1)/2;
tempInput=padarray(Input,[k1 k2]);

for i = 1 : x
    for j = 1 : y
         aValue = tempInput(i,j)*w(1,1)+tempInput(i,j+1)*w(1,2)+tempInput(i,j+2)*w(1,3)+tempInput(i+1,j)*w(2,1)+tempInput(i+1,j+1)*w(2,2)+tempInput(i+1,j+2)*w(2,3)+ tempInput(i+2,j)*w(3,1)+tempInput(i+2,j+1)*w(3,2)+tempInput(i+2,j+2)*w(3,3);
         Output(i,j)= aValue;
    end
end

Matlab: Smoothing spatial filter using manual convulation V1

%Smoothing spatial filter using manual convulation
%page=145

clear all;
close all;
img=imread('8.jpg');

%img=imread('moon.tif');
img=rgb2gray(img);

subplot(1,2,1);
imshow(img);
title('Original Image');

%L=[1 2 1;2 4 2;1 2 1];
L=(1/9)*[1 1 1;1 1 1;1 1 1];

out_img=uint8(conv_img(L,img));

subplot(1,2,2);
imshow(out_img);
title('Smoothing Image');

%----------------Function----------
%convolution of image
function [Output] = img_conv2( mask,Input)
      Output=conv2(mask,Input);

Matlab: using second derivative for image sharpening - the laplacian v4

%using second derivative for image sharpening - the laplacian
%page 161
%mask 4
clc;
clear all;
close all;

A=imread('8.jpg');
A=rgb2gray(A);

subplot(1,2,1);
imshow(A);
title('Original Image');

L=[-1 -1 -1; -1 8 -1; -1 -1 -1 ];
if(L(2,2)<0 br="">    C_into=-1;
else
    C_into=1;
end


[r,c]=size(A);
B=zeros(r,c);
for i=2:(r-1)
    for j=2:(c-1)    
           m=double(A(i,j));
       
        a1=double(A(i-1,j-1));
        a2=double(A(i-1,j));
        a3=double(A(i-1,j+1));
       
        a4=double(A(i,j-1));
        a5=double(A(i,j+1));
       
        a6=double(A(i+1,j-1));
        a7=double(A(i+1,j));
        a8=double(A(i+1,j+1));
       
        sum=m*L(2,2)+L(1,1)*a1+L(1,2)*a2+L(1,3)*a3+L(2,1)*a4+L(2,3)*a5+L(3,1)*a6+L(3,2)*a7+L(3,3)*a8;
       
%         sum1=L(1,1)*A(i-1,j-1)+L(1,2)*A(i-1,j)+L(1,3)*A(i-1,j+1);
%         sum2=L(2,1)*A(i,j-1)+L(2,2)*A(i,j)+L(2,3)*A(i,j+1);
%         sum3=L(3,1)*A(i+1,j-1)+L(3,2)*A(i+1,j)+L(3,3)*A(i+1,j+1);
%         sum=double(sum1+sum2+sum3);
        C(i,j)=A(i,j)+uint8(C_into*sum);
    end
end

subplot(1,2,2);
imshow(C);
title('Sharp Image');

Matlab: using second derivative for image sharpening - the laplacian v3

%using second derivative for image sharpening - the laplacian
%page 161
%mask 3

clc;
clear all;
close all;

A=imread('8.jpg');
A=rgb2gray(A);

subplot(1,2,1);
imshow(A);
title('Original Image');

L=[0 -1 0; -1 4 -1; 0 -1 0 ];
if(L(2,2)<0 br="">    C_into=-1;
else
    C_into=1;
end


[r,c]=size(A);
B=zeros(r,c);


for i=2:(r-1)
    for j=2:(c-1)
        m=double(A(i,j));
       
        a1=double(A(i-1,j-1));
        a2=double(A(i-1,j));
        a3=double(A(i-1,j+1));
       
        a4=double(A(i,j-1));
        a5=double(A(i,j+1));
       
        a6=double(A(i+1,j-1));
        a7=double(A(i+1,j));
        a8=double(A(i+1,j+1));
       
        sum=m*L(2,2)+L(1,1)*a1+L(1,2)*a2+L(1,3)*a3+L(2,1)*a4+L(2,3)*a5+L(3,1)*a6+L(3,2)*a7+L(3,3)*a8;
       
        %do in this format not in the above 2 way
       
      
        C(i,j)=A(i,j)+uint8(C_into*sum);
    end
end

subplot(1,2,2);
imshow(C);
title('Sharp Image');

Matlab: using second derivative for image sharpening - the laplacian v2

%using second derivative for image sharpening - the laplacian
%page 161
%mask 2

clc;
clear all;
close all;

A=imread('8.jpg');
A=rgb2gray(A);

subplot(1,2,1);
imshow(A);
title('Original Image');

L=[1 1 1; 1 -8 1; 1 1 1 ];
if(L(2,2)<0 br="">    C_into=-1;
else
    C_into=1;
end


[r,c]=size(A);
B=zeros(r,c);
for i=2:(r-1)
    for j=2:(c-1)
       
        sum1=L(1,1)*A(i-1,j-1)+L(1,2)*A(i-1,j)+L(1,3)*A(i-1,j+1);
        sum2=L(2,1)*A(i,j-1)+L(2,2)*A(i,j)+L(2,3)*A(i,j+1);
        sum3=L(3,1)*A(i+1,j-1)+L(3,2)*A(i+1,j)+L(3,3)*A(i+1,j+1);
        sum=sum1+sum2+sum3;
       C(i,j)=A(i,j)+uint8(C_into*sum);
    end
end

subplot(1,2,2);
imshow(C);
title('Sharp Image');

Matlab: using second derivative for image sharpening - the laplacian

%using second derivative for image sharpening - the laplacian
%page 161
%mask 1


clc;
clear all;
close all;

A=imread('8.jpg');
A=rgb2gray(A);

subplot(1,2,1);
imshow(A);
title('Original Image');

L=[0 1 0; 1 -4 1; 0 1 0 ];
if(L(2,2)<0 br="">    C_into=-1;
else
    C_into=1;
end


[r,c]=size(A);
B=zeros(r,c);
for i=2:(r-1)
    for j=2:(c-1)
       
        sum1=L(1,1)*A(i-1,j-1)+L(1,2)*A(i-1,j)+L(1,3)*A(i-1,j+1);
        sum2=L(2,1)*A(i,j-1)+L(2,2)*A(i,j)+L(2,3)*A(i,j+1);
        sum3=L(3,1)*A(i+1,j-1)+L(3,2)*A(i+1,j)+L(3,3)*A(i+1,j+1);
        sum=sum1+sum2+sum3;
        C(i,j)=A(i,j)+uint8(C_into*sum);
    end
end

subplot(1,2,2);
imshow(C);
title('Sharp Image');

Matlab: using second derivative for image sharpening - the laplacian

%using second derivative for image sharpening - the laplacian
%page 161
%mask 1

clc;
clear all;
close all;

A=imread('8.jpg');
A=rgb2gray(A);

subplot(1,2,1);
imshow(A);
title('Original Image');

L=[0 1 0; 1 -4 1; 0 1 0 ];
if(L(2,2)<0 br="">    C_into=-1;
else
    C_into=1;
end


[r,c]=size(A);
B=zeros(r,c);
for i=2:(r-1)
    for j=2:(c-1)
       
        sum1=L(1,1)*A(i-1,j-1)+L(1,2)*A(i-1,j)+L(1,3)*A(i-1,j+1);
        sum2=L(2,1)*A(i,j-1)+L(2,2)*A(i,j)+L(2,3)*A(i,j+1);
        sum3=L(3,1)*A(i+1,j-1)+L(3,2)*A(i+1,j)+L(3,3)*A(i+1,j+1);
        sum=sum1+sum2+sum3;
        C(i,j)=A(i,j)+uint8(C_into*sum);
    end
end

subplot(1,2,2);
imshow(C);
title('Sharp Image');

Matlab: Smoothing weighted averaging filter

%Smoothing weighted averaging filter
%page=153

clear all;
close all;
img=imread('8.jpg');
img=rgb2gray(img);

subplot(1,2,1);
imshow(img);
title('Original Image');

L=[1 2 1;2 4 2;1 2 1];
%L=[1 1 1;1 1 1;1 1 1];
total=sum(L(:));

mask=(1/total)*L;


[r c]=size(img);


copy=img;

for i=2:(r-1)
    for j=2:(c-1)
        sum=0;
    
        s=1;
        t=1;
        for m=(i-1):(i+1)
            for n=(j-1):(j+1)
                sum=sum+double( ((img(m,n))*mask(s,t)) /mask(s,t) );
                t=t+1;
            end
            t=1;
            s=s+1;
        end
    %    sum=sum/total;     
    copy(i,j)=uint8 (sum);
    end
end

subplot(1,2,2);
imshow(copy);
title('Weighted Sharpen Image');

Matlab: Smoothing spatial filter

% Smoothing spatial filter
%page=145

clear all;
close all;
img=imread('8.jpg');

%img=imread('moon.tif');
img=rgb2gray(img);

subplot(1,2,1);
imshow(img);
title('Original Image');

%L=[1 2 1;2 4 2;1 2 1];
L=[1 1 1;1 1 1;1 1 1];
total=sum(L(:));

mask=(1/total)*L;


[r c]=size(img);


copy=img;

for i=2:(r-1)
    for j=2:(c-1)
        sum=0;
    
        s=1;
        t=1;
        for m=(i-1):(i+1)
            for n=(j-1):(j+1)
                sum=sum+double( ((img(m,n))*mask(s,t))  );
                t=t+1;
            end
            t=1;
            s=s+1;
        end
    %    sum=sum/total;     
    copy(i,j)=uint8 (sum);
    end
end

subplot(1,2,2);
imshow(copy);
title('Sharpen Image');

Matlab: Smoothing Filter--Simple

%Smoothing Filter--Simple
%page=145

clear all;
close all;
img=imread('8.jpg');
img=rgb2gray(img);

subplot(1,2,1);
imshow(img);
title('Original Image');


mask=(1/9)*ones(3,3);


[r c]=size(img);


copy=img;

for i=2:(r-1)
    for j=2:(c-1)
        sum=0;
        for m=(i-1):(i+1)
            for n=(j-1):(j+1)
                sum=sum+double(img(m,n));
            end
        end
    sum=sum/9;   
    copy(i,j)=uint8 (sum);
    end
end

subplot(1,2,2);
imshow(copy);
title('Sharpen Image');

Matlab: histogram matching

%histogram matching


clc
clear all;
close all;

%part 1
%image histogram original image


Img1=imread('input.tif');
[h w]=size(Img1);
count1= zeros(1,256);
s_cdf1=zeros(1,256);



%  pdf =img1

for p = 0 : 255
    number1 = find(  Img1(:)==p )
    count1(p+1) = length(number1)/(h*w);
end

%  cdf =img1

s_cdf1(1)=count1(1);
for i = 2: 256
    s_cdf1(i)=s_cdf1(i-1)+count1(i);
end


% mul by 255
for i = 1: 256
    sk(i)=s_cdf1(i)*255;
end




%part 1
%image histogram reference image

Img2= imread('ref.tif');
[h2 w2]=size(Img2);
count2 = zeros(1,256);
s_cdf2=zeros(1,256);

%  pdf =img2

for p = 0 : 255
    number2 = find(  Img2(:)==p )
    count2(p+1) = length(number2)/(h2*w2);
end

% cdf =img2

s_cdf2(1)=count2(1);

for i = 2: 256
   s_cdf2(i)=s_cdf2(i-1)+count2(i);
end

% mul by 255
for i = 1: 256
    sk2(i)=s_cdf2(i)*255;
end


%compute g(zk)-sk>=0
%sk =[0 .1 .2 .5 .5 0.5 0.9 1.0];
%sk2=[0 .1 .3 .7 .9 1.0 1.0 1.0];


pix=256; %=8
sp=zeros(1,pix);

%compute sp
%main part--------------------

for i=1:256
    for j=1:256
        if sk(i)==sk2(j)
            k=j;
            break;
        else if sk(i)< sk2(j)
                k=j-1;
                break;
            end
        end
    end
    sp(i)=k;
end


%image generation
%b= output image


b=Img1;
for p = 0 : 255
    for i = 1 : h
        for j = 1 : w
            if( Img1(i,j) == p )
                b(i,j) = sp(p+1);
            end
        end
    end
end


%image show

subplot(3,3,1);
imshow(Img1)
title('Original Image');

subplot(3,3,2);
plot(count1);
title('Original pdf');

subplot(3,3,3);
plot(s_cdf1);
title('Original cdf');

subplot(3,3,4);
imshow(Img2);
title('Reference Image');


subplot(3,3,5);
plot(count2);

title('Reference pdf');

subplot(3,3,6);
plot(s_cdf2)
title('Reference cdf');

subplot(3,3,7);
imshow(b);
title('Output Image');

subplot(3,3,8);
plot(sp);
title('Image Transformation Function');

Matlab: image histogram equlization

%image histogram equlization

image1 = imread('ranahamid.jpg');
Img2=rgb2gray(image1);

[h w]=size(Img2);
count = zeros(1,256);
sk=zeros(1,256);


%  count=probability

for p = 0 : 255
    number = find(  Img2(:)==p )
    count(p+1) = length(number)/(h*w);
end

sk(1)=count(1);

for i = 2: 256
    sk(i)=sk(i-1)+count(i);
end


pix=255;

for i=1:256
    %s(i)= ( (sk(i)-min(sk))/(max(sk)-min(sk)) )*pix+0.5;
    s(i)=sk(i)*pix+0.5;
end


%image generation



b=Img2;
for p = 0 : 255
    for i = 1 : h
        for j = 1 : w
            if( Img2(i,j) == p )
                b(i,j) = s(p+1);
            end
        end
    end
end


subplot(2,2,1:2);
p = 0:255;
plot(p/255,s);


subplot(2,2,3);
imshow(Img2)


subplot(2,2,4);
imshow(b)

Matlab: image histogram simple

%image histogram simple

image1 = imread('ranahamid.jpg');
image=rgb2gray(image1);

count = zeros(1,256);
for i = 0 : 255
    number = find( i== image(:)  )
    count(i+1) = length(number);  
end


figure(1);

subplot(2,2,1);
imshow(img);
title('Input Image');

p = 0:255;

subplot(2,2,2);
plot(count);
title('Ploted  Histogram');


subplot(2,2,3:4);

stem(p,count);
title('Stem  Histogram');

Matlab: image histogram by 10 steps

%image histogram by 10 steps

image1 = imread('ranahamid.jpg');
image=rgb2gray(image1);

count = zeros(1,261);
for i = 0 : 255
    number = find( i== image(:)  )
    count(i+1) = length(number);  
end



%2nd part
n=10;
m=round(256/n);

ans=zeros(1,m);
for i=1:m
    sum=0;
    for j=1:n
        sum=sum+count((i-1)*n+j);
    end
    ans(i)=sum;
end;





steps =10:10:260;


figure(1);

subplot(2,2,1);
imshow(img);
title('Input Image');


subplot(2,2,2);
plot(ans);
title('Ploted Normalized Histogram');


subplot(2,2,3:4);

stem(steps,ans);
title('Stem Normalized Histogram');

Matlab: image histogram normalization

%image histogram normalization

image1 = imread('ranahamid.jpg');
img=rgb2gray(image1);

[h w]=size(img);
count = zeros(1,256);
for i = 0 : 255
    number = find( i== img(:)  )
    count(i+1) = length(number)/(h*w);  
end



figure(1);

subplot(2,2,1);
imshow(img);
title('Input Image');

p = 0:255;

subplot(2,2,2);
plot(count);
title('Ploted Normalized Histogram');


subplot(2,2,3:4);

stem(p,count);
title('Stem Normalized Histogram');

Matlab: image contrast stretching v3

%image contrast stretching v3

clc;
clear all;
close all;

img1=imread('8.jpg');
%img1=imread('lena.bmp');
img1=rgb2gray(img1);
[r c]=size(img1);

x1=50;
x2=180;

y1=255;
y2=255;

for i=1:r
    for j=1:c
        x_out=img1(i,j);
        if(x_out            out_img(i,j)=x_out;
        elseif(x_out>=x1 && x_out<=x2)
                out_img(i,j)=((y2-y1)/(x2-x1))*(x_out)+y1;
        else
                out_img(i,j)=255;
        end
    end
end

subplot(2,1,1);
imshow(img1);
title('Original Image');

subplot(2,1,2);
imshow(out_img);
title('Contrast Image');

       
       
                      

Matlab: image contrast stretching v2

%image contrast stretching v2


clc;
clear all;
close all;

img1=imread('8.jpg');
%img1=imread('lena.bmp');
img1=rgb2gray(img1);
[r c]=size(img1);

x1=50;
x2=180;

y1=180;
y2=255;

for i=1:r
    for j=1:c
        x_out=img1(i,j);
        if(x_out            out_img(i,j)=x_out;
        elseif(x_out>=x1 && x_out<=x2)
                out_img(i,j)=((y2-y1)/(x2-x1))*(x_out)+y1;
        else
                out_img(i,j)=255;
        end
    end
end

subplot(2,1,1);
imshow(img1);
title('Original Image');

subplot(2,1,2);
imshow(out_img);
title('Contrast Image');

       
       
                      

Matlab: image contrast stretching v1

%image contrast stretching v1

clc;
clear all;
close all;

img1=imread('8.jpg');
%img1=imread('lena.bmp');
img1=rgb2gray(img1);
[r c]=size(img1);

x1=50;
x2=180;

y1=90;
y2=155;

for i=1:r
    for j=1:c
        x_out=img1(i,j);
        if(x_out            out_img(i,j)=x_out;
        elseif(x_out>=x1 && x_out<=x2)
                out_img(i,j)=((y2-y1)/(x2-x1))*(x_out)+y1;
        else
                out_img(i,j)=255;
        end
    end
end

subplot(2,1,1);
imshow(img1);
title('Original Image');

subplot(2,1,2);
imshow(out_img);
title('Contrast Image');

       
       
                      

Matlab: Bit plane slicing

%Bit plane slicing

clc;
clear all;
close all;

img1=imread('1.jpg');
img1=rgb2gray(img1);
[r c]=size(img1);

figure(1);
imshow(img1);
title('Input Image');

zero_img=img1*0;

figure(2);
for i=1:8
    for i_index=1:r
        for j_index=1:c
            n=bitget(img1(i_index,j_index),i);
            b_img(i_index,j_index)=bitset(zero_img(i_index,j_index),i,n);
        end
    end
   
    subplot(2,4,i);
    imshow(b_img);
    title(['Bit Plane for Bit=',int2str(i)]);
end

Matlab: Power law Gamma Transformation

%Power law Gamma Transformation

clc;
clear all;
close all;

img1=imread('8.jpg');
%img1=imread('lena.bmp');
img1=rgb2gray(img1);
[r c]=size(img1);

figure(1);
subplot(3,2,1);
imshow(img1);
title('Input Image');

double_img=im2double(img1);

test=2;
const=1;

for c=0.8:-0.2:0
    powerImg=const*double_img.^c;
    subplot(3,2,test);
    test=test+1;
    imshow(powerImg);
    title(['Power Law Image c=',num2str(c)]);
end



figure(2);

test=1;
for c=1:1:6
    powerImg=const*double_img.^c;
    subplot(3,2,test);
    test=test+1;
    imshow(powerImg);
    title(['Power Law Image c=',num2str(c)]);
end

Matlab: Log transformation

%Log transformation

clc;
clear all;
close all;

img1=imread('8.jpg');
%img1=imread('lena.bmp');
img1=rgb2gray(img1);
[r c]=size(img1);

figure(1);

subplot(2,2,1);
imshow(img1);
title('Input Image');

double_img=im2double(img1);

for c=1:3
    logImg=c*log(1+double_img);
    subplot(2,2,c+1);
    imshow(logImg);
    title(['Log Image c=',int2str(c)]);
end


Matlab: image averaging by adding noise

%image averaging by adding noise


clc;
clear all;
close all;


% assuming there are files named: '1.tif','2.tif',...'1000.tif'
im1 = imread('2.tif');


im2= imnoise(im1,'gaussian',0,0.025);
im3= imnoise(im1,'gaussian',0,0.050);
im4= imnoise(im1,'gaussian',0,0.075);
im5= imnoise(im1,'gaussian',0,0.125);

sum=double(im1)+double(im2)+double(im3)+double(im4)+double(im5); %must be double or it's look black
avg_img=(sum/5);%must be uint8 or it' look white


figure(1)

subplot(2,1,1)
imshow(im1);
title('Original Image');

subplot(2,1,2)
imshow(avg_img);
title('Average Image');


figure(2)
subplot(2,2,1)
imshow(im2)
title('Noise Image 1');

subplot(2,2,2)
imshow(im3)
title('Noise Image 2');

subplot(2,2,3)
imshow(im4)
title('Noise Image 3');

subplot(2,2,4)
imshow(im5)
title('Noise Image 4');





Matlab: show image portion of an image

%show image portion of an image

clc;
clear all;
close all;

img1=imread('8.jpg');
%img1=imread('lena.bmp');
img1=rgb2gray(img1);
[r c]=size(img1);

subplot(2,2,1);
imshow(img1);
title('Original Image');


out_img=img1(:,1:c/2);
subplot(2,2,2);
imshow(out_img);
title('Image Portion 1');


out_img=img1(1:r/2,1:c/2);
subplot(2,2,3);
imshow(out_img);
title('Image Portion 2');


out_img=img1(1:r/2,:);
subplot(2,2,4);
imshow(out_img);
title('Image Portion 3');

       
       
                      

Matlab: image subtraction of 2 image -- one from another

%image subtraction of 2 image -- one from another

clc;
clear all;
close all;

%image subtraction
clear all;
close all;

%image histogram original image


image1 = imread('ref.tif');
image2 = imread('input.tif');

Img2=image2-image1;
%Img2=uint8(double(Img1)-128);


subplot(2,2,1)
imshow(image2);
title('input image');

subplot(2,2,2)
imshow(image1);
title('Reference image');

subplot(2,2,3)
imshow(uint8(Img2));

title('subtracted image');

Matlab: image subtraction in 1 image by 128

%image subtraction in 1 image by 128

clear all;
close all;

%image histogram original image

image1 = imread('8.jpg');
Img1=rgb2gray(image1);

Img2=imsubtract(Img1,128);
%Img2=uint8(double(Img1)-128);


subplot(2,1,1)
imshow(Img1);
subplot(2,1,2)
imshow(Img2);

Matlab: black white negetive by imcomplement

%black white negetive by imcomplement
close all;
clear all;
aa=imread('ranahamid.jpg');
a=im2bw(aa);

b=imcomplement(a);

figure()
imshow(a)
figure()
imshow(b)
imwrite(b,'b.jpg');

Matlab: gray image to negetive conversion

%gray image to negetive conversion

close all;
clear all;
aa=imread('ranahamid.jpg');
a=rgb2gray(aa);

c=size(a)

%m=1;
%n=1;

for i = 1:1:c(1)
    for j = 1:1:c(2)
             b(i,j)=256-1-a(i,j);
    end
    n=1;
end

figure()
imshow(a)
figure()
imshow(b)
imwrite(b,'b.jpg');

Matlab: binary to negetive image conversion

%binary to negetive image conversion

close all;
clear all;
aa=imread('ranahamid.jpg');
a=im2bw(aa);

[h w]=size(a)

%m=1;
%n=1;

for i = 1:1:h
    for j = 1:1:w
         if( a(i,j)==0)
             b(i,j)=1;
         else
             b(i,j)=0;
       
         end
    end
    n=1;

end

figure()
imshow(a)
figure()
imshow(b)
imwrite(b,'b.jpg');

Matlab: gray to binary image

%gray to binary image


close all;
clear all;
aa=imread('ranahamid.jpg');
a=rgb2gray(aa);

c=size(a)

for i = 1:1:c(1)
    for j = 1:1:c(2)
         if( a(i,j)>=0 && a(i,j)<=127)
             b(i,j)=0;
         else
             b(i,j)=1;
         end
    end
end

figure()
imshow(a)
figure()
imshow(b)
imwrite(b,'b.jpg');

Matlab: zoom out by block by median

%zoom out by block by median

clear all;
close all;

aa=imread('r.jpg');
a=rgb2gray(aa);

c=size(a);

k=1;%height of new image
l=1;%width of new image


[height,width,color]=size(a);


sum=0;

for m =1:2:c(1)    %increase by 2
    for n =1:2:c(2)%increase by 2
         sum1=a(m,n);
         sum2=a(m,n+1);
         sum3=a(m+1,n);
         sum4=a(m+1,n+1);
        
         sum=[sum1,sum2,sum3,sum4];
                b(k,l)=median(sum);
         
         sum1=0;
         sum2=0;
         sum3=0;
         sum4=0;
        
        % n=n+2;
        
         l=l+1;
        
         if(n>=width-2)
             break;
         end
    end
   
    k=k+1;
    l=1;
   
    n=1;
  %  m=m+2;
   
    if(m>=height-2)
        break;
    end
end

figure()
imshow(a)
figure()
imshow(b)
imwrite(b,'b.jpg');

Matlab: zoom out by block by max

%zoom out by pixel reduction
close all;
clear all;
aa=imread('ranahamid.jpg');
a=rgb2gray(aa);
c=size(a)
for i = 1:2:c(1)
    for j = 1:2:c(2)
        b((i+1)/2,(j+1)/2)=a(i,j);
    end
end

figure()
imshow(a)
figure()
imshow(b)
imwrite(b,'b.jpg');

Matlab: Zoom out by pixel reduction

%zoom out by pixel reduction
close all;
clear all;
aa=imread('ranahamid.jpg');
a=rgb2gray(aa);
c=size(a)
for i = 1:2:c(1)
    for j = 1:2:c(2)
        b((i+1)/2,(j+1)/2)=a(i,j);
    end
end

figure()
imshow(a)
figure()
imshow(b)
imwrite(b,'b.jpg');

Matlab: zoom out by block

%zoom out by block
clear all;
close all;

aa=imread('r.jpg');
a=rgb2gray(aa);

c=size(a);

k=1;%height of new image
l=1;%width of new image


[height,width,color]=size(a);


sum=0;

for m =1:2:c(1)    %increase by 2
    for n =1:2:c(2)%increase by 2
         sum=sum+a(m,n);
         sum=sum+a(m,n+1);
         sum=sum+a(m+1,n);
         sum=sum+a(m+1,n+1);
        
                b(k,l)=sum/4;
         
         sum=0;
        
        % n=n+2;
        
         l=l+1;
        
         if(n>=width-2)
             break;
         end
    end
   
    k=k+1;
    l=1;
   
    n=1;
  %  m=m+2;
   
    if(m>=height-2)
        break;
    end
end

figure()
imshow(a)
figure()
imshow(b)
imwrite(b,'b.jpg');

Sunday, April 3, 2016

Zoom In an Image in MatLab

%zoom in by block
close all;
clear all;
aa=imread('r.jpg');
a=rgb2gray(aa);

c=size(a)

m=1;
n=1;

for i = 1:1:c(1)
    for j = 1:1:c(2)
         b(m,n)=a(i,j);
         b(m,n+1)=a(i,j);
         b(m+1,n)=a(i,j);
         b(m+1,n+1)=a(i,j);
         n=n+2;        
    end
    n=1;
    m=m+2;
end

figure()
imshow(a)
figure()
imshow(b)
imwrite(b,'b.jpg');

Imgage Read in MatLab

%image read operation

clc;
clear all;
close all;

I=imread('bt.JPG');

subplot(2,1,1);
imshow(I);
title('Original Image');

S=size(I);

subplot(2,1,2);
b=rgb2gray(I);
imshow(b);
title('Gray Image');



c=I(b);
imshow(c);