直方图可视化,图像阈值化

#ifndef HISTOGRAM_H_
#define HISTOGRAM_H_
#include
#include
#include
#include 
#include
#include 

 using namespace std;
 using namespace cv;
  
 class Histogram1D
 {
 private:
	 int histSize[1];
	 float hranges[2];
	 const float *ranges[1];
	 int channels[1];
 public:
	 Histogram1D()
	 {
		 histSize[0]=256;
		 hranges[0]=0.0;
		 hranges[1]=255.0;
		 ranges[0]=hranges;
		 channels[0]=0;
	 };
	 cv::MatND getHistogram(const cv::Mat &image)
	 {
		 cv::MatND hist;
		 cv::calcHist(&image,1,channels,cv::Mat(),hist,1,histSize,ranges);
		 return hist;
	 };
	 cv::Mat 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(0.9*histSize[0]);
		 for(int h=0;h(h);
			 int intensity=static_cast(binVal*hpt/maxVal);
			 cv::line(histImg,cv::Point(h,histSize[0]),cv::Point(h,histSize[0]-intensity),cv::Scalar::all(0));
		 }
		 return histImg;
	 };
 };

#endif /* HISTOGRAM_H_ */

#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);
	 waitKey(0);
	 return 0;
 }


直方图可视化,图像阈值化_第1张图片

你可能感兴趣的:(opencv2)