Count point that intercepts a string with opencv python

I work in counting vehicles with opencv and python programming, I am already completing the step: 1. Detecting a moving car using BackgroundSubtractorMOG2 2. Draw a rectangle on it, then place the centroid of this 3. Draw a line (to indicate the count)

if this centroid accros / intercept with the line i want, calculated that 1. but in my code someday it will add someday no. Here is the line code:

cv2.line(frame,(0,170),(300,170),(200,200,0),2)

and there is a centroid:

if w > 20 and h > 25: 
    cv2.rectangle(frame, (x,y), (x+w,y+h), (180, 1, 0), 1)

    x1=w/2      
    y1=h/2
    cx=x+x1
    cy=y+y1
    centroid=(cx,cy)

    cv2.circle(frame,(int(cx),int(cy)),4,(0,255,0),-1)

my counting code:

 if cy==170:   
     counter=counter+1

Can someone help me. You are welcome. Thank you for your advice!

0
source share
2 answers

, 170 ( x y) , 30 , , 30 , , , 170!

, , , - . , x (y = 170) x .

, - , . , , .

0

, . , , (last_centroid centroid ) :

OpenCV (Python):

import cv2
import numpy as np
import collections

Params = collections.namedtuple('Params', ['a','b','c']) #to store equation of a line

def calcParams(point1, point2): #line equation Params computation
    if point2[1] - point1[1] == 0:
         a = 0
         b = -1.0
    elif point2[0] - point1[0] == 0:
        a = -1.0
        b = 0
    else:
        a = (point2[1] - point1[1]) / (point2[0] - point1[0])
        b = -1.0

    c = (-a * point1[0]) - b * point1[1]
    return Params(a,b,c)

def areLinesIntersecting(params1, params2, point1, point2):
    det = params1.a * params2.b - params2.a * params1.b
    if det == 0:
        return False #lines are parallel
    else:
        x = (params2.b * -params1.c - params1.b * -params2.c)/det
        y = (params1.a * -params2.c - params2.a * -params1.c)/det
        if x <= max(point1[0],point2[0]) and x >= min(point1[0],point2[0]) and y <= max(point1[1],point2[1]) and y >= min(point1[1],point2[1]):
            print("intersecting in:", x,y)
            cv2.circle(frame,(int(x),int(y)),4,(0,0,255), -1) #intersecting point
            return True #lines are intersecting inside the line segment
        else:
            return False #lines are intersecting but outside of the line segment

cv2.namedWindow('frame')
frame = np.zeros((240,320,3), np.uint8)

last_centroid = (200,200) #centroid of a car at t-1
centroid = (210,180) #centroid of a car at t

line_params = calcParams(last_centroid, centroid)
intercept_line_params = calcParams((0,170), (300,170))
print("Params:", line_params.a,line_params.b,line_params.c) 

while(1):
    cv2.circle(frame,last_centroid,4,(0,255,0), -1) #last_centroid
    cv2.circle(frame,centroid,4,(0,255,0), -1) #current centroid
    cv2.line(frame,last_centroid,centroid,(0,0,255),1) #segment line between car centroid at t-1 and t
    cv2.line(frame,(0,170),(300,170),(200,200,0),2) #intercepting line
    print("AreLinesIntersecting: ",areLinesIntersecting(intercept_line_params,line_params,last_centroid,centroid)) 
    cv2.imshow('frame',frame)
    if cv2.waitKey(15) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()

:

Fig. 1.  The segment crosses the line
1. ( - last_centroid centroid ).

Fig.  2. A segment does NOT cross a line
Fig2.

N.B. .

, .

0

Source: https://habr.com/ru/post/1684111/


All Articles