【opencv】Mat数据直方图计算函数


Mat calcHistogramsImage(Mat src, int &valMax)
{
	int nChannel = src.channels();

	valMax = 0;
	if (nChannel == 1)
	{
		/// Establish the number of bins
		int histSize = 256;
		/// Set the ranges ( for B,G,R) )
		float range[] = { 0, 256 } ;
		const float* histRange = { range };

		bool uniform = true;
		bool accumulate = false;

		Mat hist;
		/// Compute the histograms:
		calcHist( &src, 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform, accumulate );

		// Draw the histograms for B, G and R
		int hist_w = 512; 
		int hist_h = 400;

		int bin_w = cvRound( (double) hist_w/histSize );
		Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );
		/// Normalize the result to [ 0, histImage.rows ]
		normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );

		Mat lineImg(10, hist_w, CV_8UC3, Scalar( 0,0,0) );

		line(lineImg,Point(0,1),Point(hist_w,1),Scalar( 255, 255, 255),1);

		int step = 10;
		int bin = hist_w/step;
		for (int i=0;i<bin;i++)
		{
			line(lineImg,Point(10*i,0),Point(10*i,10),Scalar( 255, 255, 255),1);

			if (i%4 ==0)
			{
				line(lineImg,Point(10*i,0),Point(10*i,10),Scalar(255 , 255, 255),2);
			}
			else
			{
				line(lineImg,Point(10*i,0),Point(10*i,10),Scalar( 100, 100, 100),1);
			}
		}

		line(lineImg,Point(200,0),Point(200,10),Scalar( 255, 255, 255),6);
		line(lineImg,Point(400,0),Point(400,10),Scalar( 255, 255, 255),6);

		float val_tmp = 0;
		valMax = 0;
		for( int i = 1; i < histSize; i++ )
		{
			if (hist.at<float>(i) >val_tmp)
			{
				val_tmp = hist.at<float>(i);
				valMax = i;
			}
		}

		/// Draw for each channel
		for( int i = 1; i < histSize; i++ )
		{
			line( histImage, Point( bin_w*(i-1), hist_h - cvRound(hist.at<float>(i-1))) ,
				Point( bin_w*(i), hist_h - cvRound(hist.at<float>(i)) ),
				Scalar( 255, 255, 255), 1, 8, 0 );
		}

		Mat showM;
		showM.push_back(histImage);
		showM.push_back(lineImg);
		return showM;	
	}
	else if (nChannel ==3)
	{
		/// Separate the image in 3 places ( B, G and R )
		vector<Mat> bgr_planes;
		split( src, bgr_planes );
		/// Establish the number of bins
		int histSize = 256;
		/// Set the ranges ( for B,G,R) )
		float range[] = { 0, 256 } ;
		const float* histRange = { range };
		bool uniform = true; bool accumulate = false;
		Mat b_hist, g_hist, r_hist;
		/// Compute the histograms:
		calcHist( &bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate );
		calcHist( &bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate );
		calcHist( &bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate );

		// Draw the histograms for B, G and R
		int hist_w = 512; int hist_h = 400;
		int bin_w = cvRound( (double) hist_w/histSize );
		Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );
		/// Normalize the result to [ 0, histImage.rows ]
		normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
		normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
		normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
		/// Draw for each channel
		for( int i = 1; i < histSize; i++ )
		{
			line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) ,
				Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ),
				Scalar( 255, 0, 0), 1, 8, 0 );
			line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) ,
				Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ),
				Scalar( 0, 255, 0), 1, 8, 0 );
			line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) ,
				Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ),
				Scalar( 0, 0, 255), 1, 8, 0 );
		}
		return histImage;	
	}
	else
	{
		return Mat();
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小新识图

你的鼓励是我最大的分享动力。

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

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

打赏作者

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

抵扣说明:

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

余额充值