You are on page 1of 21

Digital Image Processing

Lab Experiment-1

Aim: Gray-level Mapping

Apparatus Used:

 A PC with LabVIEW software


 Vision Toolbox

Theory:

In photography and computing, a graylevel or greyscale digital image is an image in


which the value of each pixel is a single sample, that is, it carries only intensity
information. Images of this sort, also known as black-and-white, are composed
exclusively of shades of gray, varying from black at the weakest intensity to white at
the strongest.

Procedure

1. Open LabVIEW from desktop.

2. Create a separate editor window and front panel.

3. Right click, select vision motion and choose IMAQ create and choose image
type(specify U32) as grayscale.

4. Create control by choosing read image (IMAQ Read) and specifying the file path
(from which the image is to be acquired).

5. Similarly, create control for writing by choosing write image (IMAQ Write).

6. Create an indicator image for display to the front panel and join.

Block Diagram:
Output:

Result :

Thus ,the program to display level mapping using read and write operation was created.
Lab Experiment-2

Aim: To obtain negative transformation of image

Apparatus Used:

 Matlab

Theory:

Negative transformation, which is invert of identity transformation. In negative


transformation, each value of the input image is subtracted from the L-1 and mapped
onto the output image.

In this case the following transition has been done.

s = (L – 1) – r

since the input image of Einstein is an 8 bpp image, so the number of levels in this
image are 256. Putting 256 in the equation, we get this

s = 255 – r

So each value is subtracted by 255 and the result image has been shown above. So what
happens is that, the lighter pixels become dark and the darker picture becomes light.
And it results in image negative.

It has been shown in the graph below.

Program:

%Matlab Code:

clear all; close all;

img = imread('breast.jpg');

img2 = 1 - im2double(img);

figure;

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


subplot(1,2,2); imshow(img2);

title('Image after Negative Transform');

Block Diagram:

Output:
Result:

We have obtained the negative transformation of images.


Lab Experiment-3

Aim: To obtain gamma transformation of image with value 𝛾 : 0.50, 0.10, 2.0, 4.0

Apparatus Used:

 Matlab

Theory:

Gama transformations can be given by the expression:


s=cr^γ
This symbol γ is called gamma, due to which this transformation is also known as gamma
transformation.
Variation in the value of γ varies the enhancement of the images. Different display devices /
monitors have their own gamma correction, that’s why they display their image at different
intensity.
This type of transformation is used for enhancing images for different type of display
devices. The gamma of different display devices is different. For example Gamma of CRT
lies in between of 1.8 to 2.5, that means the image displayed on CRT is dark.

Correcting gamma.

s=cr^γ
s=cr^(1/2.5)

Program:

%Matlab Code

clear all; close all;

img = imread('spine.jpg');

img2 = double(img).^(0.6); img3 = double(img).^(0.4); img4 = double(img).^(0.3);

img2 = [img2 - min(img2(:))] ./ max(img2(:) - min(img2(:)));

img3 = [img3 - min(img3(:))] ./ max(img3(:) - min(img3(:)));

img4 = [img4 - min(img4(:))] ./ max(img4(:) - min(img4(:)));

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

subplot(2,2,2); imshow(img2); title('Image after Gamma Transform, \gamma = 0.6');


subplot(2,2,3); imshow(img3); title('Image after Gamma Transform, \gamma = 0.4');
subplot(2,2,4); imshow(img4); title('Image after Gamma Transform, \gamma = 0.3');
Output:

Result:

We have obtained the gamma transformation on the various scales.


Lab Experiment-4

Aim:

To obtain the Logarithmic Mapping of the input images.

Apparatus used:

 Matlab

Theory:

The log transformations can be defined by this formula


s = c log(r + 1).
Where s and r are the pixel values of the output and the input image and c is a constant. The
value 1 is added to each of the pixel value of the input image because if there is a pixel
intensity of 0 in the image, then log (0) is equal to infinity. So 1 is added, to make the
minimum value at least 1.
During log transformation, the dark pixels in an image are expanded as compare to the
higher pixel values. The higher pixel values are kind of compressed in log transformation.
This result in following image enhancement.
The value of c in the log transform adjust the kind of enhancement you are looking for. The
inverse log transform is opposite to log transform.
Calculation of C value:
The scaling constant c is chosen so that the maximum output value is 255 (providing an 8-
bit format).

Program:
%Matlab Code
clear all; close all;
img = imread('fourierspectrum.jpg');
img2 = log10(1+256*im2double(img));
img2 = [img2 - min(img2(:))] ./ max(img2(:) - min(img2(:)));
figure; subplot(1,2,1); imshow(img); title('Original Image');
subplot(1,2,2); imshow(img2); title('Image after Logarithmic

Output:
Result:

We have obtained the log transformation on the images.


Lab Experiment-5

Aim:

To obtain the exponential Mapping of the input images.

Apparatus used:

 Matlab

Theory:

The exponential and `raise to power' operators are two anamorphosis operators which
can be applied to grayscale images. Like the logarithmic transform, they are used to
change the dynamic range of an image. However, in contrast to the logarithmic
operator, they enhance high intensity pixel values.

The resulting image is given by

where P and Q are the input and output images, respectively, b is the basis and c is the
scaling factor. As we can see from the formula, P=0 yields Q=c. To avoid the resulting
offset, many implementations subtract 1 from the exponential term before the scaling. Hence,
we get the following formula:

Program:

I=imread('trees.tif');

subplot(2,2,1);

imshow(J);

title('original image');

f=ones(3,3)/9;

h=imfilter(I,f,'circular');

subplot(2,2,2); imshow(h);

title(exponential image');
Output:

Result:

We have obtained the exponential transformation on the images.


Lab Experiment-6

Aim:

To obtain the Intensity Level Slicing of the input images.

Apparatus used:

 Matlab

Theory:

Intensity level slicing means highlighting a specific range of intensities in an image. In other
words, we segment certain gray level regions from the rest of the image.

Suppose in an image, your region of interest always take value between say 80 to 150. So,
intensity level slicing highlights this range and now instead of looking at the whole image,
one can now focus on the highlighted region of interest.

Since, one can think of it as piecewise linear transformation function so this can be
implemented in several ways. Here, we will discuss the two basic type of slicing that is more
often used.

 In the first type, we display the desired range of intensities in white and suppress all
other intensities to black or vice versa. This results in a binary image. The
transformation function for both the cases is shown below.

 In the second type, we brighten or darken the desired range of intensities(a to b as


shown below) and leave other intensities unchanged or vice versa. The transformation
function for both the cases, first where the desired range is changed and second where
it is unchanged, is shown below.
Program:

import cv2

import numpy as np

# Load the image

img = cv2.imread('D:/downloads/forest.jpg',0)

# Find width and height of image

row, column = img.shape

# Create an zeros array to store the sliced image

img1 = np.zeros((row,column),dtype = 'uint8')

# Specify the min and max range

min_range = 10

max_range = 60

# Loop over the input image and if pixel value lies in desired range set it to 255 otherwise
set it to 0.

for i in range(row):

for j in range(column):

if img[i,j]>min_range and img[i,j]<max_range:

img1[i,j] = 255

else:

img1[i,j] = 0

# Display the image

cv2.imshow('sliced image', img1)

cv2.waitKey(0)
Output:

Result:

We have obtained the Intensity-level slicing of the images.


Lab Experiment-7

Aim:

To obtain the Bit-plane slicing of the input images.

Apparatus used:

 Matlab

Theory:

 Pixels are digital numbers, each one composed of bits. Instead of highlighting
gray-level range, we could highlight the contribution made by each bit.
 This method is useful and used in image compression.

 Most significant bits contain the majority of visually significant data.

Program:

%Matlab Code
clear all; close all;
img = imread('fractal.jpg');

img7 = img; img6 = img; img5 = img; img4 = img;


img3 = img; img2 = img; img1 = img; img0 = img;
img7 = double(bitand(img,128)); img6 = double(bitand(img,64));
img5 = double(bitand(img,32)); img4 = double(bitand(img,16));
img3 = double(bitand(img,8)); img2 = double(bitand(img,4));
img1 = double(bitand(img,2)); img0 = double(bitand(img,1));

figure;
subplot(2,2,1); imshow(img7); title('Bit-plane 7');
subplot(2,2,2); imshow(img6); title('Bit-plane 6');
subplot(2,2,3); imshow(img5); title('Bit-plane 5');
subplot(2,2,4); imshow(img4); title('Bit-plane 4');

figure;

subplot(2,2,1); imshow(img3); title('Bit-plane 3');


subplot(2,2,2); imshow(img2); title('Bit-plane 2');
subplot(2,2,3); imshow(img1); title('Bit-plane 1');
subplot(2,2,4); imshow(img0); title('Bit-plane 0');

Output:
Result:

We have successfully obtained the bit-slice images.


Lab Experiment-8

Q. Histogram Processing

Write code for calculating the histogram of the image (input image 19). Display
the output as the “Histogram Graph”

Answer:

%Matlab Code
clear all; close all;
img1 = imread('pollen_dark.jpg'); img2 = imread('pollen_bright.jpg');
img3 = imread('pollen.jpg'); img4 = imread('pollen_highcontrast.jpg');
[hist1, bins1] = hist(double(img1(:)),256);
[hist2, bins2] = hist(double(img2(:)),256);
[hist3, bins3] = hist(double(img3(:)),256);
[hist4, bins4] = hist(double(img4(:)),256);
hist1 = hist1./length(img1(:)); hist2 = hist2./length(img2(:));
hist3 = hist3./length(img3(:)); hist4 = hist4./length(img4(:));

figure;
subplot(2,2,1); imshow(img1); title('Dark Image');
subplot(2,2,2); bar(bins1,hist1); title('Dark Image cdf');
axis([0 255 0 .1]); xlabel('Gray Level, r');
subplot(2,2,3); imshow(img2); title('Bright Image');
subplot(2,2,4); bar(bins2,hist2); title('Bright Image cdf');
axis([0 255 0 .1]); xlabel('Gray Level, r');

figure;
subplot(2,2,1); imshow(img3); title('Low-contrast Image');
subplot(2,2,2); bar(bins3,hist3); title('Low-contrast Image cdf');
axis([0 255 0 .1]); xlabel('Gray Level, r');
subplot(2,2,3); imshow(img4); title('High-contrast Image');
subplot(2,2,4); bar(bins4,hist4); title('High-contrast Image cdf');
axis([0 255 0 .1]); xlabel('Gray Level, r');

Output:
Lab Experiment-8
Aim:

To obtain the Contrast/Histogram Stretchingof the input images.

Apparatus used:

 Matlab

Theory:
Contrast-stretching transformations increase the contrast between the darks and the
lights. That transformation kept everything at relatively similar intensities and merely
stretched the histogram to fill the image's intensity domain. Sometimes you want to
stretch the intensity around a certain level. You end up with everything darker darks
being a lot darker and everything lighter being a lot lighter, with only a few levels of
gray around the level of interest.

Program:

I=imread('tire.tif');
I2=im2double(I);
m=mean2(I2)
contrast1=1./(1+(m./(I2+eps)).^4);
contrast2=1./(1+(m./(I2+eps)).^5);
contrast3=1./(1+(m./(I2+eps)).^10);
imshow(I2)
figure,imshow(contrast1)
figure,imshow(contrast2)
figure,imshow(contrast3)

Output:
Result:

We have successfully obtain the Contrast/Histogram Stretching.

You might also like