0

我试图绘制 3 通道图像的直方图和“opencv_tut.exe 中 0x752e812f 处的未处理异常:Microsoft C++ 异常:内存位置 0x0019ef5c 处的 cv::Exception ..”它不断收到此错误。甚至无法调试它继续进入装配。

问题出在直方图中的线条绘制部分,其他的工作正常。

这是代码:

无效颜色直方图(){

Mat image = imread("c:/aaa.jpg");
vector<Mat> bgr_planes;
split(image, bgr_planes);

if(!image.data){

    cout<<"No image"<<endl;

}

int histSize = 256;
float range[] = {0, 256};
const float* histRange = {range};

Mat b_hist, g_hist, r_hist;

calcHist(&bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, true, false);
calcHist(&bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, true, false); 
calcHist(&bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, true, false); 

int hist_w = 512;
int hist_h = 400;

int bin_w = cvRound((double) hist_w/histSize);

Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0,0,0));

normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());

for(int i=0; i<histSize; i++){

  line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1))) , Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i))), Scalar( 255, 0, 0), 2, 8, 0  );
  line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1))), Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i))), Scalar( 0, 255, 0), 2, 8, 0  );
  line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1))) , Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i))), Scalar( 0, 0, 255), 2, 8, 0  );

}

namedWindow("Histogram", CV_WINDOW_AUTOSIZE);
imshow("Histogram", histImage);

}

4

1 回答 1

0

可能是一个任性的指针或超出分配内存的范围。不要假设您之前的代码是正确的;像这样的问题有时可能会更早引起,并且只会在以后出现。

无论如何,您似乎在这里修改了代码:http: //docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html

主要区别在于您的 for 循环从 0 变为 histSize,而他们的循环从 1 变为 histSize。由于此索引用于计算循环中的数组位置,我猜这是您的问题。

于 2013-03-07T14:23:30.103 回答