Python Opencv can';我的代码有什么问题?
我想显示轮廓与实时网络摄像头喂养,但它只显示一帧。 我的目标是这样的:https://www.youtube.com/watch?v=Fchzk1lDt7Q&t=757s 代码:Python Opencv can';我的代码有什么问题?,python,opencv,contour,Python,Opencv,Contour,我想显示轮廓与实时网络摄像头喂养,但它只显示一帧。 我的目标是这样的:https://www.youtube.com/watch?v=Fchzk1lDt7Q&t=757s 代码: import cv2 import numpy import math windowName = "OBJECT_MOVEMENT" cv2.namedWindow(windowName) cap = cv2.VideoCapture(0) if cap.isOpened(): ret, f
import cv2
import numpy
import math
windowName = "OBJECT_MOVEMENT"
cv2.namedWindow(windowName)
cap = cv2.VideoCapture(0)
if cap.isOpened():
ret, frame = cap.read()
else:
ret = False
print("False operation.")
while ret:
ret, frame=cap.read()
imgray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray,127,255,cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
finalIm = cv2.drawContours(frame, contours, -1, (0,255,0), 1)
cnt = contours[0]
M = cv2.moments(cnt)
contours_sizes = [(cv2.contourArea(cnt), cnt) for cnt in contours]
biggest_contour = max(contours_sizes, key=lambda x: x[0])[1]
cX = int(((M["m10"])+1) / ((M["m00"])+1))
cY = int(((M["m10"])+1) / ((M["m00"])+1))
cv2.imshow("Countours",frame)
if cv2.waitKey(0) == ord('q'):
break
cap.release()
使用cv2.waitKey(1)代替cv2.waitKey(0)
另外,在while循环外部编写cap.release()
cap.release()
这就是你的代码应该是什么样子
import cv2
import numpy
import math
windowName = "OBJECT_MOVEMENT"
cv2.namedWindow(windowName)
cap = cv2.VideoCapture(0)
if cap.isOpened():
ret, frame = cap.read()
else:
ret = False
print("False operation.")
while ret:
ret, frame=cap.read()
imgray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray,127,255,cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
finalIm = cv2.drawContours(frame, contours, -1, (0,255,0), 1)
cnt = contours[0]
M = cv2.moments(cnt)
contours_sizes = [(cv2.contourArea(cnt), cnt) for cnt in contours]
biggest_contour = max(contours_sizes, key=lambda x: x[0])[1]
cX = int(((M["m10"])+1) / ((M["m00"])+1))
cY = int(((M["m10"])+1) / ((M["m00"])+1))
cv2.imshow("Countours",frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
import cv2
import numpy
import math
windowName = "OBJECT_MOVEMENT"
cv2.namedWindow(windowName)
cap = cv2.VideoCapture(0)
if cap.isOpened():
ret, frame = cap.read()
else:
ret = False
print("False operation.")
while ret:
ret, frame=cap.read()
imgray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray,127,255,cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
finalIm = cv2.drawContours(frame, contours, -1, (0,255,0), 1)
cnt = contours[0]
M = cv2.moments(cnt)
contours_sizes = [(cv2.contourArea(cnt), cnt) for cnt in contours]
biggest_contour = max(contours_sizes, key=lambda x: x[0])[1]
cX = int(((M["m10"])+1) / ((M["m00"])+1))
cY = int(((M["m10"])+1) / ((M["m00"])+1))
cv2.imshow("Countours",frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()