Visualizing Image Data From a Photograph
How to construct a histogram from the RGB values in an image to help improve color balance.
Join the DZone community and get the full member experience.Join For Free
i think it's an interesting exercise to visualize data contained in a photograph as we might other datasets. some cameras and photo-editing software do just this, constructing histograms from the red (r), green (g) and blue (b) values (the three "primaries") in the image to help expert photographers/editors judge and improve color balance. to illustrate this idea, i'll use the color photograph and its grayscale counterpart below.
it's easiest to start with the grayscale image where the r, g and b values are, by definition, identical for any given pixel. on the left we have a grayscale value of 0, meaning an (r, g, b) value of (0, 0, 0), a.k.a. black; on the right it is (255, 255, 255) or white. in between we have 254 shades of gray.
this is one of the few cases where use of a gradient in a chart may actually help clarify things, in this case by coloring each bar according to the grayscale value it represents. the only problem with this is then picking a background color that is neutral:
with a color image we need to plot each of the r, g, and b histograms separately. it's less cluttered to use lines rather than bars, but with a sensible color scheme they probably don't need labeling.
aside from achieving a good color balance, i think there is another issue that is interesting to explore that is important for web development: compression. jpeg compression is a fairly complex topic and i'll confess i've yet to really get my head around it. the color photo above is a high-quality (low-compression) miniature of the original (eight megapixel) image i shot. it is 46 kilobytes in size. the image below is the same in terms of number of pixels but is more highly compressed and of lower quality. it is, however, only nine kilobytes in size.
the lower quality should be obvious in the photo itself, but does the rgb histogram look significantly different? before trying this out i genuinely had no idea what to expect. while the histograms below are clearly different to the ones above they don't really scream "this image has been compressed much more than the other one".
an alternative to the jpeg is the png. here's an 8-bit png version of the photograph:
this image looks a lot better than the low-quality jpeg, but it's also six times bigger than it (in terms of file size) and a third bigger than the high-quality jpeg. the histogram is also very very different and difficult to read (note also the difference in the vertical scale).
this chaotic histogram is a result of the fact that only 256 different colors can be used in an 8-bit png (just like a gif ).
the histogram data above can be extracted from (at least some versions of) photoshop and presumably other photo editing software. but having not found any major difference between the histograms for the high- and low-quality jpegs i was curious to see whether more complex representations of the rgb data would highlight the difference. this required extracting all the individual rgb values, not just sums for the different channels. i didn't know how to do this in photoshop so i used r (other options are available). in an ideal world a 3d scatter plot would work brilliantly - one dimension for each of r, g and b. but 3d scatter plots on 2d screens rarely work. so i opted for a more conventional 2d scatter plot, using point color to show the third color. in the examples below i (somewhat arbitrarily) opted to plot blue value against red value, and colored the points according to the green value. for instance, a data point representing an image pixel with an rgb value of (30, 96, 92) would be plotted at the point (30, 92) on the chart and have an rgb color of (0, 96, 0).
my original attempt to do this suffered from some serious overplotting issues . to reduce, though not remove , this issue i made the points much smaller and took a random sample of "just" 20,000 points (just under 20% of the data) for each image. i also added the marginal distributions (i.e. the relevant histograms) at the unlabeled extremities of the plot. these were derived from all the image pixels, not just the sample.
now we can see a difference between the high- and low-quality jpegs: the latter has much more pronounced diagonal bands of points and gaps. examining the data, there are around 20,000 different rgb values in the ~105,000 pixels of the high-quality jpeg but only 15,000 in the low-quality jpeg. as for the png plot, that is an extreme example of overplotting. there really was 20,000 points plotted, they just occupy only a few hundred different positions.
ultimately you can probably get through life as, say, a web designer or developer without understanding the intricacies of image compression - i'm still not all that sure how jpeg compression actually works. but i think it's interesting to know the underlying data is there to be played with and for constructing your own dataviz experiments.
this article originally appeared on the infragistics blog by tim brock.
Published at DZone with permission of Josh Anderson, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.