The countcolors
package finds pixels by color range in an image. It started as a collaboration between Sarah Hooper, Sybill Amelon, and me (Hannah Weller), in order to quantify the area of white-nose syndrome infection on bat wings. In general, it’s meant to substitute for manual region-of-interest selection, which can be time-consuming and inconsistent.
countcolors
deals primarily with RGB color space. You’re probably familiar with the fact that digital images are stored with three color layers: red, green, and blue. We can think of these like the dimensions of a three-dimensional color space1, where a color is defined by its red, green, and blue coordinates. Each of these axes ranges from 0 to 255 (or 0 to 1, if we’re dealing with unit vectors). So pure red, for example, would have a red value of 1, a green value of 0, and a blue value of 0. Magenta, which is an equal mix of red and blue, would have an RGB triplet of [1, 0, 1].
## Loading required package: scatterplot3d
To search for pixels of a certain color, countcolors
draws a set of boundaries around a region of color space, and counts all the pixels in the image that fall within those boundaries. There are two ways of defining the boundaries: either you can set upper and lower limits for each color channel, drawing a bounding ‘box’ around a region of color space, or you can pick a color center and a radius to search around it, which draws a ‘sphere’ in color space.
If we wanted to define ranges for magenta pixels, we would look for pixels near the RGB value of [1, 0, 1]. To expand this range a little, we could define the following conditions:
Then we search for pixels whose colors satisfy all three conditions. In countcolors
, you do this by specifying a lower and an upper set of RGB triplets:
This is why it’s referred to as a ‘rectangular’ range – it draws a box around a region of color space.
Sometimes, rather than boundaries, you want to specify a particular color and a search radius around that color. countcolors
uses this to draw a sphere in color space centered around a single color, whose size depends on the radius. Say we’re looking at a mossy green color, with an RGB triplet of [0.4, 0.7, 0.4]. If we set the radius very small – say, 5% of maximum color distance – we only get a few pixels back:
But if we increase that radius to 25%, we get a much bigger search space and many more pixels back:
All countcolors
does is search for pixels within a color range specified by the user, count them, and tell you how many there are and where they are in the image. It also comes with a couple of diagnostics to check that you’re picking the right color range.
1: Of course, there are many color spaces besides RGB, such as HSV (probably familiar), CMYK (maybe familiar), and CIELab (probably only familiar if you’ve worked with color spaces before, in which case you definitely don’t need my explanation). RGB is actually considered quite a poor representation of how human beings perceive color, but it works just fine for quantifying color proportions. A full discussion of color spaces is well beyond the scope of this introduction, but if you want to know more, I recommend Bruce Lindbloom’s website.
To see how this works in practice, let’s look at an aerial photograph of Norway from NASA: