Ask Your Question
0

Draw a rectangle on track vehicles

asked 2018-11-11 10:08:51 -0500

WoFFeN88 gravatar image

updated 2018-11-11 11:36:09 -0500

Im working in a little program who have the capacity to track vehicles. Now I can track but I want to draw a rectangle over each car. How can I do it? Could some one lend me a hand or give me some advice. Thank you so much in advice team.

Here my results:

image description image description

This is my track code.

import numpy as np
import cv2

videosrc = '../TFG-Vehicles/PruebasProyecto/video/road.mp4'
captura1 = cv2.VideoCapture(videosrc)
font = cv2.FONT_HERSHEY_SIMPLEX

shiTo_params = dict( maxCorners = 100,
                       qualityLevel = 0.3,
                       minDistance = 7,
                       blockSize = 7 )

lk_params = dict( winSize  = (15,15),
                  maxLevel = 2,
                  criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))

color = np.random.randint(0,255,(100,4))

ret, firstFrame = captura1.read()
firstGray = cv2.cvtColor(firstFrame, cv2.COLOR_BGR2GRAY)
blurG = cv2.GaussianBlur(firstGray, (21, 21), 0)
while (1):
    cornersFrame = cv2.goodFeaturesToTrack(blurG, mask=None, **shiTo_params)
    ret, frame = captura1.read()
    grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    p_new = np.zeros_like(cornersFrame)
    p_new, status, error = cv2.calcOpticalFlowPyrLK(blurG,grayFrame, cornersFrame, p_new, **lk_params )

    good_new = p_new[status==1]
    good_old = cornersFrame[status==1]

    mask = np.zeros_like(frame)

    for i,(new,old) in enumerate(zip(good_new,good_old)):
        a,b = new.ravel()
        c,d = old.ravel()

        cv2.line(mask, (a,b),(c,d),color[i].tolist(),2)
        cv2.circle(frame,(int(a),int (b)),4,(255,0,0),1)        

    lastFrame = cv2.add(frame,mask)    
    cv2.imshow('frame', lastFrame)
    blurG = grayFrame.copy()
    if cv2.waitKey(1) & 0xFF == ord('q') : 
        print ("PROGRAM CLOSED SUCCESSFULLY")
        break

captura1.release()
cv2.destroyAllWindows()
edit retag flag offensive close merge delete

Comments

nice attempt, but we have realtracking (needs contrib modules, though !)

Now I can track

can you ? all it does is sparse optflow, so far ..

berak gravatar imageberak ( 2018-11-11 10:19:14 -0500 )edit

I do not know what you want to tell me... :S

WoFFeN88 gravatar imageWoFFeN88 ( 2018-11-11 10:24:12 -0500 )edit
  • update your cv2 to latest pip install opencv-contrib-python
  • try sample ;)
  • don't try to "roll-your-own" tracking solution, unless you've read all relevant papers here ;) (really !)
berak gravatar imageberak ( 2018-11-11 10:29:28 -0500 )edit

I want to do my own track solution mate.. And I want to put a rectangle tracking my vehicles. I want to learn :) Could you help me with it? Thank you for your advice appreciate that

WoFFeN88 gravatar imageWoFFeN88 ( 2018-11-11 10:34:41 -0500 )edit

well, what is really "your own", here ?

apart from that, -- as long as you're lacking any "domain knowledge", it won't fly.

berak gravatar imageberak ( 2018-11-11 10:43:06 -0500 )edit

btw, is your camera moving, too, or fixed to some place ?

berak gravatar imageberak ( 2018-11-11 10:49:08 -0500 )edit

I just try to learn by examples of internet and do it something that works. It's obvious that i'm not a master-python but, I want to create a little project who can track vehicles and then count it. I just go step by step...Thanks mate.

WoFFeN88 gravatar imageWoFFeN88 ( 2018-11-11 10:50:32 -0500 )edit

Moving that's why I use optical flow

WoFFeN88 gravatar imageWoFFeN88 ( 2018-11-11 10:50:56 -0500 )edit

interesting ;)

if it's so, you could try to get a homography from the 2 point sets, and do a backprojection, -- the outliers are the moving things (against the camera's movement, -- well, more or less..) ! see e.g. here

berak gravatar imageberak ( 2018-11-11 10:56:22 -0500 )edit

Thank you first of all I try to draw a rectangles over the vehicle.. :( I'll try to follow your steps- thank you.

WoFFeN88 gravatar imageWoFFeN88 ( 2018-11-11 11:00:32 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-11-13 06:31:32 -0500

supra56 gravatar image

I used Lucas kanade and mouseevent for Raspberry pi 3b+, kernel 4.14.80, Debian Stretch 9. I used circle instead of rectangle Try this:

import cv2
import numpy as np

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

# Create old frame
_, frame = cap.read()
old_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Lucas kanade params
lk_params = dict(winSize = (15, 15), maxLevel = 4, criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))

# Mouse function
def select_point(event, x, y, flags, params):
    global point, point_selected, old_points
    if event == cv2.EVENT_LBUTTONDOWN:
        point = (x, y)
        point_selected = True
        old_points = np.array([[x, y]], dtype=np.float32)

cv2.namedWindow("Frame")
cv2.setMouseCallback("Frame", select_point)

point_selected = False
point = ()
old_points = np.array([[]])
while True:
    _, frame = cap.read()
    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    if point_selected is True:
        cv2.circle(frame, point, 5, (0, 0, 255), 2)

        new_points, status, error = cv2.calcOpticalFlowPyrLK(old_gray, gray_frame, old_points, None, **lk_params)
        old_gray = gray_frame.copy()
        old_points = new_points

        x, y = new_points.ravel()
        cv2.circle(frame, (x, y), 5, (0, 255, 0), -1)

    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1)
    if key == 27:
        break

cap.release()
cv2.destroyAllWindows()
edit flag offensive delete link more

Comments

1

nice attempt, but i doubt, if manually selection is feasible here ;(

berak gravatar imageberak ( 2018-11-13 06:38:31 -0500 )edit

Yes. You can selected any vehicles as you liked or any objects too. Lucas Kanade is good choice.

supra56 gravatar imagesupra56 ( 2018-11-13 07:06:18 -0500 )edit

main problem is left unchanged: how to derive the car's position(s) from the lk output.

berak gravatar imageberak ( 2018-11-13 07:11:36 -0500 )edit

Vote my post! :D I cant vote up the responses. It's a nice idea and thank you so much @supra56 for comment. The main problem is that I want to put a circle or a square o rectangle... around the cars some bounding box may be? I'm still thinking about solution... because I think that it's possible to do. Now, I have the corners of the cars with ShiTomasy and I just want a bounding box who can track these points across the frame. thank you, Im open to new ideas and advices thank you so much.

WoFFeN88 gravatar imageWoFFeN88 ( 2018-11-13 13:15:17 -0500 )edit

@WoFFeN88. You can't vote your post. You have to vote for me by clicking on left side of my post.

supra56 gravatar imagesupra56 ( 2018-11-13 19:26:45 -0500 )edit

Yes you can do on fly. What i'm looking for cooridinate of x, y, x1 and y1.

supra56 gravatar imagesupra56 ( 2018-11-13 19:34:32 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-11-11 10:08:51 -0500

Seen: 126 times

Last updated: Nov 13 '18