Cropping an Imagesc image

17 次查看(过去 30 天)
Hi there, I am currently working on a code which creates an image using imagesc, and then wanting to crop the image, I can do this while using the interactive cropping tool however whenever I try to use the [rect] method to create the crop it comes up with the message:
Error using imcrop>checkCData (line 410)
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{:});
Error in Cropped (line 51)
Cr=imcrop(figure(1),[220 619 650 50]);
The code is
Cr=imcrop(figure(1));
figure(2);
imagesc(flipud(Cr));
set(gca,'YDir','normal');
Figure(1) is an image created from imagesc.Just wondering if anyone can help. Thanks

采纳的回答

Walter Roberson
Walter Roberson 2015-5-27
When you pass in a handle, you cannot also pass in the crop vector. I note that the error message corresponds to a line of code that includes the vector but your claimed code does not include the vector.
I notice that you imagesc() the data returned from the crop operation. Are you intending that the crop return the original data that was submitted to be scaled for display? If so then imcrop() that matrix of data. If for some reason you do not have the matrix, then
imh = findobj(figure(1), 'type', 'image');
YourData = get(imh(1),'CData');
Cr = imcrop(YourData, [220 619 650 50]);
  2 个评论
Robert Roy
Robert Roy 2015-5-28
编辑:Robert Roy 2015-5-28
Thanks it seems to have worked. Apologies but I dont understand the 'When you pass in a handle, you cannot also pass in the crop vector' If you could explain that it would be appreciated. I made a mistake and can use the imshow function so long as i set the axis.
Walter Roberson
Walter Roberson 2015-5-28
If you look at the documented forms of imcrop, one of them is
imcrop(h)
which is the form for passing in a handle. In that form, it locates the image and gives you an interactive cropping session.
There is another form shown in the documentation,
imcrop(I)
where I is an image array; when you pass in an image array in this form, it displays the image and then gives you an interactive cropping session.
There is a third form,
imcrop(I, rect)
In that form, it takes the image array you pass in and does the cropping and returns the cropped image without displaying it.
If you look at the lists of allowed forms, there is no form for
imcrop(h, rect)
That is, it is is not allowed to pass in a handle, have it find the associated image, and automatically perform the specified cropping and return the rectangle. If you pass in a handle, you are not allowed to pass in any other options (though potentially you are allowed to pass in "x" and "y" before the handle; it is an obscure and confusing possibility.)

请先登录,再进行评论。

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by