3

I have following imaged, preprocessed with certain OpenCV functions:

Different colored lego bricks

Now I'm trying to get the number of bricks of each color (in this case 1 for every color). These are all colors I have.

My plan was to loop through all colors, use the OpenCV function "inRange" and get the area size. My problem is to recognize the different colors. I tried the red color and struggled on finding a good range. After asking google I understood, that I have to use HSV colors, to get a good range with the hue value.

So I wrote this code (in Java for some reason):

Mat img; //img is given from previos code
Mat hsv = img.clone();
Imgproc.cvtColor(img, hsv, Imgproc.COLOR_BGR2HSV);

After this I expected to have an image, with which I can get the different colors. In fact I got this:

Weird HSV image

In this image the colors are almost destroyed...red and light green look the same and other colors are completely different too.

Now my question: Do I something wrong? Is it possible to distinguish the Lego brick colors? What can I do to achieve this? I'm using OpenCV 3.1.0 for Java.

Thanks alot, Dennis

Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266
Dentastic
  • 101
  • 1
  • 5
  • 1
    Have a look [here](http://stackoverflow.com/a/31465462/5008845) – Miki Jun 01 '16 at 14:17
  • @Miki Thanks, but this doesn't work for me. For example: I try to get blue. The answers says "blue is 120 +- 15 sensity". But in the HSV image, there is no blue (in the RGB picture there are 2 blue bricks). My problem ist, that HSV conversion destroys all colors and I can't get the right colors...blue for example is found in the violet board and the right (black) area. – Dentastic Jun 02 '16 at 06:11

1 Answers1

3

I found my mistake and I'm not proud of it. It was a simple typo. With fixing this and the help of @Miki I'm able to find my colors.

My mistake:

Mat img; //img is given from previos code
Mat hsv = img.clone();
Imgproc.cvtColor(img, hsv, Imgproc.COLOR_BGR2HSV);    
Core.inRange(img, lowerBlue, upperBlue, hsv); //img

instead of

Mat img; //img is given from previos code
Mat hsv = img.clone();
Imgproc.cvtColor(img, hsv, Imgproc.COLOR_BGR2HSV);    
Core.inRange(hsv, lowerBlue, upperBlue, hsv); //hsv

In fact...I translated the image to HSV and took the RGB picture for inRange().

Thanks

Dentastic
  • 101
  • 1
  • 5