2

I want to create a histogram for an green component of an image in c++ using OpenCV. The following code is working fine for color image but once i split the image into its RGB component and using the green component to call calcHist function, I am getting the following error.

OpenCV Error: Assertion failed (j < nimages) in histPrepareImages, file /root/src/OpenCV-2.4.1/modules/imgproc/src/histogram.cpp, line 148 terminate called after throwing an instance of 'cv::Exception' what(): /root/src/OpenCV-2.4.1/modules/imgproc/src/histogram.cpp:148: error: (-215) j < nimages in function histPrepareImages Aborted (core dumped)

Here is my code for the same. I took two images to create the histogram. Anyone pls help so solve this problem.

#include <cv.h>
#include <highgui.h>

using namespace cv;

int main( int argc, char** argv )
{
    Mat src,src1, hsv, hsv1;
    if( argc != 3 || !(src=imread(argv[1], 1)).data || !(src=imread(argv[2], 1)).data)
        return -1;

    std::vector<cv::Mat> three_channels;
    cv::split(src,three_channels);

    std::vector<cv::Mat> three_channels1;
    cv::split(src1,three_channels1);


    //cvtColor(src, hsv, CV_BGR2HSV);
    //cvtColor(src1, hsv1, CV_BGR2HSV);

    // Quantize the hue to 30 levels
    // and the saturation to 32 levels
    int hbins = 30, sbins = 32;
    int histSize[] = {hbins, sbins};
    // hue varies from 0 to 179, see cvtColor
    float hranges[] = { 0, 180 };
    // saturation varies from 0 (black-gray-white) to
    // 255 (pure spectrum color)
    float sranges[] = { 0, 256 };
    const float* ranges[] = { hranges, sranges };
    MatND hist, hist1, difference;
    // we compute the histogram from the 0-th and 1-st channels
    int channels[] = {0, 1};

    calcHist( &three_channels[1], 1, channels, Mat(), // do not use mask
            hist, 2, histSize, ranges,
            true, // the histogram is uniform
            false );
    calcHist( &three_channels1[1], 1, channels, Mat(), // do not use mask
            hist1, 2, histSize, ranges,
            true, // the histogram is uniform
            false );
    double maxVal=0;
    minMaxLoc(hist, 0, &maxVal, 0, 0);
    minMaxLoc(hist1, 0, &maxVal, 0, 0);

    int scale = 10;
    Mat histImg = Mat::zeros(sbins*scale, hbins*10, CV_8UC3);
    Mat hist1Img = Mat::zeros(sbins*scale, hbins*10, CV_8UC3);
    Mat hist2Img = Mat::zeros(sbins*scale, hbins*10, CV_8UC3);

    double hist_diff =0;    
    hist_diff = compareHist(hist, hist1, CV_COMP_CORREL);

    absdiff(hist, hist1, difference);

    printf("\nHist Diff:   %f\n", hist_diff);

    for( int h = 0; h < hbins; h++ )
        for( int s = 0; s < sbins; s++ )
        {
            float binVal = hist.at<float>(h, s);
            int intensity = cvRound(binVal*255/maxVal);
            rectangle( histImg, Point(h*scale, s*scale),
                    Point( (h+1)*scale - 1, (s+1)*scale - 1),
                    Scalar::all(intensity),
                    CV_FILLED );
        }
    for( int h = 0; h < hbins; h++ )
        for( int s = 0; s < sbins; s++ )
        {
            float binVal = hist1.at<float>(h, s);
            int intensity = cvRound(binVal*255/maxVal);
            rectangle( hist1Img, Point(h*scale, s*scale),
                    Point( (h+1)*scale - 1, (s+1)*scale - 1),
                    Scalar::all(intensity),
                    CV_FILLED );
        }

    for( int h = 0; h < hbins; h++ )
        for( int s = 0; s < sbins; s++ )
        {
            float binVal = difference.at<float>(h, s);
            int intensity = cvRound(binVal*255/maxVal);
            rectangle( hist2Img, Point(h*scale, s*scale),
                    Point( (h+1)*scale - 1, (s+1)*scale - 1),
                    Scalar::all(intensity),
                    CV_FILLED );
        }


    namedWindow( "Source", 1 );
    imshow( "Source", src );

    namedWindow( "H-S Histogram", 1 );
    imshow( "H-S Histogram", histImg );

    namedWindow( "H-S Histogram1", 1 );
    imshow( "H-S Histogram1", hist1Img );

    namedWindow( "H-S Histogram2", 1 );
    imshow( "H-S Histogram2", hist2Img );
    waitKey();
}
4

1 回答 1

4

您正在尝试从只有一个通道的图像中计算两个通道(0 和 1)的直方图,因为您将其拆分。

我没有详细查看您的代码,但我想您可以省略拆分并将 src/src1 传递给 calcHist 而不是 three_channels[1]/three_channels1[1],设置channels = {1}

编辑

在您的代码中,更改channels = {0,1}channels{0},您应该不会收到任何错误。您将单通道图像传递给calcHist(),这就是为什么您应该只使用通道 0(唯一一个)。通过将 three_channels[1] 作为输入图像传递,您可以确保您实际上正在分析输入图像的第二个通道。

或执行以下操作:

    int channels[] = {1};

    calcHist( &src, 1, channels, Mat(), // do not use mask
        hist, 2, histSize, ranges,
        true, // the histogram is uniform
        false );

你不再需要cv::split(src,three_channels)了。

这是编译的两个版本,但您实际上想要计算绿色 (1D) 直方图而不是 2D 直方图。所以这是您编辑的代码,(希望)可以满足您的要求:


int main( int argc, char** argv )
{
    Mat src,src1;
    if( argc != 3 || !(src=imread(argv[1], 1)).data || !(src=imread(argv[2], 1)).data)
            return -1;



    // Quantize the green to 30 levels
    int greenbins = 30;
    int histSize[] = {greenbins};
    // green varies from 0 to 255 (pure spectrum color)
    float greenranges[] = { 0, 256 };
    const float* ranges[] = { greenranges };
    MatND hist, hist1, difference;
    // we compute the histogram from the 2nd channel (green, index is 1)
    int channels[] = {1};

    calcHist( &src, 1, channels, Mat(), // do not use mask
            hist, 1, histSize, ranges,
            true, // the histogram is uniform
            false );
    calcHist( &src1, 1, channels, Mat(), // do not use mask
            hist1, 1, histSize, ranges,
            true, // the histogram is uniform
            false );
    double maxVal1=0;
    double maxVal2 =0;
    minMaxLoc(hist, 0, &maxVal1, 0, 0);
    minMaxLoc(hist1, 0, &maxVal2, 0, 0);
    double maxVal = max(maxVal1, maxVal2);

    int scale = 10;
    int width = 50;
    Mat histImg = Mat::zeros(greenbins*scale, width, CV_8UC3);
    Mat hist1Img = Mat::zeros(greenbins*scale, width, CV_8UC3);
    Mat hist2Img = Mat::zeros(greenbins*scale, width, CV_8UC3);

    double hist_diff =0;    
    hist_diff = compareHist(hist, hist1, CV_COMP_CORREL);

    absdiff(hist, hist1, difference);

    printf("\nHist Diff:   %f\n", hist_diff);

    for( int h = 0; h<greenbins; ++h)
    {
        float binVal = hist.at<float>(h);
        int intensity = cvRound(binVal*255/maxVal);
        rectangle( histImg, Point(0, h*scale),
                    Point(width, (h+1)*scale),
                    Scalar::all(intensity),
                    CV_FILLED );
    }
    for( int h = 0; h<greenbins; ++h)
    {
        float binVal = hist1.at<float>(h);
        int intensity = cvRound(binVal*255/maxVal);
        rectangle( hist1Img, Point(0, h*scale),
                    Point(width, (h+1)*scale),
                    Scalar::all(intensity),
                    CV_FILLED );
    }

    for(int h = 0; h < greenbins; ++h)
    {
        float binVal = difference.at<float>(h);
        int intensity = cvRound(binVal*255/maxVal);
        rectangle( hist2Img, Point(0, h*scale),
                    Point(width, (h+1)*scale),
                    Scalar::all(intensity),
                    CV_FILLED );
    }

    imshow( "Source", src );
    imshow( "Source1", src1 );

    imshow( "src1 green Histogram", histImg );

    imshow( "src2 green Histogram", hist1Img );

    imshow( "diff green Histogram", hist2Img );
    waitKey();
}
于 2012-07-25T08:11:22.630 回答