Dentastic
Dentastic

Reputation: 101

Get right HSV color inRange in OpenCV (Java)

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

Upvotes: 3

Views: 5722

Answers (1)

Dentastic
Dentastic

Reputation: 101

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

Upvotes: 3

Related Questions