提问人:yong jie 提问时间:11/16/2023 更新时间:11/17/2023 访问量:39
Opencv2 cvtColor() 在树莓派上不起作用?
Opencv2 cvtColor() does not work on raspberry pi?
问:
我正在根据youtube教程编写python opencv2 python代码,这里我直接复制了代码,
import cv2
import utlis
###################################
webcam = True
path = '/home/ftsp23/vert/phone'
cap = cv2.VideoCapture(0)
cap.set(10,160)
cap.set(3,1920)
cap.set(4,1080)
scale = 3
wP = 210 *scale
hP= 297 *scale
###################################
while True:
if webcam:success,img = cap.read()
else: img = cv2.imread(path)
imgContours , conts = utlis.getContours(img,minArea=50000,filter=4)
if len(conts) != 0:
biggest = conts[0][2]
#print(biggest)
imgWarp = utlis.warpImg(img, biggest, wP,hP)
imgContours2, conts2 = utlis.getContours(imgWarp,
minArea=2000, filter=4,
cThr=[50,50],draw = False)
if len(conts) != 0:
for obj in conts2:
cv2.polylines(imgContours2,[obj[2]],True,(0,255,0),2)
nPoints = utlis.reorder(obj[2])
nW = round((utlis.findDis(nPoints[0][0]//scale,nPoints[1][0]//scale)/10),1)
nH = round((utlis.findDis(nPoints[0][0]//scale,nPoints[2][0]//scale)/10),1)
cv2.arrowedLine(imgContours2, (nPoints[0][0][0], nPoints[0][0][1]), (nPoints[1][0][0], nPoints[1][0][1]),
(255, 0, 255), 3, 8, 0, 0.05)
cv2.arrowedLine(imgContours2, (nPoints[0][0][0], nPoints[0][0][1]), (nPoints[2][0][0], nPoints[2][0][1]),
(255, 0, 255), 3, 8, 0, 0.05)
x, y, w, h = obj[3]
cv2.putText(imgContours2, '{}cm'.format(nW), (x + 30, y - 10), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.5,
(255, 0, 255), 2)
cv2.putText(imgContours2, '{}cm'.format(nH), (x - 70, y + h // 2), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.5,
(255, 0, 255), 2)
cv2.imshow('A4', imgContours2)
img = cv2.resize(img,(0,0),None,0.5,0.5)
cv2.imshow('Original',img)
cv2.waitKey(1)
但它返回的错误
Traceback (most recent call last):
File "/home/ftsp23/vert/aaa.py", line 24, in <module>
imgContours , conts = utlis.getContours(img,minArea=50000,filter=4)
File "/home/ftsp23/vert/utlis.py", line 6, in getContours
imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.6.0) /tmp/pip-wheel-u79916uk/opencv-python_ea2489746b3a43bfb3f2b5331b7ab47a/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'
我正在树莓派上运行代码,python 版本 3.9.2,使用版本 4.6.0.66(我们必须使用此版本,因为上面的任何版本都不起作用),我已经尝试了多次尝试来修复错误无济于事,如果有人有任何见解或建议,我可以尝试,将不胜感激。谢谢。
答:
0赞
Enos jeba
11/17/2023
#1
path = '/home/ftsp23/vert/phone'
在路径上,您必须正确指定图像路径,扩展名如下所示(假设手机是图像)
path = '/home/ftsp23/vert/phone.jpeg'
评论
1赞
Christoph Rackwitz
11/17/2023
也许,但代码的路径当前处于非活动状态。OP 从网络摄像头读取,而不是从文件读取。
评论
utlis.getContours
utils.getContours
assert success