Function Repository Resource:

SubimageApply

Source Notebook

Apply a function to specific subregions of an image

Contributed by: Michael Sollami

ResourceFunction["SubimageApply"][f,image,roi]

applies the function f to the pixels in subregion roi of image.

ResourceFunction["SubimageApply"][f,image,{roi1,roi2,}]

applies the function f to the pixels of image in each of the subregions roii.

ResourceFunction["SubimageApply"][{f1,f2,},image,{roi1,roi2,}]

applies the functions fi to each of the corresponding image subregions roii.

Details and Options

ResourceFunction["SubimageApply"] currently only works on 2D images.

Examples

Basic Examples (3) 

Apply Blur to a certain subregion of an image:

In[1]:=
img = ExampleData[{"TestImage", "Girl3"}];
ResourceFunction["SubimageApply"][Blur[#, 100] &, img, FindFaces[img]]
Out[1]=

Apply EdgeDetect to multiple subregions:

In[2]:=
img = ExampleData[{"AerialImage", "SanFrancisco7"}];
rois = Rectangle @@@ {{{245, 729}, {664, 791}}, {{40, 10}, {164, 766}}, {{474, 294}, {778, 428}}, {{183, 903}, {624, 999}}};
ResourceFunction["SubimageApply"][EdgeDetect[#] &, img, rois]
Out[2]=

Apply multiple functions to multiple subregions:

In[3]:=
img = ExampleData[{"TestImage", "Sailboat"}];
ResourceFunction[
 "SubimageApply"][{Blur[#, 100] &, GradientFilter[#, 1] &, ImageEffect[#, "Comics"] &, ImageEffect[#, "Solarization"] &}, img, {Rectangle[{64, 64}, {192, 192}],
  Rectangle[{320, 320}, {448, 448}], Rectangle[{64, 320}, {192, 448}],
   Rectangle[{320, 64}, {448, 192}]}]
Out[3]=

Possible Issues (1) 

When using multiple intersecting regions, the latter regions supersede previous ones:

In[4]:=
img = ExampleData[{"TestImage", "Flower"}];
ResourceFunction[
 "SubimageApply"][{(ImageAdjust@GradientFilter[#, 10]) &, Blur[#, 100] &}, img, {
Rectangle[{500, 500}, {3000, 2500}], 
Rectangle[{1800, 1500}, {3800, 3100}]}]
Out[4]=

Publisher

Michael Sollami

Version History

  • 1.0.0 – 24 February 2020

Related Resources

License Information