opencv2 学习第7天 RGB图像的直方图 & 灰度图的直方图

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <vector>
#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
//class Histgram1D
//{
//public:
//	Histgram1D()
//	{
//		histsize[0] = 256;
//		hranges[0] = 0.0;
//		hranges[1] = 255.0;
//		ranges[0] = hranges;
//		channels[0] = 0;//by default,we look at channel 0
//	}
//	// Computes the 1D histogram
//	cv::MatND gethistogram(const Mat & image)
//	{
//		MatND hist;
//		//compute histogram
//		calcHist(&image,
//			1,//histogram from 1 image only
//			channels,//the channel used
//			Mat(),//no mask is used
//			hist,//the resulting histogram
//			1,//it is a 1D histogram
//			histsize,//number of bins
//			ranges//pixel value range
//			);
//		return hist;
//	}
//	//computes the 1D histogram and returns an image of it
//	Mat getHistogramImage(const Mat &image)
//	{
//		MatND hist = gethistogram(image);//compute histogram first
//		double maxVal = 0;
//		double minVal = 0;
//		minMaxLoc(hist,&minVal,&maxVal,0,0);
//		Mat histimg(histsize[0],histsize[0],CV_8U,Scalar(255));//image on which to display histogram
//		int hpt = static_cast<int>(0.9*histsize[0]);
//		//draw a vertical line foe each bin
//		for (int h=0;h<histsize[0];h++)
//		{
//			float binval = hist.at<float>(h);
//			int intensity = static_cast<int>(binval*hpt/maxVal);
//			//this function draws a line between 2 points
//			line(histimg,Point(h,histsize[0]),Point(h,histsize[0]-intensity),Scalar::all(0));
//		}
//		return histimg;
//	}
//private:
//	int histsize[1];//number of bins
//	float hranges[2];//min and max pixel value
//	const float *ranges[1];
//	int channels[1];//only one channel used here
//
//
//};
//ColorHistogram
class Histogram {
private:
	int histSize[1];
	float hrangee[2];
	const float* ranges[1];
	int channels[1];

protected:
	cv::Mat getHistogram(const cv::Mat&);

public:
	Histogram();
	cv::Mat getHistogramImage(const cv::Mat&, int channel = 0);
};
Histogram::Histogram() {
	histSize[0] = 256;
	hrangee[0] = 0.0;
	hrangee[1] = 255.0;
	ranges[0] = hrangee;
	channels[0] = 0;
}


cv::Mat Histogram::getHistogram(const cv::Mat& image){
	cv::MatND hist;
	cv::calcHist(&image, 1, channels, cv::Mat(), hist, 1, histSize, ranges);
	return hist;
}


cv::Mat Histogram::getHistogramImage(const cv::Mat& image, int channel){
	std::vector<cv::Mat> planes;
	cv::split(image,planes);
	cv::Scalar color;
	if(planes.size() == 1){
		channel = 0;
		color = cv::Scalar(0,0,0);
	}else{
		color = cv::Scalar(channel==0?255:0, channel==1?255:0, channel==2?255:0);
	}
	cv::MatND hist = getHistogram(planes[channel]);
	double maxVal = 0;
	double minVal = 0;
	cv::minMaxLoc(hist, &minVal, &maxVal, 0, 0);
	cv::Mat histImg(histSize[0], histSize[0], CV_8UC3, cv::Scalar(255,255,255));
	int hpt = static_cast<int>(0.9*histSize[0]);
	for(int h=0; h<histSize[0]-1; h++){
		float binVal = hist.at<float>(h);
		float binVal2 = hist.at<float>(h+1);
		int intensity = static_cast<int>(binVal*hpt/maxVal);
		int intensity2 = static_cast<int>(binVal2*hpt/maxVal);
		cv::line(histImg, cv::Point(h,histSize[0]-intensity),
			cv::Point(h,histSize[0]-intensity2), color);
	}
	return histImg;
}
int main(){
	cv::Mat image = cv::imread("D:\\3.jpg");
	Histogram h;

	cv::namedWindow("Red");
	cv::namedWindow("Blue");
	cv::namedWindow("Green");
	cv::namedWindow("Original");
	cv::imshow("Original",image);
	cv::imshow("Red",h.getHistogramImage(image,2));
	cv::imshow("Green",h.getHistogramImage(image,1));
	cv::imshow("Blue",h.getHistogramImage(image));

	cv::waitKey(0);
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值