How to use the OTSU Threshold in opencv?

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:

Leave a Comment