MATLAB: Cropping an Imagesc image

image croppingimage processingimagesc

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

Best Answer

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]);