2013-01-24 52 views

回答

0

所以,你需要一个掩码图像上的2048个分箱的直方图,返回一个保存计数和分箱值的矩阵。

我想你可以使用calcHist,甚至可以自定义它或构建你自己的函数。 您需要知道如何访问数组元素,数组上的操作以及直方图上的基本理论。

您的问题隐藏了很多问题,因为Matlab的水平较高。打破你的问题,你会发现散落在论坛内的所有的答案...

+0

我发现这一点:cvCalcHist(IMG,历史,0,roi_mask);但我现在的问题是计算负值的图像的分段图。 – user1783116

+0

@ user1783116创建图像时使用IPL_DEPTH_8S。 S =有符号U =无符号。 – William

1

对于您可以使用下面的类(类是来自OpenCV的2计算机视觉书)的直方图:

class Histogram1D{ 
private: 
    int histSize[1]; 
    float hranges[2]; 
    const float* ranges[1]; 
    int channels[1]; 
    cv::MatND hist; 
public: 
    Histogram1D() 
     { 
     histSize[0] = 2048; 
     hranges[0]=0.0; 
     hranges[1] = 255.0; 
     ranges[0] = hranges; 
     channels[0] = 0;//by default, we look at channel 0 
     } 
    Histogram1D(float minval,float maxval) 
     { 
     histSize[0] = 2048; 
     hranges[0]=minval; 
     hranges[1] = maxval; 
     ranges[0] = hranges; 
     channels[0] = 0;//by default, we look at channel 0 
     } 

    //Hier wird die cv funktion zum bestimmen vom Histogramm gestartet 
    cv::MatND calcHistogram(const cv::Mat &image){ 
     //compute histogram 
     cv::calcHist(&image, 
      1,   //histogram from 1 image only 
      channels, //the channel used 
      cv::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; 
     } 

    cv::MatND getHistogram(){ 


     return hist; 
     } 

    cv::Mat getHistogramImage(){ 
     //compute histogram first 
     cv::MatND hist= getHistogram(); 

     //get min and max bin values 
     double maxVal=0; 
     double minVal=0; 
     cv::minMaxLoc(hist, &minVal, &maxVal, 0,0); 

     //Image on which to display histogram 
     cv::Mat histImg(histSize[0]+40, histSize[0],CV_8U,cv::Scalar(255)); 

     //set highest point at 90% of nbins 
     int hpt = static_cast<int>(0.9*histSize[0]); 

     //Draw a vertical line for 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 
      cv::line(histImg,cv::Point(h,histSize[0]),cv::Point(h,histSize[0]-intensity),cv::Scalar::all(0)); 
      } 

     //min und max val im Histogramm angeben 
     char maxValStr[10],minValStr[10]; 
     sprintf (maxValStr, "%d",static_cast<int>(hranges[1])); 
     sprintf (minValStr, "%d",static_cast<int>(hranges[0])); 

     int fontFace = cv::FONT_HERSHEY_SCRIPT_SIMPLEX; 
     double fontScale = 0.3; 
     int thickness = 1; 
     cv::Point textOrgmax(histSize[0]-40, histSize[0]+20),textOrgmin(5, histSize[0]+20); 

     cv::putText(histImg, maxValStr, textOrgmax, fontFace, fontScale, cv::Scalar::all(0), thickness,8); 
     cv::putText(histImg, minValStr, textOrgmin, fontFace, fontScale, cv::Scalar::all(0), thickness,8); 
     return histImg; 
     } 

    }; 

这里是你如何使用它:

// Transform it into the C++ cv::Mat format 
cv::Mat image(imagesource); 

// Setup a rectangle to define your region of interest 
cv::Rect myROI(10, 10, 100, 100); 

// Crop the full image to that image contained by the rectangle myROI 
// Note that this doesn't copy the data 
cv::Mat croppedImage = image(myROI); 


    cv::Mat tmp = croppedImage .clone();//clone image 
     double min =0,max = 0; 
    cv::minMaxLoc(tmp,&min,&max); 

    cv::namedWindow("Histogram",CV_WINDOW_AUTOSIZE); 
    Histogram1D h(min,max); 
    h.calcHistogram(tmp); 
    cv::imshow("Histogram",h.getHistogramImage()); 
+0

谢谢你的回答,但问题是投资回报率不是矩形,而是像一个圆圈(但事实并非如此)。此外,我使用calcHistogram,但我的源图像有一些负面价值。我试图纠正它把范围在0-255之间,但它不能很好地工作。是否有可能通过使用2048个相对于256个灰度级的相对值来计算具有负值的图像的直方图? – user1783116

+0

您可以生成一维图像,将圆形图像的像素值放置在该一维图像中(或更好地说明一维cv :: Mat数组)。在我的函数中,你有hranges [0] = minval; hranges [1] = maxval;你是否尝试过使用它,也许它也适用于负值。尝试使用我发布的类我认为calcHistogramm是旧的C风格函数。 – jamk