将非标准分辨率的帧写入OpenCV中的视频 [英] Write frames with non-standard resolution to video in opencv

查看:87
本文介绍了将非标准分辨率的帧写入OpenCV中的视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于此问题:在调整图像大小后,OpenCV VideWriter无法正常工作

是否可以使用opencv cv2.VideoWriter创建具有非标准"视频分辨率(即非标准纵横比)的视频?到目前为止,我的代码:

Is it possible to create videos with opencv's cv2.VideoWriter that have "non-standard" video resolutions, i.e., non-standard aspect ratios? My code so far:

fourcc = cv2.VideoWriter_fourcc(*'XVID')
video_out = cv2.VideoWriter("video_out.avi", fourcc, 25, (99, 173))

cap = cv2.VideoCapture("video_in.avi")

while(cap.isOpened()):
    ret, frame = cap.read()
    frame_out = frame[50:50+173,400:400+99]      
    video_out.write(frame_out) 
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

我也尝试了其他视频格式(H264,MJPG),但没有成功.

I have tried other video formats (H264, MJPG) as well, no success.

没有成功,表示创建了输出视频,但仍为空.如果我使用原始帧大小,则会将帧写入视频.

No success meaning that the output video gets created, but remains empty. If I use the original frame size the frames do get written to video.

Micka的答案有效,但我现在也可以运行我的python代码:彩色视频输出的布尔参数丢失了.

Micka's answer works, but I got my python code to run now as well: the boolean argument for coloured video output was missing.

   video_out = cv2.VideoWriter("video_out.avi", fourcc, 25, (99, 173), False)

推荐答案

对我来说,此代码确实有效,但是MJPG会将奇数分辨率四舍五入为偶数分辨率. H264根本无法使用该分辨率.

For me, this code does work, but MJPG does round the odd resolution to an even resolution. H264 did not work with that resolution at all.

int main(int argc, char* argv[])
{
    // start camera
    cv::VideoCapture cap(0);

    // read a single image to find camera resolution
    cv::Mat image;
    cap >> image;
    if (image.empty())
    {
        std::cout << "Could not find/open camera. Press Enter to exit." << std::endl;
        std::cin.get();
        return 0;
    }

    cv::Size targetSize(199, 171);
    cv::VideoWriter writer("out.avi", CV_FOURCC('M','J','P','G'), 25, targetSize, true); // does create a 198x170 video file.
    //cv::VideoWriter writer("out.avi", -1, 25, targetSize, true); // does not work for x264vfw for example with an error message.


    while (cv::waitKey(30) != 'q')
    {
        cap >> image;
        if (!image.empty())
        {
            cv::imshow("captured image", image);

            // resize the actual image to a target size
            cv::Mat writableImage;
            cv::resize(image, writableImage, targetSize);

            writer.write(writableImage);
        }
    }


    // release the camera
    cap.release();
    writer.release();

    std::cout << "Press Enter to exit." << std::endl;
    std::cin.get();

    return 0;
}

通常,许多编解码器仅限于某些像素块约束,例如在每个维度上具有2、4、8、16或32的倍数.要么是因为算法本身,要么是由于硬件指令的优化.

In general, many codecs are restricted to some pixel-block-constraints like having a multiple of 2, 4, 8, 16 or 32 in each dimension. Either because of the algorithm itself or some hardware instruction optimizations.

这篇关于将非标准分辨率的帧写入OpenCV中的视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆