#ifndef HISTOGRAM_H_ #define HISTOGRAM_H_ #include<opencv2/core/core.hpp> #include<opencv2/highgui/highgui.hpp> #include<opencv2/imgproc/imgproc.hpp> #include <opencv2/calib3d/calib3d.hpp> #include<iostream> #include <vector> using namespace std; using namespace cv; class Histogram1D { private: int histSize[1]; float hranges[2]; const float *ranges[1]; int channels[1]; public: Histogram1D(); cv::MatND getHistogram(const cv::Mat &image); cv::Mat getHistogramImage(const cv::Mat &image); cv::Mat applyLoopUp(const cv::Mat &image,const cv::Mat &lookup); }; #endif /* HISTOGRAM_H_ */ #include"Histogram1D.h" Histogram1D::Histogram1D() { histSize[0]=256; hranges[0]=0.0; hranges[1]=255.0; ranges[0]=hranges; channels[0]=0; }; cv::MatND Histogram1D::getHistogram(const cv::Mat &image) { cv::MatND hist; cv::calcHist(&image,1,channels,cv::Mat(),hist,1,histSize,ranges); return hist; }; cv::Mat Histogram1D::getHistogramImage(const cv::Mat &image) { cv::MatND hist=getHistogram(image); double maxVal=0; double minVal=0; cv::minMaxLoc(hist,&minVal,&maxVal,0,0); cv::Mat histImg(histSize[0],histSize[0],CV_8U,cv::Scalar(255)); int hpt=static_cast<int>(0.9*histSize[0]); for(int h=0;h<histSize[0];h++) { float binVal=hist.at<float>(h); int intensity=static_cast<int>(binVal*hpt/maxVal); cv::line(histImg,cv::Point(h,histSize[0]),cv::Point(h,histSize[0]-intensity),cv::Scalar::all(0)); } return histImg; }; cv::Mat Histogram1D::applyLoopUp(const cv::Mat &image,const cv::Mat &lookup) { cv::Mat result; cv::LUT(image,lookup,result); return result; } #include"Histogram1D.h" int main() { cv::Mat image=cv::imread("d:\\test\\opencv\\group.jpg",0); if( !image.data ) exit(0); Histogram1D h; cv::namedWindow("Histogram"); cv::imshow("Histogram",h.getHistogramImage(image)); cv::Mat thresholded; cv::threshold(image,thresholded,60,255,cv::THRESH_BINARY); cv::namedWindow("Binary Image"); cv::imshow("Binary Image",thresholded); int dim(256); cv::Mat lut(1,&dim,CV_8U); for(int i=0;i<256;i++) { lut.at<uchar>(i)=255-i; } cv::namedWindow("Negative image"); cv::imshow("Negative image",h.applyLoopUp(image,lut)); waitKey(0); return 0; }
opencv2使用查找表修改图像外观,布布扣,bubuko.com
原文:http://blog.csdn.net/williamfan21c/article/details/24309345