我试图绘制 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);
}