简体   繁体   中英

How can i make findingcontour slow down?

I want to find velocity of an object. First of all, I measure two point which ball passed there and their differences to find lenght. Also time.. Finally i divide lenght to time im getting velocity but..

My problem is computer finds contours in video very fast. When i use time.sleep() , lag starts in video. I dont want to this. I want only "speed of finding contours slowdown" without any fps or lag(i dont know) changing

for c in cnts :
    M = cv2.moments(c)
    cX = int(M["m10"] / M["m00"])
    cY = int(M["m01"] / M["m00"])
    x, y, w, h = cv2.boundingRect(c)
    cv2.rectangle(video, (x, y), (x + w, y + h), (0, 255, 0), 2)
    cv2.circle(video, (cX,cY),7,(255,255,255),-1)

if cX != cX1:
    start1 = time.time()
    Lenght = math.sqrt(abs(cX-cX1)*abs(cX-cX1)+abs(cY-cY1)*abs(cY-cY1))
    Time = start1-end1
    Velocity = Lenght/Time
    print(Velocity)
    end1 = time.time()
cX1 = cX
cY1 = cY

Do you can get a frame time and measure speed between frames on this values:

_, frame1 = video.read()
t1 = video.get(cv2.CAP_PROP_POS_MSEC)
_, frame2 = video.read()
t2 = video.get(cv2.CAP_PROP_POS_MSEC)
diff_sec = (t2 - t1) / 1000.0

Okay i fixed with this code

for i in range(0,1):
    start2 = time.time()
    if start2-end2>1.0 :
        cnts,_ = cv2.findContours(video, cv2.RETR_TREE , cv2.CHAIN_APPROX_SIMPLE)
    end2 = time.time()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM