Ask Your Question
0

HSV range for Rubik's cube color

asked 2016-04-27 15:15:16 -0600

Shunpo gravatar image

Hello everyone ! I'm currently doing an algorithm that allow me to detect the color from a Rubik's cube.

For that, I use OpenCV like this :

    cap.read(frame_RGB);
    bilateralFilter(frame_RGB, filter, 9, 75, 75);
    cvtColor(filter, frame_HSV, cv::COLOR_BGR2HSV); // Change from RGB to HSV

    // For each color, detect if there is the color in the frame
    for (int i = 0; i < this->minColor.size(); i++){
        inRange(frame_HSV, this->minColor[i], this->maxColor[i], frame_threshed);
        threshold(frame_threshed, result, 127, 255, 0);
        findContours(result, contours, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE, cv::Point(0, 0));
    }

with

/*White - Green - Red - Blue - Orange - Yellow */
this->minColor = { Scalar(70, 10, 130), Scalar(60, 110, 110), Scalar(120, 120, 140), Scalar(100, 100, 100), Scalar(5, 150, 150), Scalar(20, 100, 100) };
this->maxColor = { Scalar(180, 110, 255), Scalar(100, 220, 250), Scalar(180, 250, 200), Scalar(130, 255, 255), Scalar(15, 235, 250), Scalar(40, 255, 255) };

My problem is that I'm not sure for the choose of colors (Scalar structure). So I would like to know if someone know how to determine the range of color for each rubik's color accurately..

Thanks a lot!

edit retag flag offensive close merge delete

Comments

this post is related to your question. maybe it will be useful.

sturkmen gravatar imagesturkmen ( 2016-04-28 06:12:46 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2016-04-28 06:02:41 -0600

pklab gravatar image

You have to look at Hue color wheel and remember that:

  • In OpenCV range for the Hue is: 0..180 (for 8bit images)
  • Red is close to 0 and close to 180
  • Coloured Light might change perceived color

See my previous answer for more details

Red = Hue(0...9) AND Hue(151..180)
Orange = Hue(10...15)
Yellow = Hue(16..45)
Green = Hue(46..100)
Blue = Hue(101..150)

In addiction to select only full color I would use Saturation and Brightness 128..255

White have Low saturation and high Brightness... let say

White = Saturation(0..20) AND Value(230..255)
edit flag offensive delete link more
0

answered 2016-04-28 06:06:08 -0600

kbarni gravatar image

Take a photo of the cube so that you can see all the colors. Cut the image in parts so that each part contains one square. Take the mean in the H ans S channels for each square. You'll have two means for each color (Hi and Si where i=1..6). As the luminosity channel depends on the illumination, it's better to forget it.

Then, for a given pixel p, get the distance between the color and the means you got earlier: di=dist(Hp,Hi;Sp,Si). The smallest distance will give you the color you are looking for.

You can make your method more robust by computing the variance on each color, too.

You can get the min and max values for each color with the same method; but it will be less robust because the noise.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-04-27 15:15:16 -0600

Seen: 8,356 times

Last updated: Apr 28 '16