2013-03-07 93 views
0

我試圖繪製3通道圖像的直方圖,以及「opencv_tut.exe中0x752e812f處未處理的異常:Microsoft C++異常:內存位置0x0019ef5c處的cv :: Exception」這個錯誤。甚至不能調試它繼續進入組裝。使用3通道使用opencv創建直方圖圖表

問題是在直方圖中的線條繪製部分,其他人只是正常工作。

下面是代碼:

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

}

回答

0

大概任性指針或超越所分配的存儲器的邊界。不要以爲你以前的代碼是正確的;像這樣的問題有時可能會導致更早出現,並且只會在稍後出現。

在任何情況下,看來你在這裏修改的代碼:http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html

的主要區別在於你的for循環從0到HISTSIZE和他們從1到HISTSIZE。由於該索引用於計算循環中的陣列位置,所以我猜這就是你的問題。

+0

ty ..我只是複製這段代碼因爲它正在做我所需要的。 0是問題。 – 2013-03-07 17:52:13