0% found this document useful (0 votes)
66 views15 pages

Name - Bhavya Jain 19CS19 Batch C1 Digital Image Processing Lab

The document describes digital image processing tasks performed on various input images. It includes applying histogram equalization and matching, median filtering, mean filtering, and a weighted mean filter on images. Code solutions are provided for each task. The tasks are designed to enhance image contrast and quality by modifying histograms and filtering out noise.

Uploaded by

Yesh
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 views15 pages

Name - Bhavya Jain 19CS19 Batch C1 Digital Image Processing Lab

The document describes digital image processing tasks performed on various input images. It includes applying histogram equalization and matching, median filtering, mean filtering, and a weighted mean filter on images. Code solutions are provided for each task. The tasks are designed to enhance image contrast and quality by modifying histograms and filtering out noise.

Uploaded by

Yesh
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/ 15

Name – Bhavya Jain

19CS19
Batch C1
Digital Image Processing Lab
1. Perform Histogram Equalization for the input
“image1”.
Description: Histogram Equalization is an image processing technique that
adjusts the contrast of an image by using its histogram. To enhance the
image’s contrast, it spreads out the most frequent pixel intensity values or
stretches out the intensity range of the image. By accomplishing this,
histogram equalization allows the image’s areas with lower contrast to gain a
higher contrast.

Solution Code:
import cv2
import numpy as np
img = cv2.imread('/content/image1.jpg',0)
equ = cv2.equalizeHist(img)
res = np.hstack((img,equ)) #stacking images side-by-side
cv2.imwrite('res.png',res)

INPUT IMAGE
OUTPUT IMAGE

BEFORE AFTER
2. Perform Histogram specification/matching for the
“image2”. Take the histogram of “imagex” as the
specified histogram.
Description: Histogram matching allows us to update the pixel intensities
in an input image to match the pixel intensities’ distribution in a separate
reference image .
Histogram matching can best be thought of as a “transformation.” Our goal
is to take an input image (the “source”) and update its pixel intensities such
that the distribution of the input image histogram matches the distribution of
a reference image.

Solution Code:
import matplotlib.pyplot as plt
from skimage import exposure
from skimage.exposure import match_histograms
import cv2
` img1 = cv2.imread("/content/image2.jpg")
print('No of Channel is: ' + str(img1.ndim))
img2 = cv2.imread("/content/imagex.jpg")

print('No of Channel is: ' + str(img2.ndim))

image = img1
reference = img2

matched = match_histograms(image, reference ,


multichannel=True)

fig, (ax1, ax2, ax3) = plt.subplots(nrows=1,


ncols=3,figsize=(8,3),sharex=True, sharey=True)

for aa in (ax1, ax2, ax3):


aa.set_axis_off()

ax1.imshow(image)
ax1.set_title('Source')
ax2.imshow(reference)
ax2.set_title('Reference')
ax3.imshow(matched)
ax3.set_title('Matched')

plt.tight_layout()
plt.show()

fig, axes = plt.subplots(nrows=3, ncols=3, figsize=(8, 8))

for i, img in enumerate((image, reference, matched)):


for c, c_color in enumerate(('red', 'green', 'blue')):
img_hist, bins = exposure.histogram(img[..., c],

source_range='dtype')
axes[c, i].plot(bins, img_hist / img_hist.max())
img_cdf, bins = exposure.cumulative_distribution(img[...,c])
axes[c, i].plot(bins, img_cdf)
axes[c, 0].set_ylabel(c_color)

axes[0, 0].set_title('Source')
axes[0, 1].set_title('Reference')
axes[0, 2].set_title('Matched')

plt.tight_layout()
plt.show()

INPUT IMAGES
image2 imagex
OUTPUT IMAGE
3. Apply 3x3 and 5x5 median filter on “image3” and
“image4” (apply both masks on both the images).

Description: It is also known as nonlinear filtering. It is used to eliminate


salt and pepper noise. Here the pixel value is replaced by the median value
of the neighboring pixel.

Solution Code:

from PIL import Image, ImageFilter

im1 = Image.open(r"image3.jpg")

im2 = im1.filter(ImageFilter.MedianFilter(size = 5))


im2.show()

INPUT IMAGES

image 3 image4

OUTPUT IMAGES

Image3
3*3 median filtering 5*5 median filtering
Image 4
3*3 median filtering 5*5 median filtering

4. Apply 3x3 and 5x5 mean filter on “image3” and


“image4” (apply both masks on both the images).
Description: Average (or mean) filtering is a method of
‘smoothing’ images by reducing the amount of intensity
variation between neighbouring pixels. The average filter
works by moving through the image pixel by pixel,
replacing each value with the average value of
neighbouring pixels, including itself.
Solution Code:
import numpy as np
import cv2
from matplotlib import pyplot as plt
from PIL import Image, ImageFilter
%matplotlib inline
image = cv2.imread('/content/image4.jpg') # reads the image
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # convert to HSV
figure_size = 3 # the dimension of the x and y axis of the kernal.
new_image = cv2.blur(image,(figure_size, figure_size))
plt.figure(figsize=(11,6))
plt.subplot(121), plt.imshow(cv2.cvtColor(image,
cv2.COLOR_HSV2RGB)),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(cv2.cvtColor(new_image,
cv2.COLOR_HSV2RGB)),plt.title('Mean filter')
plt.xticks([]), plt.yticks([])
plt.show()

INPUT IMAGES
image 3 image4

OUTPUT IMAGES
3*3 mean filtering(image 3)

5*5 mean filtering (image 3)


OUTPUT IMAGES

3*3 mean filtering(image 4)

5*5 mean filtering(image 4)


5.Apply the following weighted mean filter on “image3” and
“image4” -
1/16[1 2 1
2 4 2
1 2 1]
Note: Convert the color image to grayscale image if
required.
Description: In weighted average filter, we gave more weight to the
center value, due to which the contribution of center becomes more than
the rest of the values. Due to weighted average filtering, we can control
the blurring of image.

Solution Code:
import numpy as np
from numpy.lib.stride_tricks import as_strided
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import axes3d
import matplotlib.gridspec as gridspec
import seaborn as sns
import math
import cv2
img1 = cv2.imread("/content/image3.jpg")
add_av_fil = np.array([[1, 2,1],
[2,4,2],
[1,2,1]], np.float32)/16

dst1 = cv2.filter2D(img1, -1, add_av_fil)


cv2.imwrite('4_add_av_fil.jpg', dst1)

INPUT IMAGES

image 3 image4

OUTPUT IMAGES

You might also like