MATLAB: Cropping rectangular part of an image

errorimage processingimage segmentation

I'm building a project based on the example stated in this link : http://www.mathworks.com/help/vision/gs/object-detection-and-tracking.html#btt5qyu , "Detect Objects in a Cluttered Scene Using Point Feature Matching.
After detecting the needed part of the target image , a rectangular polygon is plotted around it, i need to crop the part of the target image enclosed by this polygon.
i tried the following method.
Icropped=imcrop(II,newBoxpolygon);
This snap of the code
if true
boxPolygon = [1, 1;... % top-left
size(boxImage, 2), 1;... % top-right
size(boxImage, 2), size(boxImage, 1);... % bottom-right
1, size(boxImage, 1);... % bottom-left
1, 1]; % top-left again to close the polygon
% Transform the polygon into the coordinate system of the target image. The transformed polygon indicates the location of the object in the scene.
newBoxPolygon = transformPointsForward(tform, boxPolygon);
%Display the detected object.
figure();
imshow(sceneImage);
hold on;
line(newBoxPolygon(:, 1), newBoxPolygon(:, 2), 'Color', 'R');
title('Detected Box');
Icropped=imcrop(II,newBoxPolygon);
imshow(Icropped);
end
and also i tried Icropped=imcrop(II,line); (Note:II is my target image) in both cases im getting an error i dont know whats the problem can any one tell me whats the problem with the method im using. this is the error im getting " [x,y,a,cm,spatial_rect,h_image,placement_cancelled] = parseInputs(varargin{:}); "
Thanks in return.

Best Answer

If you want to get a rectangular part of an image, all you need is:
cropped = original(top:bottom, left:right,:);
If you want another shape, then you need to use a slightly different method:
mask = logical([0 1 0;
1 1 1;
0 0 0]); % this needs thought on your part.
image = [1 2 3;
2 2 2;
3 1 0];
cropped_ish = image .* mask