在過去的幾天里,我嘗試使用 OpenCV 將影像保存到指定的檔案夾,但是我經常發現這個錯誤:
Traceback (most recent call last):
File "C:\Users\himik\Desktop\[REDACTED]\[REDACTED]\IMGTEST.py", line 3, in <module>
cam = VideoCapture(0) # 0 -> index of camera
NameError: name 'VideoCapture' is not defined
我的代碼如下:
from cv2 import *
cam = VideoCapture(0)
s, img = cam.read()
if s:
namedWindow("cam-test",CV_WINDOW_AUTOSIZE)
imshow("cam-test",img)
waitKey(0)
destroyWindow("cam-test")
imwrite("filename.jpg",img)
關于如何解決我的問題的任何建議或想法?編輯:洗掉無關資訊!
uj5u.com熱心網友回復:
基本上問題出在你的from cv2 import *. 我只是將您指向互聯網,了解您為什么不應該使用我們import *。
FE。這篇中等文章有一篇不錯的文章。一定要關注那篇文章中的鏈接。
有兩個簡單的修復程式可用。首先,將 cv2 匯入其正確的命名空間。
import cv2 as cv
cam = cv.VideoCapture(0)
s, img = cam.read()
if s:
cv.namedWindow("cam-test")
cv.imshow("cam-test",img)
cv.waitKey(0)
cv.destroyWindow("cam-test")
cv.imwrite("filename.jpg",img)
選項 2,從 cv2 匯入特定部件。請注意,我強烈建議在這里使用選項 1。
from cv2 import (VideoCapture, namedWindow, imshow, waitKey, destroyWindow, imwrite)
cam = VideoCapture(0)
s, img = cam.read()
if s:
namedWindow("cam-test")
imshow("cam-test",img)
waitKey(0)
destroyWindow("cam-test")
imwrite("filename.jpg",img)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/442377.html
