Search code examples
pythonopencvcoordinatesphysicsframes

Kinematic study application. Python and opencv


Im trying to develop a simple code using opencv and python. My idea is the following: I have a video with a moving object(free falling or parabolic) and I've managed to separate the video in frames. What I need (and I'm a total newby in this aaaand have little time) is to extract coordinates of the object frame by frame. So the idea is to click on the moving object, and get the (x, y) coordinate and the number of frame and open the next frame so I can do the same. So basically something with clicks over the foto and extracting the data in a csv and showing the next frame. Thus I could study its movement through space with its velocity and acelerations and stuff. Haven't written any code yet. Thanks in advance.


Solution

  • Look at docs example with using mouse input in opencv w python:

    mouse example - opencv You can define callback reading the click coordinates:

    def get_clic_point(event,x,y,flags,param):
        if event == cv.EVENT_LBUTTONDBLCLK: # on left double click
            print(x,y) # here goes Your sepcific code, using x, y as click coordinates
    

    In main loop, You need to create window and supply it with callback method

    cv.namedWindow('main_win')
    cv.setMouseCallback('main_win',get_clic_point)
    

    Then using window name (in this case 'main_win') as Your window handle, You can show image, calling cv.imshow('main_win',img), where img is loaded image.

    You can write simple loop like this:

    cv.namedWindow('main_win')
    cv.setMouseCallback('main_win',get_clic_point)
    
    images = []
    # read images to this list
    # ...
    i = 0
    
    while(1):
        cv.imshow('main_win',images[i])
        k = cv.waitKey(1) & 0xFF
        if k == ord('n'): # n for next
            # change image
            i = i + 1
            i = i % len(images)
        elif k == 27:
            break
    cv.destroyAllWindows()
    

    then just substitiute call back with desired logic