c++ opencv image not display inside the boost thre

2020-05-06 19:26发布

Im developing c++ application there i used boost, opencv . and create the boost thread and call the function inside that thread. the function has got opencv imread ( i checked using cvLoadimage but i got the same result) but programe can't complete and programe exit.

please find below the code i used

boost::thread *thread_reconstruct; 

    int main( int argc, const char** argv )
    {

        thread_reconstruct = new boost::thread(  &FuncCreate  );

        return 0;

    }

    void FuncCreate()
    {
        while (true)
        {
          compute_left_descriptors(g_nameRootFolder.c_str());
    }

    }

    void compute_left_descriptors(const char* name_dir)
    {

        char namebuf[1024];


            sprintf(namebuf, "%s/Left/%04d_left.bmp", name_dir, 1);

        // Program ended with exit code: 0 programe exit from here.
        Mat input_left = imread(namebuf, CV_LOAD_IMAGE_COLOR);

        imshow("Right View", input_left);
        waitKey(0);

        printf("done\n");
    }

1条回答
可以哭但决不认输i
2楼-- · 2020-05-06 19:57

please try this version of your code and tell us whether it works or not

boost::thread *thread_reconstruct; 

int main( int argc, const char** argv )
{

    cv::namedWindow("Right View"); // this will create a window. Sometimes new windows can't be created in another thread, so we do it here in the main function.

    thread_reconstruct = new boost::thread(  &FuncCreate  );

    thread_reconstruct->join(); // this will make your program wait here until the thread has finished processing. Otherwise your program would exit directly.

    return 0;

}

void FuncCreate()
{
    while (true)
    {
      compute_left_descriptors(g_nameRootFolder.c_str());
    }
}

void compute_left_descriptors(const char* name_dir)
{

    char namebuf[1024];


        sprintf(namebuf, "%s/Left/%04d_left.bmp", name_dir, 1);

    // Program ended with exit code: 0 programe exit from here.
    Mat input_left = imread(namebuf, CV_LOAD_IMAGE_COLOR);

    if(input_left.empty()) printf("could not load image\n");

    imshow("Right View", input_left);
    waitKey(0); // if you dont want to press a key before each new image, you can change this to waitKey(30);

    printf("done\n");
}
查看更多
登录 后发表回答