CV Lab 8
CV Lab 8
5 Marks 5 5 5
Marks Marks Marks
Problem Statement
Design programs for segmenting regions from some images.
You will need to download images on your own. For the submission, you will
need to provide the codes that implement your segmentation as well as all
relevant screenshots that showcase your work. Finally, provide comparison of
the different approaches used in the segmentation.
image = cv2.imread('Women_pic.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
pixels = image.reshape((-1, 3)) # Flatten the image
k = 5 # Number of clusters
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100,
0.2)
_, labels, centers = cv2.kmeans(pixels, k, None, criteria, 10,
cv2.KMEANS_RANDOM_CENTERS)
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(image)
plt.title("Original Image")
plt.axis("off")
plt.subplot(1, 2, 2)
plt.imshow(segmented_image)
plt.title(f"Segmented Image (K={k})")
plt.axis("off")
plt.show()
### CODE ENDS HERE ###
image = io.imread('download.jpeg')
plt.imshow(labels, cmap='gray')
plt.title('Region Growing Segmentation')
plt.show()
### CODE ENDS HERE ###
Task 3 <technique_name>:
image = cv2.imread('OIP111.jpg')
original_image = image.copy()
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Convert to
grayscale
# Removing Noise
kernel = np.ones((3, 3), np.uint8)
opening = cv2.morphologyEx(binary_image, cv2.MORPH_OPEN, kernel,
iterations=2)
# Marker labelling
_, markers = cv2.connectedComponents(sure_fg)
markers = markers + 1 # Add 1 to all labels to distinguish from the background
markers[unknown == 255] = 0 # Mark unknown regions as 0
plt.figure(figsize=(15, 10))
plt.subplot(2, 3, 1)
plt.imshow(cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB))
plt.title("Original Image")
plt.axis("off")
plt.subplot(2, 3, 2)
plt.imshow(binary_image, cmap='gray')
plt.title("Binary Image")
plt.axis("off")
plt.subplot(2, 3, 3)
plt.imshow(sure_bg, cmap='gray')
plt.subplot(2, 3, 4)
plt.imshow(dist_transform, cmap='gray')
plt.title("Distance Transform")
plt.axis("off")
plt.subplot(2, 3, 5)
plt.imshow(sure_fg, cmap='gray')
plt.title("Sure Foreground")
plt.axis("off")
plt.subplot(2, 3, 6)
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title("Segmented Image with Watershed")
plt.axis("off")
plt.show()
### CODE ENDS HERE ###
Concluding Section: