I am trying to detect roi in my image dataset using Otsu thresholding. While in some cases results are on the point, some cases are not that good. I'm using the code below.
import cv2 as cv
import numpy as np
path = "./images/{}.png".format
for num in range(1000):
filename = path(num)
img = cv.imread(filename, cv.IMREAD_GRAYSCALE)
res = cv.threshold(img, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)[1]
res = np.hstack((img, res))
cv.imwrite(filename.replace(".png", "_.png"), res)
While these results are acceptable
These definitely need to be improved
How can I improve my code?