Computervision Practical
Computervision Practical
Computervision Practical
INFORMATION TECHNOLOGY
COMPUTER VISION
IT 420
LAB FILE
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
Input:
Output:
2. Hough Transform for Line Detection using OpenCV
import cv2
import numpy as np
from google.colab.patches import cv2_imshow
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
import cv2
from google.colab.patches import cv2_imshow
image = cv2.imread('dandelions.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Output:
b. Region based segmentation
import cv2
from google.colab.patches import cv2_imshow
image = cv2.imread('Photo.jpg')
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
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
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)
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
Output:
4. Write a program to implement Image reconstruction with the help
of auto encoders
import numpy as np
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.compile(optimizer='adam',loss='mean_squared_error',metrics=['accuracy'
])
model.summary()
pred=model.predict(img_final)
plt.imshow(pred[0].reshape(256,256,3))
OUTPUT: