OpenCV(3.4.1)错误:空指针

时间:2018-03-14 09:23:55

标签: c++ opencv

我刚开始使用c ++学习OpenCV,对于一个简单的项目,我试图将滑块跟踪器添加到视频播放器中。 代码编译没有问题但是当我尝试执行它时,生成了以下错误:

OpenCV(3.4.1) Error: Null pointer (No trackbar found) in cvSetTrackbarPos, file /opt/opencv/modules/highgui/src/window_gtk.cpp, line 1507
terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(3.4.1) /opt/opencv/modules/highgui/src/window_gtk.cpp:1507: error: (-27) No trackbar found in function cvSetTrackbarPos
Aborted (core dumped)**

该代码的灵感来自于使用OpenCV库-Ocilly(2017)和#34;以及使用OpenCV库来学习OpenCV 3_计算机视觉。 :

#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <fstream>

int slider_position = 0;
int run = 1;
int dontest = 0; //start out in single step mode
cv::VideoCapture capture;

void onTrackBarSlide(int position, void*){
    capture.set(cv::CAP_PROP_POS_FRAMES, position);
    if(!dontest) run = 1;
    dontest = 0;
}
int main(int argc, char** argv)
{
    cv::namedWindow("video playin", cv::WINDOW_AUTOSIZE);
    capture.open(std::string(argv[1]));
    int frames = (int) capture.get(cv::CAP_PROP_FRAME_COUNT);
    int tmpWidth = (int) capture.get(cv::CAP_PROP_FRAME_WIDTH);
    int tmpheight = (int) capture.get(cv::CAP_PROP_FRAME_HEIGHT);
    std::cout << "Video has " << frames << " frames of dimensions(" << tmpWidth 
                << " , " << tmpheight << std::endl;
    cv::createTrackbar("Position", "video play", &slider_position, frames, onTrackBarSlide);
    cv::Mat frame;
    for(;;){
        if(run != 0){ capture >> frame;
            if(frame.empty()) break;
            int current_position = (int) capture.get(cv::CAP_PROP_POS_FRAMES);
            dontest = 1;
            cv::setTrackbarPos("Position", "video play", current_position);
            cv::imshow("video play", frame);
        }
        char key_pressed = (char) cv::waitKey(10);
        if(key_pressed == 'd') //single step
        {
            run = 1;
            std::cout << " single step , run = " << run << std::endl;
        }
        if( key_pressed == 'q') //run mode
        {
            run = -1; 
            std::cout << " Run mode, run = " << run << std::endl;
        }
        if(key_pressed == 27) break;
    }
    return 0;
}

1 个答案:

答案 0 :(得分:1)

窗口名称中有错误,请更改

cv::namedWindow("video playin", cv::WINDOW_AUTOSIZE);

到这个

cv::namedWindow("video play", cv::WINDOW_AUTOSIZE);