imcrop can not work for a .fig file

When I use this function,
I = openfig('filename.fig');
J = imcrop(I,rect)
output:
Error using imcrop>checkCData (line 412)
Invalid input image.
Error in imcrop>parseInputs (line 256)
checkCData(a);
Error in imcrop (line 93)
[x,y,a,cm,spatial_rect,h_image,placement_cancelled] = parseInputs(varargin{:});
But when use another (below), it works.
[J,rect2] = imcrop(___)
Can another people help? thanks.

댓글 수: 4

Adam
Adam 2020년 3월 24일
편집: Adam 2020년 3월 25일
doc openfig
returns a figure object, i.e. the window containing the axes. You need actual image data. With numerous assumptions on what is actually in the figure you can get this data as e.g.
hFig = openfig( ... );
hAxes = hFig.CurrentAxes;
hImage = hAxes.Children;
I = hImage.CData;
It's a very brittle way to achieve this though. image data should be stored in image files or in .mat files that you can load the actual data in from and use that directly rather than having to fish the data out of a figure.
The above assumes you have 1 axes in the figure and that the only thing in that axes is the image which you want.
>> hFig = openfig('defocus6_-51.04.fig');
hAxes = hFig.axes.CurrentAxes;
hImage = hAxes.Children;
output
Unrecognized method, property, or field 'axes' for class 'matlab.ui.Figure'.
Walter Roberson
Walter Roberson 2020년 3월 24일
I notice that you have a matlab.ui.Figure, also known as a uifigure, which is produced by App Designer. I did not know that you could get a .fig from App Designer. Using CurrentAxes and so on is for old style figures. I do not have any uifigure around to test with.
Adam
Adam 2020년 3월 25일
Sorry, corrected that now. I thought the property was 'Axes' first, then double-checked, found it was 'CurrentAxes' and didn't realise I hadn't removed the first attempt!

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2020년 3월 24일
편집: Walter Roberson 2020년 3월 24일

0 개 추천

https://www.mathworks.com/help/images/ref/imhandles.html can be used to locate the images in fig. Note that more than one could potentially be returned in general.

댓글 수: 10

this function seems not working
h = openfig('defocus6_-51.04.fig');
I = imhandles(gca)
J = imcrop(I, [372, 177, 346, 273]);
Walter Roberson
Walter Roberson 2020년 3월 24일
편집: Walter Roberson 2020년 4월 2일
h = openfig('defocus6_-51.04.fig');
I = imhandles(h);
numimg = length(I);
if numimg == 0
error('figure does not have any images.')
end
J = cell(numimg);
for K = 1 : numimg
J{K} = imcrop(I(K), [372, 177, 346, 273]);
end
fprintf('%d images cropped into J\n', numimg);
Winston
Winston 2020년 4월 2일
편집: Winston 2020년 4월 2일
sorry, I realize
it says 'Undefined function or variable 'numing''.
Walter Roberson
Walter Roberson 2020년 4월 2일
편집: Walter Roberson 2020년 4월 2일
Sorry, I fixed the typing mistake.
Winston
Winston 2020년 4월 2일
편집: Walter Roberson 2020년 4월 2일
Thanks, but seems more errors
Error using imcrop>checkCData (line 412)
Invalid input image.
Error in imcrop>parseInputs (line 256)
checkCData(a);
Error in imcrop (line 93)
[x,y,a,cm,spatial_rect,h_image,placement_cancelled] = parseInputs(varargin{:});
codes:
h = openfig('defocus1_-76.04.fig');
I = imhandles(h);
numimg=length(I);
if numimg == 0
error('figure does not have any images.')
end
J = cell(numimg);
for K = 1 : numimg
J{K} = imcrop(I(K), [44, 166, 50, 50]);
end
fprintf('%d images cropped into J\n', numimg);
h = openfig('defocus1_-76.04.fig');
I = imhandles(h);
numimg = length(I);
if numimg == 0
error('figure does not have any images.')
end
J = cell(numimg, 1);
for K = 1 : numimg
J{K} = imcrop(I(K).CData, [44, 166, 50, 50]);
end
fprintf('%d images cropped into J\n', numimg);
This might not be exactly what you want. You are on the border between asking for an interactive cropping tool, or automatically cropping at a fixed location, but you cannot do both at the same time: you can only specify a crop rectangle when you do not use an interactive cropping tool. The code I show here guesses that you wanted to crop a particular rectangle instead of invoking the interactive cropping tool.
Winston
Winston 2020년 5월 23일
Hi Walter,
no matter either interactive or specifying a crop rectangle, will they always use imcrop() funcition? why it works well for .tif or jpeg, but not on .fig?
I also tried to export .fig to .tif but this will change the pixel number in the .fig.
I have tried your codes and it does work to give a 1*1 cell array, but this array can not give the image, when I imshow([J{:}]), do you have any idea?
.fig are not image files, they are complicated data structures. A .fig is effectively an entire figure window, not a particular image within the figure. You cannot crop an entire figure.
What shows up for
min(J{1}(:))
max(J{1}(:))
class(J{1})
size(J{1})
Winston
Winston 2020년 5월 24일
They are
ans =
7618074
ans =
9306097
ans =
'double'
ans =
51 51
J{K} = imcrop(I(K).CData, [44, 166, 50, 50]); is used, no idea about why the cropped gives 51*51
imshow(J{1} ,[])
imcrop includes the border, except when it does not include the border. I filed a bug report on it years ago.
I recommend using matrix indexing instead of imcrop when you already know where you want to crop.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품

릴리스

R2019a

태그

질문:

2020년 3월 24일

댓글:

2020년 5월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by