使用opencv制作3通道直方图



我试图绘制3通道图像的直方图和" openv_tut .exe中0x752e812f的未处理异常:Microsoft c++异常:cv::内存位置0x0019ef5c的异常.."它一直得到这个错误。甚至不能调试它一直进入汇编。

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

代码如下:

空白colorHistogram () {

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);

}

可能是一个任性的指针,或者超出了已分配内存的界限。不要假设你之前的代码是正确的;像这样的问题有时可能在早期就引起了,但后来才出现。

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

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

相关内容

  • 没有找到相关文章

最新更新