Repository navigation
Closed
Labels
Hackathonhttps://opencv.org/opencv-hackathon-starts-next-week/affected: 3.4EOL - not supported anymorecategory: documentationDocumentation fix or update
Milestone
Description
System information (version)
- OpenCV => OpenCV online documentation
- Operating System / Platform => Not applicable
- Compiler => Python
Detailed description
The Python code for Otsu's binarization gven in the online documentation for OpenCV Image Thresholding at
https://docs.opencv.org/3.4/d7/d4d/tutorial_py_thresholding.html
requires the following corrections:
-
Histogram normalization should be done by dividing by the sum, not max:
hist_norm = hist.ravel()/hist.sum() -
The computation of mean and variances can generate the division-by-zero run-time error when either q1 or q2 is zero. To avoid this, we must include the statement
if q1 < 1.e-6 or q2 < 1.e-6: continue
Corrected code
img = cv.imread('noisy2.png',0)
blur = cv.GaussianBlur(img,(5,5),0)
hist = cv.calcHist([blur],[0],None,[256],[0,256])
hist_norm = hist.ravel()/hist.sum()
Q = hist_norm.cumsum()
bins = np.arange(256)
fn_min = np.inf
thresh = -1
for i in xrange(1,256):
p1,p2 = np.hsplit(hist_norm,[i]) # probabilities
q1,q2 = Q[i],Q[255]-Q[i] # cum sum of classes
if q1 < 1.e-6 or q2 < 1.e-6: continue
b1,b2 = np.hsplit(bins,[i]) # weights
m1,m2 = np.sum(p1*b1)/q1, np.sum(p2*b2)/q2
v1,v2 = np.sum(((b1-m1)**2)*p1)/q1,np.sum(((b2-m2)**2)*p2)/q2
fn = v1*q1 + v2*q2
if fn < fn_min:
fn_min = fn
thresh = i
ret, otsu = cv.threshold(blur,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
print( "{} {}".format(thresh,ret) )
Metadata
Metadata
Assignees
Labels
Hackathonhttps://opencv.org/opencv-hackathon-starts-next-week/affected: 3.4EOL - not supported anymorecategory: documentationDocumentation fix or update
Activity
alalek commentedon Jan 30, 2020
Feel free to prepare PR with fix to 3.4 branch.
Sources of this tutorial are here:
https://github.com/opencv/opencv/tree/3.4/doc/py_tutorials/py_imgproc/py_thresholding
asmorkalov commentedon Feb 4, 2020
#16499
NabiKAZ commentedon Dec 3, 2023
I just tested this new code:
https://docs.opencv.org/3.4/d7/d4d/tutorial_py_thresholding.html
Or this:
https://github.com/opencv/opencv/blob/3.4/doc/py_tutorials/py_imgproc/py_thresholding/py_thresholding.markdown
(both are the same)
The output looked like this:
171 170.0
The sample python script was always one more unit than the opencv output.
Why?