Ask Your Question

mmt's profile - activity

2020-10-21 17:25:44 -0600 received badge  Popular Question (source)
2018-04-02 19:06:34 -0600 asked a question Can't get contours index array

Can't get contours index array Hello, I am trying to find the 2 biggest contours, but I am facing some problems. im2,

2015-07-28 01:30:31 -0600 asked a question How to do a mask or how to transform found contours to filled circles?

How to transform all the contours found in a frame to a filled circle?

Let me explain, my video has many small objects, and it is hard to detect using Mosse tracking, because my objects change its form. So I had an idea, change all the found contours to filled circles, it means, convert all this objects found on this image : http://imgur.com/GiPmatl,JGsJX2C to something like this : http://imgur.com/GiPmatl,JGsJX2C#1 .

I am using python.

2015-07-26 18:46:11 -0600 received badge  Student (source)
2015-07-25 01:24:14 -0600 asked a question How to keep tracking one object forever?

Hey.

I am trying to track ants, it is really hard because ants change its form and I am using grayscale image. It means, mosse and camshit didnt work on my project.

I have to track 1 special one(not a random), because I need to take its position and make some analysis. So I need a way to select one ant and track it forever, I tried everything, but nothing worked.

Well, I can set the the ant position manually, or by mouse, it is not a problem, the problem is how to keep tracking the same ant after know its first position.

I am using knn filter on my code to make it easier, because I will have all my ants white, and the background in black.

It is my code:

import numpy as np
import cv2

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

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

    def run(self):
        while True:
            f, orig_img = self.capture.read()

            if( not f ):
                break;

            fore = self.knn.apply( orig_img )
            back = self.knn.getBackgroundImage()
            kernel = np.ones((5,5),np.uint8)
            fore = cv2.erode( fore, kernel )
            fore = cv2.dilate( fore, kernel )
           # fore = fore[r:r+h, c:c+w]
            image, contours, hiearchy = cv2.findContours( fore, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE )



            maximumArea = 0
            bestContour = None
            for contour in contours:
                currentArea = cv2.contourArea(contour)
                if currentArea > maximumArea:
                    bestContour = contour
                    maximumArea = currentArea

                    x,y,w,h = cv2.boundingRect(bestContour)
                    cv2.rectangle(orig_img, (x,y),(x+w,y+h), (0,0,255), 3)        

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

            k = cv2.waitKey(24) & 0xff 
            print k
            if k == 27:
                break
if __name__ == "__main__":
    colour_tracker = ColourTracker()
    colour_tracker.run()
2015-07-22 03:35:58 -0600 commented question How to track white color on black background

Thank you for this information! I was going crazy with that code! I will try this one and update you! Thanks!

2015-07-22 02:54:34 -0600 asked a question How to track white color on black background

I have a code that doesn't works to white and black color.

On the code I select the ROI, and after that this code is supposed to keep tracking the ball, but it doesn't happen

I have this video : http://tinypic.com/r/2q1rk1g/8

and this code :

import numpy as np
import cv2

cap = cv2.VideoCapture('video.avi')


ret,frame = cap.read()


r,h,c,w = 490,50,895,50  # It is where the ball is.  
track_window = (c,r,w,h)
frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)

roi = frame[r:r+h, c:c+w]
hsv_roi =  cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_roi, np.array((0., 60.,32.)), np.array((180.,255.,255.)))
roi_hist = cv2.calcHist([hsv_roi],[0],mask,[180],[0,180])
cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)

term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )

while(1):
    ret ,frame = cap.read()

    if ret == True:
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)

        ret, track_window = cv2.meanShift(dst, track_window, term_crit)

        x,y,w,h = track_window
        img2 = cv2.rectangle(frame, (x,y), (x+w,y+h), 255,2)
        cv2.imshow('img2',img2)

        k = cv2.waitKey(60) & 0xff
        if k == 27:
            break
        else:
            cv2.imwrite(chr(k)+".jpg",img2)

    else:
        break

cv2.destroyAllWindows()
cap.release()

I have no clue about what should I do, I tried to convert from gray to bgr, but it didn't works.

2015-07-21 05:06:37 -0600 asked a question 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
2015-07-08 01:03:20 -0600 received badge  Enthusiast
2015-06-29 12:59:03 -0600 asked a question How to keep tracking the same object among others

What I have? I have a code that detects the biggest object in the video.

How do I do it? I am using knn filter, findControus, contourArea and boundingRect to detect and draw a rectangle around of the biggest object.

My problem: Sometimes my object changes the speed, then another object gets more 'background subtraction difference' and it misses the real target.

I was thinking how can I keep following only this guy. I tried to use a keyboard input to tell to my code who is the right guy, and then keep following it. But it didn't work.

The reason that I need to track only one is because I need to make an analysis based on its coordinate.

This is my code:

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)        
            print("The X, Y coordinate is: %s , %s  " % (x, y))
            f = open("hello.txt", "a")
            f.write("\n" "%s %s" % (x, y)) 
            f.close()
2015-06-19 15:14:32 -0600 asked a question How can I select 2 objects among a lot of the same object?

Hello everyone!

Do you all have any idea about how can I select only 2 objects among a lot of the same object?

To make clear my question, I will provide some details.

Imagine an video with 10 balls jumping, but you only need to know about 2 of them, and then take their coordinates.

How can I do that? I thought about use mouse event, but I do not know how I can apply it. Or identify all balls and show the number of them, so I can use my keyboard to select which one I need. But I do not know too.

What I have?

I have a video, which I applied knn filter to get only black and white. After this I used findContours and drawContours, so all I have is all balls "identified" and contoured.

Thanks in advance!

I use python to program, and it is required! :(