sponsored links

画数组的直方图

画数组的直方图

int main()
{
	//画出数组的直方图
	int density[] = {21,13,7,4,4,2,38,2,25,2};//,2,3,2,3,3,3,3,3,2,2,2,1,1,0,0,1,1,3,3,2,2,2,1,1,0,0,1,1,3,3


	Mat histImg;
	histImg.create( 100,160,CV_8UC1);
	histImg.setTo(0);


	for ( int i = 0; i < 10; i++ )
	{
		double tmpCount = density[i];
		rectangle(histImg,Point2f(i*12,100),Point2f(i*12,100-tmpCount),Scalar::all(255),-1);  //画出直方图
			
		Point2f pt0;
		pt0.x = i*12;
		pt0.y = 100 - density[i];

		Point2f pt1;
		pt1.x =  (i+1)*12;
		pt1.y = 100 - density[i+1];
			 
		line(histImg, pt0, pt1,Scalar::all(255),2); //连接直方图的顶点
	}
	

	imshow("hist",histImg);
	waitKey(0);


	return 0;
}

效果图为:

画数组的直方图



Tags: