简体   繁体   English

OpenCV程序运行时错误

[英]OpenCV Program Runtime Error

I have made a basic program using OpenCV 2.4.1 to open 2 windows. 我已经使用OpenCV 2.4.1创建了一个基本程序来打开2个窗口。 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: 首先, imgColor未声明,您需要:

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. 你不能在这里访问redgreenblue ,因为它们是主要的本地。 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); 正如sgar91已经指出的那样,你需要cvWaitKey(0); at the end of main so you program can hold there processing gui events. 在main的末尾,所以你的程序可以在那里处理gui事件。

  2. That also might sound obvious, but you haven't actually instructed OpenCV to show anything. 这听起来也很明显,但你实际上还没有指示OpenCV显示任何内容。 You'll need cvShowImage("ColorSelector", imgColor); 你需要cvShowImage("ColorSelector", imgColor); , this will trigger events for window painting inside OpenCV; ,这将触发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: waitKey()的参考手册指出:

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. 注意:此函数是HighGUI中唯一可以获取和处理事件的方法,因此需要定期调用它以进行正常事件处理,除非在负责事件处理的环境中使用HighGUI。

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. 注意:该功能仅在至少创建一个HighGUI窗口并且窗口处于活动状态时才有效。 If there are several HighGUI windows, any of them can be active. 如果有多个HighGUI窗口,则其中任何一个都可以处于活动状态。

From your example, no window seems to be active, and then waitKey(0) will do nothing. 从您的示例中,似乎没有窗口处于活动状态,然后waitKey(0)将不执行任何操作。 As in Radford Parker answer, you need to display an image. 如在Radford Parker的回答中,您需要显示图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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