名称:name' img2'不是定义。 OpenCV增强了现实
您好,我遇到了名称eRror的错误:名称“ img2”在第42行中未定义。起初我认为这是一个凹痕问题,但现在我认为它与矩阵有关,因为它没有显示所需的信息如果我尝试调用矩阵变量,这导致IMG2变得不确定。我尝试了一切,作为初学者,我只能做很多事情。
这是我的完整代码:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
myVid = cv2.VideoCapture("video.mp4")
imgTarget = cv2.imread("TargetImage.jpg")
success, imgVideo = myVid.read()
hT, wT, cT = imgTarget.shape
imgVideo = cv2.resize(imgVideo, (wT, hT))
orb = cv2.ORB_create(nfeatures=200)
kp1, des1 = orb.detectAndCompute(imgTarget, None)
while True:
success, imgWebcam = cap.read()
imgAug = imgWebcam.copy()
kp2, des2 = orb.detectAndCompute(imgWebcam, None)
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1, des2, k=2)
good = []
for m, n in matches:
if m.distance < 0.75 * n.distance:
good.append(m)
print(len(good))
imgFeatures = cv2.drawMatches(imgTarget, kp1, imgWebcam, kp2, good, None, flags=2)
if len(good) > 15:
srcPts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1,1,2)
dstPts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)
matrix, mask = cv2.findHomography(srcPts, dstPts, cv2.RANSAC,5.0)
print(matrix)
pts = np.float32([[0,0],[0,hT],[wT,hT],[wT,0]]).reshape(-1,1,2)
dst = cv2.perspectiveTransform(pts,matrix)
img2 = cv2.polylines(imgWebcam,[np.int32(dst)],True,(255,0,255),3, cv2.LINE_AA)
cv2.imshow('img2', img2)
cv2.imshow('ImgFeatures', imgFeatures)
cv2.imshow('ImgTarget', imgTarget)
cv2.imshow('myVid', imgVideo)
cv2.imshow('Webcam', imgWebcam)
cv2.waitKey(0)
Hello I'm getting an error of NameError: name 'img2' is not defined in line 42. At first I thought it was an indentation problem but now I think it has something to do with the matrix because it did not display the needed information and that caused img2 to become undefined if I try to call on the matrix variable. I tried everything and there are only so many things I could do as a beginner.
Here's my full code:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
myVid = cv2.VideoCapture("video.mp4")
imgTarget = cv2.imread("TargetImage.jpg")
success, imgVideo = myVid.read()
hT, wT, cT = imgTarget.shape
imgVideo = cv2.resize(imgVideo, (wT, hT))
orb = cv2.ORB_create(nfeatures=200)
kp1, des1 = orb.detectAndCompute(imgTarget, None)
while True:
success, imgWebcam = cap.read()
imgAug = imgWebcam.copy()
kp2, des2 = orb.detectAndCompute(imgWebcam, None)
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1, des2, k=2)
good = []
for m, n in matches:
if m.distance < 0.75 * n.distance:
good.append(m)
print(len(good))
imgFeatures = cv2.drawMatches(imgTarget, kp1, imgWebcam, kp2, good, None, flags=2)
if len(good) > 15:
srcPts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1,1,2)
dstPts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)
matrix, mask = cv2.findHomography(srcPts, dstPts, cv2.RANSAC,5.0)
print(matrix)
pts = np.float32([[0,0],[0,hT],[wT,hT],[wT,0]]).reshape(-1,1,2)
dst = cv2.perspectiveTransform(pts,matrix)
img2 = cv2.polylines(imgWebcam,[np.int32(dst)],True,(255,0,255),3, cv2.LINE_AA)
cv2.imshow('img2', img2)
cv2.imshow('ImgFeatures', imgFeatures)
cv2.imshow('ImgTarget', imgTarget)
cv2.imshow('myVid', imgVideo)
cv2.imshow('Webcam', imgWebcam)
cv2.waitKey(0)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在收到名称错误,因为
img2
对象不存在。img2
在 - 陈述中被定义。然后,您在不存在的对象上调用imshow
。我已经编辑了以下代码,仅在满足IF条件或打印错误时仅调用IMShow命令。
You are receiving a name error because the
img2
object doesn't exist.img2
is being defined inside anif
-statement, where if the condition that the length of good is less than or equal to 15 is not met, then img2 will not be defined. You then callimshow
on an object which doesn't exist.I have edited the below code to only call the imshow commands if the if condition is met, or to print an error.