How can I rescale an image while not changing the dimension of the image?

14 ビュー (過去 30 日間)
Sonoma Rich
Sonoma Rich 2012 年 12 月 21 日
コメント済み: karim botros 2020 年 8 月 3 日
How can I rescale an image while not changing the dimension of the image? When I scale it up, it just crops the image to fit within the original size. When I scale it down, it pads the edges of the image with zeros. I want to zoom in and out about the center of the image. When using "imresize", it changes the dimensions of the image to preserve all the content. I have the image processing toolbox.
  1 件のコメント
karim botros
karim botros 2020 年 8 月 3 日
I guess you have two image with similar background one of them the object/scene looks smaller than the other,
The problem is more of a logical computer vision problem not a matlab one. but matlab is always there to helps us.
solution:
Crop the scene/object in both images to be able to get similar sizes before resizeing, then resize one of the images to the size of the other image.
important: this code is not optimized, the main aim is to explain the idea, it can be written in a more efficient way and less lines, nevertheless it won't be clear if i did so.
try to run cropping of image one at a time, so you can handle the cropping,
to confirm the cropping right click then crop image or double click.
Matlab code:
im0 = imread('yourimage0.png');
im1 = imread('yourimage1.png');
%%
figure;
imshow(im0)
cropped0 = imcrop(im0);
%%
figure(1);
imshow(im1)
cropped1 = imcrop(im1);
im0f = imresize(cropped0,[size(cropped1,1) size(cropped1,2)]);
im1f = cropped1;
imwrite(im0f,'image0r.png')
imwrite(im1f,'image1r.png')

サインインしてコメントする。

回答 (4 件)

Image Analyst
Image Analyst 2012 年 12 月 21 日
編集済み: Image Analyst 2012 年 12 月 21 日
I'm not sure what you're talking about. How are you performing the scaling? Are you using the zoom() function? Are you using 'InitialMagnification' property of imshow()?
  7 件のコメント
Image Analyst
Image Analyst 2012 年 12 月 22 日
Richard - are you still there?????
Image Analyst
Image Analyst 2016 年 10 月 23 日
I don't understand. Simply use imcrop(). What's the difficulty?

サインインしてコメントする。


Jurgen
Jurgen 2012 年 12 月 21 日
編集済み: Jurgen 2012 年 12 月 21 日
I think you want to look into imtransform, specifically the Xdata and Ydata properties. E.g. you can rotate around the center--keeping original size. Or rotate around the left corner (image moves out of view if you keep original dataspace). Etcetera for other 'focus' points and transform types.
  2 件のコメント
Image Analyst
Image Analyst 2012 年 12 月 21 日
imrotate() can also crop or enlarge the canvass when you rotate. But he didn't say anything about rotation, just scaling. I'm still trying to figure out what he wants though.
Jurgen
Jurgen 2012 年 12 月 23 日
編集済み: Jurgen 2012 年 12 月 23 日
e.g. means for example, or exempli gratia according to wiki--since we all know what is meant by rotate. Based on his wording, richard may not differentiate properly between scaling and cropping, so I chose rotate as example; imtransform can resize and crop too.
I think what he means is what some photo-viewing applications do when you scroll: the display window size remains constant and the image is scaled and cropped(or padded) around the center to the windowsize.
Should also be possible with imresize, but after the resize the user would need to manually crop or padd, e.g. using padarray or imcrop.

サインインしてコメントする。


dzsuz87
dzsuz87 2018 年 4 月 17 日

I think this guy wanted something very similar to my actual problem.

Now I needed a function which can zoom in/out of an image, and keeps the original size of the matrix. The second requirement was that the center pixel should remain at the same position. This is what I use now:

function OutPicture = HardZoom(InPicture, ZoomFactor)
      if ZoomFactor == 0
          OutPicture = [];
          return;
      end
      if ZoomFactor < 0
          InPicture = rot90(InPicture, 2);
          ZoomFactor = 0 - ZoomFactor;
      end
      if ZoomFactor == 1
          OutPicture = InPicture;
          return;
      end
      xSize = size(InPicture, 1);
      ySize = size(InPicture, 2);
      xCrop = xSize / 2 * abs(ZoomFactor - 1);
      yCrop = ySize / 2 * abs(ZoomFactor - 1);
      zoomPicture = imresize(InPicture, ZoomFactor);
      if ZoomFactor > 1
          OutPicture = zoomPicture( 1+xCrop:end-xCrop, 1+yCrop:end-yCrop );
      else
          OutPicture = zeros(xSize, ySize);
          OutPicture( 1+xCrop:end-xCrop, 1+yCrop:end-yCrop ) = zoomPicture;
      end
end

Victor Leborán Alvarez
Victor Leborán Alvarez 2019 年 5 月 21 日
編集済み: Victor Leborán Alvarez 2019 年 5 月 21 日
Corrected version that avoids the problem of the even/odd dimensions, with the previous solution the code fails when the dimensions of x or y are even, due to the division by 2. It also works with RGB images of MxNx3. Also I've renamed x and y, because in matlab the first dimension is the height of the image so I've used
ySize = vertical height.
xSize= horiz width.
% Función que permite hacer zoom del contenido pero sin variar las dimensiones de la matriz
function OutPicture = HardZoom(InPicture, ZoomFactor)
% Si el factor de escala es 1, no se hace nada
if ZoomFactor == 1
OutPicture = InPicture;
return;
end
% Se obtienen las dimensiones de las imágenes
ySize = size(InPicture, 1);
xSize = size(InPicture, 2);
zSize = size(InPicture, 3);
yCrop = floor(ySize / 2 * abs(ZoomFactor - 1));
xCrop = floor(xSize / 2 * abs(ZoomFactor - 1));
% Si el factor de escala es 0 se devuelve una imagen en negro
if ZoomFactor == 0
OutPicture = uint8(zeros(ySize, xSize, zSize));
return;
end
% Se reescala y se reposiciona en en centro
zoomPicture = imresize(InPicture, ZoomFactor);
ySizeZ = size(zoomPicture, 1);
xSizeZ = size(zoomPicture, 2);
if ZoomFactor > 1
OutPicture = zoomPicture( 1+yCrop:yCrop+ySize, 1+xCrop:xCrop+xSize, :);
else
OutPicture = uint8(zeros(ySize, xSize, zSize));
OutPicture( 1+yCrop:yCrop+ySizeZ, 1+xCrop:xCrop+xSizeZ, :) = zoomPicture;
end
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by