I have a jpg image I want to crop by an arbitrary polygon. I'm trying to do it by imcrop: imcrop(X, map, rect), but it only allows to specify a rectangular area in rect and I have more complex polygons... In detail, I want to extract some features from an ortho-image by the polygons of the relative shapefile, here my code:

Just get the x coordinates of your polygon, and find the min and max
with the min() and max() functions. Do the same for your y vertices
coordinates. Then you have your cropping rectangle
[xMin yMin abs(xMax-xMin+1) abs(yMax-yMin+1)]

I agree I can crop it that way, but it doesn't solve my problem as I want to extract the exact area of a general-shaped polygon (which is not a rectangle). This would be the starting point, extracting a submatrix of the bounding box, but then I have to go one more step to keep the matrix elements within my shape and "remove" the rest.. roipoly could be good, but requires knowledge of matrix indices to contruct the shape..I have x and y coordinates instead (obviously in consistent geo-reference system)..

"Clint_E " <luca_bergamasco@hotmail.com> wrote in message <ibbkm0$l5c$1@fred.mathworks.com>...
> ImageAnalyst, thanks for your reply!
>
> I agree I can crop it that way, but it doesn't solve my problem as I want to extract the exact area of a general-shaped polygon (which is not a rectangle). This would be the starting point, extracting a submatrix of the bounding box, but then I have to go one more step to keep the matrix elements within my shape and "remove" the rest.. roipoly could be good, but requires knowledge of matrix indices to contruct the shape..I have x and y coordinates instead (obviously in consistent geo-reference system)..

Clint_E :
You could use polyarea if you just wanted the area and you had the
vertex coordinates. If you want other stuff, like a masked image,
etc. take a look at this demo:

% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;

hi,
i want to say thanks for your question and the solution. because i'm trying to do this veryy very long time and i can't do it. then tonight i find your message, i apply it to my code and it works. Thank you...
i apologize you for my english.