ip_lab_manual
ip_lab_manual
7) Write a Program to read a digital image. Split and display image into 4
quadrants, up, down, right and left.
import cv2
# Load the image
image = cv2.imread('test.jpg')
# Get image height and width
height, width = image.shape[:2]
# Split the image into four quadrants
top_left = image[0:height//2, 0:width//2]
top_right = image[0:height//2, width//2:width]
bottom_left = image[height//2:height, 0:width//2]
bottom_right = image[height//2:height, width//2:width]
# Display the original image and the quadrants
cv2.imshow('Original Image', image)
cv2.imshow('Top Left Quadrant', top_left)
cv2.imshow('Top Right Quadrant', top_right)
cv2.imshow('Bottom Left Quadrant', bottom_left)
cv2.imshow('Bottom Right Quadrant', bottom_right)
# Wait for a key press and close all windows
cv2.waitKey(0)
cv2.destroyAllWindows()
Sample Output :
import cv2
import numpy as np
# Apply rotation
rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))
# Apply scaling
scaled_image = cv2.resize(image, None, fx=scale_x, fy=scale_y)
# Apply translation
translation_matrix = np.float32([[1, 0, tx], [0, 1, ty]])
translated_image = cv2.warpAffine(image, translation_matrix, (width, height))
# Display images
cv2.imshow('Original Image', image)
cv2.imshow('Rotated Image', rotated_image)
cv2.imshow('Scaled Image', scaled_image)
cv2.imshow('Translated Image', translated_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
9) Read an image and extract and display low-level features such as edges,
textures using filtering techniques.
import cv2
import numpy as np
# Load the image
image = cv2.imread('test.jpg', cv2.IMREAD_GRAYSCALE)
# Apply edge detection using Canny
edges = cv2.Canny(image, 100, 200)
# Apply texture analysis using Laplacian of Gaussian (LoG)
image_blur = cv2.GaussianBlur(image, (3, 3), 0)
image_log = cv2.Laplacian(image_blur, cv2.CV_64F)
image_log = np.uint8(np.absolute(image_log))
# Display images
cv2.imshow('Original Image', image)
cv2.imshow('Edges', edges)
Sample output :
import cv2
# Load the image
image = cv2.imread('test.jpg')
# Apply Gaussian blur
gaussian_blur = cv2.GaussianBlur(image, (15, 15), 0)
# Apply median blur
Sample output :
Output:
import cv2
# Load the pre-trained Haar Cascade classifier for face detection
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
'haarcascade_frontalface_default.xml')
# Load the image
image = cv2.imread('sample.jpg')
# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect faces in the image
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=6,
minSize=(30, 30))
# Draw rectangles around the detected faces
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Display the image with detected faces
cv2.imshow('Faces Detected', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output :