How to resize an image by adding extra pixels using matlab

a 夏天 提交于 2020-01-25 03:56:54

问题


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.


回答1:


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

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), :);

Update

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;

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;


来源:https://stackoverflow.com/questions/36484804/how-to-resize-an-image-by-adding-extra-pixels-using-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!