I am using OpenCV in python to make a program that solves a Rubik's Cube. I am not creating my own solving algorithm, but I am using the python implementation of kociemba.
Anyways, in order to use this algorithm, I have to pass a valid string that represents the colors of the cube. So, as of right now, I am trying to detect the colors of the faces of the Rubik's Cube.
I have tried:
_, img = self.cap.read()
print('frame captured!')
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
#scan each pixel in the list
for a in self.pixelsToScan:
hue = hsv[a[1],a[0],0]
print(hue)
sat = hsv[a[1],a[0],1]/255
print(sat)
val = hsv[a[1],a[0],2]/255
print(val)
#CHANGE COLOR CHECKING TO USE inRange()#
if sat<.15 and val>.85:
#WHITE
self.colors = self.colors + 'U'
elif hue<7 or hue>=173:
#RED
self.colors = self.colors + 'F'
elif hue>=7 and hue<22:
#ORANGE
self.colors = self.colors + 'B'
elif hue>=22 and hue<37:
#YELLOW
self.colors = self.colors + 'D'
elif hue>=37 and hue<67:
#GREEN
self.colors = self.colors + 'L'
elif hue>=67 and hue<135:
#BLUE
self.colors = self.colors + 'R'
else:
#BROKEN
self.colors = self.colors + 'E'
As well as:
_, img = self.cap.read()
print('frame captured!')
#scan each pixel in the list
for a in self.pixelsToScan:
if cv.inRange(numpy.copy(img), self.WHITE_MIN, self.WHITE_MAX)[a[1],a[0]]==255:
#WHITE
self.colors = self.colors + 'U'
elif cv.inRange(numpy.copy(img), self.RED_LOWER_MIN, self.RED_LOWER_MAX)[a[1],a[0]]==255 or cv.inRange(numpy.copy(img), self.RED_UPPER_MIN, self.RED_UPPER_MAX)[a[1],a[0]]==255:
#RED
self.colors = self.colors + 'F'
elif cv.inRange(numpy.copy(img), self.ORANGE_MIN, self.ORANGE_MAX)[a[1],a[0]]==255:
#ORANGE
self.colors = self.colors + 'B'
elif cv.inRange(numpy.copy(img), self.YELLOW_MIN, self.YELLOW_MAX)[a[1],a[0]]==255:
#YELLOW
self.colors = self.colors + 'D'
elif cv.inRange(numpy.copy(img), self.GREEN_MIN, self.GREEN_MAX)[a[1],a[0]]==255:
#GREEN
self.colors = self.colors + 'L'
elif cv.inRange(numpy.copy(img), self.BLUE_MIN, self.BLUE_MAX)[a[1],a[0]]==255:
#BLUE
self.colors = self.colors + 'R'
else:
#BROKEN
self.colors = self.colors + 'E'
I have no idea how I would reliably check what color each piece is, since lighting is always changing. I thought using the inRange()
functions would be what I needed, but it just doesn't work for some reason. And eachrange is different, so I don't know of any way to check the same pixel from the same image against all of them.
Seriously, any help would be greatly appreciated. Thanks in advance!
EDIT: I have now also tried the following:
_, img = self.cap.read()
print('frame captured!')
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
#scan each pixel in the list
for a in self.pixelsToScan:
r = img[a[1],a[0],2]
g = img[a[1],a[0],1]
b = img[a[1],a[0],0]
if r>220 and g>220 and b>220:
#WHITE
self.colors = self.colors + 'U'
elif r>=175 and g<=60 and b<=60:
#RED
self.colors = self.colors + 'F'
elif r>=175 and g>=96 and g<=171 and b<=54:
#ORANGE
self.colors = self.colors + 'B'
elif r>=205 and r <= 213 and g>=175 and b<=41:
#YELLOW
self.colors = self.colors + 'D'
elif r<=96 and g>=175 and b<=128:
#GREEN
self.colors = self.colors + 'L'
elif r>=54 and r<=85 and g<=116 and b >=179:
#BLUE
self.colors = self.colors + 'R'
else:
#BROKEN
self.colors = self.colors + 'E'
The rgb limits were created using my hsv boundaries from before.
Here is an image with the desired output:
I have this image of one face of a Rubik’s Cube:
This should produce the output LDLLFLBRR
but it will either produce an output of EEEEEEEEE
, representing all errors, or `LULLFRFRR’, meaning that orange is detected as red, yellow as white, and green occasionally as blue.
What I need is a reliable way to always be able to tell which color is which.
Thanks for any help!