Friday, March 30, 2012

Image Processing by point processing

Image Processing by point processing

Image Processing by point processing

This program will demonstrate various image enhancement techniques using gray level manipulation

Contents

Start

clear all;
close all;
clc;

Read the Image

x1 = imread('coins.png');
x = double(x1)/255;
imshow(x);

Digital Negative

In this method: f(x,y) = L-Im(x,y), where L is the largest gray level in the image
x_neg = 1-x;
figure; imshow(x_neg)


Log Transformation

In this method: f(x,y) = c*log(Im(x,y)) where c is any constant and Im > 0
for c = 0.4
c = 0.4;
x_log = c*log(1+x);
figure; imshow(abs(x_log));


For c = 3
c = 3;
x_log = c*log(1+x);
figure; imshow(abs(x_log));

Power Law Transformation

In this method: f(x,y) = c*Im(x,y)^g where c and g are positive constants
for c = 0.5, g = 2
c = 0.5;
g = 2;
x_pow = c*x.^g;
figure; imshow(abs(x_pow));
for 1.5 = 2, g = 0.5
c = 1.5;
g = 0.5;
x_pow = c*x.^g;
figure; imshow(abs(x_pow));

Contrast Stretching

This program demonstrate piecewise continuous gray level transformation here we user three regions as: 0 to a1 transformed with the slope of m1 and a2 to 255 with slope of m1
m1 = 2.5;
m3 = 0.8;
a1 = 50;
a2 = 120;
c1 = 255 - 255*m3;
m2 = (m3*a2+c1-m1*a1)/(a2-a1);
c2 = m1*a1-m2*a1;
[row col] = size(x1);
x_contrast = ones(row,col);
for i = 1:row
    for j = 1:col
        if x1(i,j) < a1
            x_contrast(i,j) = x1(i,j)*m1;
        elseif x1(i,j) > a2
            x_contrast(i,j) = x1(i,j)*m3+c1;
        else
            x_contrast(i,j) = x1(i,j)*m2;
        end
    end
end
figure;imshow(uint8(x_contrast));
y = [(0:a1-1)*m1 ((a1:a2-1))*m2+c2 (a2:255)*m3+c1];
figure; plot(y);



Gray Level Slicing

This program demonstrate the gray level slicing operation on image
Without Background:
x_GLS = ones(row,col);
a = 48;
b = 80;
c = 190;
for i = 1:row
    for j = 1:col
        if x1(i,j) < a || x1(i,j) > b
            x_GLS(i,j) = c;
        else
            x_GLS(i,j) = 0;
        end
    end
end
figure;imshow(uint8(x_GLS));
y = [(0:a1-1)*0 (ones(1,a2-a1+1))*c (a2:255)*0];
figure; plot(y);





With Background:
c = 190;
for i = 1:row
    for j = 1:col
        if x1(i,j) < a || x1(i,j) >b
            x_GLS(i,j) = c;
        else
            x_GLS(i,j) = x1(i,j);
        end
    end
end
figure;imshow(uint8(x_GLS));
y = [(0:a1-1) (ones(1,a2-a1+1))*c (a2:255)];
figure; plot(y);

Thursday, March 29, 2012

Spatial Domain Filtering

Spatial Domain Filtering

Spatial Domain Filtering

This program demonstrate the image filtering in spatial domain using various types of box filters

Contents

Start

clear all;
close all;
clc;

Choose your mask

hLP = ones(3,3)/9;
hLap = [0 -1 0; -1 4 -1; 0 -1 0]; %Laplacian
hPreH = [-1 -1 -1; 0 0 0; 1 1 1]; %Prewitt
hPreV = [1 0 -1; 1 0 -1; 1 0 -1]; %Prewitt
hSobV = [1 0 -1; 2 0 -2; 1 0 -1]; %Sobel
hSobH = [-1 -2 -1; 0 0 0; 1 2 1]; %Sobel
hHP = [-1 -1 -1; -1 8 -1; 1 -1 -1]; %Sharpening
hHB = [-1 -1 -1; -1 8.2 -1; 1 -1 -1]; % High boost
hUnsharp = [-1 -1 -1; -1 1 -1; -1 -1 -1]/9; %Unsharp Masking

Read your image

x = imread('coins.png');
x = double(x)/255;

Filter the image

imLP = conv2(x,hLP,'same');
imLap = conv2(x,hLap,'same');
imPreH = conv2(x,hPreH,'same');
imPreV = conv2(x,hPreV,'same');
imPre = sqrt(imPreH.^2+imPreV.^2);
imSobH = conv2(x,hSobH,'same');
imSobV = conv2(x,hSobV,'same');
imSob = sqrt(imSobH.^2+imSobV.^2);
imHP = conv2(x,hHP,'same');
imHB = conv2(x,hHB,'same');
imUnsharp = conv2(x,hUnsharp,'same');

Display the result

Original Image
imshow(x);


Low Pass Image
figure; imshow(abs(imLP));


Laplacian Image
figure; imshow(abs(imLap));


Prewitt Operated Image
figure; imshow(abs(imPre));


Sobel Operated Image
figure; imshow(abs(imSob));


High Pass Image
figure; imshow(abs(imHP));
High Boost Filtering
figure; imshow(abs(imHB));


Unsharp Masking
figure; imshow(abs(imUnsharp));




Wednesday, March 28, 2012

Frequency Domain Filtering

Frequency Domain Filtering

Frequency Domain Filtering

This program demonstrate the frequency domain filtering techniques like lowpass, high pass, laplacian filtering etc

Contents

Start

clear all;
close all;
clc;

Read the Image

%x1 = imread('broken.jpg');
%x1 = double(rgb2gray(x1))/255;
x1 = imread('cameraman.tif');
%x1 = imread('coins.png');
x1 = double(x1)/255;
%x1 = ones(9,9);

Take the fourier transform

F = fft2(x1);

Filter Design

[x y] = size(F);
d0 = 50;
H = ones(size(F));
for i = 1:x-1
for j =1:y-1
dist = ((i-x/2)^2 + (j-y/2)^2)^.5;
if dist > d0
H(i,j) = 0;
end
end
end

Pass the image through filter

F = fftshift(F);
Fhat = H.*F;
Fhat = fftshift(Fhat);
r = ifft2(Fhat);
r = abs(r/max(max(r)));

Display the result

Original Image
imshow(x1);

Filtered image
figure;imshow(r);