16 min read

Cluster analysis and image size reduction

Idea

This post is a remake of this casestudy: https://fallstudien.netlify.com/fallstudie_bildanalyse/bildanalyse

brought to you by Karsten Lübke.

The main purpose is to replace the base R command that Karsten used with a more tidyverse-friendly style. I think that’s easier (for me).

We will compute a cluster analysis to find the typical RGB color per cluster.

WARNING

There’s still a bug in the code. That’s why the image at the end appear blurred. I suspect that rows and columns need to be transposed.

Load packages

library(tidyverse)

Setup

library(jpeg)
library(scales)
library(mosaic)
library(tidyverse)
library(knitr)

Get iris photo

Download a photo of a iris, such as this one: https://commons.wikimedia.org/wiki/File:Blood_iris.jpg

iris_url <- "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Blood_iris.jpg/320px-Blood_iris.jpg"
iris_path <- paste0(here::here(),"/static/img/iris.jpg")
img <- download.file(url = iris_url, destfile = iris_path)

Read the image:

img <- readJPEG(iris_path)
knitr::include_graphics("/img/iris.jpg")

What are the dimensions?


dimension <- dim(img)  # 1: rows, 2: cols, 3: layers
dimension
#> [1] 213 320   3

Note that the first dimension indicate rows, second the columns, and third the RGB value.

Note that we have a 3D data cube.

Reshape to data frame (2D)

In order to work with the data, better transform to a 2D variant.

That’s how it should like like after pivoting to the long form:

Note that we have a 3D data cube.

img_df <-map_df(1:3, ~ bind_rows(data.frame(img[,, .])))
dim(img_df)
#> [1] 639 320

Now the rows is 3 times the original numbers of rows, as we have now a long-format data frame.

We need to note that the first 213 rows are the “r” color part, the next the “g” part, and the last ones the “b” part:

img_df <- img_df %>% 
  mutate(color_part = rep(c("r", "g", "b"), 
                          each = dimension[1]),  # nr of rows
         y = rep(dimension[1]:1, times = dimension[3]))  # nr of rgb parts
dim(img_df)
#> [1] 639 322

Move the interesting columns to the front:

img_df <- img_df %>% select(color_part, y, everything())

img_df %>% head() %>% 
  kable()
color_partyX1X2X3X4X5X6X7X8X9X10X11X12X13X14X15X16X17X18X19X20X21X22X23X24X25X26X27X28X29X30X31X32X33X34X35X36X37X38X39X40X41X42X43X44X45X46X47X48X49X50X51X52X53X54X55X56X57X58X59X60X61X62X63X64X65X66X67X68X69X70X71X72X73X74X75X76X77X78X79X80X81X82X83X84X85X86X87X88X89X90X91X92X93X94X95X96X97X98X99X100X101X102X103X104X105X106X107X108X109X110X111X112X113X114X115X116X117X118X119X120X121X122X123X124X125X126X127X128X129X130X131X132X133X134X135X136X137X138X139X140X141X142X143X144X145X146X147X148X149X150X151X152X153X154X155X156X157X158X159X160X161X162X163X164X165X166X167X168X169X170X171X172X173X174X175X176X177X178X179X180X181X182X183X184X185X186X187X188X189X190X191X192X193X194X195X196X197X198X199X200X201X202X203X204X205X206X207X208X209X210X211X212X213X214X215X216X217X218X219X220X221X222X223X224X225X226X227X228X229X230X231X232X233X234X235X236X237X238X239X240X241X242X243X244X245X246X247X248X249X250X251X252X253X254X255X256X257X258X259X260X261X262X263X264X265X266X267X268X269X270X271X272X273X274X275X276X277X278X279X280X281X282X283X284X285X286X287X288X289X290X291X292X293X294X295X296X297X298X299X300X301X302X303X304X305X306X307X308X309X310X311X312X313X314X315X316X317X318X319X320
r2130.31764710.32156860.32549020.32549020.32156860.32156860.32549020.32941180.34117650.34509800.35294120.36078430.36470590.36862750.37647060.38431370.38039220.38431370.38823530.39215690.39607840.39607840.39215690.38039220.38823530.38823530.38431370.37647060.37254900.36862750.36862750.36470590.36862750.36862750.36862750.36078430.36078430.36078430.36078430.36078430.35686270.35294120.35686270.35294120.35294120.35294120.35686270.36078430.38039220.39607840.40392160.40392160.40784310.41568630.41960780.41568630.42352940.41960780.41176470.41176470.41568630.42352940.42352940.42352940.41176470.40784310.40392160.40392160.41568630.42352940.43137250.43137250.43529410.43137250.42745100.42745100.42352940.41568630.40784310.41176470.41176470.41176470.41176470.41176470.41568630.41960780.42745100.43137250.43137250.43137250.43137250.43137250.44313730.44705880.45098040.45098040.45098040.45490200.45490200.45882350.45882350.46666670.47450980.48235290.48627450.49411760.50588240.51764710.52156860.52549020.52156860.52156860.52941180.53333330.53333330.53333330.52941180.52549020.52156860.51764710.52156860.52156860.52156860.52156860.52156860.52156860.52156860.52156860.49803920.49411760.49019610.48627450.48235290.48235290.48235290.48235290.47843140.47843140.47843140.49019610.49019610.49019610.49411760.49411760.49019610.49803920.50196080.50588240.50588240.50196080.50196080.49803920.50588240.50980390.50588240.50588240.50196080.49803920.49803920.49803920.49411760.49411760.49411760.49411760.49411760.49411760.49803920.49803920.49019610.49019610.49019610.49411760.49411760.49411760.49411760.49411760.49803920.50588240.50980390.50980390.50980390.51372550.52156860.52549020.52549020.53725490.54509800.54509800.54509800.54117650.54509800.54509800.55294120.54901960.54509800.54901960.54901960.54901960.55294120.55686270.55294120.56078430.56078430.56078430.55686270.55686270.55294120.55294120.54509800.54509800.54117650.53725490.53725490.53333330.52941180.53333330.52941180.52549020.52549020.52156860.52156860.52156860.52156860.52156860.52549020.51764710.51372550.50588240.49803920.48627450.48627450.48235290.47058820.47450980.47450980.47843140.48627450.49019610.49019610.49411760.48627450.49019610.49019610.49411760.49411760.50196080.50588240.50588240.50588240.50588240.50980390.51764710.52156860.52156860.52549020.52941180.52156860.52156860.52156860.52156860.52156860.52549020.52549020.52549020.52941180.52941180.52941180.53725490.53725490.53333330.53333330.52941180.53333330.53333330.52941180.52549020.52156860.51764710.51372550.51372550.50588240.50588240.50588240.50588240.50588240.50588240.50588240.50588240.50980390.50980390.50588240.50196080.50196080.50196080.50196080.50980390.51764710.51764710.52156860.52156860.52549020.52549020.52549020.52549020.52941180.52549020.52156860.51372550.50980390.50588240.50196080.50196080.49411760.49803920.49803920.49803920.49411760.49411760.49019610.4901961
r2120.31764710.32156860.32156860.32156860.32156860.32156860.32941180.33333330.34509800.34901960.35686270.36470590.36862750.37254900.38039220.38823530.38431370.38431370.38823530.39215690.39607840.39607840.39607840.38431370.39215690.39215690.38823530.38039220.37647060.37254900.36862750.36862750.38039220.38039220.36862750.36862750.36862750.36862750.36862750.36862750.36470590.36078430.36470590.36078430.36078430.36078430.36470590.37254900.39215690.40392160.41568630.41568630.41960780.42745100.43137250.43137250.42352940.41960780.41960780.41960780.42352940.42745100.42745100.42352940.41176470.40784310.40784310.40784310.41960780.42745100.43137250.43137250.43921570.43529410.43137250.43529410.42745100.41960780.41176470.41568630.41568630.41568630.41568630.41960780.41960780.42745100.43529410.43529410.43921570.43921570.43921570.44313730.45490200.45882350.46274510.46274510.45490200.45882350.46274510.46666670.47058820.47450980.48235290.49019610.49803920.50196080.51372550.52156860.52941180.52941180.52941180.52549020.54117650.54117650.54509800.54509800.54117650.53725490.52941180.52941180.52549020.52549020.52156860.52156860.52156860.52156860.52156860.51764710.49803920.49411760.49019610.48627450.48235290.48627450.48627450.48627450.48235290.48235290.48235290.49411760.49411760.49803920.49803920.49803920.49803920.49803920.50588240.50980390.50980390.50980390.50980390.50588240.50980390.51372550.51372550.50980390.50588240.50588240.50196080.50196080.49803920.49803920.49803920.50196080.50196080.50196080.50196080.50196080.49411760.49411760.49803920.49803920.50196080.50196080.50196080.50588240.50588240.50980390.51764710.51764710.51764710.52156860.52549020.53333330.52941180.54509800.54901960.54901960.54901960.54901960.54901960.55294120.55686270.55294120.55294120.55294120.55294120.55686270.55686270.56078430.55686270.55686270.56862750.56470590.56470590.56078430.56078430.55686270.56078430.56078430.56078430.55686270.55294120.54901960.54901960.54901960.53725490.53333330.53333330.52941180.52941180.52549020.52941180.52549020.52941180.52156860.51764710.50980390.50588240.49411760.49019610.48627450.47843140.47843140.47843140.48235290.48627450.49019610.49019610.49411760.48627450.48627450.49019610.49411760.49411760.50196080.50588240.50588240.50588240.50588240.50588240.51764710.51764710.52156860.52549020.52549020.52549020.52549020.52549020.52941180.52941180.53333330.53333330.53333330.53725490.53725490.53725490.54509800.54509800.54117650.53725490.53725490.54117650.53725490.53725490.53333330.52941180.52549020.52156860.52156860.51764710.51764710.51764710.51764710.51764710.51764710.51764710.51764710.52549020.52156860.52156860.51764710.51764710.51764710.51764710.52549020.52549020.52941180.52941180.52941180.52941180.52941180.53333330.53333330.52941180.52549020.52156860.51764710.50980390.50588240.50588240.50588240.50196080.50196080.50196080.50196080.50196080.50196080.49803920.4980392
r2110.31372550.31372550.31372550.31372550.31372550.31764710.32549020.33333330.34117650.34901960.35686270.36078430.36470590.36862750.37647060.38431370.38823530.38823530.39215690.39215690.39607840.40000000.39215690.39215690.40000000.39607840.39215690.38823530.38039220.38039220.37647060.37254900.37647060.37647060.37647060.37647060.37647060.37254900.37254900.37254900.36862750.37254900.37254900.37254900.37254900.37254900.38039220.38431370.40392160.41568630.42745100.42745100.43137250.43921570.43921570.43529410.42745100.42745100.42745100.42745100.42352940.42352940.42352940.41960780.41176470.40784310.41176470.41176470.42352940.43137250.43137250.43137250.44313730.43529410.43137250.43529410.43137250.42745100.41960780.42352940.42352940.42352940.42352940.42352940.42745100.43137250.43921570.44313730.44313730.44313730.44705880.44705880.45490200.46666670.47058820.47450980.46274510.46666670.47450980.47843140.48235290.49019610.49803920.50588240.50980390.51372550.52156860.52941180.53725490.53725490.53725490.53333330.54901960.54901960.54901960.54901960.54901960.54509800.54117650.53725490.52941180.52549020.52549020.52549020.52156860.51764710.51764710.51764710.50196080.49803920.49411760.49019610.48627450.49019610.49019610.49019610.48627450.48627450.49019610.49803920.49803920.50196080.50196080.50196080.50196080.50588240.51372550.51764710.52156860.52156860.51764710.51764710.51764710.52156860.51764710.51764710.51372550.51372550.50980390.50980390.50196080.50196080.50588240.50588240.50980390.50980390.51372550.51372550.50196080.50196080.50588240.50588240.50980390.51372550.51764710.51764710.51764710.52156860.52941180.52941180.52941180.53333330.53725490.54117650.53725490.54901960.55294120.55686270.55294120.55294120.55686270.55686270.56078430.56078430.55686270.55686270.56078430.56078430.56470590.56470590.56470590.56862750.56862750.57647060.57254900.57254900.56862750.56470590.56470590.56470590.56078430.55686270.55686270.55294120.54901960.54901960.54509800.54509800.54117650.54117650.53725490.53725490.53333330.53333330.53333330.52941180.52549020.51764710.51372550.50196080.49803920.49019610.48235290.48235290.48235290.48627450.49019610.49019610.49019610.49019610.48627450.48627450.49019610.49019610.49411760.50196080.50588240.50588240.50588240.50588240.50588240.51372550.51372550.51764710.52156860.52156860.52941180.52941180.53333330.53333330.53333330.54117650.54117650.54117650.54509800.54509800.54509800.55294120.54901960.54901960.54509800.54509800.54901960.54509800.54509800.54117650.53725490.53333330.52941180.52941180.52941180.52941180.52941180.52941180.52941180.52941180.52941180.52941180.53725490.53333330.53333330.52941180.52941180.52941180.53333330.53333330.53725490.53725490.54117650.54117650.54117650.54117650.53725490.53725490.53333330.52941180.52549020.52156860.51372550.51372550.50980390.50980390.50980390.50980390.51372550.51372550.51372550.50980390.50980390.5058824
r2100.31764710.31764710.31764710.31372550.31372550.32156860.33333330.34117650.34901960.35686270.36078430.36862750.36862750.37647060.38039220.38823530.39215690.39215690.39215690.39607840.38823530.39215690.39607840.40000000.40392160.40392160.39607840.39215690.38823530.38431370.38039220.38039220.38039220.38039220.38039220.38039220.37647060.37647060.37647060.37254900.38039220.38039220.38431370.38431370.38431370.38431370.38823530.39215690.40000000.41176470.42352940.42352940.42745100.43529410.43921570.43529410.42352940.42352940.42352940.42745100.42745100.42745100.42352940.41960780.41176470.41176470.41568630.41568630.42745100.43137250.42745100.42745100.44313730.43137250.43137250.43529410.43529410.43137250.42352940.42352940.42745100.42745100.42745100.42745100.43137250.43529410.43921570.44705880.43921570.44313730.44313730.44705880.45490200.45882350.47450980.47843140.47058820.47450980.48235290.49019610.49411760.49803920.50980390.51372550.51764710.52156860.52941180.53333330.53725490.54117650.53725490.53725490.54509800.54509800.54901960.54901960.54901960.54509800.54117650.53725490.53333330.53333330.52941180.52549020.52156860.52156860.51764710.51764710.50588240.50196080.49803920.49411760.49411760.49411760.49411760.49411760.49019610.49019610.49411760.50196080.50588240.50588240.50588240.50588240.50980390.50980390.51764710.52549020.52941180.52941180.52549020.52549020.52549020.52156860.52549020.52156860.52156860.51764710.51764710.51372550.50588240.50588240.50980390.51372550.51372550.51764710.51764710.52156860.50980390.50980390.51372550.51372550.51764710.52156860.52156860.52156860.52549020.52941180.53333330.54117650.54117650.54117650.54509800.54509800.54117650.54509800.55686270.56078430.55686270.55686270.56078430.56078430.56470590.56470590.56470590.56078430.56078430.56470590.56862750.56862750.57254900.57254900.57254900.57254900.58039220.57647060.57254900.57254900.55686270.56078430.56078430.55686270.55294120.54901960.54901960.54509800.54901960.55294120.54901960.54901960.54509800.54509800.54117650.54117650.53725490.53725490.53333330.52549020.52156860.50588240.50196080.49411760.48627450.48627450.48627450.48627450.49019610.49019610.48627450.48627450.48627450.48627450.49019610.49411760.49803920.50196080.50588240.50980390.50980390.50980390.50588240.51372550.51372550.51764710.51764710.52156860.53333330.53333330.53333330.53725490.53725490.54509800.54509800.54509800.54901960.54901960.54901960.55686270.55686270.55294120.54901960.54901960.55294120.55294120.54901960.54509800.54117650.53725490.53333330.53333330.52941180.52941180.52941180.52941180.52941180.52941180.52941180.52941180.52549020.53333330.53333330.52941180.52941180.53333330.53333330.53333330.54117650.54509800.54509800.54901960.54901960.54509800.54509800.54117650.53725490.53333330.52941180.52549020.52156860.51764710.51764710.51764710.51764710.51764710.51764710.52156860.52156860.51764710.51764710.5176471
r2090.31764710.31764710.31764710.31764710.32156860.32941180.34117650.35294120.35686270.36470590.36862750.37254900.37647060.37647060.38431370.38823530.38431370.38431370.38431370.38823530.39215690.39607840.40000000.40000000.40784310.40000000.40000000.39607840.39215690.39215690.38823530.38823530.38823530.38823530.38823530.38823530.38431370.38431370.38039220.38039220.38431370.38823530.39215690.39215690.38823530.38823530.39607840.40000000.40392160.41568630.42352940.42745100.41960780.42745100.43137250.42745100.42745100.42745100.42352940.42352940.42745100.42352940.41960780.41176470.40784310.41568630.41176470.41568630.42745100.43137250.42352940.42352940.43529410.42352940.42352940.43137250.43529410.43137250.42745100.41960780.42745100.42745100.42745100.43137250.43529410.43921570.44313730.44313730.44313730.44313730.44705880.45098040.45882350.46274510.47058820.47450980.47450980.47843140.49019610.49411760.50196080.50588240.51372550.52156860.52549020.52941180.53333330.53725490.54117650.54117650.54117650.53725490.54509800.54901960.54901960.54901960.54901960.54509800.54117650.53725490.53725490.53725490.53333330.52941180.52549020.52549020.52156860.51764710.50980390.50588240.50196080.49803920.49803920.50196080.50196080.50196080.49411760.49411760.49803920.50588240.50980390.50980390.50980390.50980390.51372550.51764710.52156860.52549020.53333330.53333330.53333330.53333330.52941180.52549020.52549020.52549020.52549020.52156860.52156860.51764710.50980390.51372550.51372550.51764710.51764710.52156860.52549020.52549020.51764710.51764710.51764710.52156860.52156860.52156860.52156860.52156860.52941180.53725490.54117650.54509800.54509800.54509800.54901960.54901960.54509800.54901960.55294120.56470590.56078430.56078430.56078430.56470590.56470590.56470590.56470590.56862750.56862750.56470590.56470590.56862750.57647060.57647060.57647060.57647060.57254900.57254900.56862750.57254900.56470590.56470590.56078430.56078430.56470590.56078430.56078430.55686270.55294120.55294120.55686270.55294120.54901960.54901960.54509800.54509800.54117650.54117650.53333330.52941180.51764710.50980390.50196080.49803920.49019610.49019610.48627450.48627450.49019610.48627450.48627450.48627450.48627450.48627450.49019610.49411760.49803920.50588240.50980390.50980390.51372550.51372550.50980390.51764710.51764710.52156860.52156860.52156860.53725490.53725490.53725490.54117650.54117650.54901960.54901960.54901960.55294120.55294120.55294120.56078430.55686270.55294120.54901960.54901960.55294120.55294120.54901960.54509800.54117650.53725490.53725490.53333330.53333330.53333330.53333330.53333330.53333330.53333330.53333330.53333330.52549020.52549020.52156860.52156860.52941180.52941180.53333330.53333330.54117650.54117650.54901960.54901960.54901960.54901960.54509800.54509800.53725490.53725490.52941180.52549020.52156860.52156860.51764710.51764710.51764710.52156860.52156860.52156860.52156860.52156860.52156860.5176471
r2080.31764710.31764710.32156860.32156860.32549020.33333330.34509800.35686270.36078430.36470590.36862750.37254900.37254900.37647060.38039220.38431370.38039220.38431370.38823530.39215690.39215690.39607840.40000000.39607840.40392160.40000000.40000000.40000000.39607840.39607840.39607840.39215690.39607840.39607840.39607840.40000000.39607840.39607840.39607840.39607840.39215690.39607840.39215690.39215690.39215690.39607840.40392160.40784310.40784310.41960780.42745100.42745100.43137250.43529410.43921570.43137250.43137250.42745100.42745100.42745100.42745100.41960780.41176470.40784310.41176470.41568630.41176470.41568630.42352940.42352940.41960780.41568630.42745100.41568630.41568630.42745100.43529410.43529410.43137250.42352940.43137250.43137250.43137250.43529410.43921570.44313730.44705880.44705880.44705880.45490200.45490200.46274510.46666670.47450980.47843140.48235290.47843140.48627450.49411760.50196080.50980390.51372550.52156860.52549020.53333330.53333330.53725490.53725490.54117650.54117650.54117650.54117650.54901960.55294120.55294120.55686270.55686270.55294120.54901960.54509800.54117650.54117650.53725490.53725490.53333330.52941180.52549020.52549020.50980390.50980390.50588240.50588240.50196080.50588240.50588240.50588240.50196080.50196080.50588240.51372550.51372550.51764710.51764710.51764710.52156860.52549020.52941180.53333330.53725490.53725490.53725490.53725490.53333330.53333330.52941180.52941180.52941180.52941180.52549020.52549020.51764710.51764710.52156860.52156860.52549020.52549020.52941180.52941180.52549020.52549020.52549020.52549020.52549020.52549020.52549020.52549020.53725490.54117650.54901960.54901960.54901960.54901960.55294120.55294120.54901960.55294120.55686270.56078430.56470590.56470590.56470590.56862750.56078430.56078430.57254900.57254900.57254900.57254900.56862750.56862750.57254900.57647060.57647060.57647060.57647060.57254900.57254900.56862750.57254900.57254900.56862750.56862750.56470590.56078430.56862750.56470590.56078430.55686270.55686270.55294120.55686270.55294120.55294120.54901960.54509800.54117650.54117650.53725490.52549020.51764710.50588240.49803920.49411760.49411760.49019610.49019610.49411760.49019610.49019610.49019610.49019610.49019610.49411760.49803920.50196080.50980390.51372550.51764710.51764710.51764710.51372550.52156860.52549020.52549020.52941180.52941180.54117650.54117650.54117650.54509800.54509800.54901960.55294120.55294120.55686270.55686270.55686270.56078430.55686270.55294120.54901960.54901960.54901960.54901960.54509800.54117650.53725490.53333330.52941180.52941180.53333330.53333330.53333330.53333330.53333330.53333330.53333330.53333330.52941180.52549020.52549020.52549020.52549020.52941180.53725490.53725490.53725490.54117650.54117650.54509800.54901960.54901960.54901960.54901960.54117650.54117650.53725490.53333330.52941180.52549020.52549020.52549020.52549020.52549020.52941180.52941180.52941180.52941180.52941180.5294118

Check

tally(~ color_part, data = img_df)
#> color_part
#>   b   g   r 
#> 213 213 213
tally(~ y, data = img_df) %>% all(. == 3)
#> [1] TRUE

Reshape to long format

The long format is the standard for many operations, such as the cluster analysis. So, let’s reshape:

Pivot to long format

img_df_long <- img_df %>% 
  pivot_longer(cols = -c(color_part, y),
               names_to = "x",
               values_to = "value")

dim(img_df_long)
#> [1] 204480      4

The number of rows of this data frame should be the product of

  • the number of rows by
  • the number of columns by
  • the number of color parts (ie., rgb)

of the original data frame. Let’s check:

nrow(img_df_long) == dimension[1] * dimension[2] * dimension[3]
#> [1] TRUE

OK.

head(img_df_long)
#> # A tibble: 6 x 4
#>   color_part     y x     value
#>   <chr>      <int> <chr> <dbl>
#> 1 r            213 X1    0.318
#> 2 r            213 X2    0.322
#> 3 r            213 X3    0.325
#> 4 r            213 X4    0.325
#> 5 r            213 X5    0.322
#> 6 r            213 X6    0.322

Transfer col_nr values to pure numbers:

img_df_long2 <- img_df_long %>% 
  mutate(x = parse_number(x))
head(img_df_long2)
#> # A tibble: 6 x 4
#>   color_part     y     x value
#>   <chr>      <int> <dbl> <dbl>
#> 1 r            213     1 0.318
#> 2 r            213     2 0.322
#> 3 r            213     3 0.325
#> 4 r            213     4 0.325
#> 5 r            213     5 0.322
#> 6 r            213     6 0.322

Checks

summarise(img_df_long2, n_distinct(x))
#> # A tibble: 1 x 1
#>   `n_distinct(x)`
#>             <int>
#> 1             320
summarise(img_df_long2, n_distinct(y))
#> # A tibble: 1 x 1
#>   `n_distinct(y)`
#>             <int>
#> 1             213
summarise(img_df_long2, n_distinct(color_part))
#> # A tibble: 1 x 1
#>   `n_distinct(color_part)`
#>                      <int>
#> 1                        3

Check:

count(img_df_long2, color_part)
#> # A tibble: 3 x 2
#>   color_part     n
#>   <chr>      <int>
#> 1 b          68160
#> 2 g          68160
#> 3 r          68160

Spread RGB parts in separate columns

img_rgb <- img_df_long2 %>% 
  pivot_wider(names_from = "color_part",
              values_from = "value")

head(img_rgb)
#> # A tibble: 6 x 5
#>       y     x     r     g     b
#>   <int> <dbl> <dbl> <dbl> <dbl>
#> 1   213     1 0.318 0.333 0.278
#> 2   213     2 0.322 0.337 0.282
#> 3   213     3 0.325 0.341 0.286
#> 4   213     4 0.325 0.341 0.286
#> 5   213     5 0.322 0.337 0.282
#> 6   213     6 0.322 0.337 0.282

k-Means

We have 3 dimensions, so we would like to kind’a find a number of bee swarms in a room. Let’s take 16 clusters.

set.seed(1896)
k_means <- kmeans(img_rgb[,c("r","g","b")], centers = 16, 
                  iter.max = 25, nstart = 10)

Here are the colors:

k_means$centers %>%
  rgb() %>%
  show_col()

Frequencies of colors

gf_col(k_means$size ~ 1:16, fill = rgb(k_means$centers))

Compress colors

Now we replace the colors of the pixels by the cluster center colors. By that, we will end up with 16 colors only, thereby compressing the image.

First, add the cluster to which each pixel belongs to the data frame:

img_rgb <- img_rgb %>% 
  mutate(cluster = k_means$cluster)

dim(img_rgb)
#> [1] 68160     6
head(img_rgb)
#> # A tibble: 6 x 6
#>       y     x     r     g     b cluster
#>   <int> <dbl> <dbl> <dbl> <dbl>   <int>
#> 1   213     1 0.318 0.333 0.278       6
#> 2   213     2 0.322 0.337 0.282       6
#> 3   213     3 0.325 0.341 0.286       6
#> 4   213     4 0.325 0.341 0.286       6
#> 5   213     5 0.322 0.337 0.282       6
#> 6   213     6 0.322 0.337 0.282       6

Extract the cluster centers with their colors:

centers_rgb <- k_means$centers %>% 
  as_tibble() %>% 
  mutate(cluster = 1:16)

Define the new image as the image where each pixel gets the color of its cluster center color:

img_new <- img_rgb %>% 
  select(x, y, cluster) %>% 
  full_join(centers_rgb) %>% 
  select(r, g, b) %>% 
  as.matrix()

Check

Let’s check that the row number remained the same:

nrow(img_new) == nrow(img_rgb)
#> [1] TRUE

Initialize 3D data cube for image

We now convert to a matrix, as we need a (3D) matrix again to write the jpg image:

img_new_array <- array(NA, dimension)
dim(img_new_array)
#> [1] 213 320   3

Write to 3D array

for(i in 1:3) img_new_array[,,i] <- matrix(img_new[,i], 
                                           nrow=dimension[1]) 

Write to file

file_output_path <- paste0(here::here(), "/static/img/iris_reduced.jpg")
writeJPEG(img_new_array, file_output_path)
knitr::include_graphics("/img/iris_reduced.jpg")