如何通过使用matlab添加额外像素来调整图像大小 [英] How to resize an image by adding extra pixels using matlab

查看:2563
本文介绍了如何通过使用matlab添加额外像素来调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将512X512图像调整为363X762图像,这将大于原始图像(大小为512X512)。那些额外的像素值必须是0-255范围内的不同值。
我尝试了以下代码:

I would like to resize a 512X512 image into 363X762 image which will be larger than the original image(of size 512X512). Those extra pixel values must be different values in the range of 0-255. I tried the following code:

I=imread('photo.jpg'); %photo.jpg is a 512X512 image
B=zeros(363,726);
sizeOfMatrixB=size(B);
display(sizeOfMatrixB);
B(1:262144)=I(1:262144);
imshow(B);
B(262155:263538)=0;

但我认为这是一个冗长的,输出也不是所希望的。任何人都可以建议我用更好的代码来执行此操作。提前谢谢你。

But I think this is a lengthy one and the output is also not as desired. Could anyone suggest me with a better piece of code to perform this. Thank you in advance.

推荐答案

我认为你的代码实际上非常接近理想,除非你有很多那里的硬编码值。那些应该真正计算在飞行中。我们可以使用 numel 来计算 B 中元素的数量。

I think that the code you have is actually pretty close to ideal except that you have a lot of hard-coded values in there. Those should really be computed on the fly. We can do that using numel to count the number of elements in B.

B = zeros(363, 726);

%// Assign the first 262144 elements of B to the values in I
%// all of the rest will remain as 0
B(1:numel(I)) = I;

这种灵活性很重要,实际上通过最后一行的拼写错误证明了重要性:

This flexibility is important and the importance is actually demonstrated via the typo in your last line:

B(262155:263538)=0;

%// Should be
B(262144:263538)=0;

此外,您最后不需要将这些额外的分配归零,因为您将矩阵初始化为首先是全零。

Also, you don't need these extra assignments to zero at the end because you initialize the matrix to be all zeros in the first place.

A Side Note

它看起来就像你在多列中传播每列的原始图像数据一样。我猜这不是你想要的。您可能只想抓住前<363> 行的 I 放入 B 。你可以这样做:

It looks like you are spreading the original image data for each column across multiple columns. I'm guessing this isn't what you want. You probably only want to grab the first 363 rows of I to be placed into B. You can do that this way:

B = zeros(363, 726);
B(1:size(B, 1), 1:size(I, 2)) = I(1:size(B, 1), :);

更新

如果您希望其他值为其他而不是零,则可以将矩阵初始化为该值

If you want the other values to be something other than zero, you can initialize your matrix to be that value instead.

value = 2;
B = zeros(363, 726) + value;
B(1:numel(I)) = I;

如果您希望它们是0到255之间的随机整数,请使用 randi 初始化矩阵。

If you want them to be random integers between 0 and 255, use randi to initialize the matrix.

B = randi([0 255], 363, 726);
B(1:numel(I)) = I;

这篇关于如何通过使用matlab添加额外像素来调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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