Re: Extracting pixel values from an image under a polygon (region of interest)

*To*: mathgroup at smc.vnet.net*Subject*: [mg115342] Re: Extracting pixel values from an image under a polygon (region of interest)*From*: Matthias Odisio <matthias at wolfram.com>*Date*: Sat, 8 Jan 2011 03:38:29 -0500 (EST)

Mac, On 1/7/11 3:10 AM, Mac wrote: > I've tried solving this general scientific image processing problem > for some time, alas without success. Given an image and a polygon with > coordinates describing a region of interest, I would like to extract > all the image pixels falling completely within the boundaries of the > polygon (not necessarily a square). > > As an illustrative example consider the following synthetic image + > region of interest marked in red > > img = Graphics[{Raster[RandomReal[{0, 1}, {10, 10}]], Red, > Opacity[0.5], Polygon[{{0.9, 0.9}, {4, 4.1}, {7, 4.1}, {8, 1}}]}, > Frame -> True] > > I would like to extract all the values of img falling within the > boundary of the red polygon. The real world problem I face is that I > have a radar map of the earth's surface with the pixel values > representing the radar intensity and would like to extract and analyse > all pixels falling within a specific region (say for instance > Greenland). Despite the advances in image processing in Mathematica 8 > I still do not see a solution. There is no PixelTake function or > similar.... > > Anway, any help would be much appreciated. > > Mac This is possible in version 8: Create an image: img = RandomImage[{0, 1}, {100, 100}]; Draw a polygon on top of it using the FE right click menu (Graphics Editing>Drawing Tools) Select the image, and extract the mask of the polygon using again the FE right click menu (Graphics Editing>Create mask) Make a binary image from the resulting graphics object: mask = Binarize[previousresult, 0]; Extract the corresponding pixels in the original image: Extract[ImageData[img], Position[ImageData[mask], 1]] Note that with small images (say, 10 by 10), the rasterized graphic mask may not have the same dimensions as the image. That is a bug which will be fixed in a future release of Mathematica. Matthias Odisio Wolfram Research