我在OpenCV中有运动跟踪功能,但我想让python在运动检测中提取轮廓内的任何东西的图像,以便将来在程序中使用或作为图像写入
迄今为止的代码:
import cv2
import numpy as np
from windowcapture import WindowCapture
wincap = WindowCapture('your window here')
frame1 = wincap.get_screenshot()
frame2 = wincap.get_screenshot()
while(True):
diff = cv2.absdiff(frame1, frame2)
gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
_, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY)
dilated = cv2.dilate(thresh, None, iterations=3)
contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(frame1, contours, -1, (0, 255, 0), 2)
cv2.imshow("window", frame1)
frame1 = frame2
frame2 = wincap.get_screenshot()
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
欢迎提供任何帮助或建议。谢谢
这个功能可以帮助你。它计算轮廓的边界框区域,并从图像中裁剪轮廓
相关问题 更多 >
编程相关推荐