【opencv】閾值處理函數threshold()詳解

參考opencv的思想,使用C++重新實現圖像的閾值操作,代碼詳見:代碼

數字圖像處理中,閾值操作佔有非常重要的地位,例如圖像的二值化可以使圖像中數據量大爲減少,從而能凸顯出目標的輪廓。opencv中提供了函數cv::threshold()用於圖像的閾值操作。

函數原型

double cv::threshold(InputArray src, OutputArray dst, double thres, double maxval, int type)

參數說明

src:源圖像,可以爲8位的灰度圖,也可以爲32位的彩色圖像;

dst:輸出圖像;

thresh:閾值;

maxval:二值圖像中灰度最大值;

type:閾值操作類型,具體的閾值操作實現如下圖所示:

測試代碼

	//全局二值化
	int th = 100;
	cv::Mat threshold1, threshold2, threshold3, threshold4, threshold5, threshold6, threshold7, threshold8;
	cv::threshold(gray, threshold1, th, 255, cv::THRESH_BINARY);
	cv::threshold(gray, threshold2, th, 255, cv::THRESH_BINARY_INV);
	cv::threshold(gray, threshold3, th, 255, cv::THRESH_TRUNC);
	cv::threshold(gray, threshold4, th, 255, cv::THRESH_TOZERO);
	cv::threshold(gray, threshold5, th, 255, cv::THRESH_TOZERO_INV);
	cv::threshold(gray, threshold7, th, 255, cv::THRESH_OTSU);
	cv::threshold(gray, threshold8, th, 255, cv::THRESH_TRIANGLE);
	cv::imshow("gray", gray);
	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_OTSU", threshold7);
	cv::imshow("THRESH_TRIANGLE", threshold8);

測試結果對比

原灰度圖
THRESH_BINARY
THRESH_BINARY_INV
THRESH_TRUNC
THRESH_TOZERO
THRESH_TOZERO_INV
THRESH_OTSU
THRESH_TRIANGLE

參考:

https://blog.csdn.net/weixin_42296411/article/details/80901080

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章