0% found this document useful (0 votes)
10 views10 pages

CV Lab 8

Its a lab on computer vision.

Uploaded by

Naima Yaqub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

CV Lab 8

Its a lab on computer vision.

Uploaded by

Naima Yaqub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Department of Electrical Engineering

Faculty Member: <> Date: <>


Semester: <> Group: <>

CS477 Computer Vision


Lab 8: Image Segmentation
< Open-Ended Lab >

PLO4 - PLO PL PLO


CLO4 5 -CLO5 O8 - 9 -CLO7
CLO6
Name Reg. No Investig Mod Ethi Indi
ation ern Tool cs vidual
Usage and
Team
Work

5 Marks 5 5 5
Marks Marks Marks

CS477 Computer Vision


This laboratory exercise will focus on segmenting images into more meaningful
representations that are simpler to analyze. Images can be divided into
segmented regions using various techniques which depend on the color variation
and the complexity of the required segments.

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.

Task 1 <K-means clustering>:

### CODE STARTS HERE ###


import cv2
import numpy as np
import matplotlib.pyplot as plt
from google.colab import files
from google.colab.patches import cv2_imshow

image = cv2.imread('Women_pic.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
pixels = image.reshape((-1, 3)) # Flatten the image

CS477 Computer Vision


pixels = np.float32(pixels) # Convert to float32 for K-means

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)

centers = np.uint8(centers) # Convert centers to uint8


segmented_image = centers[labels.flatten()] # Map labels to centers
segmented_image = segmented_image.reshape(image.shape) # Reshape back
to the original image shape

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 ###

### SCREENSHOTS START HERE ###

CS477 Computer Vision


### SCREENSHOTS END HERE ###

### EXPLANATION AND DISCUSSION START HERE ###

### EXPLANATION AND DISCUSSION END HERE ###

Task 2 <Gegion Growing>:

### CODE STARTS HERE ###


import numpy as np
from skimage import io, color, segmentation
from skimage.segmentation import random_walker
import matplotlib.pyplot as plt

image = io.imread('download.jpeg')

CS477 Computer Vision


grayscale = color.rgb2gray(image)

markers = np.zeros_like(grayscale, dtype=int)


markers[grayscale < 0.3] = 1
markers[grayscale > 0.7] = 2

labels = random_walker(grayscale, markers)

plt.imshow(labels, cmap='gray')
plt.title('Region Growing Segmentation')
plt.show()
### CODE ENDS HERE ###

### SCREENSHOTS START HERE ###

CS477 Computer Vision


### SCREENSHOTS END HERE ###

CS477 Computer Vision


### EXPLANATION AND DISCUSSION START HERE ###

### EXPLANATION AND DISCUSSION END HERE ###

Task 3 <technique_name>:

### CODE STARTS HERE ###


#Watershed Algorithm
import cv2
import numpy as np
import matplotlib.pyplot as plt
from google.colab import files
from google.colab.patches import cv2_imshow

image = cv2.imread('OIP111.jpg')
original_image = image.copy()
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Convert to
grayscale

_, binary_image = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY_INV +


cv2.THRESH_OTSU)

# Removing Noise
kernel = np.ones((3, 3), np.uint8)
opening = cv2.morphologyEx(binary_image, cv2.MORPH_OPEN, kernel,
iterations=2)

# identifying sure background and sure foreground


sure_bg = cv2.dilate(opening, kernel, iterations=3) # Sure background

CS477 Computer Vision


dist_transform = cv2.distanceTransform(opening, cv2.DIST_L2, 5) # Distance
transform
_, sure_fg = cv2.threshold(dist_transform, 0.7 * dist_transform.max(), 255, 0) #
Sure foreground

# Identify unknown regions


sure_fg = np.uint8(sure_fg)
unknown = cv2.subtract(sure_bg, sure_fg)

# 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

# Applying the Watershed algorithm


markers = cv2.watershed(image, markers)
image[markers == -1] = [255, 0, 0] # Mark boundaries in red

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')

CS477 Computer Vision


plt.title("Sure Background")
plt.axis("off")

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 ###

### SCREENSHOTS START HERE ###

CS477 Computer Vision


### SCREENSHOTS END HERE ###

### EXPLANATION AND DISCUSSION START HERE ###

### EXPLANATION AND DISCUSSION END HERE ###

Concluding Section:

### COMPARISON STARTS HERE ###

### COMPARISON ENDS HERE ###

CS477 Computer Vision

You might also like