Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi This is the opencv tutorial example code, but when I'm trying to run it i get "floating point" Error! why?

C++
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
    Mat src, dst;
    src=imread("/home/chris/Pictures/1.png",1);
    if(!src.data){return -1;}

    vector<Mat> bgr_planes;
    split(src, bgr_planes);

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

    bool uniform=true; bool accumulate=false;
    Mat b_hist, g_hist, r_hist;

    calcHist(&bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate);
    calcHist(&bgr_planes[1], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate);
    calcHist(&bgr_planes[2], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate);

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

    }
    cvNamedWindow("Demo", CV_WINDOW_AUTOSIZE);
    imshow("Demo",histImage );
    waitKey(0);

    return 0;
}
Posted
Comments
Sergey Alexandrovich Kryukov 24-Aug-14 2:12am    
In what line?
—SA
hor_313 24-Aug-14 3:46am    
I don't know! my IDE doesn't give me any Error or message!
http://www.uploadax.com/images/59541239857892400333.png
Richard MacCutchan 24-Aug-14 5:14am    
You need to use your debugger to catch the exception.
hor_313 24-Aug-14 6:33am    
I don't know how can I do it :(
Richard MacCutchan 24-Aug-14 6:36am    
I don't know it depends on which development tools you are using. If you are using Visual Studio, then add a breakpoint to the first call to calcHist, start the program in the debugger and then step through until the exception fires. You should then be able to figure out what values caused the error.

1 solution

Thank you guys! I solved the problem. my mistake was in writing "b_hist" in all calcHist function,while I must wrote g_hist and r_hist in 2nd and 3th calcHist functions respectively!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900