1. AIR UNIVERSITY
Department of Electrical and Computer Engineering
Digital Image Processing Lab
Lab #4: Sampling and Quantization Variation
Student Name: Umar Mustafa
Roll No: 200365
Instructor: Engr. M. Farooq Khan
Problem 2: Sampling variation </h2>
Let us now observe the variation in image perception, when we change the sampling of the image. In this regard, you are required to do the desired tasks,
1. Load the image ‘droplets.jpg’, and convert it from RGB to gray.
2. Now subsample the image by 2. (Take every alternate pixels in rows as well as column)
3. Do you observe any change in the image outlook or your visual perception? Please discuss
4. Repeat step 5 on iteratively twice and for each subsampling result, discuss the variation in the image perception. Can you explain what is happening?
Do you observe any change in the image outlook or your visual perception? Please discuss
the variation in the image perception. Can you explain what is happening?
Yes, there is a change in the image outlook after subsampling the image by 2. The subsampled image has half the dimensions of the original image, and contains only every other pixel in
both the rows and columns of the original image. This results in a loss of detail and sharpness in the subsampled image compared to the original image. The subsampled image may appear
slightly blurry or pixelated, as some of the fine details in the original image have been lost due to downsampling. However, the overall structure and composition of the image should still be
recognizable, and important features such as edges and shapes should still be preserved to some extent.
Problem 3: Quantization variation
Let us now observe the variation in image perception, when we change the quantization of the image. In this regard, you are required to do the desired tasks,
1. Load the image ‘droplets.jpg’ and convert it from RGB to gray.
2. Now reduce the quantization of the image to 6bit. (the pixel mapping goes from 0-63 instead of 0-255)
3. Do you observe any change in the image outlook or your visual perception? Please discuss
4. Repeat step 5 on for 4-bit, 2-bit and 1-bit quantization, and discuss the variation in the image perception. Can you explain what is happening?
Do you observe any change in the image outlook or your visual perception? and discuss
the variation in the image perception. Can you explain what is happening?
When I performed 6-bit quantization on the image, I reduced the number of possible pixel values from 256 (8-bit) to 64 (6-bit). The result is a loss of detail and a more 'blocky' appearance, as
the image is now represented by a smaller set of discrete values. When I repeated the quantization process with lower bit depths (4-bit, 2-bit, and 1-bit), I further reduced the number of
possible pixel values and introduce more quantization artifacts. So I observed low visual quality as I decreased the number of bits used to represent this image.
Conclusion
Subsampling an image by 2 means reducing the dimensions of the image to half in both the rows and columns, resulting in a smaller image with fewer pixels. It can be done using
formulae or libraries in Python.
Subsampling an image can result in a loss of detail and sharpness, and the degree of loss depends on the degree of subsampling.
Libraries such as NumPy and OpenCV make subsampling simpler and more convenient. Overall, subsampling preserves the overall structure and composition of the image, while
sacrificing some fine details in the process.
Quantization is the process of reducing the number of bits used to represent an image's pixel values. This process reduces the image's file size and computational complexity. However, it
also results in a loss of information and visual quality. Higher quantization levels result in greater loss of detail and more significant visual distortions.
In [28]: import matplotlib.pyplot as plt
import numpy as np
import cv2
from IPython import display
from PIL import Image
# Load an image using cv2.imread
img = cv2.imread('F:/imagelab/droplets.jpg')
plt.subplot(331)
plt.imshow(img,cmap = 'gray')
plt.title('Original Image')
# Gray (Gray_Scale color space):
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Save the image to a file using cv2.imwrite
cv2.imwrite('F:/imagelab/gray.jpg', gray_image)
plt.subplot(332)
plt.imshow(gray_image,cmap = 'gray')
plt.title('Gray Image')
gray_image = np.array(gray_image)
#With Formula
# Subsample the image by a factor of 2
plt.subplot(333)
subsampled2 = gray_image[::2, ::2]
subsampled2 = Image.fromarray(subsampled2)
plt.imshow(subsampled2,cmap = 'gray')
plt.title('Sub Sampled')
# Subsample the image by a factor of 4
plt.subplot(334)
subsampled4 = gray_image[::4, ::4]
subsampled4 = Image.fromarray(subsampled4)
plt.imshow(subsampled4,cmap = 'gray')
plt.title('Sub Sampled4')
# Subsample the image by a factor of 6
plt.subplot(335)
subsampled6 = gray_image[::6, ::6]
subsampled6 = Image.fromarray(subsampled6)
plt.imshow(subsampled6,cmap = 'gray')
plt.title('Sub Sampled6')
#Now without using direct formula
plt.subplot(336)
subsample2 = cv2.resize(gray_image, (gray_image.shape[1]//2, gray_image.shape[0]//2))
plt.imshow(subsample2,cmap = 'gray')
plt.title('Sub Sampled2')
plt.subplot(337)
subsample4 = cv2.resize(gray_image, (gray_image.shape[1]//4, gray_image.shape[0]//4))
plt.imshow(subsample4,cmap = 'gray')
plt.title('Sub Sampled4')
plt.subplot(338)
subsample6 = cv2.resize(gray_image, (gray_image.shape[1]//6, gray_image.shape[0]//6))
plt.imshow(subsample6,cmap = 'gray')
plt.title('Sub Sampled6')
plt.show()
In [29]: # Load an image using cv2.imread
img = cv2.imread('F:/imagelab/droplets.jpg')
plt.subplot(331)
plt.imshow(img,cmap = 'gray')
plt.title('Original Image')
# Gray (Gray_Scale color space):
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Save the image to a file using cv2.imwrite
cv2.imwrite('F:/imagelab/gray.jpg', gray_image)
plt.subplot(332)
plt.imshow(gray_image,cmap = 'gray')
plt.title('Gray Image')
# 6bit Quantization
r_6bit = (np.asarray(gray_image) >> 2) << 2
r_6bit = Image.fromarray(r_6bit)
plt.subplot(333)
plt.imshow(r_6bit,cmap = 'gray')
plt.title('quantized 6 bit')
# 4bit Quantization
r_4bit = (np.asarray(gray_image) >> 4) << 4
r_4bit = Image.fromarray(r_4bit)
plt.subplot(334)
plt.imshow(r_4bit,cmap = 'gray')
plt.title('quantized 4bit')
# 2bit Quantization
r_2bit = (np.asarray(gray_image) >> 6) << 6
r_2bit = Image.fromarray(r_2bit)
plt.subplot(335)
plt.imshow(r_2bit,cmap = 'gray')
plt.title('quantized 2bit')
# 1bit Quantization
r_1bit = (np.asarray(gray_image) >> 7) << 7
r_1bit = Image.fromarray(r_1bit)
plt.subplot(336)
plt.imshow(r_1bit,cmap = 'gray')
plt.title('quantized 1bit')
plt.show()