Data Science

Machine learning & data science for beginners and experts alike.
BenMoss
ACE Emeritus
ACE Emeritus

Legolytics Part 1: Extracting Colours From an Image With Alteryx and R

Legolytics Part 2: Assigning the Lego Colour Palette (you are here)

Legolytics Part 3: All Possible Bricks

Legolytics Part 4: Optimizing Cost

Legolytics Part 5: The Product

The Problem

 

Now that we have our 48x48 pixel image of Phil’s face, and the associated colours, we have a problem. Phil’s face is, colour-wise, friggin’ complex. There are 1704 unique colours in this image; there’s only 2304 pixels!

 

BenMoss_0-1587063442883.png

 

Now, I’m not sure this will surprise you, but Lego does not stock their bricks in every possible colour. So, we’ll need to form a process which reduces the complexity of the colours used in the image before we can assign them to a colour which Lego does stock.

 

Clustering Colours

 

One method that we can use to group colours that are similar in Alteryx is via the K-Centroids Clustering tools, which form part of the ‘Predictive Grouping’ tool category within Alteryx.

 

Traditionally, when using the K-Means clustering tools, you will first want to use the ‘K-Centroids Diagnostics’ tool to determine the optimal number of groups in your data. However, in this case, I know that I want to assign five colours to our image, which are available in the Lego Store, and used by the original Mosaic Maker algorithm. Therefore, we won’t need to use the ‘K-Centroids Diagnostics’ tools and we can jump straight to creating and appending our cluster groups.

 

If you’ve never looked at how the clustering tools work in Alteryx, I would advise looking at the sample workflow that is available for this type of problem, which can be found by going to ‘Help’ > ‘Sample Workflows’ > ‘Predictive Tool Samples’ > ‘Predictive Analytics’ > ‘K-Centroids Cluster Analysis Sample’.

 

This blog by my colleague Ellie Mason also gives a great walkthrough of these tools.

 

Once we have successfully assigned our clusters against each Pixel, we can then attain the average RGB colour for each cluster group, using the Summarize tool.

 

BenMoss_1-1587063442888.png

 

This process of clustering and retrieving the average colour for each group gives us an image that looks something like the below; again, it’s a positive sign that we have retained Phil’s likeness at this stage.

 

BenMoss_2-1587063442889.png

 

Assigning Colours

 

Now that we have our five colour groups, we need to move through the process of assigning our Lego colours to the image, which are as follows:

 

  • #27251f – Black
  • #5b6770 – Dark Stone Grey
  • #a2aaad – Medium Stone Grey
  • #ffffff – White
  • #ffcd00 – Bright Yellow

 

The purpose of the ‘Bright Yellow’ colour is to act as the background colour for our image. In this case, I have used an arbitrary method to acknowledge the colour in our clustered image which should act as the background, this is as follows:

The background colour shall be assigned by finding the clustered colour with the most pixels alongside the edges of our images.

Now, whilst this method is relatively ‘loose’, it’s yet to fail me, so I’m happy with it for now, but if you have any ideas on how this can be improved, please let me know!

 

BenMoss_3-1587063442890.png

 

The next step is the process of assigning the rest of the colours, which can be arranged on a sequential colour palette from black to white, or from dark to light; therefore, it made sense to sort the remaining cluster colours in this order, and then assign them accordingly. This meant I had to create the ‘Lightness’ value from our RGB columns.

 

Lightness is a fairly simple value to create (apparently), the formula is as follows:

 

BenMoss_4-1587063442890.png

 

Which in Alteryx translates to:

 

 

(max([red],[blue],[green])+min([red],[green],[blue]))/2

 

 

Which in human, translates too:

 

Add the largest value out of our Red, Blue, and Green values, add that to the smallest value out of our Red, Blue, and Green values, and divide the sum by 2.

This equation gives us a figure on a scale of 0-1, where 0 is white, and 1 is black. These values, and the representative colour on the lightness scale, is shown below.

 

BenMoss_5-1587063442891.png

 

Now that we have this information for each of our cluster colours, we can simply rank them in order, before assigning them to the similarly ranked colour when performing the same process with our Lego colours (i.e. black being assigned to the darkest colour in our image, and white being assigned to the lightest colour in our image).

 

BenMoss_6-1587063442892.png

 

Next Steps

 

Though we now have our final image, which can be created by buying the appropriate number of 1x1 bricks from the Lego store, there is some optimisation that can be done to reduce the cost of our product.

 

For example, there are clearly areas in the background of this image (the yellow part), where we could use larger brick sizes, which come with a reduced ‘cost per square’, when comparing the price should we try to fill the same area with 1x1 pieces.

 

In Alteryx we can achieve this by acknowledging what parts we can actually fit into our image (making use of the core Alteryx tools), and then use the ‘Optimization’ tool to make sure that are cost is, well, optimized!

 

See you soon in part 3!