Cropping images around the center of an image with a particular size

26 次查看(过去 30 天)
Hi,
I have an image named FP00000s323d04u_01.png. The dimensions of the image is 512*512. I want to crop the image automatically around the center of the image while the dimensions of the final cropped image should be 224*224. The new.png is a cropping that I tried to perform. But the dimension of the image is 1920*936. I want to keep the dimensions to 224*224. Please find my code below. Any suggestions would be appreciated.
I=imread('FP00000s323d04u_01.png');
x_cent = 256;
y_cent = 256;
size_of_cropped_img = 224;
centroide = [x_cent y_cent];
imshow(I);
%I2 = imcrop(I,rect) crops the image I. rect is a four-element position vector of the
%form [xmin ymin width height] that specifies the size and position of the crop rectangle.
%imcrop returns the cropped image, I2.
xmin = x_cent-size_of_cropped_img/2;
ymin = y_cent-size_of_cropped_img/2;
I2 = imcrop(I,[xmin ymin size_of_cropped_img size_of_cropped_img]);
figure();
imshow(I2)

采纳的回答

DGM
DGM 2021-10-14
编辑:DGM 2021-10-14
I don't know how you're getting that huge size. EDIT: I know why. I'll mention that at the end.
The code will return a 225x225 image if fed a 512x512 image. That's all down to how imcrop works. It'll often give you an image that's 1px wider than you might expect. The synopsis describes why it behaves that way. You could do something like this
I = imresize(imread('cameraman.tif'),[512 512]);
x_cent = 256;
y_cent = 256;
size_of_cropped_img = 224;
centroide = [x_cent y_cent]; % this isn't used
%I2 = imcrop(I,rect) crops the image I. rect is a four-element position vector of the
%form [xmin ymin width height] that specifies the size and position of the crop rectangle.
%imcrop returns the cropped image, I2.
xmin = x_cent-size_of_cropped_img/2;
ymin = y_cent-size_of_cropped_img/2;
I2 = imcrop(I,[xmin+1 ymin+1 size_of_cropped_img-1 size_of_cropped_img-1]);
size(I2)
ans = 1×2
224 224
Or you could just not use imcrop.
I = imresize(imread('cameraman.tif'),[512 512]);
sz = size(I);
sout = [224 224]; % [y x]
centroide = sz(1:2)/2; % this isn't used
pad = (sz(1:2) - sout)/2;
yrange = max(floor(pad(1))+1,1):min(floor(pad(1))+sout(1),sz(1));
xrange = max(floor(pad(2))+1,1):min(floor(pad(2))+sout(2),sz(2));
I2 = I(yrange,xrange);
size(I2)
ans = 1×2
224 224
The reason you're getting a giant useless image is because you're saving the figure, not the image. Figures are for display only. Saving an image by saving the figure to a raster format is equivalent to taking a screenshot. It's typically destructive, as the image is scaled for display using nearest-neighbor interpolation. So quality is degraded and there's no trivial control over output size (and there's usually a bunch of padding).
If you want to save the image, use imwrite.
imwrite(I2,'myfancyimage.png')
  3 个评论
DGM
DGM 2021-10-14
编辑:DGM 2021-10-14
You might want to double-check that. If you used the first example with imcrop(), there was something I neglected to fix. The first two terms of the RECT parameter need to be offset by one in order for the image to be correctly on-center. I've edited the answer to include the offset.

请先登录,再进行评论。

更多回答(1 个)

David Hill
David Hill 2021-10-14
I=imread('FP00000s323d04u_01.png');
i=I(144:367,144:367,:);
imshow(i);
  1 个评论
DGM
DGM 2021-10-14
编辑:DGM 2021-10-14
You're northwest by one pixel
Consider that if
padwidth = (512-224)/2
padwidth = 144
then the leading border occupies pixels in the range
[1 padwidth]
ans = 1×2
1 144
and the trailing border is
[512-padwidth+1 512]
ans = 1×2
369 512
so the image should occupy
[padwidth+1 padwidth+224]
ans = 1×2
145 368

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by