opencv 二值化图像 像素统计 countNonZero
生活随笔
收集整理的這篇文章主要介紹了
opencv 二值化图像 像素统计 countNonZero
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
對二值化圖像執行countNonZero。可得到非零像素點數.
IplImage* srcImg = cvLoadImage("Lena.jpg");//注意:當將參數copyData設為true后,則為深拷貝(復制整個圖像數據) Mat M(srcImg, true);#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <string>using namespace cv;void main() {cv::Mat image = imread("threshold.jpg");cv::namedWindow("original");cv::imshow("original",image);string windowstring = "result 0";string imagestring = "result 0.jpg";cv::Mat result;enum thresholdtype{THRESH_BINARY ,THRESH_BINARY_INV,THRESH_TRUNC,THRESH_TOZERO,THRESH_TOZERO_INV};for (int thresh = 0;thresh<5;thresh++){/* 0: 二進制閾值1: 反二進制閾值2: 截斷閾值3: 0閾值4: 反0閾值*/threshold(image,result,150,255,thresholdtype(thresh));//改變參數實現不同的threshold// 得到非0的像素值int iVal255 = countNonZero(result);cv::namedWindow(windowstring);cv::imshow(windowstring,result);//顯示輸出結果cv::imwrite(imagestring,result);windowstring[7]++;imagestring[7]++;}waitKey(0); }程序結果:
threshold.jpg(源圖像)
THRESH_BINARY(二進制閾值)
?
THRESH_BINARY_INV(反二進制閾值)
?THRESH_TRUNC(截斷閾值)
??
?THRESH_TOZERO(0閾值)
?
THRESH_TOZERO_INV(反0閾值)
?
總結
以上是生活随笔為你收集整理的opencv 二值化图像 像素统计 countNonZero的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: send/recv与socket
- 下一篇: 空间统计分析(一)