Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Changing the ROI position to keep tracking the same object

I am trying to tracking only one specific object among a lot of the same objects. I didn't find solution, then I thought that I could select a ROI using mouseEvent, then I could make the ROI follow the object and track it. It means, I select a square around the object, my program finds the center of this object, then my ROI move its center to the same center as the object, so on the next frame the object will move a lit bit again, then my ROI will move too.

I tried to do it, but it is not working, I think that I have some coding problems. PS: I didnt code the mouseEvent yet, then I used a random value to start x and y.

Errors: OpenCV Error: Assertion failed (m.dims >= 2) in cv::Mat::Mat, file C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\core\src\matrix.cpp, line 441 Traceback (most recent call last): File "C:\Users\thiagomm\workspace\MySweetInsects\hueheuheu.py", line 58, in <module> colour_tracker.run() File "C:\Users\thiagomm\workspace\MySweetInsects\hueheuheu.py", line 28, in run fore = cv2.erode( thresh, kernel ) cv2.error: C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\core\src\matrix.cpp:441: error: (-215) m.dims >= 2 in function cv::Mat::Mat

class ColourTracker:
    def __init__(self):
        cv2.namedWindow("Background")
        cv2.namedWindow("Frame")

        self.capture = cv2.VideoCapture('oieee.avi')
        self.knn = cv2.createBackgroundSubtractorKNN()

    def run(self):
        x = 700 # I'm not sure if these variables are at the right place
        y = 500
        while True:
            f, orig_img = self.capture.read()
            if( not f ):
                break;
            fore = self.knn.apply( orig_img )
            back = self.knn.getBackgroundImage()

            roi1 = orig_img[x:x+100, y:y+100]
            mask1 = cv2.cvtColor(roi1, cv2.COLOR_BGR2GRAY)
            gray_blur = cv2.GaussianBlur(mask1, (15, 15), 0)
            thresh = cv2.adaptiveThreshold(gray_blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 1)            
            kernel = np.ones((5,5),np.uint8)          
            fore = cv2.erode( thresh, kernel )
            fore = cv2.dilate( fore, kernel )
            image, contours, hiearchy = cv2.findContours( fore, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE )
            #cv2.drawContours(orig_img, contours, -1, (0, 0, 255), 2)
            maximumArea = 0
            bestContour = None
            for contour in contours:
                currentArea = cv2.contourArea(contour)
                if currentArea > maximumArea:
                    bestContour = contour
                    maximumArea = currentArea
            if bestContour is not None:
                x,y,w,h = cv2.boundingRect(bestContour)
                cv2.rectangle(orig_img, (x,y),(x+w,y+h), (0,0,255), 3)                        
                x = x-10
                y = y-10

            cv2.imshow( "Background", back )
            cv2.imshow( "Frame", orig_img )

            k = cv2.waitKey(24) & 0xff 
            print k
            if k == 27:
                break