【问题标题】:Converting Live Video Frames to Grayscale (OpenCV)将实时视频帧转换为灰度 (OpenCV)
【发布时间】:2016-09-01 04:20:13
【问题描述】:

首先,我应该说我是 OpenCV 的初学者。我正在尝试将网络摄像头中的实时视频流从 RGB 转换为灰度。

我的函数中有以下代码:

VideoCapture cap(0);

while (true)
{
    Mat frame;
    Mat grayscale;
    cvtColor(frame, grayscale, CV_RGB2GRAY);
    imshow("Debug Window", grayscale);
    if (waitKey(30) >=0)
    {
        cout << "End of Stream";
        break;
    }
}

我知道它不完整。我试图找到一种方法来获取视频的一帧并将其发送到 frame,使用 cvtColor 对其进行操作,然后将其输出回 grayscale 所以我可以在我的屏幕上显示它。

如果有人可以提供帮助,将不胜感激。

【问题讨论】:

    标签: c++ opencv


    【解决方案1】:

    请看这个例子,这里有完整的代码,希望这对你有用:

    #include "opencv2/highgui/highgui.hpp"
    #include <iostream>
    
    using namespace cv;
    using namespace std;
    
    int main(int argc, char* argv[])
    {
        VideoCapture cap(0); // open the video camera no. 0
    
        if (!cap.isOpened())  // if not success, exit program
        {
            cout << "Cannot open the video cam" << endl;
            return -1;
        }
    
    
        namedWindow("MyVideo",CV_WINDOW_AUTOSIZE);
    
        while (1)
        {
            Mat frame;
    
            bool bSuccess = cap.read(frame); // read a new frame from video
    
             if (!bSuccess)
            {
                 cout << "Cannot read a frame from video stream" << endl;
                 break;
            }
    
            Mat grayscale;
            cvtColor(frame, grayscale, CV_RGB2GRAY); 
    
            imshow("MyVideo", grayscale); 
    
            if (waitKey(30) == 27) 
           {
                cout << "esc key is pressed by user" << endl;
                break; 
           }
        }
        return 0;
    
    }
    

    【讨论】:

    • #include
    【解决方案2】:

    您刚刚初始化了变量“frame”,却忘记为其分配图像。由于变量“frame”是空的,你不会得到输出。从视频序列“cap”中抓取图像并复制到帧。这段代码将为您完成这项工作。

        Mat frame;
        bool bSuccess = cap.read(frame); // read a frame from the video
        if (!bSuccess)
        {
             cout << "Cannot read a frame from video stream" << endl;
             break;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-25
      • 1970-01-01
      • 2019-02-23
      • 1970-01-01
      • 2017-06-18
      • 2015-11-19
      • 1970-01-01
      • 2018-08-20
      相关资源
      最近更新 更多