【问题标题】:How can I draw a tacking line on opencv如何在opencv上画一条定位线
【发布时间】: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


【解决方案1】:

如果你想要沿着蓝色对象的路径画一条线,只需在你的帧上从当前帧的 (cx,cy) 到前一帧的位置画一条线。进行此更改-

# 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)


blank=np.zeros((640,480,3),dtype=np.uint8)
ocx=0  #initializing old centres
ocy=0  # this will draw a line from (0,0) to centre in the beginning
       # you can draw lines from second frame if you want to correct this


# 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.line(blank,(ocx,ocy),(cx,cy),(255,255,255),5)
        out=cv2.add(blank,blur)
        ocx=cx
        ocy=cy

        # show the frame
        cv2.imshow("Frame", out)
        #cv2.imshow('thresh',thresh2)
        key = cv2.waitKey(1) & 0xFF

【讨论】:

  • 不像在每一帧上画一个圆圈,如果你想在一帧上保留线条,只需在while循环之前制作一个空白图像,然后在上面画线。如果你想在你的框架上添加线条,然后添加空白和框架,如果这是我的想法,我会相应地更改代码
  • 感谢@I.Newton。我的想法是保留框架上的线条以显示跟踪。一件事: While(1) 我把它放在我的代码循环之前?当您说“在while循环之前制作一个空白图像然后在其上画线”时,我需要在while循环之前使用“blur = cv2.(image,(3,3)) and cv2.line”?跨度>
  • 抱歉,我之前没有看到 for 循环。添加了带有更改的完整代码。
  • 谢谢。我已经测试了这段代码,编译器在 out = cv2.add(blank, blur) 的第 73 行向我显示了一个错误。错误:-209。该操作既不是“数组到数组”(其中数组具有相同的大小和相同的通道数)。我要更改数组空白的大小吗?
  • 我尝试只打印“空白”,但出现错误:PiCameraValueError: Incorrect buffer length for resolution 640x480
猜你喜欢
  • 2013-09-09
  • 1970-01-01
  • 2020-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-01
  • 2013-09-21
相关资源
最近更新 更多