def refine_and_color_edges(edges, img_rgb, color=[0, 0, 255]):
# Perform morphological operations to refine edges
kernel = np.ones((5, 5), np.uint8)
refined_edges = cv2.dilate(edges, kernel, iterations=1)
refined_edges = cv2.erode(refined_edges, kernel, iterations=1)
# Create an RGB version of the refined edges
refined_colored = np.zeros_like(img_rgb)
refined_colored[refined_edges == 255] = color
# Overlay the refined edges on the original image
overlay = cv2.addWeighted(img_rgb, 0.8, refined_colored, 0.2, 0)
return refined_colored, overlay
# Refine and color edges for each image
refined_a, overlay_refined_a = refine_and_color_edges(edges_a, img_a_rgb, [0, 0, 255])
refined_b, overlay_refined_b = refine_and_color_edges(edges_b, img_b_rgb, [0, 0, 255])
refined_c, overlay_refined_c = refine_and_color_edges(edges_c, img_c_rgb, [0, 0, 255])
# Display the results
fig, axes = plt.subplots(2, 3, figsize=(15, 10))
axes[0, 0].imshow(refined_a)
axes[0, 0].axis('off')
axes[0, 1].imshow(refined_b)
axes[0, 1].axis('off')
axes[0, 2].imshow(refined_c)
axes[0, 2].axis('off')
axes[1, 0].imshow(overlay_refined_a)
axes[1, 0].set_title('Overlay Fig (a)')
axes[1, 0].axis('off')
axes[1, 1].imshow(overlay_refined_b)
axes[1, 1].set_title('Overlay Fig (b)')
axes[1, 1].axis('off')
axes[1, 2].imshow(overlay_refined_c)
axes[1, 2].set_title('Overlay Fig (c)')
axes[1, 2].axis('off')
plt.show()