Extracting foreground with otsu thresholding
I am trying to extract region of interest in the following image with otsu thresholding.
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)
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)
However part of the brain is still not properly segmented.How can i improve the algorithm
I do not understand you very well, but I think that you should try this: res = cv2.bitwise_and(img, img, mask=imgf)
I think a better way to segment the mask is to flood fill the white region inside the brain
@albertofernandez check my updated question