0% found this document useful (0 votes)
66 views

Digital Image Processing Lab Manual# 5

This lab aims to implement image thresholding, transformations, and histogram calculation. Contrast stretching improves image contrast by mapping intensity values to a desired range like (0,255). Histogram calculation uses OpenCV functions to find histograms by channel. Transformations like power law and gamma correction enhance images by applying logarithmic or power functions. Thresholding and gray level slicing convert images to binary or highlight specific intensity ranges.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Digital Image Processing Lab Manual# 5

This lab aims to implement image thresholding, transformations, and histogram calculation. Contrast stretching improves image contrast by mapping intensity values to a desired range like (0,255). Histogram calculation uses OpenCV functions to find histograms by channel. Transformations like power law and gamma correction enhance images by applying logarithmic or power functions. Thresholding and gray level slicing convert images to binary or highlight specific intensity ranges.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

LAB-04: Transformation Operations and Histrogram calculation

Objective:

The objective of this lab is to implement thresholding on images to convert them to binary,
perform different transformation operations on images, find out the histogram of an image
and perform histogram equalization.

Theory:

Histogram Calculation
Here, we use cv2.calcHist()(in-built function in OpenCV) to find the histogram.
cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]])
images : it is the source image of type uint8 or float32 represented as “[img]”.
channels : it is the index of channel for which we calculate histogram. For grayscale
image, its value is [0] and
color image, you can pass [0], [1] or [2] to calculate histogram of blue, green or red channel
respectively.
mask : mask image. To find histogram of full image, it is given as “None”.
histSize : this represents our BIN count. For full scale, we pass [256].
ranges : this is our RANGE. Normally, it is [0,256].

Page 1 of 4
Contrast stretching is a simple image enhancement technique that attempts to improve the
contrast in an image by `stretching' the range of intensity values it contains to span a desired
range of values. Normally (min, max)  (0, 255)

p=
((p-min)/(max-min)) *255

Instead of blindly computing the dynamic range using just the minimum and maximum pixel
values, a more robust and adaptive technique is to use the 5th and 95th percentiles of the
input values when deriving the dynamic range of the input image.

Page 2 of 4
Thresholding is the operation through which an image can be converted into a binary
image/black & white i.e. having only two distinct levels. threshold value can be the mean or
median etc. of the image.
Gray level Slicing is used to highlight a specific range of gray levels in an image
Transformation operations help enhance the quality of the image by applying operations
like log, inverse log and power on the entire image. Different type of transformation yields
different results.

Power law transformation: The general form of Power law (Gamma) transformation
function is

s = c*rγ

Where, ‘s’ and ‘r’ are the output and input pixel values, respectively and ‘c’ and γ are the
positive constants.

Gamma correction is important for displaying images on a screen correctly, to prevent


bleaching or darkening of images when viewed from different types of monitors with
different display settings. This is done because our eyes perceive images in a gamma-
shaped curve, whereas cameras capture images in a linear fashion. Below is the Python
code to apply gamma correction.

Some Useful Commands:

1. To calculate the mean of 2D array using NumPy: my_mean = numpy.mean (my_array)


2. To calculate min (or max) of an array: my_min = numpy.amin(my_array)
3. To calculate the power of an array using NumPy: array_power = numpy.power
(my_array, power)
4. To obtain a sorted copy of an array. sorted_array = numpy.sort(my_array,
axis=None)
5. To change data type of array. my_array = my_array.astype(numpy.uint16)

Page 3 of 4
NAME: ROLL:

Lab Tasks:

(a) (b
)

Lab Task 1:
Apply contrast stretching on image (a) by setting min and max of the input values to 0 and
255 respectively.Apply contrast stretching on image (b) by setting 5th and 95th percentiles of
the input values to 0 and 255 respectively. Also convert into binay using mean as threshold.

Lab Task 2:
After obtaining constrast streatched images from lab task 1 .Apply Power Law transformation
for the following values of γ (0.2, 0.5, 1.2 and 1.8) . Make sure to adjust data types
accordingly.

Lab Task 3:

After obtaining constrast streatched images from lab task 1 .Apply Gray level slicing using
lower limit 100 and upper limit 200. Set all these values to 210.

Page 4 of 4

You might also like