简体   繁体   中英

OpenCV Program Runtime Error

I have made a basic program using OpenCV 2.4.1 to open 2 windows. The program is compiling correctly. When I try to run the object file then it does not open the windows. The source code and the compilation command are given below. Where am I going wrong?

#include <cstdio>
#include "cv.h"
#include "highgui.h"

void changeColor(int pos)
{
cvSet(imgColor, CV_RGB(red, green, blue), NULL);    
}

int main()
{
    int red, blue, green;
    cvNamedWindow("DrawArea", 0);   //area for inputting digits
    cvNamedWindow("ColorSelector", 0);  //area for selecting colour of input

    cvCreateTrackbar("Red", "ColorSelector", &red, 255, &changeColor);
    cvCreateTrackbar("Green", "ColorSelector", &green, 255, &changeColor);
    cvCreateTrackbar("Blue", "ColorSelector", &blue, 255, &changeColor);


    cvSetMouseCallback(“Demo”,&on_mouse, 0 );
}

The command used for compilation is:

gcc `pkg-config opencv --cflags` paint.cpp  -o paint `pkg-config opencv --libs`

On running the object file the following output is displayed:

./paint
init done 
opengl support available 

Your program has a couple of issues.

First of all imgColor is not declared, you'll need:

IplImage* imgColor = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 3);

Also, on:

void changeColor(int pos)
{
    cvSet(imgColor, CV_RGB(red, green, blue), NULL);    
}

you can't access neither red , nor green , nor blue here, for they are local to main. I take it this is just a concept proof example, let's declare these global.

Now onto why no windows is displayed. There are two reasons:

  1. That may sound obvious, but well, main is returning, your program is simply exiting. As sgar91 already pointed out, you'll need cvWaitKey(0); at the end of main so you program can hold there processing gui events.

  2. That also might sound obvious, but you haven't actually instructed OpenCV to show anything. You'll need cvShowImage("ColorSelector", imgColor); , this will trigger events for window painting inside OpenCV;

The following quick and dirty example works fine and I'm able to select the color which is displayed on the window.

#include <cstdio>
#include "cv.h"
#include "highgui.h"

int red, blue, green;
IplImage* imgColor = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 3);

void changeColor(int pos)
{
    cvSet(imgColor, CV_RGB(red, green, blue), NULL);   
    cvShowImage("ColorSelector", imgColor);
}

int main()
{
//     cvNamedWindow("DrawArea", 0);   //area for inputting digits
    cvNamedWindow("ColorSelector", 0);  //area for selecting colour of input

    cvShowImage("ColorSelector", imgColor);

    cvCreateTrackbar("Red", "ColorSelector", &red, 255, &changeColor);
    cvCreateTrackbar("Green", "ColorSelector", &green, 255, &changeColor);
    cvCreateTrackbar("Blue", "ColorSelector", &blue, 255, &changeColor);

//     cvSetMouseCallback("Demo", &on_mouse, 0);

    cvWaitKey(0);
}

I tried to simplify your problem, this worked for me:

#include <cstdio>
#include <cv.h>
#include <highgui.h>

using namespace cv;

void changeColor(int pos) {}

int main() {
    int value;

    cvNamedWindow("ColorSelector", 0);  //area for selecting colour of input
    cvCreateTrackbar("Red", "ColorSelector", &value, 255, &changeColor);

    char key = 0;
    Mat original = imread("/path/to/fileimage.png");
    while ((int)key != 27) {
        Mat temp = original.clone();
        for (int i = 0; i < temp.rows; ++i) {
            for (int j = 0; j < temp.cols; ++j) {
                temp.at<uchar>(i, j, 0) = value * (float)temp.at<uchar>(i, j, 0) / 255;
            }
        }

        imshow("ColorSelector", temp);
        key = waitKey(1);
    }
}

The reference manual for waitKey() states that:

Note: This function is the only method in HighGUI that can fetch and handle events, so it needs to be called periodically for normal event processing unless HighGUI is used within an environment that takes care of event processing.

So you definitely have to call it at some point for windows to appear and display the images.

It also notes:

Note: The function only works if there is at least one HighGUI window created and the window is active. If there are several HighGUI windows, any of them can be active.

From your example, no window seems to be active, and then waitKey(0) will do nothing. As in Radford Parker answer, you need to display an image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM