B120041 IVP Assignment
B120041 IVP Assignment
Output:
Q2. Mirror and flip an Image.
Code:
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)
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:
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:
Output:
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)
Output:
Q7. Read a gray scale image and write a program to zoom and shrink the image using-
b. Interpolation method.
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"))
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:
import cv2
import numpy as np
import os
import math
Q9. Read a RGB image. Extract its red, green, and blue components and display it individually.
Code:
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:
Output:
Q11. Convert an RGB image to indexed image and followed by RGB image.
Code:
Output:
Q12. Convert the RGB image into CMY model and CMY model into RGB model.
Code:
Output: