0

有人可以帮我把这个命令翻译成 C++(OpenCV)吗?

[h t] = hist(img(roi_mask>0),2048);
4

2 回答 2

1

对于直方图,您可以使用以下类(类来自 openCV 2 Computer Vision 书):

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());
于 2013-01-24T10:41:01.063 回答
0

因此,您需要蒙版图像上的 2048 个 bin 的直方图,返回一个包含计数和 bin 值的矩阵。

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

您的问题隐藏了许多问题,因为 Matlab 级别更高。分解你的问题,你会发现所有的答案都散落在论坛里......

于 2013-01-24T10:38:38.087 回答