Netlogo 如何正确计算车辆数量?

Netlogo 如何正确计算车辆数量?,netlogo,Netlogo,我有一个半径为10米的圆。我想计算进入圆圈it的车辆数量(与中心车辆的距离我对您的问题不是很确定,但也许此代码可以帮助您: set total-cars count cars with [distancexy 0 0 <= 10] 使用[distance XY 0 0导入cv2设置车辆总数 导入时间 bgsMOG=cv2.createBackgroundSubtractorMOG2(detectShadows=False) 内核=cv2.getStructuringElement(cv2

我有一个半径为10米的圆。我想计算进入圆圈it的车辆数量(与中心车辆的距离我对您的问题不是很确定,但也许此代码可以帮助您:

set total-cars count cars with [distancexy 0 0 <= 10]
使用[distance XY 0 0
导入cv2设置车辆总数
导入时间
bgsMOG=cv2.createBackgroundSubtractorMOG2(detectShadows=False)
内核=cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
cap=cv2.视频捕获(0)
计数器=0
时间。睡眠(2)
如果上限:
尽管如此:
ret,frame=cap.read()
如果ret:
#fgmask=bgsMOG.apply(帧,无,0.01)
模糊=cv2.高斯模糊(帧,(5,5,0)
fgmask=bgsMOG.apply(模糊)
morhpho=cv2.morphologyEx(fgmask,cv2.morpho_OPEN,kernal)
#探测线
cv2.线(框架,(20270),(320270),(175175,0),5)
_,等高线,层次=cv2.查找到的轮廓(morhpho,cv2.外部检索,cv2.链近似简单)
ax1=20#如果相交,车辆将被计数的线坐标
ay1=270
ax2=320
ay2=270
try:hierarchy=hierarchy[0]
除外:层次结构=[]
#对于轮廓,zip中的层次(轮廓,层次):
对于枚举(等高线)中的(i,等高线):
(x,y,w,h)=cv2.边界矩形(轮廓)
如果w>20且h>25:
rec=cv2.矩形(框,(x,y),(x+w,y+h),(180,0,0),1)
x1=w/2#以找到质心
y1=h/2
cx=x+x1
cy=y+y1
质心=(cx,cy)
M=cv2.力矩(轮廓)
cX=int(M[“m10”]/M[“m00”])
cY=int(M[“m01”]/M[“m00”])
#在图像上绘制形状的轮廓和中心
cv2.圆(帧,(cX,cY),2,(255,255,255),-1)
cv2.圆(帧,(int(cx),int(cy)),1,(0255,0),-1)
dy=cY-270#增加计数器的第一个代码
打印(“质心”,cX,:”,cY)
如果dy==0:
如果(cX=20):
计数器=计数器+1
打印(“第一ct”,计数器)
打印透镜(轮廓)
#FileName=“D:/data/”+str(y)+“.jpg”
#cv2.imshow(“裁剪”,rec)
#cv2.imwrite(文件名,rec)
如果cy==270:
如果质心>(27268)和质心<(325285):
如果(cX=20):
计数器=计数器+1
打印“counter=”,计数器
如果cY>10且cY<250:
cv2.putText(frame,str(counter),(10150),cv2.FONT\u HERSHEY\u SIMPLEX,2,(255,0,0),1,True)
#cv2.调整窗口大小(“输出”,320180)
cv2.imshow(“输出”,帧)
cv2.imshow('mor',morhpho)
cv2.imshow(“模糊”,模糊)
#cv2.imshow('FGMASK',morhpho)
key=cv2.waitKey(1)
如果key==ord('q'):
打破
第1章释放()
cv2.destroyAllWindows()

尝试计数一辆车,但有一些问题请解释代码解决问题的原因和方式,而不是只提供代码答案。1.通过使用轮廓,你可以为检测到的车辆绘制矩形2.然后你必须计算矩形3的质心。之后,如果匹配,你可以将cx、cy与虚拟线匹配,然后增加计数器。
set total-cars count cars with [distancexy 0 0 <= 10]
count cars with [distancexy 0 0 <= 10]
import cv2
import time

bgsMOG = cv2.createBackgroundSubtractorMOG2(detectShadows=False)
kernal=cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
cap = cv2.VideoCapture(0)
counter =0
time.sleep(2)
if cap:
    while True:

        ret, frame = cap.read()

        if ret:

            #fgmask = bgsMOG.apply(frame, None, 0.01)
            blur = cv2.GaussianBlur(frame, (5, 5), 0)
            fgmask = bgsMOG.apply(blur)
            morhpho = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernal)

            #line for detection
            cv2.line(frame,(20,270),(320,270),(175,175,0),5)
            _,contours, hierarchy = cv2.findContours(morhpho,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

            ax1=20 #coordinate of line where vehicle will be count if intersect
            ay1=270
            ax2=320
            ay2=270

            try: hierarchy = hierarchy[0]
            except: hierarchy = []

            #for contour, hier in zip(contours, hierarchy):
            for (i, contour) in enumerate(contours):

                (x,y,w,h) = cv2.boundingRect(contour)

                if w > 20 and h > 25:

                    rec=cv2.rectangle(frame, (x,y), (x+w,y+h), (180, 0, 0), 1)

                    x1=w/2 #to find centroid
                    y1=h/2
                    cx=x+x1
                    cy=y+y1
                    centroid=(cx,cy)
                    M = cv2.moments(contour)
                    cX = int(M["m10"] / M["m00"])
                    cY = int(M["m01"] / M["m00"])

                    # draw the contour and center of the shape on the image
                    cv2.circle(frame, (cX, cY), 2, (255, 255, 255), -1)
                    cv2.circle(frame,(int(cx),int(cy)),1,(0,255,0),-1)
                    dy=cY-270 #my first code to increase counter
                    print("centroid",cX,":",cY)
                if dy==0:
                    if (cX<=320)and(cX>=20):
                        counter=counter+1
                        print("1st ct",counter)
                        print len(contour)
                        #FileName = "D:/data/" + str(y) + ".jpg"
                        #cv2.imshow("cropped",rec)
                        #cv2.imwrite(FileName,rec)

                if cy==270:
                    if centroid > (27, 268) and centroid < (325, 285):
                        if (cX <= 320) and (cX >= 20):
                            counter =counter+1
                            print "counter=", counter
                            if cY > 10 and cY < 250:
                                cv2.putText(frame, str(counter),(10,150),cv2.FONT_HERSHEY_SIMPLEX,2, (255, 0, 0), 1, True)


        #cv2.resizeWindow('Output',320,180)
        cv2.imshow('Output', frame)
        cv2.imshow('mor', morhpho)
        cv2.imshow('blur', blur)

        #cv2.imshow('FGMASK', morhpho)


        key = cv2.waitKey(1)
        if key == ord('q'):
            break
cap.release()
cv2.destroyAllWindows()