0

Hi i'm trying to convert a matlab code to opencv code. So I need something that does what graythesh in matlab does and give me a proper threshold for my grayscale image. Has this been already implemented in opencv cause I couldn't find it.

Thank you

Shai
  • 111,146
  • 38
  • 238
  • 371
soroosh
  • 3
  • 1
  • 3
  • OpenCV Threshold functions are described here : [Basic Thresholding Operations](http://docs.opencv.org/doc/tutorials/imgproc/threshold/threshold.html) – lucasg Apr 17 '13 at 09:58
  • These are thresholding functions and I already knew about them. What I wanted was a function that calculates a good threshold. Of course using otsu's thresholding method. – soroosh Apr 17 '13 at 10:01
  • 1
    there is a type THRESH_OTSU for threshold function [link here](http://opencv.willowgarage.com/documentation/cpp/miscellaneous_image_transformations.html#cv-threshold). Otherwise, there are several implementations ([here](http://opencv-users.1802565.n2.nabble.com/Otsu-thresholding-td3491712.html) and [there](http://stackoverflow.com/questions/12953993/otsu-thresholding-for-depth-image) ) – lucasg Apr 17 '13 at 10:52

2 Answers2

3

As @georgesl mentioned, combine THRESH_OTSU with other types, for example:

threshold ( grey_image, bin_image, 0, 255, THRESH_BINARY | THRESH_OTSU );
Safir
  • 902
  • 7
  • 9
0

You can use Opencv. In order to do so, the cv.threshold() function is used, where cv.THRESH_OTSU is passed as an extra flag. The threshold value can be chosen arbitrary. The algorithm then finds the optimal threshold value which is returned as the first output.

import cv2 as cv
ret2,th2 = cv.threshold(img,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
#th2 is the thresholded image

Source: https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html