Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- import matplotlib.pyplot as plt
- # Read the image
- image = cv2.imread("image.png")
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
- # Perform connected components analysis
- num_labels, labels_im = cv2.connectedComponents(gray)
- # Create a copy of the original image for drawing
- result_image = image.copy()
- # Perform corner detection for each connected component
- for label in range(1, num_labels): # Start from 1 to skip the background
- # Create a mask for the current component
- component_mask = np.uint8(labels_im == label)
- # Apply the mask to the grayscale image
- component_gray = cv2.bitwise_and(gray, gray, mask=component_mask)
- # Perform corner detection on the component
- corners = cv2.goodFeaturesToTrack(component_gray, 1, 0.01, 10)
- M = cv2.moments(component_mask)
- cX = int(M["m10"] / M["m00"])
- cY = int(M["m01"] / M["m00"])
- if corners is not None:
- corners = np.int0(corners)
- for corner in corners:
- x, y = corner.ravel()
- cv2.arrowedLine(result_image, (cX, cY), (x, y), (0, 255, 0), 3)
- # Display the result
- plt.figure(figsize=(12, 6))
- plt.subplot(121), plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)), plt.title(
- "Original Image"
- )
- plt.subplot(122), plt.imshow(cv2.cvtColor(result_image, cv2.COLOR_BGR2RGB)), plt.title(
- "Corners Detected"
- )
- plt.tight_layout()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement