Opencv2 cvtColor()在raspberry pi上不起作用?

ezykj2lf  于 2023-11-22  发布在  其他
关注(0)|答案(1)|浏览(137)

我正在做一个python opencv2的python代码,基于youtube的教程,Here我直接复制了代码,

  1. import cv2
  2. import utlis
  3. ###################################
  4. webcam = True
  5. path = '/home/ftsp23/vert/phone'
  6. cap = cv2.VideoCapture(0)
  7. cap.set(10,160)
  8. cap.set(3,1920)
  9. cap.set(4,1080)
  10. scale = 3
  11. wP = 210 *scale
  12. hP= 297 *scale
  13. ###################################
  14. while True:
  15. if webcam:success,img = cap.read()
  16. else: img = cv2.imread(path)
  17. imgContours , conts = utlis.getContours(img,minArea=50000,filter=4)
  18. if len(conts) != 0:
  19. biggest = conts[0][2]
  20. #print(biggest)
  21. imgWarp = utlis.warpImg(img, biggest, wP,hP)
  22. imgContours2, conts2 = utlis.getContours(imgWarp,
  23. minArea=2000, filter=4,
  24. cThr=[50,50],draw = False)
  25. if len(conts) != 0:
  26. for obj in conts2:
  27. cv2.polylines(imgContours2,[obj[2]],True,(0,255,0),2)
  28. nPoints = utlis.reorder(obj[2])
  29. nW = round((utlis.findDis(nPoints[0][0]//scale,nPoints[1][0]//scale)/10),1)
  30. nH = round((utlis.findDis(nPoints[0][0]//scale,nPoints[2][0]//scale)/10),1)
  31. cv2.arrowedLine(imgContours2, (nPoints[0][0][0], nPoints[0][0][1]), (nPoints[1][0][0], nPoints[1][0][1]),
  32. (255, 0, 255), 3, 8, 0, 0.05)
  33. cv2.arrowedLine(imgContours2, (nPoints[0][0][0], nPoints[0][0][1]), (nPoints[2][0][0], nPoints[2][0][1]),
  34. (255, 0, 255), 3, 8, 0, 0.05)
  35. x, y, w, h = obj[3]
  36. cv2.putText(imgContours2, '{}cm'.format(nW), (x + 30, y - 10), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.5,
  37. (255, 0, 255), 2)
  38. cv2.putText(imgContours2, '{}cm'.format(nH), (x - 70, y + h // 2), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.5,
  39. (255, 0, 255), 2)
  40. cv2.imshow('A4', imgContours2)
  41. img = cv2.resize(img,(0,0),None,0.5,0.5)
  42. cv2.imshow('Original',img)
  43. cv2.waitKey(1)

字符串
但它返回的错误

  1. Traceback (most recent call last):
  2. File "/home/ftsp23/vert/aaa.py", line 24, in <module>
  3. imgContours , conts = utlis.getContours(img,minArea=50000,filter=4)
  4. File "/home/ftsp23/vert/utlis.py", line 6, in getContours
  5. imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  6. 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'


我运行的代码乌藨子pi,python版本3.9.2,使用版本4.6.0.66(我们必须使用这个版本,因为上面的任何版本都不会工作),我已经尝试了多次尝试来修复错误无济于事,如果有人有任何见解或建议,我可能会尝试将不胜感激.谢谢.

omhiaaxx

omhiaaxx1#

  1. path = '/home/ftsp23/vert/phone'

字符串
在路径上,您必须正确指定图像路径,扩展名如下(假设手机是图像)

  1. path = '/home/ftsp23/vert/phone.jpeg'

相关问题