"PYTHON+OPENCV Practice" a color-based object tracking (lower) __python

Source: Internet
Author: User
Tags abs

In this paper, "PYTHON+OPENCV practice" A, color-based object tracking (I.)

The functional enhancements were made as follows:

(1) The addition of PTS emptying, that is, when the target is not detected, empty PTS, showing no longer traces of the image;

(2) The direction of motion and the current coordinate can be judged by the addition of the movement directions.

From collections import deque import NumPy as NP import time #import imutils import Cv2 #设定红色阈值, HSV space redlower = Np.array ([170, MB]) Redupper = Np.array ([179, 255, 255]) #初始化追踪点的列表 Mybuffer = pts = deque (maxlen=mybuffer) counter = 0 # Open the camera camera = Cv2.
    Videocapture (0) #等待两秒 time.sleep (3) #遍历每一帧 to detect a red cap while True: #读取帧 (ret, frame) = Camera.read () #判断是否成功打开摄像头 If not ret:print ' No Camera ' break #frame = Imutils.resize (frame, width=600) #转到HSV空间 HSV = Cv2.cvtcolor (frame, cv2. COLOR_BGR2HSV) #根据阈值构建掩膜 mask = Cv2.inrange (HSV, Redlower, redupper) #腐蚀操作 mask = Cv2.erode (Mask, None, it erations=2) #膨胀操作, in fact, the first corrosion and expansion of the effect is open operation, remove noise mask = cv2.dilate (Mask, None, iterations=2) CNTs = cv2.findcontours (mas K.copy (), Cv2. Retr_external, Cv2.
        Chain_approx_simple) [-2] #初始化瓶盖圆形轮廓质心 Center = None #如果存在轮廓 If len (CNTs) > 0: #找到面积最大的轮廓
      c = max (CNTs, key = Cv2.contourarea) #确定面积最大的轮廓的外接圆  ((x, y), radius) = Cv2.minenclosingcircle (c) #计算轮廓的矩 M = Cv2.moments (c) #计算质心 Center = ( Int (m["M10"]/m["m00"]), int (m["m01"]/m["m00"]) #只有当半径大于10时 to perform paint if radius > 10:CV2.CIRCL
            E (frame, (int (x), int (y)), int (RADIUS), (0, 255, 255), 2) cv2.circle (frame, center, 5, (0, 0, 255),-1)
        #把质心添加到pts中, and is added to the left Pts.appendleft (center) Else: #如果图像中没有检测到瓶盖, the PTS is emptied and the trajectory is not displayed on the image.
        Pts.clear () for I-xrange (1, Len (pts)): If pts[i-1] is ' None ' or pts[i ' is none:continue #计算所画小线段的粗细 thickness = Int (np.sqrt (mybuffer/float (i + 1)) * 2.5) #画出小线段 Cv2.line (frame,
            Pts[i-1], Pts[i], (0, 0, 255), thickness) #判断移动方向 if counter >= ten and i = 1 and Len (pts) >= 10:
            
            DX = pts[-10][0]-pts[i][0] DY = pts[-10][1]-pts[i][1] (DirX, DirY) = ("", "") If Np.abs (DX) > 20:dirx = "East" if Np.sign (dX) = = 1 Else ' West ' if Np.abs (DY) > 20:
                DirY = "North" if Np.sign (dY) = = 1 Else "South" if DirX!= "" and DirY!= "": Direction = "{}-{}". Format (DirY, DirX) else:direction = DirX if DirX!= "" Else di RY Cv2.puttext (frame, direction, (m), Cv2. Font_hershey_simplex, 2, (0, 255, 0), 3) Cv2.puttext (frame, "DX: {}, dy: {}". Format (d X, DY), (Frame.shape[0]-ten), Cv2. Font_hershey_simplex, 0.35, (0, 0, 255), 1 cv2.imshow (' frame ', frame) #键盘检测, detect Esc key Exit K = Cv2.wa Itkey (1) &0xff counter + + 1 if k = = 27:break #摄像头释放 camera.release () #销毁所有窗口 cv2.destroyallwindows ()

Because the video is mirrored, the south-east result on the picture is correct.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.