MATLAB - 如何从 .fig 文件裁剪图像的某些部分并将其另存为 .mat?

MATLAB - How can I crop some part of an image from .fig file and save it as .mat?

我有一个 .fig 文件。我把 32x32 正方形放在图像上我想要的地方。我写这段代码:

h = imrect(gca,[1 1 32 32]);

但是我没有裁剪图像的 32x32 部分。如何从 .fig 裁剪并另存为 .mat?

您可以使用 imrect object. If you are working with a .fig file, you can initially use the getimage 函数的 getPosition 属性 从图像句柄中获取图像。

%% If you are working with a image file.    
%Sample image.
%I = imread('cameraman.tif');

%Display image.
%imshow(I);

%% If you are working with a .fig file. 
%In the following example, yourfile.fig is cameraman.tif previously saved as a .fig file.
I = open('yourfile.fig')
I = getimage(I); 

%Draw rectangle.
h = imrect(gca,[100 30 40 32]);

%Crop rectangle.
J=imcrop(I,h.getPosition);

%Show rectangle.
imshow(J);

%Save as .mat file
save('mymatfile.mat','J');

初始图像:

添加了不正确的对象:

裁剪元素: