OpenCV:countNonZero函数

countNonZero():返回灰度值不为0的像素数,可用来判断图像是否全黑。

                                 

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);
}

参考文章:

1. https://blog.csdn.net/zdyueguanyun/article/details/50857089

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值