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阈值)

 


  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

围观岳老师

雁过留音人过留名你鼓励为我动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值