0% found this document useful (0 votes)
24 views4 pages

DIP Lab-6

This lab focuses on intensity transformations and histogram plotting of images. Students will apply linear, logarithmic, and power law transformations to an image and calculate the intensity histogram of an image. Exercises include writing functions to implement various intensity transformations without using built-in functions, using OpenCV's cv2.calcHist function to compute an image histogram, and applying power law transformations with different gamma values to an image then comparing the original and processed image histograms.

Uploaded by

Golam Daiyan
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)
24 views4 pages

DIP Lab-6

This lab focuses on intensity transformations and histogram plotting of images. Students will apply linear, logarithmic, and power law transformations to an image and calculate the intensity histogram of an image. Exercises include writing functions to implement various intensity transformations without using built-in functions, using OpenCV's cv2.calcHist function to compute an image histogram, and applying power law transformations with different gamma values to an image then comparing the original and processed image histograms.

Uploaded by

Golam Daiyan
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

CEN 440 Digital Image Processing Lab- 5

Intensity transformations and histogram plotting

Name: _________________________________

Enrollment #: _________________________________

Class: _________________________________

Objective

In today’s lab you will explore intensity transformations and histogram plotting of an image. By
the end of this lab, you should be able to apply linear, log and power law transformations to
an image and find intensity histogram of an image.

Tasks

Intensity Transformations
Intensity transformation T is a function which, when applied to an intensity ‘r’ gives a
processed or transformed intensity ‘s’ at that specific pixel.

Inverse Transformation

Inverse transformation is also known as Image Complement. Mathematically, it can be defined


as:

where, r is the current intensity at a pixel point (x, y) and s is the resultant intensity after the
transformation function is applied onto r. L is the max. gray level value, usually L = 28 – 1 = 256
–1 = 255.
Python Function to implement inverse transformation:
im.point(lambda x: 255 - x) using PIL

Power Law Transformation:

Power transformation is also known as Gamma Transformation.


Mathematically, it can be described as follows:
where, r is the current intensity at a pixel coordinate (x, y) and s is the processed intensity after
the transformation is applied. ɣ (gamma) specifies how the intensities are mapped i.e. towards
brighter side or darker side. If ɣ < 1 then mapping is weighted more towards brighter side.
Otherwise, for ɣ > 1 more weight would be given to the darker side and so the image will be
darker. It ɣ = 1 then it would act as a linear function. c is a constant and is usually 1. Python
function:
im1 = im**gamma

Logarithmic Transformation:
Logarithmic transformations are implemented as follows:

where, r is the current intensity at a pixel point (x, y) and s is the resultant intensity after
the transformation function c * log(1 + r) is applied onto r. Example:

Exercise 1

Write a function named ‘transform’ to implement intensity transformations:


INPUTS: 1) Image, 2) Transformation name i.e. ‘inverse’ or ‘power’. OUTPUTS:
Show your results for each of the transformations. For power law
transformations, experiment with different values of gamma. (DO NOT USE ANY
BUILT-IN FUNCTIONS)

Histogram Calculation in OpenCV


So now we use cv2.calcHist() function to find the histogram. Let’s familiarize with the function
and its parameters :
cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]])
1. images : it is the source image of type uint8 or float32. it should be given in square
brackets, ie, “[img]”.
2. channels : it is also given in square brackets. It the index of channel for which we
calculate histogram. For example, if input is grayscale image, its value is [0]. For color

image, you can pass [0],[1] or [2] to calculate histogram of blue,green or red
channel respectively.
3. mask : mask image. To find histogram of full image, it is given as “None”. But if you want
to find histogram of particular region of image, you have to create a mask image for that
and give it as mask. (I will show an example later.)
4. histSize : this represents our BIN count. Need to be given in square brackets. For full
scale, we pass [256].
5. ranges : this is our RANGE. Normally, it is [0,256].

import cv2
def plot_image(imag, title=''):
pylab.title(title, size=20), pylab.imshow(imag)
pylab.axis('off') # comment this line if you want axis ticks

def plot_hist(r, title=''):


r= img_as_ubyte(r)
pylab.hist(np.array(r).ravel(), bins=256, range=(0, 256), color='r',al
pha=0.5)
pylab.xlabel('pixel value', size=20), pylab.ylabel('frequency',size=20
)
pylab.title(title, size=20)

im = cv2.imread("/content/sample_data/images/parrot.JPG")
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
pylab.style.use('ggplot')
pylab.figure(figsize=(15,5))
pylab.subplot(121), plot_image(gray, 'original image')
pylab.subplot(122), plot_hist(gray,'histogram for RGB channels')
pylab.show()
Exercise 2

Write a built in function to compute image histogram as described in the LAB:


INPUTS: image
OUTPUTS: Draw the computed histogram using plt.hist and compare your
histogram with the histogram generated by cv2.calcHist function.

Exercise 3

Write a function which loads the image ‘cameraman.jpg’. Apply the power law
transformation to this image using gamma = 2 and gamma = 0.5. Compare and
analyze the histograms of the original and the processed images. The program
output should be similar to the one indicated in Figure 1.

Figure 1Expected output of Exercise 3

You might also like