用opencv python计算在一行中截取的点 [英] Counting the point which intercept in a line with opencv python
问题描述
我正在使用opencv和python编程进行车辆计数,我已经完成了以下步骤: 1.使用BackgroundSubtractorMOG2检测行驶中的车辆 2.在其上画一个矩形,然后将其重心 3.画一条线(以指示计数)
I am working in vehicle counting with opencv and python programming, I already complete step: 1. Detect moving vehicle with BackgroundSubtractorMOG2 2. Draw rectangle on it, then poin a centroid of that 3. Draw a line (to indicate of the counting)
如果该质心在行中加/截,我想算为1.,但是在我的代码中有时会添加no.这里是行代码:
if that centroid accros/intercept with the line I want count that 1. but in my code sometime it add sometime no. Here the line code:
cv2.line(frame,(0,170),(300,170),(200,200,0),2)
还有质心:
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)
我的计数代码:
if cy==170:
counter=counter+1
任何人都可以帮助我.请.给您的建议谢谢!
Can anyone help me. please. for your advice thankyou!
推荐答案
这是我的方法,可以独立地处理视频帧频.假设您能够在每一帧跟踪汽车的质心,我将保存最后两个质心的位置(在我的代码中为last_centroid
和centroid
),并按以下步骤进行处理:
Here is my approach that would work independently of the video frame rate. Assuming that you are able to track a car's centroid at each frame, I would save the last two centroids' position (last_centroid
and centroid
in my code) and process as follows:
- 从 aX + bY + c = 0 )
- 计算
last_centroid
和centroid
之间的线段的方程的参数 - 查找两行是否相交
- 如果是这样,请增加您的计数器
- compute the intercepting line equation's parameters ( (a,b,c) from aX + bY + c = 0)
- compute the equation's parameters of the segment line between
last_centroid
andcentroid
- find if the two lines are intersecting
- if so, increment your counter
这是我在OpenCV(Python)中实现它的方式:
Here is how I implemented it in 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's 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()
这是一些结果:
图1.线段与线相交(蓝色中的线段-红色中的last_centroid
和centroid
之间的线段)
图2.段与线不相交
Fig1. Segment is intersecting the line (intercepting line in blue - segment line between last_centroid
and centroid
in red)
Fig2. Segment is NOT intersecting the line
N.B. I found the formulas to calculate the intersection point here.
我希望我的方法将有助于解决您的问题.
I hope my approach will help to address your problem.
这篇关于用opencv python计算在一行中截取的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!