Ask Your Question

Extracting foreground with otsu thresholding

asked 2018-12-09 02:00:49 -0500

Th3_noob gravatar image

updated 2018-12-13 23:08:58 -0500

I am trying to extract region of interest in the following image with otsu thresholding.

tiff image

img = cv2.imread("IM_00024.TIF", 0) 
ret, imgf = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) 
cv2.imwrite('imgobsu.png', imgf)

image description

Based on my understanding of THRESH_BINARY option , my region of interest will be the white section of the obsu image. How can i apply the segmented obsu image to the original image so that i can segment it in such as way that i obtain both the foreground and the background

Edit 1:

In my attempt to create a mask that contain everything except the brain(i.e the background mask) , i found the largest contour and then i fill it with black.

_, contours, _ = cv2.findContours(otsu_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
largest_contour = max(contours, key=cv2.contourArea)
cv2.drawContours(otsu_mask, largest_contour, -1, 0, 200)

image description

However part of the brain is still not properly segmented.How can i improve the algorithm

edit retag flag offensive close merge delete


I do not understand you very well, but I think that you should try this: res = cv2.bitwise_and(img, img, mask=imgf)

albertofernandez gravatar imagealbertofernandez ( 2018-12-09 13:21:09 -0500 )edit

I think a better way to segment the mask is to flood fill the white region inside the brain

Th3_noob gravatar imageTh3_noob ( 2018-12-10 10:22:27 -0500 )edit

@albertofernandez check my updated question

Th3_noob gravatar imageTh3_noob ( 2018-12-13 23:09:27 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-12-14 01:53:50 -0500

Th3_noob gravatar image

I obtained better results by changing my approach.Instead of trying to take the brain out of the MRI , I separate the text from the image since the text are completely white.

img = cv2.imread("IM_00024.TIF", 0)
ret, mask = cv2.threshold(img, 254, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)
foreground = cv2.bitwise_and(img, img, mask=mask_inv)
background = cv2.bitwise_and(img, img, mask=mask)
save_img("IM_00024_bg.pnm", background)
save_img("IM_00024_fg.pnm", foreground)
edit flag offensive delete link more
Login/Signup to Answer

Question Tools



Asked: 2018-12-09 02:00:49 -0500

Seen: 379 times

Last updated: Dec 14 '18