Image

Imageth1alb wrote in Imagecpp

My first algorithm / Web cam fun


holy crap, I have been working on this 8 hours almost straight!

I created an almost intelligent algorithm for my awesome class in C++. objective of this algorithm is, to recognize background from non background (in this case a tic tac toe grid).
my first attempt, was to take the average color of the background (in this case green) and declare everything with that color as background.
Sounds simple, here is what that does:
Image
I am yet not using any threshold, the black part is always what was determined as background.
Not good at all.

A very long time later, I "taught" my algorithm how to split up the image in 2:
Image
Somewhat better, but the (here on purpose) extreme difference in lightning creates huge differences in color output (Red, Green and Blue).

Long time later again, I made the splitting variable. Now I can just change one value and the image get split up that many times. Here with 3 now:
Image

Here with 10:
Image

And 30:
Image

Pretty cool already, time to adjust the light to a more realistic background. First with 0 splitts:
Image
Now, lets see what my algorithm can do already (20 splits):
Image

Ok, time copy (by far not as easy as you think) this splitting "technology" to have not only vertical but also horizontal splits.
Here 1x1 split:
Image

1x8 split:
Image

And for shitts 20 horizontal splits:
Image

Enough playing around, lets get rid of the white space, 5x5:
Image

Almost, thank god i only have to change 2 numbers, 10x10:
Image

Before we move on, just for giggles a picture as background with 10x10 split. The algorithm fails horribly, I don't blame it:
Image

Back to the green paper, I flipped it over. You can see, the program can tell pretty decend from background and non background:
Image

I was pretty happy when I drew the first TicTacToe grid, to try it out. I don't know if it was the colors or the thick lines, but my algorithm failed:
Image

Time to fix this...
My first approach was, to prevent devient colors (in this case the tic tac toe grid) to be considered by the average color calculations. First I had many many errors, division by zero mostly, because
the program would look at the average color while calculating the average color; wereas some avg. color had to be there in the first place. :-P
I made it, so that it would calculate the avg. color first normal, and then again. But the seocond time, it would disregard any pixels that where different from the original avg. color.
Image
The result sucked. In some splitted sectors, the first average color was more the color of the grid, so the second calculation disregarded the actual background color.

My next try simular to the above but instead of the first calculation, it would just start with the second right away, not even considering the overall average color of that sector. I wasted my time with this other than fixing the errors mentioned in the first approach.
It did look better, but what if the calculation started right on the grid itself...
Image

Next, I further changed the sequence and calculations. First, it would get the avg. color of the entire frame (with 0 splitts), then while slowly adding more splitts, it would calculate a median avg. color for ever sector, roughly disergarding pixels that where much different from the median color; and finally moving to the last calculation with all the splitts and using final average colors, that had been calculated out of the median ones. Sounds confusing but anyways, it worked:
Image

Now using a different grid (once again, i dont know if it was the color or the thickness) excellent results!
Image
Also notice how the paper wasnt completely covering the area, but the algorithm took care of that anyways :))))
This picture shows average colors to the one above:
Image

Now here comes a tough one, which isn't really necesairry but helped me to further improove the quality of the algorithm:
Image

I was from now on, mostly adjusting values, mainly the 6 threshold values.
Image

Look, how good I got it to work, (I was focusing on the yellow X):
Image