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

Assignment 6(S24031)

Uploaded by

Shubham Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Assignment 6(S24031)

Uploaded by

Shubham Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

EE-608 (Digital Image Processing)

Assignment-6

Shubham Sharma
S24031

AIM:

1) Write a function to apply first order gradient and second order gradient filters on
the given image.

2) Write a function to do histogram equalization on a given image and plot before and
after histograms.

3) Take two different images of the same size and Apply histogram matching between
them. And plot the image after its histogram transformation to its desired match.

Google colab Link: Assignment_6.ipynb


Aim 1: Write a function to apply first order gradient and second order gradient filters on
the given image.

First order gradient

Matrix 1

-1 0 1

-2 0 2

-1 0 1

Matrix 2

-1 -2 -1

0 0 0

1 2 1

In the first order derivative we have two matrices and we need to convolve on Image
with both the matrices. And after the convolution with both of the matrices we need to
calculate the combined result which will be the square root of the sum of the squares of
both the matrices.

Code

def first_order_gradient(image):
Gx = np.array([
[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]
])

Gy = np.array([
[-1, -2, -1],
[0, 0, 0],
[1, 2, 1]
])

grad_x = np.zeros((image.shape[0],image.shape[1]))
grad_y = np.zeros((image.shape[0],image.shape[1]))
padded_image = np.zeros((image.shape[0] + 2,image.shape[1] + 2))
for i in range(image.shape[0]):
for j in range(image.shape[1]):
padded_image[i+1][j+1] = image[i][j]

for i in range(padded_image.shape[0]-2):
for j in range(padded_image.shape[1]-2):

image_area = padded_image[i:i+3, j:j+3]

grad_x[i][j] = np.sum(image_area*Gx)
grad_y[i][j] = np.sum(image_area*Gy)

Final_image = np.sqrt(grad_x**2+grad_y**2)
return cv2_imshow(Final_image)
first_order_gradient(image)

In the code above we have initialized the required matrices after that we are performing padding
on the image which will give us a padded image.

Next we need to perform convolution on the padded image. For which we are running two loops
to iterate through the padded_image and will extract the image area and then will multiply both
the matrices which will store the elements in grad_x and grad_y.

After that we will take the square root of sum of squares of grad_x and grad_y which will return
us the Final Image.

Result
Second order gradient

Matrix

0 -1 0

-1 4 -1

0 -1 0

In case of second order derivative we need the matrix given above and then we’ll convolve it on
the image which will give us the required output image

Code

def second_order_gradient(image):
G = np.array([
[0, -1, 0],
[-1, 4, -1],
[0, -1, 0]
])

grad = np.zeros((image.shape[0],image.shape[1]))

padded_image = np.zeros((image.shape[0] + 2,image.shape[1] + 2))


for i in range(image.shape[0]):
for j in range(image.shape[1]):
padded_image[i+1][j+1] = image[i][j]

for i in range(padded_image.shape[0]-2):
for j in range(padded_image.shape[1]-2):

image_area = padded_image[i:i+3, j:j+3]

grad[i][j] = np.sum(image_area*G)

# Filtered_image = np.sqrt(grad_x**2+grad_y**2)
return cv2_imshow(grad)
second_order_gradient(image)

Here the only difference is that there is only one matrix instead of two, the rest of the process is
the same. After the convolution we’ll get the required image.

Result
Aim 2: Write a function to do histogram equalization on a given image and plot before
and after histograms.

Code:
height = image.shape[0]
width = image.shape[1]

hist = np.zeros(256)
for i in range(image.shape[0]):
for j in range(image.shape[1]):
hist[image[i,j]] += 1

probabilities = hist/np.sum(hist)

cdf = np.zeros(256)
cdf[0] = probabilities[0]
for i in range(1,256):
cdf[i] = cdf[i-1] + probabilities[i]

Sk = np.round(cdf*255)

New_image = np.zeros((image.shape[0],image.shape[1]))
for i in range(image.shape[0]):
for j in range(image.shape[1]):
New_image[i,j] = Sk[image[i,j]]

# OLD Histogram

plt.title("Old Histogram")
plt.xlabel('Intensity')
plt.ylabel('No of Pixel')
plt.bar(range(256), hist, width=1)
plt.show()

New_image = New_image.astype(np.uint8)

# NEW Histogram
New_hist = np.zeros(256)
for i in range(New_image.shape[0]):
for j in range(New_image.shape[1]):
New_hist[New_image[i,j]] += 1

plt.title("New Histogram")
plt.xlabel('Intensity')
plt.ylabel('No of Pixel')
plt.bar(range(256), New_hist, width=1)
plt.show()

In the code above first we are initializing a histogram (hist) for the image, which has 256
zeros corresponding to 256 possible pixel intensity levels in an 8-bit grayscale image.
We then iterate through each pixel in the image to count how often each intensity level
appears. After the completion of the loops we have the required histogram, which we
can plot later.

The next step is to compute probabilities, for this we are simply dividing (hist) with the
sum, which will give us the probabilities of all the pixel Intensities.

Then we are calculating the cumulative distribution function (CDF) of the intensity
probabilities. For this we are initializing the CDF at zeroth index with the probability at
zeroth index. Then we are iterating in order to compute CDF for all the probabilities by
adding the next probabilities with the current CDF.

Now with the help of CDF, a transformation function Sk is calculated, which maps the
original pixel values to new intensity values. The transformation is scaled to the range
[0, 255] to maintain 8-bit image intensity values by multiplying the cdf with 255.

Now we are initializing a New Image with same shape as of the original image having
values zeros. And new values are assigned to it with are calculated in Sk with the help
of two running loops.

After that we are visualizing the histograms of both Old and New Image.
Results:
Aim 3: Take two different images of the same size and Apply histogram matching
between them. And plot the image after its histogram transformation to its desired
match.

Code:

def cal_hist_and_Sk(image):
height = image.shape[0]
width = image.shape[1]

hist = np.zeros(256)
for i in range(image.shape[0]):
for j in range(image.shape[1]):
hist[image[i,j]] += 1

probabilities = hist/np.sum(hist)

cdf = np.zeros(256)
cdf[0] = probabilities[0]
for i in range(1,256):
cdf[i] = cdf[i-1] + probabilities[i]

Sk = np.round(cdf*255)
return hist, Sk

def histogram_matching(image1, image2):

hist1, cdf_image = cal_hist_and_Sk(image)


hist2, cdf_image2 = cal_hist_and_Sk(image2)

mapped_values = np.zeros(256, dtype=np.uint8)


for i in range(256):
diff = np.abs(cdf_image[i] - cdf_image2)
mapped_values[i] = np.argmin(diff)

matched_image = np.zeros_like(image1)
for i in range(image1.shape[0]):
for j in range(image1.shape[1]):
matched_image[i, j] = mapped_values[image1[i, j]]

return plot_hist(matched_image)

def plot_hist(img):
cv2_imshow(img)

Updated_hist = np.zeros(256)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
Updated_hist[img[i,j]] += 1

plt.title("Histogram")
plt.xlabel('Intensity')
plt.ylabel('No of Pixel')
plt.bar(range(256), Updated_hist, width=1)
plt.show()

print("Histogram Image1")
plot_hist(image)

print("Histogram Image2")
plot_hist(image2)

histogram_matching(image, image2)

Here in the code the function (cal_hist_and_Sk)is calculating the histogram and
also returning Sk which contains the transformed values. The steps are same as
mentioned in previous questions.

Next we have the function (histogram_matching) in which we are passing


(image1, image2)on which we want to perform histogram matching. It calculates the
CDFs for both images and creates a mapping of intensity values from image1 to image2 by
minimizing the difference between the two CDFs.

Now with the help of the mapped values we have calculated we are creating
(matched_image) by running two loops in the range of height and width of the original
image then we are putting those values in mapped_values into the indexes of
matched_image.
We have also created an another function to plot histogram which is working same as
explained in last question. This function we are returning in the output of
histogran_matching function and passing matched image in it which will print the image and
the histogram of the image.

Results:

Image 1

Image2
Output/Matched Image

You might also like