Python 如何跟踪多个轮廓的增长?
目标是找到轮廓面积大于170的轮廓,该轮廓持续增长5帧。 我能为一个轮廓做这个。我的方法是新手的方法,因为我目前将过滤后的轮廓中心值附加到一个deque,并将相应的轮廓面积值附加到一个单独的deque。 如果在5帧之后,值连续变大,我会从deque中弹出第一个值(指示增长的起始位置) 关键问题是: -如何跟踪两个或三个等高线,以便等高线区域的连续增长 补充质询如下: -目前,我假设增长将从第一个过滤轮廓开始,这将而不是始终如此,轮廓增长可能在位置10、11、12、13、14开始发生-然后我需要在坐标系中取位置10的坐标值-我如何比较连续轮廓区域以检查连续增长的5帧,同时跟踪增长的起始坐标 -是否有比在通过轮廓面积阈值后将中心和轮廓面积值附加到DEQUE更有效的方法来处理中心和轮廓面积值Python 如何跟踪多个轮廓的增长?,python,algorithm,loops,opencv,computer-vision,Python,Algorithm,Loops,Opencv,Computer Vision,目标是找到轮廓面积大于170的轮廓,该轮廓持续增长5帧。 我能为一个轮廓做这个。我的方法是新手的方法,因为我目前将过滤后的轮廓中心值附加到一个deque,并将相应的轮廓面积值附加到一个单独的deque。 如果在5帧之后,值连续变大,我会从deque中弹出第一个值(指示增长的起始位置) 关键问题是: -如何跟踪两个或三个等高线,以便等高线区域的连续增长 补充质询如下: -目前,我假设增长将从第一个过滤轮廓开始,这将而不是始终如此,轮廓增长可能在位置10、11、12、13、14开始发生-然后我需要在
coords=deque()
cGrowth=deque()
尺寸=(1280720)
第一帧=无
j=0
#循环播放视频的帧
尽管如此:
帧=cv2.视频捕获(pathToVideo)
如果框架为无:
打破
帧=cv2.调整大小(帧、尺寸、插值=cv2.内部区域)
灰色=cv2.CVT颜色(边框,cv2.COLOR\u BGR2GRAY)
灰色=cv2.高斯模糊(灰色,(21,21,0)
如果firstFrame为无:
第一帧=灰色
持续
frameDelta=cv2.absdiff(第一帧,灰色)
thresh=cv2.threshold(frameDelta,25255,cv2.thresh_二进制)[1]
thresh=cv2.扩张(thresh,无,迭代次数=2)
等高线,=cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,
cv2.链条(近似简单)
中心=[]
cAreas=[]
中心=无
对于范围内的i(透镜(轮廓)):
如果cv2.轮廓面积(轮廓[i])<150:
持续
力矩=cv2.力矩(等高线[i])
cAreas.append(cv2.contourArea(contours[i]))
中心。附加((int(矩['m10']/矩['m00']),int(矩['m01']/矩['m00']))
cv2.圆(框架,中心[-1],3,(0,0,0),-1)
坐标附加(中心)
cGrowth.append(cAreas)
如果len(cGrowth)==5:
i=0
对于成对的x,y(cGrowth):
#打印(x,y)
如果(x
无法花时间真正理解问题所在。如果要在不同的帧中找到相应的轮廓,并且它们不会移动太多,则可以尝试使用排序算法或简单的IoU(联合交集)计算来识别相应的轮廓。
coords = deque()
cGrowth = deque()
dim = (1280, 720)
firstFrame = None
j = 0
# loop over the frames of the video
while True:
frame = cv2.VideoCapture(pathToVideo)
if frame is None:
break
frame = cv2.resize(frame, dim, interpolation = cv2.INTER_AREA)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21, 21), 0)
if firstFrame is None:
firstFrame = gray
continue
frameDelta = cv2.absdiff(firstFrame, gray)
thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh, None, iterations=2)
contours, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
centres = []
cAreas = []
center = None
for i in range(len(contours)):
if cv2.contourArea(contours[i]) < 150:
continue
moments = cv2.moments(contours[i])
cAreas.append(cv2.contourArea(contours[i]))
centres.append((int(moments['m10']/moments['m00']), int(moments['m01']/moments['m00'])))
cv2.circle(frame, centres[-1], 3, (0,0,0), -1)
coords.append(centres)
cGrowth.append(cAreas)
if len(cGrowth) == 5:
i = 0
for x, y in pairwise(cGrowth):
#print(x, y)
if (x < y):
i += 1
if (i == 4):
impX = coords.popleft()
print("Origin of motion is at: " + str(impX))
break
break
j += 1
cv2.imshow("Motion Feed", frame)
cv2.imshow("Thresh", thresh)
cv2.imshow("Frame Delta", frameDelta)
key = cv2.waitKey(1) & 0xFF
# if the 'q' key is pressed, destroy all windows
if key == ord("q"):
cv2.destroyAllWindows()