Ask Your Question

How to create a Binary Mask for medical images

asked 2020-04-05 08:04:31 -0600

Sagi gravatar image

updated 2020-04-06 07:24:19 -0600


I'm try to create binary mask for medical images, im new to OpenCV and any advice will help on how, or if it possible, to do so with OpenCV. Below is the type of pictures I try to create mask for:

image description

I need to color the vocal cords area in white(Marked with triangle) and the rest in black, so it will look something like that:

image description

i tried to find the contours using that code:

import cv2
import numpy as np
img = cv2.imread("LPR.png", cv2.IMREAD_GRAYSCALE)
_, threshold = cv2.threshold(img, 50, 255, cv2. THRESH_BINARY_INV)
_, contours, _ = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
for cnt in contours:
    cv2.drawContours(img, [cnt], 0, (0))
cv2.imshow("LPR", img)
cv2.imshow("Threshold", threshold)

this is the result: image description

i tried to clean the rest of the image using some adaptiveThreshold but the result was worse. i also try to detect the triangle shape that created by the Vocal cords, also without success.

Does anyone have any suggestions on how I can get the desired result or what method I can use?

Any advice will help, Thanks.

The original image for example:

image description

edit retag flag offensive close merge delete


you may need some grabcut or gmm method (which is good for the image with similar color in ROI but bad contours) to extract the triangle region

gino0717 gravatar imagegino0717 ( 2020-04-06 05:02:28 -0600 )edit

@Sagi. Can you post original image w/out marking triangle?

supra56 gravatar imagesupra56 ( 2020-04-06 07:11:16 -0600 )edit

@supra56 I added the original image, Thank you.

Sagi gravatar imageSagi ( 2020-04-06 07:27:35 -0600 )edit

In line 4.Whitespaces cv2. THRESH_BINARY_INV should be cv2.THRESH_BINARY_INV

supra56 gravatar imagesupra56 ( 2020-04-06 08:43:23 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2020-04-06 14:06:04 -0600

supra56 gravatar image

updated 2020-04-06 14:15:59 -0600

The problem had been solved. That is all I can't go any furthers. For opencv 3.x add this `_, contours, _ = . New python code has been changed. For opencv 4.x, see below:

#Raspberry pi 3B/3B+, 4B, Buster, OpenCV 4.2.0
#Date: 5th April, 2020

import cv2 as cv
import numpy as np

img = cv.imread('organ1.jpg')
image_contours = np.zeros((img.shape[1],
                           img.shape[0], 1),

image_binary = np.zeros((img.shape[1],
                         img.shape[0], 1),

for channel in range(img.shape[2]):
    ret, image_thresh = cv.threshold(img[:, :, channel],
                                     38, 255,

    contours = cv.findContours(image_thresh, 1, 1)[0]   
                    contours, -1,
                    (255,255,255), 3)

contours = cv.findContours(image_contours, cv.RETR_LIST,

cv.drawContours(image_binary, [max(contours, key = cv.contourArea)],
                -1, (255, 255, 255), -1)

cv.imwrite('LPR.jpg', image_binary)
cv.imshow('LPR', image_binary)
cv.waitKey(0) & 0xFF is 27


image description

edit flag offensive delete link more


If the had stereoscope moved to left. So that why top left had blocked wall. IF stereoscope position in middle would be better.

supra56 gravatar imagesupra56 ( 2020-04-06 14:13:55 -0600 )edit

@supra56Thank you very much for your help!It works great on the image!! What part of the code can I change to make it work on a different image of the same organ, but that the triangular area is captured more left or right?

Sagi gravatar imageSagi ( 2020-04-07 03:11:29 -0600 )edit

Very sadly. Actually, I can't help you, because I'm moving to OpenCV 4.3.0. As for your next question is ...You're captured more on right.. If you put camera in middle position, you will capture both left on right.

supra56 gravatar imagesupra56 ( 2020-04-08 08:03:29 -0600 )edit

Question Tools

1 follower


Asked: 2020-04-05 08:04:31 -0600

Seen: 18,841 times

Last updated: Apr 06 '20