如何在python中创建可靠的perler珠子颜色检测?
我正在尝试对珠子进行颜色检测。硬件在工作,但软件部分我在努力。我正在使用Picamera和树莓皮a+、opencv和Python 3 这就是我到目前为止所做的:如何在python中创建可靠的perler珠子颜色检测?,python,opencv,colors,detection,Python,Opencv,Colors,Detection,我正在尝试对珠子进行颜色检测。硬件在工作,但软件部分我在努力。我正在使用Picamera和树莓皮a+、opencv和Python 3 这就是我到目前为止所做的: from picamera.array import PiRGBArray from picamera import PiCamera import numpy as np import time import cv2 import serial ser = serial.Serial( port='/dev/ttyS
from picamera.array import PiRGBArray
from picamera import PiCamera
import numpy as np
import time
import cv2
import serial
ser = serial.Serial(
port='/dev/ttyS0', #Replace ttyS0 with ttyAM0 for Pi1,Pi2,Pi0
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (1312, 976)
camera.framerate = 15
camera.saturation = 0
camera.exposure_compensation = 10
camera.brightness = 50
camera.exposure_mode = 'auto'
rawCapture = PiRGBArray(camera, size=(1312, 976))
# allow the camera to warmup
time.sleep(0.1)
# time variable
myTime = 0.25
#Enable motor control
print ("Enable Motors")
ser.write(str.encode('y'))
time.sleep(5)
ser.write(str.encode('hl'))
time.sleep(20)
#define color arrays
colors = [ [(162, 192, 118),(182, 212, 198),'Red'],
[(105, 217, 158),(125, 237, 238),'Blue'],
[(16, 173, 139),(36, 193, 220),'Yellow'],
[(120, 164, 170),(140, 184, 250),'Violett'],
[(63, 245, 45),(83, 265, 125),'Green'],
[(-10, 165, 148),(10, 185, 228),'Orange'],
[(151, 70, 28),(170, 119, 108),'Brown'],
[(150, 197, 218),(165, 238, 248),'Pink'],
[(0, 79, 37),(179, 143, 73),'Black'],
[(44, 109, 115),(64, 132, 200),'Lime_Green'],
[(72, 109, 71),(92, 129, 151),'Mint_Green'],
[(96, 186, 177),(116, 206, 257),'Light_Blue'],
[(107, 227, 99),(127, 247, 179),'Dark_Blue']]
threshold = 5000
#find color function
def findColor(input_image):
img = cv2.imread(input_image,1)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
for color in colors:
low_val, high_val, label = color
mask = cv2.inRange(hsv, low_val, high_val)
if np.sum(mask) > threshold:
return label
return 'undefined' # or a label for 'color not found'
#loop
for x in range(0,500):
camera.capture(rawCapture, format="bgr")
image = rawCapture.array
crop_img = image[350:1000,320:990]
cv2.imwrite('bead.jpg',crop_img)
cv2.imshow("Color",crop_img)
key = cv2.waitKey(1) & 0xFF
rawCapture.truncate(0)
print (findColor('bead.jpg'))
#No color detected
if findColor('bead.jpg') == 'undefined':
ser.write(str.encode('0'))
time.sleep(myTime)
ser.write(str.encode('r'))
#Red detected
if findColor('bead.jpg') == 'Red':
ser.write(str.encode('1'))
time.sleep(myTime)
ser.write(str.encode('r'))
#Blue detected
if findColor('bead.jpg') == 'Blue':
ser.write(str.encode('2'))
time.sleep(myTime)
ser.write(str.encode('r'))
#Yellow detected
if findColor('bead.jpg') == 'Yellow':
ser.write(str.encode('3'))
time.sleep(myTime)
ser.write(str.encode('r'))
#Violett detected
if findColor('bead.jpg') == 'Violett':
ser.write(str.encode('4'))
time.sleep(myTime)
ser.write(str.encode('r'))
#Green detected
if findColor('bead.jpg') == 'Green':
ser.write(str.encode('5'))
time.sleep(myTime)
ser.write(str.encode('r'))
#Orange detected
if findColor('bead.jpg') == 'Orange':
ser.write(str.encode('6'))
time.sleep(myTime)
ser.write(str.encode('r'))
#Brown detected
if findColor('bead.jpg') == 'Brown':
ser.write(str.encode('7'))
time.sleep(myTime)
ser.write(str.encode('r'))
#Pink detected
if findColor('bead.jpg') == 'Pink':
ser.write(str.encode('8'))
time.sleep(myTime)
ser.write(str.encode('r'))
#Black detected
if findColor('bead.jpg') == 'Black':
ser.write(str.encode('9'))
time.sleep(myTime)
ser.write(str.encode('r'))
#Mint Green detected
if findColor('bead.jpg') == 'Mint_Green':
ser.write(str.encode('a'))
time.sleep(myTime)
ser.write(str.encode('r'))
#Lime_Green detected
if findColor('bead.jpg') == 'Lime_Green':
ser.write(str.encode('b'))
time.sleep(myTime)
ser.write(str.encode('r'))
#Light_Blue detected
if findColor('bead.jpg') == 'Light_Blue':
ser.write(str.encode('c'))
time.sleep(myTime)
ser.write(str.encode('r'))
#Dark_Blue detected
if findColor('bead.jpg') == 'Dark_Blue':
ser.write(str.encode('d'))
time.sleep(myTime)
ser.write(str.encode('r'))
#end color detection
time.sleep(myTime)
首先,我为每个perler珠定义了HSV。在每个循环中,我拍摄一张照片,并使用“finColor”功能检查hsv范围。但这还不够可靠。有时棕色的珠子被认为是黑色的。我的代码在某种程度上可以工作,但没有真正优化。我不是一个好的python程序员,我想问一下,是否有人可以给我一些建议,优化我的代码,以创建一个可靠的颜色检测。欢迎任何帮助。我在这里上传了我用不同颜色制作的所有图像:
提前谢谢
凯