如何动态更改cv :: Mat图片尺寸? [英] How to change cv::Mat image dimensions dynamically?

查看:906
本文介绍了如何动态更改cv :: Mat图片尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想声明一个cv :: Mat对象,并在我的代码中的其他地方更改其尺寸(rows和ncols).我在OpenCV的文档中找不到任何方法.他们总是建议在构造器中包括尺寸.

I would like to declare an cv::Mat object and somewhere else in my code, change its dimension (nrows and ncols). I couldn't find any method in the documentation of OpenCV. They always suggest to include the dimension in the constuctor.

推荐答案

一种简单明了的方法是使用create()方法.您可以根据需要多次调用它,当它的参数与现有缓冲区不匹配时,它将重新分配图像缓冲区:

An easy and clean way is to use the create() method. You can call it as many times as you want, and it will reallocate the image buffer when its parameters do not match the existing buffer:

Mat frame;

for(int i=0;i<n;i++)
{
    ...
    // if width[i], height[i] or type[i] are != to those on the i-1
    // or the frame is empty(first loop)
    // it allocates new memory
    frame.create(height[i], width[i], type[i]); 
    ... 
    // do some processing
}

可在 https://docs.opencv.org上获得文档/3.4/d3/d63/classcv_1_1Mat.html#a55ced2c8d844d683ea9a725c60037ad0

这篇关于如何动态更改cv :: Mat图片尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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