0

I have created a colour detector for a Rubik's Cube using OpenCV. My plan for this it will then output the solution to the users. So far it detects some colours using OpenCV when the cube is held in the webcam, however some of the colours detect wrongly. There could be a few factors causing this such as lighting or even just the code however so far I've been unable to solve this. As you can see in the image below, the cubies are appearing in the wrong colours and i'm at a loss as to why.

Cube

Any help would be appreciated. Here is the code that I use to detect the colours.

import numpy as np
import cv2

    bounds = {
        "red" : (np.array([160, 75, 75]), np.array([180, 255, 255])),
        "blue" : (np.array([110, 75, 75]), np.array([130, 255, 255])),
        "green" : (np.array([35, 0, 0]), np.array([85, 255, 255])),
        "yellow" : (np.array([20, 75, 75]), np.array([40, 255, 255])),
        "white" : (np.array([0, 0, 20]), np.array([180, 30, 255])),
        "orange" : (np.array([10,100,100]), np.array([20, 255, 255]))
    }
    
    def density(img, color):
        lower = bounds[color][0]
        upper = bounds[color][1]
        mask = cv2.inRange(img, lower, upper)
        return np.sum(mask)/255
    
    def cubestr(data):
        ret = ""
        for i in "URFDLB":
            ret += "".join(data[i])
        for i in "URFDLB":
            ret = ret.replace(data[i][4], i)
        return ret

If my question is missing any details please let me know. Thanks.

Emmett
  • 1
  • Since this is more of a conceptual issue rather than a specific problem in the code, seemingly, you might find more help on one of these other StackExchange sites listed here, which are all related to Machine Learning: https://meta.stackexchange.com/a/254090/858527 – Random Davis Apr 27 '22 at 18:51
  • Your ranges are wrong. The red square in your image is (255,83,70), but you don't have the red range include full red, and you have the "white" range nearly black. Are you sure you're using RGB and not CMYK? – Tim Roberts Apr 27 '22 at 18:59
  • ahhhh that may be the issue then, i'll have another look at my code to double check. thanks for the help. Is there any ranges/websites you'd suggest for this? – Emmett Apr 27 '22 at 19:06
  • Write a code to extract the required value from it – Parthiban Marimuthu Apr 27 '22 at 19:08
  • like this https://i.stack.imgur.com/KjSVS.png – Parthiban Marimuthu Apr 27 '22 at 19:17

0 Answers0