Python 如何存储上一帧的中心点列表以比较当前帧';s中心点列表
我需要比较前一帧的列表和当前帧的列表。我可以通过get_contours函数存储和打印当前帧的列表。它将视频中检测到的对象的中心点附加到称为白坐标和其他坐标的空列表中Python 如何存储上一帧的中心点列表以比较当前帧';s中心点列表,python,opencv,image-processing,object-detection,Python,Opencv,Image Processing,Object Detection,我需要比较前一帧的列表和当前帧的列表。我可以通过get_contours函数存储和打印当前帧的列表。它将视频中检测到的对象的中心点附加到称为白坐标和其他坐标的空列表中 # returns every center point from video's objects to a list. whiteCoordinates = [] #empty lists otherCoordinates = [] def get_contours(hsv, target, mask): cnts
# returns every center point from video's objects to a list.
whiteCoordinates = [] #empty lists
otherCoordinates = []
def get_contours(hsv, target, mask):
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
for contour in cnts:
if filter_contours(contour):
try:
M = cv2.moments(contour)
cX = int(M["m10"] / M["m00"]) #returns center points
cY = int(M["m01"] / M["m00"])
cv2.circle(target, (cX,cY), 5, (0, 0, 255), -1)
mcolor = mean_color(hsv, contour)
if colour_filter(mcolor):
cv2.putText(target,"White %s" % mcolor.astype(np.int), (cX, cY), font, 0.5, fontColor, lineType=lineType)
whiteCoordinates.append((cX, cY)) #appends center points to list
else:
cv2.putText(target,"Other %s" % mcolor.astype(np.int), (cX, cY), font, 0.5, fontColor, lineType=lineType)
otherCoordinates.append((cX, cY)) #appends center points to list
except:
pass
return target
问题是,我不知道如何存储前一帧的白坐标和其他坐标列表。我尝试过不同的循环,但总是得到两个列表当前帧的中心点。下一次迭代总是将前一帧的列表擦除为与当前相同
那怎么做呢?如何将以前的列表存储到whiteCoordinates 2和otherCoordinates 2,并使用它们与当前帧的列表whiteCoordinates和otherCoordinates进行比较
# returns every center point from video's objects to a list.
whiteCoordinates = [] #empty lists
otherCoordinates = []
def get_contours(hsv, target, mask):
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
for contour in cnts:
if filter_contours(contour):
try:
M = cv2.moments(contour)
cX = int(M["m10"] / M["m00"]) #returns center points
cY = int(M["m01"] / M["m00"])
cv2.circle(target, (cX,cY), 5, (0, 0, 255), -1)
mcolor = mean_color(hsv, contour)
if colour_filter(mcolor):
cv2.putText(target,"White %s" % mcolor.astype(np.int), (cX, cY), font, 0.5, fontColor, lineType=lineType)
whiteCoordinates.append((cX, cY)) #appends center points to list
else:
cv2.putText(target,"Other %s" % mcolor.astype(np.int), (cX, cY), font, 0.5, fontColor, lineType=lineType)
otherCoordinates.append((cX, cY)) #appends center points to list
except:
pass
return target
理想的结果是比较以前的列表和当前列表,以检测帧之间是否有移动。这对任何人都有意义吗?感谢您阅读此文,如果有人有任何建议,请特别感谢
完整代码链接:
whiteCoordinates 2=whiteCoordinates
不复制列表,而是将引用复制到内存中的列表。因此,对其中一项的任何更改也将出现在另一项中
要正确复制列表,可以使用whiteCoordinates 2=whiteCoordinates.copy()