如何在MATLAB中使用padarray填充图像以匹配另一个图像的大小 [英] How to pad an image to match another image's size using padarray in MATLAB

查看:862
本文介绍了如何在MATLAB中使用padarray填充图像以匹配另一个图像的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 padarray 来增加 image2 的大小,即 256 x 256 大小 image1 ,大小 384 x 512 ,但我得到新的 image3 的大小 1024 x 1280 。为什么和出了什么问题?

I am trying to use padarray to increase the size of image2, which is 256 x 256 to the size of image1, which is of size 384 x 512, but I get that the new image3 is of size 1024 x 1280. Why and what is wrong?

这是我写的代码:

Image1 = rgb2gray(imread('pillsetc.png'));
Image2 = (imread('rice.png'));
[height1, width1] = size(Image1);
[height2, width2] = size(Image2);

image3 = padarray(Image2,[height1, width1]);


推荐答案

你的错误是因为你是误解 padarray 的工作原理。第二个元素指定要为每个维度沿图像边框填充的元素数。例如,执行 out = padarray(im,[2 1]); 将指定垂直2个零和水平1 0的边框。

Your error is arising from the fact that you're misunderstanding how padarray works. The second element specifies how many elements you want to pad along the border of the image for each dimension. For example, doing out = padarray(im, [2 1]); will specify a border of 2 zeroes vertically and 1 zero horizontally.

示例:

>> im = [1 2; 3 4]

im =

     1     2
     3     4

>> padarray(im, [2 1])

ans =

     0     0     0     0
     0     0     0     0
     0     1     2     0
     0     3     4     0
     0     0     0     0
     0     0     0     0

请注意,填充是对称的。因此,第一维中的 2 意味着您在图像顶部和底部看到2像素零边框。第二个维度中的 1 表示您在图像的左侧和右侧看到1像素零边框。您正在指定总宽度和高度,这是不正确的。此外,如果您有彩色图像, width1 width2 实际上会变成 width * 3 其中宽度是任一图片的原始宽度。

Take note that the padding is symmetric. So the 2 in the first dimension means that you see a 2 pixel zero border on top of the image and on the bottom. The 1 in the second dimension means that you see a 1 pixel zero border to the left and right of the image. You are specifying the total width and height instead, which isn't correct. In addition, if you have a colour image, width1 and width2 would actually become width*3 where width is the original width of either image.

如果你想要正确地执行此操作,您需要为图像的宽度和高度计算正确的填充大小,并且还需要从两个图像中获取正确的宽度和高度:

If you want to do this correctly, you'll need to calculate the correct padding size for the width and height of the image, and you'll also need to get the correct width and height from the two images:

Image1 = rgb2gray(imread('pillsetc.png'));
Image2 = (imread('rice.png'));
height1 = size(Image1,1); %// Change
width1 = size(Image1,2); %// Change
height2 = size(Image2,1); %// Change
width2 = size(Image2,2); %// Change

image3 = padarray(Image2,[(height1-height2)/2, (width1-width2)/2]); %// Change

height1-height2 width1-width2 查找高度和宽度的差异,即两个维度中所需的零总数。但是,因为填充是对称,所以你需要做的是将每个值除以2,这样差异的一半在一侧有这么多的零,其余的则在每一个的另一边放置尺寸。实际上,您将较小的图像放在中心,并在图像的中心周围填充。另外,请记住,这仅适用于两个图像之间的均匀尺寸。如果你没有这个,你可能想要使用 floor

height1-height2 and width1-width2 finds the difference in height and width, which is the total number of zeroes required in both dimensions. However, because the padding is done symmetrically, what you have to do is divide each value by 2 so half of the difference has this many zeroes on one side and the rest gets placed on the other side for each dimension. In effect, you are placing the smaller image in the centre, and are padding around the centre of the image. Also, bear in mind that this only works for even sized dimensions between both images. If you don't have this, you'll want to perhaps use floor.

这篇关于如何在MATLAB中使用padarray填充图像以匹配另一个图像的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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