UIn the previous entry in the Propaganda Tiles series, I wrote about the history of the tiles, and my plans to sort them by color. It turns out this requires a dramatic amount of mathematics, almost all of which are miles above my head.

The primary field that works on this kind of image processing is the computer vision branch of artificial intelligence research. Luckily, there’s the OpenCV library – a vast collection of resources and program code – to help out. It’s another one of those remarkable open-source community-run projects that rivals any commercial solution.

I’ll be using some of the OpenCV libraries to process the Propaganda tiles and extract the top three colors in each image, along with the percentage of each. This will be done with a technique called k-means clustering, and this is how it works:

That’s from Wikipedia. I really have no idea what it means. I ain’t never had enough education to learn such fancy math. Heck, I couldn’t even get WordPress to draw a border around the image, so I had to replace it manually.

I realized this morning that searching for tiles based on a chosen color would be complicated. A color is a three-dimensional value, the most common being the levels of red, green, and blue. In the web world, these are expressed as a set of hexadecimal (base-16) numbers, like #a47eb1. That’s a4 (164) for red, 7e (126) for blue, and b1 (177) for green. These combine to make a sort of lavender purple color:

But it dawned on me that I can’t just “add X to blue” to find similar colors, because just moving along the blue scale isn’t helpful. If you take this color and fiddle with only the blue value, it ranges from purple to orange. Adding to or subtracting from all three colors just moves you closer to white or black. Not helpful.

There’s another way to represent colors: HSV, which stands for hue, shade, and value. This method is a lot less intuitive than red, green, and blue. The distinctions between hue, shade, and value are more difficult to grasp, at least for me. The Wikipedia page for HSV (and its cousin, HSL) runs to fifteen printed pages and includes diagrams like this:


Which is pretty, and it also makes a kind of sense of the whole HSV concept. But then the math comes in. Again. And you run into things like this:

It’s all three-dimensional computation, polar coordinates, vectors, and other mathematical concepts that are beyond my ability comprehend.

Luckily, I’m using OpenCV to do this work. It has a community of users. Users who are helpful and friendly. I asked my question on the OpenCV forums and got some helpful tips. It appears I don’t have any choice but to learn some of this math if I want my project to succeed. Or I at least need to understand how to use the OpenCV functions. Specifically something called flann. I’ll get back to you once I figure it out.

In the mean time, happy Propaganda to all, and thanks to the OpenCV members who shared their knowledge.


Leave a Reply

Your email address will not be published. Required fields are marked *