[英]I used python and opencv3 to contour the largest image on live stream, it looks my code went wrong, can anyone help me
我使用python和opencv3在實時流上繪制了最大的圖像輪廓,看起來我的代碼出錯了,有人可以幫我嗎
import cv2,platform
import numpy as np
def larger(x):
gray = cv2.cvtColor(x, cv2.COLOR_BGR2GRAY)
image = cv2.Canny(gray,35,200)
(_,cnts,_) = cv2.findContours(image.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
c = max(cnts, key=cv2.contourArea)
return cv2.minAreaRect(c)
capture = cv2.VideoCapture(0)
retval, im = capture.read()
y = larger(im)
box = np.int0(cv2.boxPoints(y))
cv2.drawContours(im,[box],-1,(0,255,0),2)
cv2.imshow("Image",im)
cv2.waitKey(0)
cv2.destroyAllWindows()
我有此代碼的兩個問題:
1) is my web cam was on but cant see any video image, getting this error
"Traceback (most recent call last):
File "C:\Users\Snehith\Desktop\project vision\cnt.py", line 14, in <module>
y = larger(im)
File "C:\Users\Snehith\Desktop\project vision\cnt.py", line 8, in larger
c = max(cnts, key=cv2.contourArea)
ValueError: max() arg is an empty sequence"
2) i want a continuous video stream along with the contour, please explain the mistakes and also solutions in python and opencv
我只需要對您的代碼做兩個小改動:
1.)您可以檢查capture.isOpened()以查看是否連接了攝像機。
2.)我包含一個while循環,以便您連續拉動幀,直到按下'q'。
import cv2,platform
import numpy as np
def larger(x):
gray = cv2.cvtColor(x, cv2.COLOR_BGR2GRAY)
image = cv2.Canny(gray,35,200)
(_,cnts,_) = cv2.findContours(image.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
c = max(cnts, key=cv2.contourArea)
return cv2.minAreaRect(c)
capture = cv2.VideoCapture(0)
if capture.isOpened():
while(True):
retval, im = capture.read()
y = larger(im)
box = np.int0(cv2.boxPoints(y))
cv2.drawContours(im,[box],-1,(0,255,0),2)
cv2.imshow("Image",im)
if cv2.waitKey(1) & 0xFF == ord('q'):
capture.release()
break
else:
print "No camera detected."
cv2.destroyAllWindows()
您可能會發現OpenCV Python教程對類似的問題有所幫助,這是pdf版本的鏈接: https : //media.readthedocs.org/pdf/opencv-python-tutroals/latest/opencv-python-tutroals.pdf
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.