Function Repository Resource:

ImageRegionCrop

Source Notebook

Crop an image to contain only pixels included in a predefined region

Contributed by: Erik Mahieu

ResourceFunction["ImageRegionCrop"][img,reg]

crops image img to include only those pixels that are members of the region reg.

Examples

Basic Examples (2) 

Crop an image to a circular shape:

In[1]:=
img = ImageResize[ExampleData[{"TestImage", "Mandrill"}], 250];
ResourceFunction["ImageRegionCrop"][img, Disk[{0, 0.1}, .9]]
Out[2]=

Create a pentagonal image:

In[3]:=
ResourceFunction["ImageRegionCrop"][img, RegularPolygon[{0, 0.1}, {.95, (11 \[Pi])/10}, 5]]
Out[3]=

Scope (1) 

Any 2D region can be used:

In[4]:=
ResourceFunction["ImageRegionCrop"][img, RandomPolygon[{"Convex", 100}]]
Out[4]=
In[5]:=
ResourceFunction["ImageRegionCrop"][img, Annulus[{0, .1}, {.5, .9}]]
Out[5]=

Possible Issues (1) 

Regions may need to be rescaled to fit within the image:

In[6]:=
ResourceFunction["ImageRegionCrop"][img, RegularPolygon[{.5, .35}, {1.5, (11 \[Pi])/10}, 5]]
Out[6]=

Neat Examples (1) 

Use a geographical region to delimit the cropping:

In[7]:=
\[ScriptCapitalR] = CountryData["Belgium", "Polygon"] /. {x_?NumericQ, y_} :> {(x - 49.8)/1.69, (y - 4.4)/2.52};
ResourceFunction["ImageRegionCrop"][img, \[ScriptCapitalR]]
Out[7]=

Publisher

Erik Mahieu

Version History

  • 1.0.0 – 09 November 2020

Related Resources

License Information