Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Image segmentation

Uploaded by

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

Image segmentation

Uploaded by

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

Image segmentation (Point Line Edges)

import cv2
import numpy as np
import matplotlib.pyplot as plt

# Read the input image


image_path = 'cat.jpg' # Path to the image (update this with the actual path)
image = cv2.imread(image_path) # Reads the image from the given path using OpenCV

# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Converts the image to grayscale (OpenCV uses
BGR by default)

# Part 1: Segmentation (using Thresholding)


# Thresholding is used to segment an image into two regions: one above a threshold and the other
below

# Apply a simple binary threshold


# The threshold value is set to 127, where pixels greater than 127 will be set to 255 (white),
# and those less than 127 will be set to 0 (black).
_, thresholded_image = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

# Display the segmented image


plt.figure(figsize=(10, 5)) # Set the size of the plot

# Plot the original image on the left side of the subplot


plt.subplot(1, 2, 1) # 1 row, 2 columns, this is the first subplot
plt.title('Original Image') # Set the title for the original image
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) # Convert BGR to RGB for correct color
display with matplotlib
plt.axis('off') # Turn off axis labels

# Plot the segmented image (thresholded image) on the right side


plt.subplot(1, 2, 2) # 1 row, 2 columns, this is the second subplot
plt.title('Segmented Image (Thresholding)') # Title for the thresholded image
plt.imshow(thresholded_image, cmap='gray') # Display the binary image in grayscale
plt.axis('off') # Turn off axis labels

# Show the plots


plt.show()

# Part 2: Point-Line Edges Detection (using Canny edge detection)


# Canny edge detection is used to detect edges in an image by identifying areas with rapid intensity
changes

# Apply the Canny edge detector


# The first parameter is the image, and the second and third are the low and high thresholds for edge
detection.
edges = cv2.Canny(gray, 100, 200) # Detect edges in the grayscale image

# Display the edges detected by Canny


plt.figure(figsize=(10, 5)) # Set the size of the plot
# Plot the original image again on the left side of the subplot
plt.subplot(1, 2, 1)
plt.title('Original Image') # Title for the original image
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) # Display the original image in RGB format
plt.axis('off') # Turn off axis labels

# Plot the detected edges on the right side


plt.subplot(1, 2, 2)
plt.title('Detected Edges (Canny)') # Title for the edges image
plt.imshow(edges, cmap='gray') # Display the edge-detected image in grayscale
plt.axis('off') # Turn off axis labels

# Show the plots


plt.show()

# Optionally: Extracting points along the edges


# The edges array contains the results of the Canny edge detection, with non-zero values indicating
edges.

# np.column_stack(np.where(edges > 0)) extracts the coordinates (i.e., x, y points) of all non-zero
pixels (edges).
edge_points = np.column_stack(np.where(edges > 0))

# Displaying the number of edge points detected


print(f'Number of points detected on the edge: {len(edge_points)}') # Print the number of edge
points found

#out Put

You might also like