1

How can I extract the color values of this table programmatically?

colortable

The output should look something like

[[0.7, 0.7, 0.6, 0.0,...],,
 [0.4, 0.1, 0.0, 0.2,...],
 ...
 [1.0, 0.0, 0.4, 0.3, ...]]

In numpy format. The solution would involve 2 steps:

1) Read the colorbar on the right and construct a colormap from it.

2) Decompose the table into squares and apply the reverse colormap to each square.

I have tried to repurposed this repository but so far to no avail. An answer using OpenCV would be nice but of course whatever works best!

Takoda
  • 354
  • 3
  • 12
  • 1
    https://stackoverflow.com/questions/49471502/how-to-digitize-extract-data-from-a-heat-map-image-using-python – Shijith Jun 08 '20 at 17:27

1 Answers1

0

An answer using OpenCV would be nice but of course whatever works best!

Rather than Python/OpenCV, here is a way to do that using ImageMagick 6.

I note that the scale is 0 when most white and 1 when most blue. So if one converts the image to HSV and gets the value channel, stretches the dynamic range to 0 to 255 and inverts that so that dark is 0 and bright is 1, then one simply needs to get the brightness of the cells. The grid is nearly 300x370 pixels in dimension. So cropping to 300x370 and making the excess transparent gives 10x10 cells of 30x37 pixels. So we can average the pixels in each 30x37 patch to get the value.

  • Read the image
  • Crop to size 335x370 to get the grid region of interest and the scale
  • Copy that and make transparent anything perfectly white, which is the image background white and extract and save the alpha channel in memory (mpr) as a mask
  • Convert the cropped region to HSV and extract the Value channel only
  • Put the mask into the alpha channel of the Value channel image
  • Stretch to full dynamic range
  • Crop the 300x370 grid region
  • Scale (by block averaging) the 300x370 region to 10x10 pixels by averaging each 30x37 block, ignoring the transparent pixels
  • Convert the image to txt: format, extract the gray level in the range 0 to 100, and divide by 100 to get the gray levels in the range 0 to 1 and list to the terminal


Input:

enter image description here

convert chart.png -crop 335x370+59+58 +repage +write chart_cropped.png \
\( -clone 0 -transparent white -alpha extract +write chart_mask.png -write mpr:mask +delete \) \
-colorspace HSV -channel 2 -separate +channel +write chart_value.png \
mpr:mask -alpha off -compose copy_opacity -composite +write chart_value_a.png \
-auto-level -crop 300x370+0+0 +repage \
-scale 10x10! -alpha off -negate \
txt: | tail -n +2 | sed -n 's/^.*gray[(]\(.*\)\%[)]$/\1/p' | awk '{print($1/100)}'


Cropped image as 335x370 pixels:

enter image description here

Mask image:

enter image description here

Value channel with alpha:

enter image description here

300x370 Cropped grid:

enter image description here

10x10 Resulting cell values:

0.587839
0.594598
0.412161
0.0608072
0.334691
0.836713
0.98175
0.455177
0.494606
0.0405432
0.243244
0.0540475
0.0405432
0.128374
0.386923
0.0565347
0.317128
0.12636
0.661707
0.749996
0.425681
0.337835
0.141894
0.121614
0.248661
0.0189212
0.172305
0.0761273
0.159457
0.594598
0.864866
0.256764
0.499992
0.00675975
0.153368
0.0999924
0.255863
0.239857
0.562173
0.959457
0.425681
0.243244
0.0945907
0.20473
0.0792859
0.409918
0.149096
0.189639
0.574319
0.324331
0.324331
0.189197
0.263508
0.0351263
0.261936
0.025452
0.182666
0.369375
0.148653
0.277028
0.10135
0.364858
0.202701
0.191897
0.2813
0.584466
0.104723
0.399542
0.412161
0.364858
0.22298
0.459464
0.00675975
0.0195926
0.579049
0.315328
0.0599069
0.231083
0.250004
0.0608072
0.364858
0.358114
0.202701
0.147082
0.118242
0.597971
0.294591
0.063508
0.668925
0.905409
0.22298
0.189197
0.0932326
0.420706
0.0889754
0.331991
0.533791
fmw42
  • 46,825
  • 10
  • 62
  • 80