如何在Opencv中创建直方图投影? [英] How Can I Create Histogram Projection In Opencv ?

查看:106
本文介绍了如何在Opencv中创建直方图投影?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将从图像中创建直方图投影(水平直方图和垂直直方图),我处理滤镜图像和阈值后的图像。我该怎么办?



获取我正在使用的图片:



i'll create histogram projection (horizontal histogram and vertical histogram)from image that the image after i do process filter image and thresholding. how should i do???

for get the image i'm using :

Mat img  = cvLoadImage("C:/Chipmonk.jpg", CV_LOAD_IMAGE_COLOR); //open and read the image

	if (img.empty())
	{
		cout << "Image cannot be loaded..!!" << endl;
		return -1;
	}





然后我会过滤平均值和边缘检测。然后我会处理阈值

之后我会创建直方图投影。



我该怎么办?



感谢



then i do filter average and edge detection. then i do process threshold
after that i'll create histogram projection.

what should i do?

thanks before

推荐答案

我不确定这是否是你想要的。但请查看以下代码。它创建了阈值图像的水平和垂直直方图。实际上它只是计算所有非零像素并相对于X或Y坐标存储在Mat对象中。



I am not sure whether this is you want exactly. But have a look on the following code. It creates horizontal and vertical histograms of a thresholded image. What it actually does is simply count all nonzero pixels and store in a Mat object with respect to X or Y coordinate.

//To store the gray version of the image
	Mat gray;
	//To store the thresholded image
	Mat ret;
	//convert the image to grayscale
	cvtColor(img,gray,CV_BGR2GRAY);
	imshow("Gray Image",gray);
	//threshold the image
	threshold(gray,ret,0,255,CV_THRESH_BINARY_INV+CV_THRESH_OTSU);

	Mat horizontal(ret.cols,1,CV_32S);//horizontal histogram
	horizontal = Scalar::all(0);
	Mat vertical(ret.rows,1,CV_32S);//vertical histogram	
	vertical = Scalar::all(0);
	
	for(int i=0;i<ret.cols;i++)
	{
		horizontal.at<int>(i,0)=countNonZero(ret(Rect(i,0,1,ret.rows)));
	}

	for(int i=0;i<ret.rows;i++)
	{
		vertical.at<int>(i,0) = countNonZero(ret(Rect(0,i,ret.cols,1)));
	}


这篇关于如何在Opencv中创建直方图投影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆