opencv 二值化图像 像素统计 countNonZero
生活随笔
收集整理的這篇文章主要介紹了
opencv 二值化图像 像素统计 countNonZero
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
對(duì)二值化圖像執(zhí)行countNonZero。可得到非零像素點(diǎn)數(shù).
IplImage* srcImg = cvLoadImage("Lena.jpg");//注意:當(dāng)將參數(shù)copyData設(shè)為true后,則為深拷貝(復(fù)制整個(gè)圖像數(shù)據(jù)) 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: 二進(jìn)制閾值1: 反二進(jìn)制閾值2: 截?cái)嚅撝?: 0閾值4: 反0閾值*/threshold(image,result,150,255,thresholdtype(thresh));//改變參數(shù)實(shí)現(xiàn)不同的threshold// 得到非0的像素值int iVal255 = countNonZero(result);cv::namedWindow(windowstring);cv::imshow(windowstring,result);//顯示輸出結(jié)果cv::imwrite(imagestring,result);windowstring[7]++;imagestring[7]++;}waitKey(0); }程序結(jié)果:
threshold.jpg(源圖像)
THRESH_BINARY(二進(jìn)制閾值)
?
THRESH_BINARY_INV(反二進(jìn)制閾值)
?THRESH_TRUNC(截?cái)嚅撝?#xff09;
??
?THRESH_TOZERO(0閾值)
?
THRESH_TOZERO_INV(反0閾值)
?
總結(jié)
以上是生活随笔為你收集整理的opencv 二值化图像 像素统计 countNonZero的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: send/recv与socket
- 下一篇: 空间统计分析(一)