user1146904
user1146904

Reputation: 263

Adaptive thresholding on image causing data loss

I am determining adaptive threshold using OTSU and then using determined threshold to convert image to black & white. On this processed image I want to carry out further steps of determining density of each circle, but my black & white image is over corrected and resulting in data loss. Any suggestions on how to tweak adaptive threshold.

im_gray = cv2.imread(img, cv2.CV_LOAD_IMAGE_GRAYSCALE)
(thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
img_bw = cv2.threshold(im_gray, thresh, 255, cv2.THRESH_BINARY)[1]

enter image description here enter image description here

Upvotes: 2

Views: 3288

Answers (1)

Vlad
Vlad

Reputation: 4525

Try adaptiveThreshold() from openCV, It will calculate the threshold based on the intensities in the window. It seems that the OTSU method doesn’t work as expected in your case since adaptiveThreshold uses just an average (minus a constant) and works better (see image below) than OTSU that uses a more optimal criterion.

It is also not clear what is a spatial extent of the OTSU. If it is a whole image, then it should fail since the right side of the image is more blurred than the left and thus dark is washed out on the right. The adaptive threshold makes calculations in a window so it is locally adaptive. Note that the last two parameters in the function below are the size of the window and the value to subtract from the average when forming a threshold.

adaptiveThreshold(I, dst, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 21, 15);

You may get better results when putting together OTSU and locally adaptive properties. However, the white color is typically oversampled and this causes a bias in the estimate. It is better to sample on the both sides of the gradient to get equal samples of white and dark. It is even better to take into account connectivity and color when thresholding, see grab cut;

Finally, the loss of information is always inevitable during thresholding. enter image description here

Upvotes: 3

Related Questions