Function Repository Resource:

ImageMosaic

Source Notebook

Generate a mosaic version of an image

Contributed by: Karen Danielyan (karen_danielyan@outlook.com)
Mikayel Egibyan (mikayele@wolfram.com)

ResourceFunction["ImageMosaic"][img,samples]

reconstructs img as a mosaic art representation using sample images samples.

Details and Options

ResourceFunction["ImageMosaic"] only works with 2D images.
ImageMosaic returns an Image object.
The following options can be specified:
"BlendingFraction".3use fraction from 0 to 1 to blend the result with img
"ProcessingFunction"Automaticpreprocessing applied to the image
RasterSizeAutomaticresize img before processing
"TileSize"12resize samples before reconstruction
By default img is not preprocessed.

Examples

Basic Examples (1) 

Create a mosaic version of an image using a random set of colored pixels:

In[1]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/e70edd91-eae8-4076-a8cd-bfe1b39b26a1"]
Out[1]=

Scope (2) 

Load an image dataset:

In[2]:=
people = DeleteMissing[
   EntityValue[
    EntityList[
     EntityClass[
      "Person", {"BirthPlace" -> Entity["Country", "Armenia"]}]], "Image"]];

Use the dataset to reconstruct a mosaic version of an image:

In[3]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/751e9716-7369-4cc9-a92a-8ec9ee257254"]
Out[3]=

Options (10) 

BlendingFraction (2) 

Load an image dataset:

In[4]:=
people = DeleteMissing[
   EntityValue[
    EntityList[
     EntityClass[
      "Person", {"BirthPlace" -> Entity["Country", "Armenia"]}]], "Image"]];

Use custom blending when reconstructing a mosaic image:

In[5]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/0bbd5dff-aab7-4af0-8185-5765bc81890e"]
Out[5]=

ProcessingFunction (3) 

Load an image dataset:

In[6]:=
lisbon = ExampleData[{"TestImageSet", "Lisbon2016"}, "ThumbnailList"];

By default no image processing operations are performed to preprocess the input image:

In[7]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/b5b17a94-0dc6-4633-b4c2-5199a4b79165"]
Out[7]=

Use of a custom preprocessing function affects the output quality:

In[8]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/acd26f74-3010-4358-a8e8-65d6925c86c1"]
Out[8]=

RasterSize (2) 

By default, the input image dimensions are preserved (though the resulting size may not be exactly the same, as discussed in "Possible Issues" below):

In[9]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/59fb38f3-9811-4352-845e-36aca4dc46c9"]
Out[9]=
In[10]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/5ecfbf68-b9b5-4e38-aaf8-4718f2a776eb"]
Out[10]=

Resize the image:

In[11]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/4766d5e1-f6ca-4258-9494-59eb83853d31"]
Out[11]=

TileSize (3) 

Load an image dataset:

In[12]:=
people = DeleteMissing[
   EntityValue[
    EntityList[
     EntityClass[
      "Person", {"BirthPlace" -> Entity["Country", "Armenia"]}]], "Image"]];

Use the dataset to reconstruct the mosaic version of an image:

In[13]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/ea07f38d-5f2a-4db5-8d83-64e10becae5c"]
Out[13]=

Specifying a custom tile size may result in a more faithful rendition of the input image:

In[14]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/b0323809-c6f5-462e-b8b4-f020c6a5ee7e"]
Out[14]=

Possible Issues (1) 

The output image size might not exactly match the input image size:

In[15]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/9a3d6785-cee5-474e-a1b9-34de5a565b89"]
Out[15]=
In[16]:=
ResourceFunction["ImageMosaic"][i, Table[ConstantImage[RandomColor[], {12, 12}], 50]] // ImageDimensions
Out[16]=

Neat Examples (5) 

Search for images online:

In[17]:=
vanGoghImages = WebImageSearch["van gogh", 100];

Select a random sample:

In[18]:=
random = First[RandomSample[vanGoghImages, 1]];
Show[random, ImageSize -> 300]
Out[18]=

Try to reconstruct the selected image using the full set of the images:

In[19]:=
ResourceFunction["ImageMosaic"][random, vanGoghImages, "BlendingFraction" -> 0]
Out[19]=

Add more images to the dataset in order to cover a bigger color range:

In[20]:=
animals = WebImageSearch["animal", 100];
In[21]:=
dataset = Join[vanGoghImages, animals];
Length[dataset]
Out[21]=

Create a mosaic version with no blending:

In[22]:=
ResourceFunction["ImageMosaic"][random, dataset, "BlendingFraction" -> 0, RasterSize -> 1920, "TileSize" -> 8]
Out[22]=

Publisher

Karen Danielyan

Version History

  • 1.0.0 – 14 December 2020

Related Resources

License Information