DIP Programs
DIP Programs
Values below 0 are set to 0 and values about 255 are set to 255
Code:
clear all;
close all;
clc;
%% Reading an image
a=imread('cameraman.tif');
a=double(a);
s=size(a);
%% Defingin points and calculating equation parameters
p1=[0,0];
p2=[150,20];
p3=[200,200];
p4=[255,255];
m1=(p1(1,2)-p2(1,2))/(p1(1,1)-p2(1,1));
m2=(p2(1,2)-p3(1,2))/(p2(1,1)-p3(1,1));
m3=(p3(1,2)-p4(1,2))/(p3(1,1)-p4(1,1));
c1=p1(1,2)-m1*p1(1,1);
c2=p2(1,2)-m2*p2(1,1);
c3=p3(1,2)-m3*p3(1,1);
%% Transformation function
t=[];
for x=0:255
if(x<=p2(1,1))
t=[t (m1*x+c1)];
end
if(x>p2(1,1) && x<=p3(1,1))
t=[t (m2*x+c2)];
end
if(x>p3(1,1) && x<=p4(1,1))
t=[t (m3*x+c3)];
end
end
%% Getting output image
for n=1:s(1,1)
for m=1:s(1,2)
ot(n,m)=t(a(n,m)+1);
end
end
plot(t)
grid on;
xlabel('Intensity in input image');
ylabel('Intensity in output image')
title('Piece-wise linear transformation : Contrast stretching function')
figure()
subplot(1,2,1)
imshow(a/255)
title('Original image')
subplot(1,2,2)
imshow(ot./255)
title('Contrast stretching')
2. Zooming by interpolation and replication
Explaination:
It is also known as Nearest neighbor interpolation. As its name suggest, in this method,
we just replicate the neighboring pixels. As we have already discussed in the tutorial of
Sampling, that zooming is nothing but increase amount of sample or pixels. This
algorithm works on the same principle.
Working:
In this method we create new pixels form the already given pixels. Each pixel is
replicated in this method n times row wise and column wise and you got a zoomed
image. Its as simple as that.
For example:
if you have an image of 2 rows and 2 columns and you want to zoom it twice or 2 times
using pixel replication, here how it can be done.
For a better understanding, the image has been taken in the form of matrix with the pixel values
of the image.
1 2
3 4
The above image has two rows and two columns, we will first zoom it row wise.
1 1 2 2
3 3 4 4
As you can that in the above matrix, each pixel is replicated twice in the rows.
Column size zooming:
The next step is to replicate each of the pixel column wise, that we will simply copy the
column pixel to its adjacent new column or simply below it.
Here how it would be done.
1 1 2 2
1 1 2 2
3 3 4 4
3 3 4 4
Code:
% Zooming
Explaination:
Histogram equalization is used to enhance contrast. It is not necessary that contrast
will always be increase in this. There may be some cases were histogram equalization
can be worse. In that cases the contrast is decreased.
Lets start histogram equalization by taking this image below as a simple image.
Image
PMF
First we have to calculate the PMF (probability mass function) of all the pixels in this
image. If you donot know how to calculate PMF, please visit our tutorial of PMF
calculation.
CDF
Our next step involves calculation of CDF (cumulative distributive function). Again if
you donot know how to calculate CDF , please visit our tutorial of CDF calculation.
0 0.11
1 0.22
2 0.55
3 0.66
4 0.77
5 0.88
6 0.99
7 1
Then in this step you will multiply the CDF value with (Gray levels (minus) 1) .
Considering we have an 3 bpp image. Then number of levels we have are 8. And 1 subtracts 8 is
7. So we multiply CDF by 7. Here what we got after multiplying.
0 0.11 0
1 0.22 1
2 0.55 3
3 0.66 4
4 0.77 5
5 0.88 6
6 0.99 6
7 1 7
Now we have is the last step, in which we have to map the new gray level values into
number of pixels.
Lets assume our old gray levels values has these number of pixels.
0 2
1 4
2 6
3 8
4 10
5 12
6 14
7 16
0 0 2
1 1 4
2 3 6
3 4 8
4 5 10
5 6 12
6 6 14
7 7 16
Now map these new values you are onto histogram, and you are done.
Lets apply this technique to our original image. After applying we got the following
image and its following histogram.
imshow(uint8(z));
title('Grey level slicing with background');
7. Program to performs gray level slicing without background
% Image