Cropping image with Bounding Box

13 次查看(过去 30 天)
I keep getting this error when I am trying to crop the image, any advice?
??? Error using ==> imageDisplayParseInputs at 173
Invalid input arguments.
Error in ==> imshow at 173
[common_args,specific_args] = ...
Error in ==> imcrop>parseInputs at 242
imshow(a,cm);
Error in ==> imcrop at 94
[x,y,a,cm,spatial_rect,h_image,placement_cancelled] = parseInputs(varargin{:});
Error in ==> TestMeasure at 69
subImage = imcrop(Q, bound);
This is my code:
Q = imread('IMG_1800.jpg');
bound = regionprops(Q, 'BoundingBox');
subImage = imcrop(Q, bound);
imshow(subImage);
Thank you!

回答(3 个)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019-8-6
编辑:KALYAN ACHARJYA 2019-8-6
Please note imcrop creates an interactive Crop Image tool with the image displayed in the current figure. See detail
Q = imread('.....');
bound=regionprops(Q,'BoundingBox');
Here regionprops measure properties of image regions, which return in structure
>> whos bound
Name Size Bytes Class Attributes
bound 255x1 40864 struct
Now you try to crop??
subImage = imcrop(Q,bound);
It would be better if you define the region of subimage as like
subImage=imcrop(Q,[R1 C1 R2 C2];
Please elaborate, if you need further help.

Neuropragmatist
Neuropragmatist 2019-8-6
What sort of image is 'IMG_1800.jpg'?
Because regionprops expects a logical or labelled image as the first input.
If your image is a logical or labelled image the output of regionprops will likely have multiple values (in struct format in your code) and the error results because imcrop doesn't know what to do with that.

Mohammad Farhad Aryan
编辑:Mohammad Farhad Aryan 2020-3-27
You can use the edited version of your code as below:
Q = imread('IMG_1800.jpg');
binaryImage = im2bw(Q);
labeledImage = bwlabel(binaryImage);
bound = regionprops(binaryImage, 'BoundingBox');
coord = bound.BoundingBox;
subimage = imcrop(Q, [coord(1), coord(2), coord(3), coord(4]); % You can x, y, height and width for coord(k)
imshow(subImage);

产品


版本

R2007b

Community Treasure Hunt

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

Start Hunting!

Translated by