S.EB
S.EB

Reputation: 2226

How to show the overlay of an object on an image with only borders in python?

I saw a very nice representation of overlaying results on image in a paper and trying to do a similar approach on my data. Here ground truth is the object overlayed with red borderlines and segmentation result is with green color.

enter image description here I have 3 images: MRI, ground truth (gt) and results obtained from an algorithm (res). How should I write the code for this similar representation in python? Or if there is any free code available to use, please share here. Thanks

Upvotes: 3

Views: 5481

Answers (2)

Chalie
Chalie

Reputation: 11

Assuming the ground truth is a binary segmentation mask, this skimage find contour should work. Multiple contours may be found and you can choose to only plot one of them. Set different values for parameter 'level' to extract contours from multiclass segmentation mask.

Upvotes: 1

Masoud
Masoud

Reputation: 1280

If the ground-truth and res are 2D masks, you can create a RGB image from your grayscale image and change the color of pixels where res indicates to. Here is an example of highlighting edges extracted using Canny edge detector on a sample image:

import cv2
from matplotlib import pyplot as plt

img = cv2.imread('brain.png',0)
edges = cv2.Canny(img,50,200)   # canny edge detector

img = cv2.merge((img,img,img))  # creat RGB image from grayscale
img2 = img.copy()
img2[edges == 255] = [255, 0, 0]  # turn edges to red

plt.subplot(121),plt.imshow(img)
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img2)
plt.title('Edge Highlighted'), plt.xticks([]), plt.yticks([])

plt.show()

result

Upvotes: 4

Related Questions