How can I rescale an image while not changing the dimension of the image?
Show older comments
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 Comment
karim botros
on 3 Aug 2020
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')
Answers (4)
Image Analyst
on 21 Dec 2012
Edited: Image Analyst
on 21 Dec 2012
0 votes
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 Comments
Sonoma Rich
on 21 Dec 2012
Image Analyst
on 21 Dec 2012
Assuming you're doing your scaling with zoom(), this does not affect what you process. It only affects what is displayed. The display region on your monitor is the same number of pixels of course but calling zoom() will replicate pixels for display, but only for display. You can click and drag the image to move it around once it's zoomed. If you zoom way out, so that your image gets tiny on the screen, I don't know what it does - if it just shrinks the axes control, or if it keeps the axes the same size and just makes it black outside the tiny image. But it doesn't really matter - your image is still the same variable and all this is just for display only. It won't affect what parts of the image that you process.
Sonoma Rich
on 21 Dec 2012
Walter Roberson
on 21 Dec 2012
imresize() does output the processed image ?
Image Analyst
on 21 Dec 2012
Like Walter says, imresize() does output a processed (i.e. resized) image. You can then cast that to a different bit depth, if you want. Look:
smallImage = imread('pout.tif');
whos smallImage
% Enlarge the image to 4000 by 6000, bigger than the monitor.
bigImage = uint16(imresize(smallImage, [4000,6000]));
whos bigImage
Name Size Bytes Class Attributes
smallImage 291x240 69840 uint8
Name Size Bytes Class Attributes
bigImage 4000x6000 48000000 uint16
No zooming or displaying was done at all. Please tell us why this does not meet your needs.
Image Analyst
on 22 Dec 2012
Richard - are you still there?????
Image Analyst
on 23 Oct 2016
I don't understand. Simply use imcrop(). What's the difficulty?
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 Comments
Image Analyst
on 21 Dec 2012
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.
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
on 17 Apr 2018
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;
endend
Victor Leborán Alvarez
on 21 May 2019
Edited: Victor Leborán Alvarez
on 21 May 2019
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
Categories
Find more on Blocked Images in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!