Sunday, May 10, 2020

Different Lighting for Lego image

Tried the flash on my camera instead of room lighting.  Here are the results.  Input is a 1200x1200 pixel image.

Input:


Output (centroids marked with white dots):

Here's that big blue rectangle on the right, 'extracted' from the image and put into it's own, along with it's RGB histograms:


As you can see from the histogram, all of the blue values are larger than red and green, and therefore the color is 'blue'.  Also notice that the number of pixels is an indication of size.

So with one histogram, I can know approximate color and size.  How well does this translate to picking a particular object or color is TBD.

... and now I'm finding the 'corners' of each found object.  With this I can compute various parameters like the length of each side, the area, and the orientation / rotation wrt to vertical or horizontal.  Small green dots mark the 'corners' and purple dots are the centroids.

Best yet!  47 total objects.  It got everything but the white pieces.  Pretty decent color averages, too.

and the input again for comparison:

Each object is marked with four small green dots, showing its corners.  47 total objects found, indexed, and stored ready to be used as input to the next stage.  There are actually a total of 48 pieces.  I missed two, but I also have one false positive (near the glary part lower center).  The software found 47 objects, missed two white pieces (49) and fp-ed one to give a count of 48.  Correct!!!

Saturday, May 9, 2020

Lego Project Update

I've almost got object finding completed.  There's a bug in the algorithm somewhere that is making some interesting results.

I start with this image as input:


I run my code and I get this:


The individual objects that the software finds is color coded.  So all the red pixels above are part of one 'object'.  Same with green and purple.  Notice, however, that there's some strange stuff going on.

One part of the definition of an 'object' is that the pixels have to all be connected.  The software counted three objects in the input image.  I'm sure you notice that there's some funny stuff going on with the red and green pixels on the right.  The reds, for instance, are all part of the same object -- but that stuff on the right clearly isn't connected to the red pixels on the left!  Not sure what's going on here.  I'm using a recursive algorithm to find those pixels that are part of an 'object', but clearly there's something going wrong.  It's hard to see, but there are some green pixels around the purple object, too.

Once I get the object locator code working, I can turn to object identification.  The first thing I'll try to do is identify color.  I'll try to do it by brute force at first (conditional statements in the code like if red and green are nearly equal, then the color is probably yellow), but I'm thinking that this'll be my first opportunity to use a neural network for the color identification.

UPDATE

LOL after writing the above, I realized what I'd done wrong on the code.  I fixed that with these results:



So here are three found objects.  The little black dots show the centers of each one, proving that the code is finding individual objects.

I've jumped forward to writing out centered found objects to individual images.  So I start with this different image:



... and end up with these four extracted images showing four found objects.  These images can be used as input to a trained neural net or whatever:

Object 1

Object 2

Object 3

Object 4


Kinda rough, but not too bad for a first try.

As you can see, it didn't pick up the white piece next to the red.  That's mainly because I'm picking out these objects by measuring their contrast to the background.  A white Lego on a white background makes it hard to pick out.  I'll either need to use a different background (a non-Lego color), or change how I find the individual objects.

So the next step is color identification.  As I said, I'm gonna try hard-coding with RGB thresholding to see if I can measure colors that way.  If not, I'll turn to a neural net solution.

The step after that is object identification.  This seems to be where everyone gets stuck.  I have no idea if I will, so I'm proceeding.

MORE Update

Ahhhh!  I can't stop!  Here's an image with a few more objects.  The code found 8 objects, which I'd say that five are useable for matching.  It's also 1000x1000 pixels -- the largest array sofar.

probably not usable






probably not usable

probably not usable

More more update:

I've taken a new image of some close-packed legos to see how many my software can find.  This is, again, a 1000x1000 array.  It found 41 objects in this image!  Not too bad!

Input Image

Found Objects

LOL looks like it missed all the gray pieces.  Again, because of their closeness to the background.  Each object is marked with a red dot.  Notice all the spaces?

Average colors (dot) of found objects:

Wow this next image is impressive.  Here's each found object with its average color.  Compare this to the input image and the similarity is pretty stricking!


Friday, May 8, 2020

Lego Project

The very simple project I thought up a week or so ago, which was to build something that uses every single one of my Legos.  This project has now exploded / expanded a little bit.  My primary focus is to see how well I can create an automatic Lego piece sorting machine.  This is the first step:


Thinking Lego.  Writing code this weekend to number the objects (tagging) seen in this image.  I probably won't get it done.  Identifying and tagging pieces, and then classifying and sorting them according to various rules will come later.  I plan to use 'Deep Reinforcement Learning', which is a multi-level neural network that operates by maximizing a reward parameter.

Why are there no Lego automatic sorting machines that actually work and will sort in many different ways?????

There are quite a few ways to sort Legos.  I need to come up with at least a few ways so that when I'm ready to starting working on that part I will have thought about it a little beforehand.

One of the interesting tangents is looking at the color information in an interesting way.  Each channel: red, green, and blue, can have a value between 0 and 255 for each pixel.  So I can create a histogram of the distribution of red, green, and blue of any image.  The plots below use the left image above as input.  I can also look at the distribution of red+green (yellow), red+blue (violet), and green+blue (cyan).  I can plot these distributions with the y-axis being a logarithmic scale, and I get this:

Here's the same data on a linear scale:


But that's just a nice tangent, which might be helpful for training the neural net.