DEPARTMENT OF
INFORMATION TECHNOLOGY
COMPUTER VISION
IT 420
LAB FILE
SUBMITTED TO: Ms. ASHA KUMARI
SUBMITTED BY: Akash (2K21/IT/19)
Experiment 7
Aim:
1. Perform Image Segmentation using K-Means Clustering with k = 3.
2. Implement Hough Transform for line detection using OpenCV.
3. Prepare a program to display the use of:
a. Edge based Segmentation
b. Region based segmentation
4. Write a program for object detection using Histogram of Oriented
Gradients (HOG).
Code:
1. Image Segmentation using K-Means Clustering with k = 3
import numpy as np
import cv2
from google.colab.patches import cv2_imshow
# Load the image
image = cv2.imread('Photo.jpg')
# Reshape the image into a 2D array of pixels
pixels = image.reshape((-1, 3))
# Convert to float type
pixels = np.float32(pixels)
# Define criteria and apply kmeans()
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER,
100, 0.2)
k=3
_, labels, centers = cv2.kmeans(pixels, k, None, criteria, 10,
cv2.KMEANS_RANDOM_CENTERS)
# Convert back to 8 bit values
centers = np.uint8(centers)
# Flatten the labels array
segmented_image = centers[labels.flatten()]
# Reshape back to the original image dimensions
segmented_image = segmented_image.reshape(image.shape)
# Display the original and segmented image
cv2_imshow(image)
cv2_imshow(segmented_image)
Input:
Output:
2. Hough Transform for Line Detection using OpenCV
import cv2
import numpy as np
from google.colab.patches import cv2_imshow
# Load the image
image = cv2.imread('Photo.jpg')
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply edge detection
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
# Apply Hough Line Transform
lines = cv2.HoughLines(edges, 1, np.pi / 180, 200)
# Draw detected lines on the original image
if lines is not None:
for rho, theta in lines[:, 0]:
a = np.cos(theta)
b = np.sin(theta)
x0 = a * rho
y0 = b * rho
x1 = int(x0 + 1000 * (-b))
y1 = int(y0 + 1000 * (a))
x2 = int(x0 - 1000 * (-b))
y2 = int(y0 - 1000 * (a))
cv2.line(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
# Display the result
cv2_imshow(image)
Output:
3. Prepare a program to display the use of:
a. Edge based Segmentation
import cv2
from google.colab.patches import cv2_imshow
image = cv2.imread('dandelions.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150)
# Display the result
cv2_imshow(edges)
Output:
b. Region based segmentation
import cv2
from google.colab.patches import cv2_imshow
image = cv2.imread('Photo.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, thresholded = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY +
cv2.THRESH_OTSU)
thresholded = cv2.bitwise_not(thresholded)
cv2_imshow(thresholded)
Output:
4. Write a program for object detection using Histogram of Oriented
Gradients (HOG).
import cv2
from google.colab.patches import cv2_imshow
# Load the image
image = cv2.imread('dandelions.jpg')
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
rects, _ = hog.detectMultiScale(image, winStride=(4, 4), padding=(8, 8),
scale=1.05)
for (x, y, w, h) in rects:
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# Display the result
cv2_imshow(image)
OUTPUT:
Experiment 8
Aim:
1. Write a program for face detection using Haar Cascade Library.
2. Prepare a program to display SIFT features using openCV.
3. Write a program to implement a PCA algorithm using OpenCV.
4. Write a program to implement Image reconstruction with the help of
auto encoders.
Code:
1. Write a program for face detection using Haar Cascade Library.
import cv2
from google.colab.patches import cv2_imshow
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
'haarcascade_frontalface_default.xml')
image_path = "photooo.jpg"
image = cv2.imread(image_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5,
minSize=(30, 30))
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)
cv2_imshow(image)
Output:
2. Prepare a program to display SIFT features using openCV.
import cv2
from google.colab.patches import cv2_imshow
# Load an image
image_path = "photooo.jpg"
image = cv2.imread(image_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
sift = cv2.SIFT_create()
keypoints, descriptors = sift.detectAndCompute(gray, None)
image_with_keypoints = cv2.drawKeypoints(image, keypoints, None)
cv2_imshow(image_with_keypoints)
Output:
3. Implement PCA algorithm using OpenCV
import cv2
import numpy as np
# Load the image
image_path = "photoo.jpg"
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
pixels = image.flatten()
mean, eigenvectors = cv2.PCACompute(pixels, mean=None)
projected_data = cv2.PCAProject(pixels, mean, eigenvectors)
reconstructed_data = cv2.PCABackProject(projected_data, mean, eigenvectors)
reconstructed_image = np.reshape(reconstructed_data, image.shape)
# Display the original and reconstructed images
cv2_imshow(image)
cv2_imshow(reconstructed_image)
Output:
4. Write a program to implement Image reconstruction with the help
of auto encoders
import numpy as np
from keras.preprocessing.image import img_to_array
from keras.layers import Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Sequential
import cv2
import matplotlib.pyplot as plt
np.random.seed(42)
img_size=256
img_data=[]
img=cv2.imread('photooo.jpg', 1)
rgb_img=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
rgb_img=cv2.resize(rgb_img, (256,256))
img_data.append(img_to_array(rgb_img))
img_final=np.reshape(img_data, (len(img_data),256, 256, 3))
img_final=img_final.astype('float32')/255
model=Sequential()
model.add(Conv2D(64, (3,3), activation='relu', padding='same',
input_shape=(256,256,3)))
model.add(MaxPooling2D((2,2), padding='same'))
model.add(Conv2D(32, (3,3),activation='relu',padding='same'))
model.add(MaxPooling2D((2,2), padding='same'))
model.add(Conv2D(16, (3,3),activation='relu',padding='same'))
model.add(MaxPooling2D((2,2), padding='same'))
model.add(Conv2D(16, (3,3), activation='relu', padding='same'))
model.add(UpSampling2D((2,2)))
model.add(Conv2D(32, (3,3), activation='relu', padding='same'))
model.add(UpSampling2D((2,2)))
model.add(Conv2D(64, (3,3), activation='relu', padding='same'))
model.add(UpSampling2D((2,2)))
model.add(Conv2D(3, (3,3), activation='relu', padding='same'))
model.compile(optimizer='adam',loss='mean_squared_error',metrics=['accuracy'
])
model.summary()
model.fit(img_final, img_final, epochs=2000, shuffle=True)
pred=model.predict(img_final)
plt.imshow(pred[0].reshape(256,256,3))
OUTPUT: