I have a group of images. Each image is a collage of multiple images.
I want to extract all the smaller images from the collage. I have tried multiple edge detection algorithms. Please suggest an alternative to my following approach.
def detect_objects(frame):
# Convert the frame to grayscale
gray_image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Apply Canny edge detection
edges = cv2.Canny(gray_image, 5, 150)
# Convert edges to binary image using a threshold
_, binary_edges = cv2.threshold(edges, 50, 255, cv2.THRESH_BINARY)
# Find contours in the binary image
contours, _ = cv2.findContours(
binary_edges.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
)
# Iterate through each contour
for i, contour in enumerate(contours):
# Approximate the contour to a polygon
epsilon = 0.02 * cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, epsilon, True)
# Check if the polygon has 4 sides
if len(approx) == 4 and cv2.contourArea(contour) > 1500:
# Draw the contour on the frame
cv2.drawContours(frame, [contour], -1, (0, 255, 0), 3)
# Calculate the area of the contour
area = cv2.contourArea(contour)
print(f"Area of contour {i+1}: {area}")
# Return the frame with contours
return frame
How to make this code better? Is there a better approach to this?
)](https://i.stack.imgur.com/S0Vxp.jpg)