Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the OTSU Threshold in opencv?

I was using a fixed threshold but turns out that it's not so good for me. Then, someone told me about the otsu threshold. How can I use it in my code? I read about it and I don't understand very well. Could someone explain to me how to use it in OpenCV the otsu threshold?

Here is my code now:

    #include <opencv2/imgproc/imgproc.hpp>     #include <opencv2/highgui/highgui.hpp>      using namespace cv;      int main ( int argc, char **argv )     {        Mat im_gray = imread("img3.jpg",CV_LOAD_IMAGE_GRAYSCALE);         Mat im_rgb  = imread("img3.jpg");        cvtColor(im_rgb,im_gray,CV_RGB2GRAY);         Mat img_bw = im_gray > 115;         imwrite("img_bw3.jpg", img_bw);         return 0;     }   

With this I have to change the threshold to any image that I want to convert to binary. I found this:

    cvThreshold(scr, dst, 128, 255, CV_THRESH_BINARY | CV_THRESH_OTSU); 

Is that right? I don't understand very well and because of that, didn't know how I could adapt to my code.

like image 346
U23r Avatar asked Jun 17 '13 06:06

U23r


People also ask

What is Otsu thresholding OpenCV?

In Otsu Thresholding, a value of the threshold isn't chosen but is determined automatically. A bimodal image (two distinct image values) is considered. The histogram generated contains two peaks. So, a generic condition would be to choose a threshold value that lies in the middle of both the histogram peak values.

How do I set threshold value in OpenCV?

We use the cv2. THRESH_BINARY_INV method, which indicates that pixel values p less than T are set to the output value (the third argument). The cv2. threshold function then returns a tuple of 2 values: the first, T, is the threshold value.

How does threshold work in OpenCV?

Thresholding is a technique in OpenCV, which is the assignment of pixel values in relation to the threshold value provided. In thresholding, each pixel value is compared with the threshold value. If the pixel value is smaller than the threshold, it is set to 0, otherwise, it is set to a maximum value (generally 255).


2 Answers

Following line makes otsu thresholding operation:

cv::threshold(im_gray, img_bw, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU); 
  • im_gray is a source 8-bit image,
  • img_bw is a result,
  • 0 means threshold level which actually is omitted because we used CV_THRESH_OTSU flag,
  • 255 is a value that is going to be assigned to respectively pixels in the result (namely, to all pixels which value in the source is greater then computed threshold level)
  • CV_THRESH_BINARY | CV_THRESH_OTSU is a required flag to perform Otsu thresholding. Because in fact we would like to perform binary thresholding, so we use CV_THRESH_BINARY (you can use any of 5 flags opencv provides) combined with CV_THRESH_OTSU

Link to documentation: http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html#threshold

like image 82
marol Avatar answered Sep 20 '22 14:09

marol


In python it is simple

import cv2  img = cv2.imread('img.jpg',0)  #pass 0 to convert into gray level  ret,thr = cv2.threshold(img, 0, 255, cv2.THRESH_OTSU) cv2.imshow('win1', thr) cv2.waitKey(0)   cv2.destroyAllWindows() 
like image 44
ashish Avatar answered Sep 17 '22 14:09

ashish