Ask Your Question
-1

How to count if circle (cv2.circle) cross the line (cv2.line)

asked 2020-04-28 00:36:22 -0600

manos gravatar image

updated 2020-04-30 07:23:59 -0600

I am able to create rectangles, circle(circle on a object) and a line, after that i want to count that if circle cross the line on a video. below is the example code:

while True:
    ret, frame = cap.read()

    imgray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    fgmask1 = cv2.GaussianBlur(imgray, (7,7), 0)

    fgmask = fgbg.apply(fgmask1)

    erode=cv2.erode(fgmask,None,iterations=3)
    moments=cv2.moments(erode,True)
    #area=moments['m00']
    if point1 and point2:
        cv2.line(frame, point1, point2, (0, 255, 0), 3)


    contours, hierarchy = cv2.findContours(erode, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

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

    for contour, hier in zip(contours, hierarchy):
        (x, y, w, h) = cv2.boundingRect(contour)
        if w > 80 and h > 80:
            cv2.rectangle(frame, (x,y), (x+w,y+h), (0, 255, 0), 2)

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

            moments=cv2.moments(contour)
            area=moments['m00']

            if moments['m00'] >=minArea:
                x=int(moments['m10']/moments['m00'])
                y=int (moments['m01']/moments['m00'])
                if x<lineCount:
                    sen=sen<<1
                else:
                    sen=(sen<<1)|1
                sen=sen&0x03
                if sen==1:
                    counter=counter+1   

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

            cv2.line
            font = cv2.FONT_HERSHEY_SIMPLEX
            cv2.putText(frame,'counter='+str(counter), (10,30),font,1, (255, 0, 0), 2)

    cv2.imshow("App", frame)

Point1 and point2 are mouse click callback. Is it possible to count if object cross the cv2.line. Or is there any solution other than that.

image description

edit retag flag offensive close merge delete

Comments

In case OpenCV doesn't offer a function for this, justt use basic trigonometric calculation to find out if distance from point to line is less than circle's radius

mvuori gravatar imagemvuori ( 2020-04-28 06:01:55 -0600 )edit

@manos. Does my answer is helped count if centroid. Just used my snippet code.

supra56 gravatar imagesupra56 ( 2020-04-28 06:05:23 -0600 )edit

@mvuori and @supra56. I got this calculation (ax + by + c = 0) i am trying to merge with my code to solve this.. if you have any idea to merge to the code that please let me know.

manos gravatar imagemanos ( 2020-04-29 06:04:28 -0600 )edit

I'm uncertainly about formula.

supra56 gravatar imagesupra56 ( 2020-04-29 08:29:02 -0600 )edit

@mvuori and @supra56. I added a example image please find. and actually It need to be count if the red circle touch the green line every time on a video.

manos gravatar imagemanos ( 2020-04-29 10:57:03 -0600 )edit

Now I understood. It is counting vehicles, stupid fool. Forgot your bloody formula. Now here is link that make you understand. people countingpeopel count 1counting vehicles What you trying to do is you must known x, y of lines crossing lines. The centroid cordinates is greater than crossing line. But not same same as rectangle. it is up to you. Studying people crossing. OK. Be nice.

supra56 gravatar imagesupra56 ( 2020-04-29 12:11:05 -0600 )edit

Put the centroid in middle of cv2.rectangle() Forget about mouse click event.

supra56 gravatar imagesupra56 ( 2020-04-29 12:16:37 -0600 )edit

@supra56. I looked the examples of "1counting vehicles" that is good but i need to draw the line manually that where needed.

manos gravatar imagemanos ( 2020-04-30 03:03:26 -0600 )edit

Just used cv2.line to draw horizontal line.

supra56 gravatar imagesupra56 ( 2020-04-30 03:26:17 -0600 )edit

@supra56. Please find my modified code. its not working.

manos gravatar imagemanos ( 2020-04-30 07:24:10 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-04-30 08:54:37 -0600

supra56 gravatar image

updated 2020-04-30 08:56:51 -0600

Try this:

import cv2

backsub = cv2.createBackgroundSubtractorMOG2()  
capture = cv2.VideoCapture('../../video/traffic.mp4') 
i = 0
minArea = 5
font = cv2.FONT_HERSHEY_SIMPLEX

while True:
    ret, frame = capture.read()
    fgmask = backsub.apply(frame, None, 0.01)
    erode = cv2.erode(fgmask, None, iterations=2)      
    moments = cv2.moments(erode, True)               
    area = moments['m00']

    if moments['m00'] >= minArea:


       x = (moments['m10'] // moments['m00'])
        y = (moments['m01'] // moments['m00'])
        cv2.line(frame, (220, 470),(560, 470), (0, 0, 255),2)
        if x >= 190 and x <= 560 and y >= 470 and y <= 500:        
            i += 1
            print(i)

    cv2.putText(frame,'COUNT: %r' %i, (10,30), font, 1, (255, 0, 0), 2)
    cv2.imshow("Track", frame)
    cv2.imshow("background sub", fgmask)
    key = cv2.waitKey(1)
    if key is ord('q'):
        break

In line 19 and 20. Change cv2.line coordinates to suit your needed. Change x, y coordinates.

cv2.line(frame, (220, 470),(560, 470), (0, 0, 255),2)
if x >= 190 and x <= 560 and y >= 470 and y <= 500:

You will have to play around.

edit flag offensive delete link more

Comments

@supra56. I tried your code it is counting unnecessarily like its showing more than 50 number with in 10 seconds. And It's counting even though there is no line.

manos gravatar imagemanos ( 2020-04-30 12:18:58 -0600 )edit

@supra56. Can't we do as of my idea shown in the picture. Or can't we count the vehicles If rectangle or centorid touch or pass over the line.

manos gravatar imagemanos ( 2020-04-30 12:28:16 -0600 )edit

When did you started learning opencv......entry level.........intermediate..........advance?

supra56 gravatar imagesupra56 ( 2020-04-30 21:43:38 -0600 )edit

@supra56. I am in entry level @ learning opencv.

manos gravatar imagemanos ( 2020-04-30 23:02:31 -0600 )edit

Can you send video clip? Click my icon. Don't forget forget my email spelled wrong address.

supra56 gravatar imagesupra56 ( 2020-05-01 07:15:26 -0600 )edit

Where did you put webcam? Inside house?

supra56 gravatar imagesupra56 ( 2020-05-01 07:17:05 -0600 )edit

Actually its not web cam, its a recorded video.

manos gravatar imagemanos ( 2020-05-01 12:22:32 -0600 )edit

is it depends on video? And i could not find your email address. We can use any video.

manos gravatar imagemanos ( 2020-05-01 12:43:51 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-04-28 00:36:22 -0600

Seen: 2,801 times

Last updated: Apr 30 '20