【发布时间】:2017-08-30 12:44:44
【问题描述】:
我在我的 Raspberry Pi 中使用代码来跟踪带有红色圆圈的蓝色对象。我想逐行改变圆圈。我在 opencv 文档中看到我必须将初始坐标和最终坐标放入函数中以绘制线条。我怎样才能确定绘制它的点?我什至不知道我该如何检测。一些想法? 按照代码:
# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import numpy as np
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 50
camera.hflip = True
rawCapture = PiRGBArray(camera, size=(640, 480))
# allow the camera to warmup
time.sleep(0.1)
# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
# grab the raw NumPy array representing the image, then initialize the timestamp
# and occupied/unoccupied text
image = frame.array
blur = cv2.blur(image, (3,3))
#hsv to complicate things, or stick with BGR
#hsv = cv2.cvtColor(blur,cv2.COLOR_BGR2HSV)
#thresh = cv2.inRange(hsv,np.array((0, 200, 200)), np.array((20, 255, 255)))
lower = np.array([76,31,4],dtype="uint8")
#upper = np.array([225,88,50], dtype="uint8")
upper = np.array([210,90,70], dtype="uint8")
thresh = cv2.inRange(blur, lower, upper)
thresh2 = thresh.copy()
# find contours in the threshold image
image, contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
# finding contour with maximum area and store it as best_cnt
max_area = 0
best_cnt = 1
for cnt in contours:
area = cv2.contourArea(cnt)
if area > max_area:
max_area = area
best_cnt = cnt
# finding centroids of best_cnt and draw a circle there
M = cv2.moments(best_cnt)
cx,cy = int(M['m10']/M['m00']), int(M['m01']/M['m00'])
#if best_cnt>1:
cv2.circle(blur,(cx,cy),10,(0,0,255),-1)
# show the frame
cv2.imshow("Frame", blur)
#cv2.imshow('thresh',thresh2)
key = cv2.waitKey(1) & 0xFF
【问题讨论】:
-
看看这个球跟踪算法。它显示围绕球的圆圈和轨迹:pyimagesearch.com/2015/09/14/ball-tracking-with-opencv
-
我正在尝试识别一个蓝色物体,我想跟随物体画线的轨迹
-
所以只需将 HSV 颜色掩码更改为蓝色设置,并使用此链接中的算法?也许有些部分需要修改,但它是同一种线吗?
标签: python opencv raspberry-pi tracking