IVP Assignment
Name: Piyush Mishra
ID: B120041
Input Image
Q1. Read, write and display an Image.
Code:
if not os.path.exists('./output images/q1'):
os.makedirs('./output images/q1')
img = cv2.imread('image1.png')
cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('./output images/q1/output.png', img)
Output:
Q2. Mirror and flip an Image.
Code:
if not os.path.exists('./output images/q2'):
os.makedirs('./output images/q2')
img = cv2.imread('image1.png')
img_mirror = cv2.flip(img, 1)
img_flip = cv2.flip(img, 0)
cv2.imshow('Mirrored Image', img_mirror)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('./output images/q2/mirrored.png', img_mirror)
cv2.imshow('Flipped Image', img_flip)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('./output images/q2/flipped.png', img_flip)
Output:
Q3. Divide an image into n square parts and saves the output images.
Code:
import itertools
if not os.path.exists('./output images/q3'):
os.makedirs('./output images/q3')
img = cv2.imread('image1.png')
height, width, channels = img.shape
n = int(input("Enter a square number: "))
if int(math.sqrt(n))**2 != n:
print("Invalid number")
else:
square_size = int(math.sqrt((height*width)/n))
num_squares = int(math.sqrt(n))
for i, j in itertools.product(range(num_squares), range(num_squares)):
square_img = img[i*square_size:(i+1)*square_size,
j*square_size:(j+1)*square_size]
cv2.imwrite(f"./output images/q3/grid_{i*num_squares+j+1}.png",
square_img)
Output: (divided in 9 pieces)
Q4. Read a gray-scale image, modify the pixel intensities which are greater than 120 as white, less than
50 as black and rest as unmodified.
Code:
if not os.path.exists('./output images/q4'):
os.makedirs('./output images/q4')
img = cv2.imread('image1.png', cv2.IMREAD_GRAYSCALE)
img[img > 120] = 255
img[img < 50] = 0
cv2.imshow('Modified Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('./output images/q4/output.png', img)
Output:
Q5. Find the mean of a gray-scale image and set it as the threshold value. Using the threshold value
convert the image into binary image and display it.
Code:
if not os.path.exists('./output images/q5'):
os.makedirs('./output images/q5')
# Reading the grayscale image
img = cv2.imread('image1.png', cv2.IMREAD_GRAYSCALE)
# Finding the mean of the image
mean = np.mean(img)
# Setting the threshold value
threshold = mean
# Converting the image into binary using the threshold value
_, binary_img = cv2.threshold(img, threshold, 255, cv2.THRESH_BINARY)
# Displaying the binary image
cv2.imshow('Binary Image', binary_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Writing the binary image
cv2.imwrite('./output images/q5/output.png', binary_img)
Output:
Q6. Change the resolution of an image from 1 bit to 8 bit.
Code:
if not os.path.exists('./output images/q6'):
os.makedirs('./output images/q6')
img = cv2.imread('image1.png')
def downscale(img,bits:
level = 2**bits - 1
return (img // (256//level)) * (256//level)
def dispXwrite(img,bits):
imgx=downscale(img,bits)
cv2.imshow(f'{str(bits)}-bit Image', imgx)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite(f'./output images/q6/output_{str(bits)}bit.png', imgx)
for bits in range(8,0,-1):
dispXwrite(img,bits)
Output:
Q7. Read a gray scale image and write a program to zoom and shrink the image using-
a. Pixel replication method.
b. Interpolation method.
Store the images as necessary.
Code:
if not os.path.exists('./output_images/q7'):
os.makedirs('./output_images/q7')
img = cv2.imread('image1.png', cv2.IMREAD_GRAYSCALE)
zoom_factor = int(input("Enter Zoom Factor"))
resized_pixel = np.zeros((img.shape[0]*zoom_factor, img.shape[1]*zoom_factor),
dtype=np.uint8)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
for k in range(zoom_factor):
for l in range(zoom_factor):
resized_pixel[i*zoom_factor+k, j*zoom_factor+l] = img[i, j]
resized_interpolation = cv2.resize(img, None, fx=zoom_factor, fy=zoom_factor,
interpolation=cv2.INTER_LINEAR)
shrink_factor = int(input("Enter Shrink Factor"))
resized_pixel_shrink = np.zeros((int(img.shape[0]/shrink_factor),
int(img.shape[1]/shrink_factor)), dtype=np.uint8)
for i in range(0, img.shape[0], shrink_factor):
for j in range(0, img.shape[1], shrink_factor):
resized_pixel_shrink[int(i/shrink_factor), int(j/shrink_factor)] = img[i,
j]
resized_interpolation_shrink = cv2.resize(img, None, fx=1/shrink_factor,
fy=1/shrink_factor, interpolation=cv2.INTER_LINEAR)
cv2.imwrite('./output_images/q7/resized_pixel.jpg', resized_pixel)
cv2.imwrite('./output_images/q7/resized_interpolation.jpg',
resized_interpolation)
cv2.imwrite('./output_images/q7/resized_pixel_shrink.jpg', resized_pixel_shrink)
cv2.imwrite('./output_images/q7/resized_interpolation_shrink.jpg',
resized_interpolation_shrink)
Output:
Q8. Read a gray scale image and negate the image.
Code:
import cv2
import numpy as np
import os
import math
if not os.path.exists('./output images/q8'):
os.makedirs('./output images/q8')
img = cv2.imread('image1.png', cv2.IMREAD_GRAYSCALE)
negated_img = 255 - img
cv2.imshow('Negated Image', negated_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('./output images/q8/negated.png', negated_img)
Output:
Q9. Read a RGB image. Extract its red, green, and blue components and display it individually.
Code:
if not os.path.exists('./output images/q9/'):
os.makedirs('./output images/q9/')
img = cv2.imread('image1.png')
bx, gx, rx = cv2.split(img)
zeros = np.zeros(bx.shape, np.uint8)
b = cv2.merge([bx,zeros,zeros])
g = cv2.merge([zeros,gx,zeros])
r = cv2.merge([zeros,zeros,rx])
cv2.imshow('Red Component', r)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('./output images/q9/red.png', r)
cv2.imshow('Green Component', g)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('./output images/q9/green.png', g)
cv2.imshow('Blue Component', b)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('./output images/q9/blue.png', b)
Output:
Q10. Read a RGB image and convert it to a YIQ model and reconvert it back to RGB.
Code:
if not os.path.exists('./output images/q10/'):
os.makedirs('./output images/q10/')
img = cv2.imread('image1.png')
img_yiq = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
y, i, q = cv2.split(img_yiq)
img_rgb = cv2.cvtColor(img_yiq, cv2.COLOR_YCrCb2BGR)
cv2.imshow('Original Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imshow('YIQ model', img_yiq)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imshow('Converted Image', img_rgb)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('./output images/q10/YIQ model.png', img_yiq)
cv2.imwrite('./output images/q10/RGB converted.png', img_rgb)
Output:
Q11. Convert an RGB image to indexed image and followed by RGB image.
Code:
if not os.path.exists('./output images/q11/'):
os.makedirs('./output images/q11/')
rgb_image =cv2.imread('image1.png')
indexed_image = cv2.cvtColor(rgb_image, cv2.COLOR_BGR2GRAY)
indexed_image = cv2.applyColorMap(indexed_image, cv2.COLORMAP_JET)
cv2.imwrite("./output images/q11/indexed_image.png", indexed_image)
rgb_image2 = cv2.cvtColor(indexed_image, cv2.COLOR_BGR2RGB)
cv2.imwrite("./output images/q11/rgb_image2.png", rgb_image2)
Output:
Q12. Convert the RGB image into CMY model and CMY model into RGB model.
Code:
if not os.path.exists('./output images/q12/'):
os.makedirs('./output images/q12/')
rgb_image = cv2.imread('image1.png')
cmy_image = 255 - rgb_image
rgb_image2 = 255 - cmy_image
cv2.imwrite("./output images/q12/cmy_image.png", cmy_image)
cv2.imwrite("./output images/q12/rgb_image3.png", rgb_image2)
Output: