Professional Documents
Culture Documents
Experiment 7 - DSIP-1
Experiment 7 - DSIP-1
Experiment 7 - DSIP-1
Faculty of Technology
Department of Information and Communication Technology
Subject: Digital Signal and
Aim: Perform Gray Level Transformations
Image Processing (01CT0513)
Experiment No: 07 Date:18-10-2022 Enrolment No:92000133018
Objectives:
i. Linear transformations
ii. Logarithmic transformations
iii. Power-Law transformations
Software: Python
Theory:
mage Negatives:
The negative of an image with gray
levels in the range [0, L-1] is obtained
by using the negative
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Digital Signal and
Aim: Perform Gray Level Transformations
Image Processing (01CT0513)
Experiment No: 07 Date:18-10-2022 Enrolment No:92000133018
The negative of an image with gray levels in the range [0, L-1] is obtained by using the negative transformation
shown in Fig.1.1, which is given by the expressions = L - 1 - r. Reversing the intensity levels of an
image in this manner produces the equivalent of a photographic negative. This type of processing is
particularly suited for enhancing white or gray detail embedded in dark regions of an image, especially when
the black areas are dominant in size.
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Digital Signal and
Aim: Perform Gray Level Transformations
Image Processing (01CT0513)
Experiment No: 07 Date:18-10-2022 Enrolment No:92000133018
Log Transformations:
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Digital Signal and
Aim: Perform Gray Level Transformations
Image Processing (01CT0513)
Experiment No: 07 Date:18-10-2022 Enrolment No:92000133018
The general form of the log transformation shown in Fig.1.1 is where c is a constant, and it is assumed that r ≥
0.The shape of the log curve in Fig. 1.1 shows that this transformation maps a narrow range of low gray-level
values in the input image into a wider range of output levels. The opposite is true of higher values of
input levels. We would use a transformation of this type to expand the values of dark pixels in an image
while compressing the higher-level values. The opposite is true of the inverse log transformation.
faithfully such a wide range of intensity values. The net effect is that a significant degree of detail will be lost in
the display of a typical Fourier spectrum.
Power-Law Transformations:
Power-law transformations have the basic form where c and g are positive constants. Sometimes Eq. is written
as to account for an offset (that is, a measurable output when the input is zero).However, offsets typically are
an issue of display calibration and as a result they are normally ignored in Eq. Plots of s versus r for various
values of g are shown in Fig. 1.2. As in the case of the log transformation, power-law curves with fractional
values of g map a narrow range of dark input values into a wider range of output values,with the opposite being
true for high-er values of input levels. Unlike the log function, however, we notice here a family of possible
transformation curves obtained simply by varying γ. As expected, we see in Fig.1.2 that curves generated with
values of g>1 have exactly the opposite effect as those generated with values of g<1. Finally, we note that Eq.
reduces to the identity transformation when c = γ = 1. A variety of devices used for image capture, printing, and
display respond according to a power law.By convention, the exponent in the power-law equation is referred to
as gamma. The proces used to correct this power-law response phenomena is called gamma correction. For
example, cathode ray tube (CRT) devices have an intensity-to-voltage response that is a power function,
with exponents varying from approximately 1.8 to 2.5.With reference to the curve for g=2.5 in Fig.1.2, we
see that such display systems would tend to produce images that are darker than intended
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Digital Signal and
Aim: Perform Gray Level Transformations
Image Processing (01CT0513)
Experiment No: 07 Date:18-10-2022 Enrolment No:92000133018
Code:
i. Linear transformations
Code:
import cv2
import numpy as np
# Function to map each intensity level to output intensity level.
def pixelVal(pix, r1, s1, r2, s2):
if (0 <= pix and pix <= r1):
return (s1 / r1)*pix
elif (r1 < pix and pix <= r2):
return ((s2 - s1)/(r2 - r1)) * (pix - r1) + s1
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Digital Signal and
Aim: Perform Gray Level Transformations
Image Processing (01CT0513)
Experiment No: 07 Date:18-10-2022 Enrolment No:92000133018
else:
return ((255 - s2)/(255 - r2)) * (pix - r2) + s2
# Open the image.
img = cv2.imread('image.jpg')
# Define parameters.
r1 = 70
s1 = 0
r2 = 140
s2 = 255
# Vectorize the function to apply it to each value in the Numpy array.
pixelVal_vec = np.vectorize(pixelVal)
# Apply contrast stretching.
contrast_stretched = pixelVal_vec(img, r1, s1, r2, s2)
# Save edited image.
cv2.imwrite('linear_transformation.jpg', contrast_stretched)
output:
Code:
import cv2
import numpy as np
# Open the image.
img = cv2.imread('image.jpg')
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Digital Signal and
Aim: Perform Gray Level Transformations
Image Processing (01CT0513)
Experiment No: 07 Date:18-10-2022 Enrolment No:92000133018
# Apply log transform.
c = 255/(np.log(1 + np.max(img)))
log_transformed = c * np.log(1 + img)
# Specify the data type.
log_transformed = np.array(log_transformed, dtype = np.uint8)
# Save the output.
cv2.imwrite('log_transformation.jpg', log_transformed)
output:
Code:
import cv2
import numpy as np
# Open the image.
img = cv2.imread('image.jpg')
# Trying 4 gamma values.
for gamma in [0.1, 0.5, 1.2, 2.2]:
# Apply gamma correction.
gamma_corrected = np.array(255*(img / 255) ** gamma, dtype = 'uint8')
# Save edited images.
cv2.imwrite('power_law_transformed'+str(gamma)+'.jpg', gamma_corrected)
output:
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Digital Signal and
Aim: Perform Gray Level Transformations
Image Processing (01CT0513)
Experiment No: 07 Date:18-10-2022 Enrolment No:92000133018
Observations:
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Digital Signal and
Aim: Perform Gray Level Transformations
Image Processing (01CT0513)
Experiment No: 07 Date:18-10-2022 Enrolment No:92000133018
Conclusion:
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Digital Signal and
Aim: Perform Gray Level Transformations
Image Processing (01CT0513)
Experiment No: 07 Date:18-10-2022 Enrolment No:92000133018
Perform contrast stretching operation to increase the dynamic range of gray levels in the image being processed.
Code:
import cv2
import numpy as np
import glob
from sklearn.cluster import KMeans
import cv2
# import os
## getting the mask from the rgb images
def preprocessing(img):
# resizing using aspect ratio intact and finding the circle
# reduce size retain aspect ratio intact
# invert BGR 2 RGB
RGB = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
Ig = RGB[:, :, 2]
[w,h] = np.shape(Ig)
r=1200.0/Ig.shape[1]
dim=(1200,int(Ig.shape[0]*r))
rz = cv2.resize(Ig,dim,interpolation=cv2.INTER_AREA)
# convert in to float and get log trasform for contrast streching
g = 0.2 * (np.log(1 + np.float32(rz)))
# change into uint8
cvuint = cv2.convertScaleAbs(g)
# cvuint8.dtype
ret, th = cv2.threshold(cvuint, 0, 255, cv2.THRESH_OTSU)
ret1,th1 = cv2.threshold(Ig,0,255,cv2.THRESH_OTSU)
# closeing operation
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (35,35))
cls = cv2.morphologyEx(th, cv2.MORPH_OPEN, kernel)
Im = cls*rz # the mask with resize image
# cv2.imwrite('mynew.jpg', mask)
return (Im,th,th1,cls,g,RGB)
path_dir = glob.glob('/content/image.jpg')
for k in path_dir:
print('running code....',k)
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Digital Signal and
Aim: Perform Gray Level Transformations
Image Processing (01CT0513)
Experiment No: 07 Date:18-10-2022 Enrolment No:92000133018
img = cv2.imread(k)
(Im,th,th1,cls,g,RGB) = preprocessing(img)
from matplotlib import pyplot as plt
# plt.imshow(cls)
# plt.show()
# cv2.imshow('asd',cls)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# plot the data
titles = ['Original Image', 'log_transform','mask using logT','mask without log_T
']
images = [RGB,g,cls,th]
for i in range(0,np.size(images)):
print(i)
plt.subplot(2, 3, i + 1)
plt.imshow((images[i]),'gray')
plt.title(titles[i])
plt.xticks([]), plt.yticks([])
plt.show()
output: