2013-01-24 48 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