Ask Your Question

Revision history [back]

Example code Histogram Calculation not generating a window

I am extremely new to C++ and I am trying to run the example code from the online documentation, located in the imgproc module module tutorials on the OpenCV 2.4.13.2 documentation. I would post a link but I have insufficient karma.

I have made a new project using the openFrameworks projectGenerator, opened it in XCode (I am running el Capitan) and when I run it, it builds successfully but no window opens. First I wanted to check, should I be writing my code in the main.cpp file, or the ofApp.cpp, or ofApp.h file?

I have left the ofApp.cpp and the ofApp.h files as they were when the project was generated.

Here is what I currently have in the main.cpp file:

    #include "ofMain.h"
    #include "ofApp.h"
    #include "ofxOpenCv.h"
    #include "highgui.h"

using namespace cv;

//========================================================================
int main(int argc, char** argv){
    ofSetupOpenGL(1024,768,OF_WINDOW);// <-------- setup the GL context
    cv::Mat src, dst;

    // Load image
    string imageName( "../data/grad.png");

    if (argc > 1){
        imageName = argv[1];
    }

    src = imread( imageName, IMREAD_COLOR);

    if( src.empty() ){
        return -1;
    }

    // separate image into RGB planes
    vector<cv::Mat> bgr_planes;
    split( src, bgr_planes);

    // establish number of bins
    int histSize = 256;

    // set ranges for B, G, R,
    float range[] = { 0, 256 };
    const float* histRange = { range };

    bool uniform = true; bool accumulate = false;

    cv::Mat b_hist, g_hist, r_hist;

    // compute the histograms
    calcHist( &bgr_planes[0], 1, 0, cv::Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate );
    calcHist( &bgr_planes[1], 1, 0, cv::Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate );
    calcHist( &bgr_planes[2], 1, 0, cv::Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate );

    // draw the histograms for B, G, R
    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 the result to [ 0, histImage.rows ]
    normalize(b_hist, b_hist, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat() );
    normalize(g_hist, g_hist, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat() );
    normalize(r_hist, r_hist, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat() );

    // draw for each channel
    for( int i = 1; i < histSize; i++ ){
        line( histImage,
             cv::Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) ,
             cv::Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ),
             cv::Scalar( 255, 0, 0), 2, 8, 0  );
        line( histImage,    cv::Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) ,
             cv::Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ),
             cv::Scalar( 0, 255, 0), 2, 8, 0  );
        line( histImage,    cv::Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) ,
             cv::Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ),
             cv::Scalar( 0, 0, 255), 2, 8, 0  );
    }

    // display

    namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE );
    imshow("calcHist Demo", histImage );

    waitKey(0);


    return 0;
}


// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
// ofRunApp(new ofApp());

//}

Thanks in advance for helping me get started with OpenCV.