当前位置:   article > 正文

报错module ‘cv2‘ has no attribute ‘aruco‘_attributeerror: module 'cv2' has no attribute 'aru

attributeerror: module 'cv2' has no attribute 'aruco

 运行代码如下

  1. """
  2. This script generates some marker images ready to be printed using Aruco
  3. """
  4. # Import required packages
  5. import cv2
  6. from matplotlib import pyplot as plt
  7. import sys,os
  8. dirname, filename = os.path.split(os.path.abspath( __file__))
  9. os.chdir(dirname)
  10. def show_img_with_matplotlib(color_img, title, pos):
  11. """ Shows an image using matplotlib capabilities """
  12. # Convert BGR image to RGB
  13. img_RGB = color_img[:, :, ::-1]
  14. ax = plt.subplot(1, 3, pos)
  15. plt.imshow(img_RGB)
  16. plt.title(title)
  17. plt.axis('off')
  18. # Create the dimensions of the figure and set title:
  19. fig = plt.figure(figsize=(12, 5))
  20. plt.suptitle("Aruco markers creation", fontsize=14, fontweight='bold')
  21. fig.patch.set_facecolor('silver')
  22. # The first step is to create the dictionary object. Aruco has some predefined dictionaries.
  23. # (DICT_4X4_100, DICT_4X4_1000, DICT_4X4_250, DICT_4X4_50 = 0, .... , DICT_7X7_1000)
  24. # We are going to create a dictionary, which is composed by 250 markers.
  25. # Each marker will be of 7x7 bits (DICT_7X7_250):
  26. aruco_dictionary = cv2.aruco.Dictionary_get(cv2.aruco.DICT_7X7_250)
  27. # Now we can draw a marker using 'cv2.aruco.drawMarker()'.
  28. # The function 'cv2.aruco.drawMarker()' returns the marker image ready to be printed (in a canonical form)
  29. # The first parameter is the dictionary object 'aruco_dictionary' we have created previously
  30. # using 'cv2.aruco.Dictionary_get()'
  31. # The second parameter is the marker id, which ranges between 0 and 249 (our dictionary has 250 markers)
  32. # The third parameter is the size of the image to be drawn. in this case, the marker will have a size of 600x600 pixels
  33. # The fourth (optional, by default 1) parameter is the number of bits in marker borders
  34. aruco_marker_1 = cv2.aruco.drawMarker(dictionary=aruco_dictionary, id=2, sidePixels=600, borderBits=1)
  35. aruco_marker_2 = cv2.aruco.drawMarker(dictionary=aruco_dictionary, id=2, sidePixels=600, borderBits=2)
  36. aruco_marker_3 = cv2.aruco.drawMarker(dictionary=aruco_dictionary, id=2, sidePixels=600, borderBits=3)
  37. # We save the created markers using 'cv2.imwrite()':
  38. cv2.imwrite("marker_DICT_7X7_250_600_1.png", aruco_marker_1)
  39. cv2.imwrite("marker_DICT_7X7_250_600_2.png", aruco_marker_2)
  40. cv2.imwrite("marker_DICT_7X7_250_600_3.png", aruco_marker_3)
  41. # Plot the images:
  42. show_img_with_matplotlib(cv2.cvtColor(aruco_marker_1, cv2.COLOR_GRAY2BGR), "marker_DICT_7X7_250_600_1", 1)
  43. show_img_with_matplotlib(cv2.cvtColor(aruco_marker_2, cv2.COLOR_GRAY2BGR), "marker_DICT_7X7_250_600_2", 2)
  44. show_img_with_matplotlib(cv2.cvtColor(aruco_marker_3, cv2.COLOR_GRAY2BGR), "marker_DICT_7X7_250_600_3", 3)
  45. # Show the Figure:
  46. plt.show()

运行如果出这个错误:module 'cv2' has no attribute 'aruco'

可以先把opencv卸载了,再重新安装,按照下面步骤执行;

重新安装的时候,如果下载到一半出错了,继续重试,不断重试,最终可以安装好的

  1. pip uninstall opencv-contrib-python
  2. pip uninstall opencv-python
  3. pip install opencv-python
  4. pip install opencv-contrib-python

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/874603
推荐阅读
相关标签
  

闽ICP备14008679号