//main文件 #include <opencv2/opencv.hpp> using namespace cv; using namespace std; #include "Histogram.h" int main(){ cv::Mat image = cv::imread("f:\\Img\\Lena.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); return 0; } Histogram cpp文件 #include "Histogram.h" 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; } Histogram头文件 #ifndef HISTOGRAM_H_ #define HISTOGRAM_H_ #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <iostream> #include <vector> 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); }; #endif /* HISTOGRAM_H_ */
#include <opencv2/opencv.hpp> using namespace cv; using namespace std; int main(){ Mat mat = imread("f:\\img\\changhong.jpg"); Mat mergeImg;//合并后的图像 //用来存储各通道图片的向量 vector<Mat> splitBGR(mat.channels()); //分割通道,存储到splitBGR中 split(mat,splitBGR); //对各个通道分别进行直方图均衡化 for(int i=0; i<mat.channels(); i++) equalizeHist(splitBGR[i],splitBGR[i]); //合并通道 merge(splitBGR,mergeImg); imshow("src",mat); imshow("hist",mergeImg); cv::waitKey(0); return 0; }