|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 lcx99 于 2023-3-9 21:25 编辑
import cv2
import numpy as np
cap = cv2.VideoCapture(r"C:\Users\12137\Desktop\qiche.mp4")
#创建mog对象
mog = cv2.bgsegm.createBackgroundSubtractorMOG()
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
# 循环读取视频
while True:
ret,frame = cap.read()
if ret == True:
#把原始帧灰度化去噪
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
#去噪
blur = cv2.GaussianBlur(gray,(3,3),5)
mask = mog.apply(blur)
# 腐蚀
erode = cv2.erode(mask,kernel)
#膨胀
dialte = cv2.dilate(erode,kernel,iterations=2)
#消除小快,闭运算
close = cv2.morphologyEx(dialte,cv2.MORPH_CLOSE,kernel)
#轮廓
result ,con = cv2.findContours(close, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for co in con:
(x, y, w, h) = cv2.boundingRect(co)
#过滤小举
# 要求坐标点是整数
cv2.rectangle(frame, (int(x), int(y)), (int(x + w), int(y + h)), (0,0,255) ,2)
cv2.imshow("frame",frame)
Key = cv2.waitKey(1)
#用户按esc退出
if Key == 27:
break
# 释放资源
cap.release()
cv2.destroyAllWindows()
以上是程序
Traceback (most recent call last):
File "F:/python/22.py", line 26, in <module>
for co in con:
TypeError: 'NoneType' object is not iterable
这是显示错误
不明白哪里错了
本帖最后由 isdkz 于 2023-3-9 22:12 编辑
第 25 行的 result, con = cv2.findContours(close, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) 把 result 和 con 写反了
import cv2
import numpy as np
cap = cv2.VideoCapture(r"C:\Users\12137\Desktop\qiche.mp4")
#创建mog对象
mog = cv2.bgsegm.createBackgroundSubtractorMOG()
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
# 循环读取视频
while True:
ret,frame = cap.read()
if ret == True:
#把原始帧灰度化去噪
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
#去噪
blur = cv2.GaussianBlur(gray,(3,3),5)
mask = mog.apply(blur)
# 腐蚀
erode = cv2.erode(mask,kernel)
#膨胀
dialte = cv2.dilate(erode,kernel,iterations=2)
#消除小快,闭运算
close = cv2.morphologyEx(dialte,cv2.MORPH_CLOSE,kernel)
#轮廓
con, result = cv2.findContours(close, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # result 和 con 写反了
for co in con:
(x, y, w, h) = cv2.boundingRect(co)
#过滤小举
# 要求坐标点是整数
cv2.rectangle(frame, (int(x), int(y)), (int(x + w), int(y + h)), (0,0,255) ,2)
cv2.imshow("frame",frame)
Key = cv2.waitKey(1)
#用户按esc退出
if Key == 27:
break
# 释放资源
cap.release()
cv2.destroyAllWindows()
|
|