我正在尝试制作一个赛车机器人,它将停在终点线(洋红色颜色)。我试图通过将图像转换为hsv,然后屏蔽它,但它没有按预期工作。你的帮助会非常有用。
我写的代码是:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while(True):
ret,frame=cap.read()
blurredimg = cv2.GaussianBlur(frame,(11,11),0)
imghsv=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
lowermagenta= np.array([0,50,50])
uppermagenta= np.array([10,255,255])
mask=cv2.inRange(imghsv,lowermagenta,uppermagenta)
outputimg=frame.copy()
outputimg[np.where(mask==0)]=0
grayedimg = cv2.cvtColor(outputimg , cv2.COLOR_BGR2GRAY)
cannyimg=cv2.Canny(grayedimg,75,150)
ret,thresh = cv2.threshold(cannyimg,127,255,0)
image , contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
finallimg= cv2.drawContours(frame,contours, -1,(0,255,0),35)
cv2.imshow('hyg' , finallimg)
cv2.imshow('hyg1' , cannyimg)
for x in range (0,480):
red,green,blue=finallimg[x,150]
if red==0 and green==255 and blue==0 :
print('magenta line found')
break