Adam T. Bradley

Posts in tag “color”

Reading Images in R

I've been experimenting with the ReadImages package to analyse JPEGs in R, largely following this is-r() post

Most of the code behind this is in this gist, which you can also see below.

Rather than use k-means clustering, I've been experimenting with translating the colors in images into human-descriptive names. For example, what I have now can take this image:
Say Digital Humanities

And generate a plot showing the frequency of different colors in the image:

library(devtools)
library(xtable)
source_gist(4676470)
sayit <- codeColors("http://www.nottamuntown.com/img/6a017c36310338970b017c368bf26f970b-400wi")
# A sample of what this data looks like now:
print(xtable(head(sayit)), type = "html", include.rownames = F)
Y X R G B rgb rounded code
-1 1 0.90 0.82 0.61 #E6D19C #FFCC99 white
-2 1 0.79 0.71 0.48 #C9B47B #CCCC66 yellow
-3 1 0.80 0.72 0.47 #CDB879 #CCCC66 yellow
-4 1 0.83 0.74 0.47 #D3BC78 #CCCC66 yellow
-5 1 0.82 0.73 0.43 #D0B96D #CCCC66 yellow
-6 1 0.85 0.75 0.44 #D8BF6F #CCCC66 yellow

plotImage(sayit)

Unnamed-chunk-1 (1)

It can also generate images showing the intermediate steps in generating the plot, such as this image reduced to 216 colors…

plotImage(sayit, 'rounded')

Plot_zoom_png

And this one, using the color names to color the pixels, instead of the much more specific color codes:

plotImage(sayit, 'code')

Plot_zoom_code

The color data itself is a csv list of the 216 “web-safe” colors with each color's “name” as I perceived it while looking at swatches on a web page, for example:

print(xtable(head(read.csv("http://sandbox.adamtbradley.com/colors/")), n = 10),
type = "html", include.rownames = F)
colorID colorCode
000000 black
000033 blue
000066 blue
000099 blue
0000CC blue
0000FF blue

So far, the main point of failure is in images with a lot of white and/or shadows in them—shadows tend to have a purplish tinge, and a solid white wall can appear as a variety of colors depending on the light. Effectively, this problem results from the fact that our perception of color is heavily influenced by context–a color that looks purple or grey when you have a large rectangle of it on a web page might be clearly white in a picture of the Providence Athenaeum's interior, resulting in something like this image

Providence Athenaeum by Ken Zirkel

being perceived this way:

library <- codeColors("http://farm9.staticflickr.com/8463/8381437913_4f30c9bd72_n.jpg")
plotImage(library, "code")

Unnamed-chunk-3

plotImage(library)

Unnamed-chunk-32