OpenCV中threshold函數的使用


轉自:https://blog.csdn.net/u012566751/article/details/77046445

一篇很好的介紹threshold文章;

圖像的二值化就是將圖像上的像素點的灰度值設置為0或255,這樣將使整個圖像呈現出明顯的黑白效果。在數字圖像處理中,二值圖像占有非常重要的地位,圖像的二值化使圖像中數據量大為減少,從而能凸顯出目標的輪廓。OpenCV中提供了函數cv::threshold();
 
注意:作者采用OpenCV 3.0.0
 
函數原型
 
參數說明
src:源圖像,可以為8位的灰度圖,也可以為32位的彩色圖像。(兩者由區別)
dst:輸出圖像
thresh:閾值
maxval:dst圖像中最大值
type:閾值類型,可以具體類型如下:
編號 閾值類型枚舉
注意
1 THRESH_BINARY  
2 THRESH_BINARY_INV  
3 THRESH_TRUNC  
4 THRESH_TOZERO  
5 THRESH_TOZERO_INV  
6 THRESH_MASK
不支持
7 THRESH_OTSU
不支持32位
8 THRESH_TRIANGLE
不支持32位
具體如下表
 
生成關系如下表
 
 
測試代碼
Mat gray;
    cvtColor(src, gray, CV_BGR2GRAY); 
    // 全局二值化
    int th = 100;
    cv::Mat threshold1,threshold2,threshold3,threshold4,threshold5,threshold6,threshold7,threshold8;
    cv::threshold(gray, threshold1, th, 255, THRESH_BINARY);
    cv::threshold(gray, threshold2, th, 255, THRESH_BINARY_INV);
    cv::threshold(gray, threshold3, th, 255, THRESH_TRUNC);
    cv::threshold(gray, threshold4, th, 255, THRESH_TOZERO);
    cv::threshold(gray, threshold5, th, 255, THRESH_TOZERO_INV);
    //cv::threshold(gray, threshold6, th, 255, THRESH_MASK);
    cv::threshold(gray, threshold7, th, 255, THRESH_OTSU);
    cv::threshold(gray, threshold8, th, 255, THRESH_TRIANGLE);
    cv::imshow("THRESH_BINARY", threshold1);
    cv::imshow("THRESH_BINARY_INV", threshold2);
    cv::imshow("THRESH_TRUNC", threshold3);
    cv::imshow("THRESH_TOZERO", threshold4);
    cv::imshow("THRESH_TOZERO_INV", threshold5);
    //cv::imshow("THRESH_MASK", threshold6);
    cv::imshow("THRESH_OTSU", threshold7);
    cv::imshow("THRESH_TRIANGLE", threshold8);
    cv::waitKey(0);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM