opencv中waitkey()函数返回值为255

最近在跑realsense的opencv例程时,发现一直不进while循环:

int main(int argc, char * argv[]) try
{
    // Declare depth colorizer for pretty visualization of depth data
    rs2::colorizer color_map;

    // Declare RealSense pipeline, encapsulating the actual device and sensors
    rs2::pipeline pipe;
    // Start streaming with default recommended configuration
    pipe.start();
    printf("1\n");
    using namespace cv;
    const auto window_name = "Display Image";
    namedWindow(window_name, WINDOW_AUTOSIZE);


    while (waitKey(1) < 0 && getWindowProperty(window_name, WND_PROP_AUTOSIZE) >= 0)
    {
        rs2::frameset data = pipe.wait_for_frames(); // Wait for next set of frames from the camera
        rs2::frame depth = data.get_depth_frame().apply_filter(color_map);

        // Query frame size (width and height)
        const int w = depth.as().get_width();
        const int h = depth.as().get_height();

        // Create OpenCV matrix of size (w,h) from the colorized depth data
        Mat image(Size(w, h), CV_8UC3, (void*)depth.get_data(), Mat::AUTO_STEP);

        // Update the window with new data
        imshow(window_name, image);
    }

    return EXIT_SUCCESS;
}

测试while循环里的条件:

int a = getWindowProperty(window_name, WND_PROP_AUTOSIZE);
int b = waitKey(1);
printf("%d+%d\n",a,b);

发现输出1+255,说明 waitKey(1) 返回值为255。

waitKey(int delay)函数的功能是不断刷新图像,频率为delay,表示等待毫秒数,单位是ms。返回值为按下的键盘的ASCII值,没有按键时返回-1;如果delay为0,则表示无限期的等待键盘输入。

所以此处应返回-1,却返回了255。而-1用8bits的无符号数的值就是255,所以应该是系统问题,将代码中小于0改成大于0即可:

while (waitKey(1) > 0 && getWindowProperty(window_name, WND_PROP_AUTOSIZE) >= 0)

你可能感兴趣的:(c语言,开发语言,c++,opencv,计算机视觉)