Draw a rectangle on track vehicles
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:
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()
nice attempt, but we have realtracking (needs contrib modules, though !)
can you ? all it does is sparse optflow, so far ..
I do not know what you want to tell me... :S
pip install opencv-contrib-python
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
well, what is really "your own", here ?
apart from that, -- as long as you're lacking any "domain knowledge", it won't fly.
btw, is your camera moving, too, or fixed to some place ?
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.
Moving that's why I use optical flow
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
Thank you first of all I try to draw a rectangles over the vehicle.. :( I'll try to follow your steps- thank you.