# Transform or filter which selects similarly colored regions?

Is there any transform, which "looks" at any region of a picture and then compares it with neighbor region, and if two regions are similarly colored, then unifies two regions in one and colors it with average color.

Finally, this transform will distinguish "paper" from "writing" on it.

It will color with constant value any gradient fields or dimmed pictures, while remain uncolored any area with frequently changed colors, like in writings.

Key idea is that this transform should unify any big color change if it is gradual.

Below is my manual example

Here background is unified to gray since it is average color of gradually changing background region, while rectangle is colored with dirty green because it is it's average color. Two regions are separated with high gradient margin.

There is a notion of color difference, for example in `Lab` color space it can be defined as . So, with this value one can calculate gradient, i.e. this value per pixel step. Also one may introduce threshold, separating "big" gradient from "small" one. So, pixels with small gradient can be merged together and colored with average color.

This is one rough approach coming to mind.

-
I think the problem with your example is that any single-color text on that page will, at some point, be a very close to the background color, resulting in a gradual change which will be averaged. – horatio Jun 7 '13 at 19:14

For the example you provide a better technique would be to select the region you want to average. The system would sample the spectrum of selected colors and find the midpoint. Or, more crudely, you could manually apply a blur to "mix" the colors yourself.

Alternatively, you would have to provide the system with input on what you consider "similar". Presumably, your example distinguishes between the red/green and white/black gradients based solely on saturation, since neither hue nor value would compare well. Even within saturation you're looking at a broad range. Of course, you could apply the parameters with Lab if you prefer.

The only tool that comes to mind for this kind of analysis is Mathematica.

Mathematica provides broad and deep built-in support for both programmatic and interactive modern industrial-strength image processing—fully integrated with Mathematica's powerful mathematical and algorithmic capabilities. Mathematica's unique symbolic architecture and notebook paradigm allow images in visual form to be included and manipulated directly, both interactively and in programs.

-
So, how to do this in Mathematica? – Dims Aug 7 '13 at 11:07
I'm afraid I can't help you there. There is a Mathmatica SE. – plainclothes Aug 7 '13 at 15:46
I think Mathematica is just a library, like Matlab or OpenCV. It has building blocks for everything, but the question is how to combine these blocks to achieve result. – Dims Aug 7 '13 at 20:12
True. But it does provide tools for image processing. Possibly something you could build on with help. – plainclothes Aug 7 '13 at 20:20

I think there's a fundamental problem with this. You say transform/merge similarly colored regions but there is nothing at all similar about red and green as you used in your example. So no it doesn't exist.

-
not so fast: in the gradient, the transitions are in fact similar to each other even if the end points are not. – horatio Jun 7 '13 at 19:13
if you look at it that way then everything would be blurred together though. So either everything is different or the same... or you need to outline each thing which is then determining the borders by shape not by light. – Ryan Jun 7 '13 at 20:00