Sunday, April 10, 2016

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