track.py 文件源码

python
阅读 41 收藏 0 点赞 0 评论 0

项目:DrosophilaCooperative 作者: avaccari 项目源码 文件源码
def trackObjects(self):
        for area in self.trackedAreasList:
            # Template matching
            gray = cv2.cvtColor(self.processedFrame, cv2.COLOR_BGR2GRAY)
            templ = area.getGrayStackAve()
            cc = cv2.matchTemplate(gray, templ, cv2.TM_CCOEFF_NORMED)
            cc = cc * cc * cc * cc
            _, cc = cv2.threshold(cc, 0.1, 0, cv2.THRESH_TOZERO)
            cc8 = cv2.normalize(cc, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
            mask = np.zeros_like(cc8)

            # Search match within template region
            mcorn = area.getEnlargedCorners(0) # If not 0, enalrge the search
            cv2.rectangle(mask, mcorn[0], mcorn[1], 255, -1)
            _, _, _, mx = cv2.minMaxLoc(cc8, mask)

#            kp = area.getKalmanPredict()
#            area.updateWindow(kp)
#            area.setTemplate(self.processedFrame)

            # Prevent large spatial jumps
            (c, r, _, _) = area.getcrwh()
            jump = 10
            if abs(c - mx[0]) < jump and abs(r - mx[1]) < jump:
#                area.setKalmanCorrect(mx)
                area.updateWindow(mx)
            else:
#                area.setKalmanCorrect((c, r))
                area.updateWindow((c, r))
            area.setTemplate(self.processedFrame)

            # Show the template stack
            if self.showTemplate is True:
                cv2.imshow('Stack: '+str(area), area.getStack())
            else:
                try:
                    cv2.destroyWindow('Stack: '+str(area))
                except:
                    pass

            # Show the matching results
            if self.showMatch is True:
                cv2.rectangle(cc8, mcorn[0], mcorn[1], 255, 1)
                cv2.circle(cc8, mx, 5, 255, 1)
                cv2.imshow('Match: '+str(area), cc8)
            else:
                try:
                    cv2.destroyWindow('Match: '+str(area))
                except:
                    pass

            # Draw the tracked area on the image
            corn = area.getCorners()
            cv2.rectangle(self.workingFrame,
                          corn[0], corn[1],
                          (0, 255, 0), 1)

#            self.showFrame()
#            raw_input('wait')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号