How to use the OTSU Threshold in opencv?

后端 未结 3 1484
猫巷女王i
猫巷女王i 2020-12-24 01:25

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

相关标签:
3条回答
  • 2020-12-24 01:47

    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()
    
    0 讨论(0)
  • 2020-12-24 01:56

    In Android is one line.

    Imgproc.threshold(matGrayIn, matOtsuOut, 0, 255, Imgproc.THRESH_OTSU | Imgproc.THRESH_BINARY);
    
    0 讨论(0)
  • 2020-12-24 01:59

    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

    0 讨论(0)
提交回复
热议问题