0

I have tried to apply Otsu's within ROIs of an image to get a better output. My current iteration of code is shown below where I apply Otsu's to 45x45 ROI segments of an image. The output is still not the best, especially when compared to Adaptive Thresholding methods. How can I get a clearer and all around better output? Is there another method for applying Otsu's within ROIs that is recommended?

def roiOtsu(image):
    size = 45
    column, row, result = properties(image)
    for i in range(0, column,size):
        for j in range (0, row,size):
            ret,result[i:i+size, j:j+size] = cv2.threshold(image[i:i+size, j:j+size],0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    return result

Revexr
  • 1
  • 4
  • How about adding some test images? Why do you consider the output "not the best"? Also, might want to [read the theory](https://medium.com/@hbyacademic/otsu-thresholding-4337710dc519) behind Otsu's thresholding just to make sure that's the method that will work in your case. – stateMachine Jan 06 '21 at 21:40
  • Otsu threshold is not always appropriate for all images. You might try simple tresholding or adaptive thresholding. Please show your input and output images – fmw42 Jan 06 '21 at 23:07

1 Answers1

1

Otsu's method works for bimodal images (images whose histograms have two peaks), is your image bimodal?

Anyway

By dividing the image and calculating a treshold for each patch you are using Adaptive thresholding. Otsu's treshold instead should be applied to the whole image, thus shouldn't be used like that.

Have you tried simply:

otsu_threshold, image_result = cv2.threshold(
    image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU,
)
print("Obtained threshold: ", otsu_threshold)

If the result still isn't satisfyng you could turn to adaptive methods but using the right algorithm. You can get some good exmplanation from this other answer

Hope this was usefull for you!

Francesco Pegoraro
  • 778
  • 13
  • 33