I am able to create rectangles, centroid (circle circle(circle on a object) and a line, after that i want to count that if centroid circle cross the line on a video.
below is the example code:
while True:
ret, frame = cap.read()
frame = cv2.resize(frame,None,fx=scaling_factorx,fy=scaling_factory,interpolation=cv2.INTER_AREA)
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
x1=w/1
y1=h/1
cx=x+x1
cy=y+y1
centroid = (cx,cy)
cv2.circle(frame,(int(cx),int(cy)),1,(0,0,255),2) if point1 and point2:
cv2.line(frame, point1, point2, (0, 255, 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), 3)
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.