How can I rescale an image while not changing the dimension of the image?
13 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
karim botros
el 3 de Ag. de 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')
Respuestas (4)
Image Analyst
el 21 de Dic. de 2012
Editada: Image Analyst
el 21 de Dic. de 2012
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 comentarios
Jurgen
el 21 de Dic. de 2012
Editada: Jurgen
el 21 de Dic. de 2012
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 comentarios
Image Analyst
el 21 de Dic. de 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.
Jurgen
el 23 de Dic. de 2012
Editada: Jurgen
el 23 de Dic. de 2012
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
el 17 de Abr. de 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; end
end
0 comentarios
Victor Leborán Alvarez
el 21 de Mayo de 2019
Editada: Victor Leborán Alvarez
el 21 de Mayo de 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
0 comentarios
Ver también
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!