Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将视频文件读取为灰度_C++_Opencv - Fatal编程技术网

C++ 如何将视频文件读取为灰度

C++ 如何将视频文件读取为灰度,c++,opencv,C++,Opencv,在读取图像时,有一个标志可以设置为0,以强制将其设置为灰度 cv::Mat img = cv::imread(file, 0); // keeps it grayscale 视频有等效的吗?没有。 您需要查询帧并自己将其转换为灰度 使用C接口: < C++接口> VideoCapture cap(0); if (!cap.isOpened()) { // print error msg return -1; } namedWindow("gray",1); Mat fra

在读取图像时,有一个标志可以设置为
0
,以强制将其设置为灰度

cv::Mat img = cv::imread(file, 0); // keeps it grayscale

视频有等效的吗?

没有。

您需要查询帧并自己将其转换为灰度

使用C接口:

< C++接口>

VideoCapture cap(0);
if (!cap.isOpened())
{
    // print error msg
    return -1;
}

namedWindow("gray",1);

Mat frame;
Mat gray;
for(;;)
{
    cap >> frame;

    cvtColor(frame, gray, CV_BGR2GRAY);

    imshow("gray", gray);
    if(waitKey(30) >= 0) 
        break;
}

return 0;

确保将
灰色
声明在
for
范围之外,连同
frame
一起,将防止在每一帧对该大型映像进行昂贵的内存分配和释放。修正这一点值得投票表决。