ret, labels = cv2.connectedComponents(mask) regions = cv2.connectedComponentsWithStats(mask) circle_areas = regions[2][1:3, cv2.CC_STAT_AREA] print(f"Areas of the two circles: {circle_areas}")
# Crop the image to the bounding box of the circle with the largest area largest_area = np.argmax(circle_areas) # Get the bounding box of the largest area x, y, w, h, area = regions[2][largest_area + 1] print(f"Bounding box of the largest circle: {x, y, w, h}") # Crop the image to the bounding box of the circle large_area_cropped_image = masked_image[y:y + h, x:x + w] # Crop the image to the bounding box of the circle with the smallest area smallest_area = np.argmin(circle_areas) # Get the bounding box of the smallest area x, y, w, h, area = regions[2][smallest_area + 1] print(f"Bounding box of the smallest circle: {x, y, w, h}") # Crop the image to the bounding box of the circle small_area_cropped_image = masked_image[y:y + h, x:x + w] # Pad the smaller image to the same size as the larger image large_height, large_width = large_area_cropped_image.shape[:2] small_height, small_width = small_area_cropped_image.shape[:2]
# Calculate padding for each side top_padding = (large_height - small_height) // 2 bottom_padding = large_height - small_height - top_padding left_padding = (large_width - small_width) // 2 right_padding = large_width - small_width - left_padding # Add padding to the smaller image small_area_cropped_image_padded = cv2.copyMakeBorder( small_area_cropped_image, top_padding, bottom_padding, left_padding, right_padding, cv2.BORDER_CONSTANT, value=[0, 0, 0] ) # Replot the images side by side fig, ax = plt.subplots(1, 2, figsize=(10, 5)) ax[0].imshow(large_area_cropped_image) ax[0].set_title("Largest Circle") ax[1].imshow(small_area_cropped_image_padded) ax[1].set_title("Smallest Circle") # Give one title to the entire figure fig.suptitle("Cropped Images Scaled") plt.show()
# Making a copy of the image to get the pixel values image = small_area_cropped_image_padded.copy() # Extract the pixel values and coordinates within the circle circle_pixels = [] circle_coordinates = [] # Create a mask for non-black pixels mask = np.any(image != [0, 0, 0], axis=-1) # Get the coordinates of non-black pixels circle_coordinates = np.argwhere(mask) # Get the pixel values within the circle circle_pixels = image[mask] """Within the large_area_cropped_image circle, set all pixels within circle_coordinates to match circle pixels""" # Making a copy of the image to draw on image_copy = np.copy(large_area_cropped_image) # Loop through the coordinates and pixel values for (y, x), pixel in zip(circle_coordinates, circle_pixels): # Draw the pixel onto the image copy image_copy[y, x] = pixel # Display the image plt.imshow(image_copy) plt.title("Proof: Smaller Circle in the Larger Circle") plt.show() # Calculate the percentage of size difference between the two circles large_area = np.pi * (large_width / 2) ** 2 small_area = np.pi * (small_width / 2) ** 2 difference = (small_area) / large_area * 100 # Print the text "% of the size!" inside the circle cv2.putText( image_copy, f"{difference:.1f}% of the size!", (large_width // 5, large_height // 2), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2 ) # Display the image plt.imshow(image_copy) plt.title("Percentage of Size") plt.show()