OpenCv RGB彩色视频转为灰度视频

int videoColorToGray(const string& videoPathColor, const string& videoPathGray)
{
    VideoCapture cap(videoPathColor, CAP_ANY);

    int fps = cap.get(CAP_PROP_FPS);
    Size videoSize = Size((int)cap.get(CAP_PROP_FRAME_WIDTH), (int)cap.get(CAP_PROP_FRAME_HEIGHT));
    VideoWriter writer(videoPathGray, VideoWriter::fourcc('M', 'J', 'P', 'G'), fps, videoSize, false);

    if (!writer.isOpened())
        std::cout << "writer.isOpened() failed" << std::endl;

    bool bContinue = true;
    Mat img, imgGray;
    int index = 0;
    while (bContinue)
    {
        bContinue = cap.read(img);
        if (bContinue)
        {
            cvtColor(img, imgGray, COLOR_RGB2GRAY);
            writer.write(imgGray);
            index++;
        }
    }

    return 0;
}

你可能感兴趣的:(opencv,音视频,人工智能,计算机视觉)