Sunday, April 10, 2016

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');