Question

I have been using opencv with python for two weeks. I want to find the center of a moving object.

I first find difference from two frame and bitwise bit. After bitwise it came out like a group of white dots around moving object(quad rotor) as you can see in picture below.

So my question is: What method should I use to find the center of a moving object(quad rotor) like in this case?

I thought maybe just find average center of all these dots but I don't know how to do it.

Here is sample picture

ss

ss2



Here is my code

import cv2

def diffImg(t0, t1, t2):
    d1 = cv2.absdiff(t2, t1)
    d2 = cv2.absdiff(t1, t0)
return cv2.bitwise_and(d1, d2)

cam = cv2.VideoCapture('track_outdoor.avi')

winName = "Movement Indicator"
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE)

# Read three images first:
t_minus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)


while True:
    diff_frame=  diffImg(t_minus, t, t_plus)

    cv2.imshow( 'Original', t_plus )
    cv2.imshow( winName, diff_frame )
    # Read next image
    t_minus = t
    t = t_plus
    t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)

    key = cv2.waitKey(10)
    if key == 27:
       cv2.destroyWindow(winName)
       break

Update #1

first of all thank dervish for answer

I just tried to apply method to find mass center it work but as dervish it not accurate as I need.

so I came up with another idea if I enchange brightness of diff_frame is it make more easy,accurate to using method like contours or other method?

Was it helpful?

Solution

You may apply few methods,

  • get the average of x coordinated of all points, and the same for y , but this way may be not as accurate as you need.
  • for the following we need to define the regular contours containing those points:

    • triangle => find center of triangle
    • circle => centre of circle or even rectangle.
    • for this way I invite you to see those opencv functions
  • if you could find irregular shape you may use moment to calculate the mass center: put your points in a variable, such vector<Points> contour; then do the following:

moments mom= moments(Mat(contour));
// find mass center
Point mc= Point(mom.m10/mom.m00,mom.m01/mom.m00);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top