class Histogram1D { public: Histogram1D() { histSize[0]=256; hranges[0]=0.0; hranges[1]=255.0; ranges[0]=hranges; channels[0]=0; } MatND getHistogram(const Mat &image) { MatND hist; calcHist(&image,1,channels,Mat(),hist,1,histSize,ranges); return hist; } protected: private: int histSize[1]; float hranges[2]; const float* ranges[1]; int channels[1]; }; Mat drawhist(MatND hist) { double maxvalue=0; double minvalue=0; minMaxLoc(hist,&minvalue,&maxvalue,0,0); Mat histimg(256,256,CV_8U,Scalar(255)); int hpt=static_cast<int>(0.9*256); for (int h=0;h<256;h++) { float binval=hist.at<float>(h); int intensity=static_cast<int>(binval*hpt/maxvalue); line(histimg,Point(h,256),Point(h,256-intensity),Scalar(0)); } return histimg; } int _tmain(int argc, _TCHAR* argv[]) { Mat src=imread("D:\\image\\img2\\lena.jpg",0); if (!src.data) { cout<<"can not find picture"<<endl; } //int thresh=otsu(src); Mat img; //threshold(src,img,thresh,255,CV_THRESH_BINARY); //calchist(src); Histogram1D h; MatND hist=h.getHistogram(src); Mat histimg=drawhist(hist); //for (int i=0;i<256;i++) //{ // cout<<"value"<<i<<"="<<hist.at<float>(i)<<endl; //} namedWindow("win1"); imshow("win1",histimg); waitKey(0); return 0; } |
|