如何正确计算车辆数量?

时间:2016-01-04 16:49:05

标签: netlogo

我有一个半径10米的圆圈。我想计算进入圆圈的车辆数量(距离中心车的距离<= 10m) 我是对的。我可以使用工具栏&#34; Minitor&#34;计算目前正在清算的车辆数量xe.nhung&#34; minitor&#34;比通过圆圈的实际车辆数量大得多。我附上了&#34; minitor&#34; by&#34; total-cars&#34;。 如何正确计算车辆数量?

 ask cars
  [
    if distancexy 0 0 < 10
    [
      set total-cars (total-cars + 1)
    ]
  ]

2 个答案:

答案 0 :(得分:2)

我对您的问题不太确定,但也许这段代码可以帮助您:

set total-cars count cars with [distancexy 0 0 <= 10]

您可以直接在监视器控件中使用以下代码:

count cars with [distancexy 0 0 <= 10]

答案 1 :(得分:0)

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()