有没有一种有效的方法来填充所有边上都带零的矩阵? [英] Is there an efficient way to pad a matrix with zeros on all sides?

查看:207
本文介绍了有没有一种有效的方法来填充所有边上都带零的矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Efros和Leung算法对图像进行纹理合成。我的目标是增加当前纹理图像的大小,为此,我想在所有方面用零填充当前图像矩阵。

I'm performing texture synthesis on images using the Efros and Leung Algorithm. My goal is to grow the size of my current textured image and, to do it, I'd like to pad the current image matrix with zeros on all sides.

My当前计划给出一个大小为MxN且所需增长大小为P的原始图像矩阵:

(1)创建一个大小为(M + 2P)x(N + 2P)的目标矩阵

(2)设置目标值(i + P,j + P)=原始(i,j)

(3)运行Efros和Leung

My current plan given an original image matrix of size MxN and a desired growth size of P:
(1) Create a target matrix of size (M+2P)x(N+2P)
(2) Set the value of target(i+P,j+P) = original(i,j)
(3) Run Efros and Leung

有没有办法可以消除(1)和(2),只需对原始图像进行操作,用P零点在所有方向上填充它?

Is there a way I can eliminate (1) and (2) and just operate on the original image to pad it in all directions with P zeros?

推荐答案

如果您有权访问图像处理工具箱,则可以使用 PADARRAY

If you have access to the Image Processing Toolbox, you can use the function PADARRAY:

imgPadded = padarray(img, [p p], 0, 'both');

否则你只需使用矩阵索引:

Otherwise you can simply use matrix indexing:

sz = size(img);
imgPadded = zeros([sz(1:2)+2*p size(img,3)], class(img));
imgPadded((1:sz(1))+p, (1:sz(2))+p, :) = img;

这应适用于灰度和RGB图像。

This should work for both grayscale and RGB images.

这篇关于有没有一种有效的方法来填充所有边上都带零的矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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