【发布时间】:2018-11-22 14:25:45
【问题描述】:
我在 python 中使用 OpenCV 来制作一个解决魔方的程序。我不是在创建自己的求解算法,而是在使用 kociemba 的 python 实现。
无论如何,为了使用这个算法,我必须传递一个代表立方体颜色的有效字符串。所以,截至目前,我正在尝试检测魔方表面的颜色。
我试过了:
_, 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'
还有:
_, 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'
我不知道如何可靠地检查每件作品的颜色,因为光线总是在变化。我认为使用 inRange() 函数将是我所需要的,但由于某种原因它不起作用。而且每个范围都不同,所以我不知道有什么方法可以检查同一图像中的相同像素与所有像素。
说真的,任何帮助将不胜感激。提前致谢!
编辑:我现在还尝试了以下方法:
_, 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'
rgb 限制是使用我之前的 hsv 边界创建的。
这是具有所需输出的图像:
我有一张魔方的一张脸:
这应该会产生输出LDLLFLBRR,但它会产生输出EEEEEEEEE,表示所有错误,或“LULLFRFRR”,这意味着橙色被检测为红色,黄色被检测为白色,绿色偶尔被检测为蓝色。
我需要的是一种可靠的方法,始终能够分辨出哪种颜色是哪种颜色。
感谢您的帮助!
【问题讨论】:
-
也许您选择了错误的 HSV 范围。示例图像更好地理解您的问题。 stackoverflow.com/questions/47483951/… 和 stackoverflow.com/questions/10948589/…
-
根据您使用的 IDE,我建议使用 GDB 来验证您的像素显示的值;有了它,您还可以跟进 ausk 的建议,看看您的范围是否有意义。如果你不知道如何使用 GDB,有一个很棒的 Spyder 教程(并且通常易于使用的实现)。如果您不想这样做,您可以随时查看打印语句...
标签: python opencv colors color-detection