如何在视频文件中读取灰度

zeb*_*bra 3 c++ opencv

当您读入图像时,可以设置一个标记0以强制它为灰度.

cv::Mat img = cv::imread(file, 0); // keeps it grayscale
Run Code Online (Sandbox Code Playgroud)

视频是否有同等效力?

kar*_*lip 5

没有.

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

使用C接口:https://stackoverflow.com/a/3444370/176769

使用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;
Run Code Online (Sandbox Code Playgroud)