如何跟踪图像中想要的任何特定对象?

时间:2019-07-02 07:38:05

标签: python matlab opencv object-detection tracking

我想在没有任何对象检测算法(例如haar-cascade,faster-RCNN,SSD或YOLO)的情况下跟踪视频帧中所需的任何对象。 我怎样才能做到这一点?请给我推荐任何方法。

我使用的是OpenCV Tracking API,如https://www.pyimagesearch.com/2018/08/06/tracking-multiple-objects-with-opencv/

所示。

但是,我想制作更复杂的跟踪器。 开发人员可以向我推荐任何算法或其他API吗?

2 个答案:

答案 0 :(得分:0)

您提到的算法可能是最佳选择。 顺便说一句,如果您有一些想要查找的对象图像,则可以尝试使用

  

SIFT点(在Matlab中也称为SURF点)

给自己写一个探测器。

您可能会发现缩放比例和计算时间有些问题,但是我认为您可以尝试使用此技术。

答案 1 :(得分:0)

import numpy as np
import cv2
import copy

class selectROI():

    def __init__(self):
        self.refPt = []
        self.clickEventsEnabled = False
        self.drawingRectangle = False
        self.rectangleDrawn = False
        self.x_start = -1
        self.y_start = -1
        self.ref_frame = None

    def clickPolygonPoints(self, event, x, y, flags, param):

        temp_frame = self.ref_frame.copy()

        if(self.clickEventsEnabled == True):
                if event == cv2.EVENT_LBUTTONDOWN:
                        if((self.rectangleDrawn == False)):
                            self.drawingRectangle = True
                            self.x_start,self.y_start = x,y
                            self.refPt.append((x,y))

                elif event ==  cv2.EVENT_MOUSEMOVE: 
                        if(self.drawingRectangle == True):
                            cv2.rectangle(temp_frame, (self.x_start,self.y_start), (x,y), (0,0,255), 2)
                            cv2.imshow("ref_frame", temp_frame)
                            temp_frame = self.ref_frame

                elif event == cv2.EVENT_LBUTTONUP:
                        if((self.rectangleDrawn == False)):
                            self.drawingRectangle = False
                            self.rectangleDrawn = True
                            cv2.rectangle(self.ref_frame, (self.x_start, self.y_start), (x,y), (0,0,255), 2)
                            self.refPt.append((self.x_start, self.y_start))
                            self.refPt.append((x, self.y_start))
                            self.refPt.append((x, y))
                            self.refPt.append((self.x_start, y))

                            roiPoints = [(self.x_start, self.y_start), (x, y)]

                            if len(roiPoints) == 2: #when two points were found
                                    roi = self.ref_frame[roiPoints[0][1]:roiPoints[1][1], roiPoints[0][0]:roiPoints[1][0]]
                                    cv2.imshow("Cropped", roi)


    def maskImg_WithROI(self, frame, ROIPointsList):
            pointsArray = np.array(ROIPointsList)
            mask = np.zeros(frame.shape, dtype=np.uint8)
            white = (255,255,255)
            cv2.fillPoly(mask, np.int32([pointsArray]), white)
            maskedImage = cv2.bitwise_and(frame, mask)
            return maskedImage  

    def outputROIMask(self, frame, ROIPointsList):
            pointsArray = np.array(ROIPointsList)
            pointsArray = pointsArray.reshape((-1,1,2))
            mask = np.zeros(frame.shape, dtype=np.uint8)
            white = (255,255,255)
            cv2.fillPoly(mask, np.int32([pointsArray]), white)
            return mask  

    def main(self):

            self.clickEventsEnabled = True

            self.ref_frame = cv2.imread('picture.png')
            originalRef_Frame = self.ref_frame.copy()

            cv2.namedWindow("ref_frame")
            cv2.setMouseCallback("ref_frame", self.clickPolygonPoints)

            while True:

                    cv2.imshow("ref_frame", self.ref_frame)

                    key = cv2.waitKey(0) & 0xFF

                    if key == ord("r"):
                         self.ref_frame = originalRef_Frame.copy()
                         self.refPt = []
                         self.drawingRectangle = False
                         self.rectangleDrawn = False
                         self.x_start, self.y_start = -1,-1

                    elif key == ord("p"):
                         self.clickEventsEnabled = False
                         self.ref_frame = originalRef_Frame.copy()
                         self.drawingRectangle = False
                         self.rectangleDrawn = False
                         break

                    elif key == ord("q"):
                            break

            self.ref_frame = self.maskImg_WithROI(self.ref_frame, self.refPt)
            cv2.imshow("ref_frame", self.ref_frame)

            roiMask = self.outputROIMask(self.ref_frame, self.refPt)
            cv2.imwrite("ROI.jpg", roiMask)

            cv2.waitKey(0)
            cv2.destroyAllWindows()

if __name__ == '__main__':
    selROI = selectROI()
    selROI.main()`