user4414636
user4414636

Reputation:

How to take a threshold of only a part of the image with OTSU thresholding?

I'm using OTSU threshold on a dilated and eroded image as shown below:

k = np.ones((5,5),np.float32)/1
        d = cv2.dilate(self.img, k, iterations=10)
        e = cv2.erode(d, k, iterations=10)
        self.thresh = cv2.threshold(e, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

This is the eroded and dilated image, and the one that gets thresholded:

enter image description here

I only want the circular bright region in the middle to be obtained from thresholding, but instead I'm getting this result:

enter image description here

How do I go about thresholding such that I only get the circular region in the middle, which also seems like the brightest (visually) part of the image?

Note: To avoid playing around with different values, I want to stick to OTSU thresholding, but I'm open to ideas.

Upvotes: 0

Views: 1948

Answers (2)

Artem
Artem

Reputation: 1575

You can use Dilate and Erode filters to this image, but in another order: Erode first and then Dialte. It will suppress bright areas from upper side of the image and threshold method will provide better result

enter image description here

enter image description here

enter image description here

enter image description here

Upvotes: 3

dhanushka
dhanushka

Reputation: 10682

You can try a gradient based approach. Below I've used the morphological gradient. I apply Otsu thresholding to this gradient image, followed by a similar amount of morphological closing (10 iterations), then take the morphological gradient of the resulting image.

Now the regions are easy to detect. You can filter the circular region from the contours, for example, using an area based approach: using bounding box dimensions of the contour, you can get an estimate of the radius, then compare the calculated area to the contour area.

Don't know how generic this approach would be for your collection.

Gradient image: intensity values scaled for visualization

grad

Binarized gradient image

bw

Closed image

closed

Gradient

cont

im = cv2.imread('LDxOj.jpg', 0)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
morph = cv2.morphologyEx(im, cv2.MORPH_GRADIENT, kernel)
_, bw = cv2.threshold(morph, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
morph2 = cv2.morphologyEx(bw, cv2.MORPH_CLOSE, kernel, anchor = (-1, -1), iterations = 10)
morph3 = cv2.morphologyEx(morph2, cv2.MORPH_GRADIENT, kernel)

Upvotes: 1

Related Questions