【问题标题】:<built-in function imread> returned NULL without setting an error<内置函数 imread> 返回 NULL 而不设置错误
【发布时间】:2020-04-23 17:40:52
【问题描述】:
    def image_descriptors(file):
      img = cv2.imread(file,0)
      img = cv2.resize(img, (256, 256))
      _ , descriptors = cv2.SIFT().detectAndCompute(img, None)
   return descriptors
   def folder_descriptors(folder):
     cv_img=[]
     for img in glob.glob("*.jpg"):
         n=cv2.imread(img)
         cv_img.append(n)
   print("Calculating descriptos. Number of images is", len(cv_img))
   return np.concatenate([image_descriptors(file) for file in cv_img])

我在输出屏幕中得到以下信息: 计算描述。图像数量为 274 SystemError: 返回 NULL 而没有设置错误

【问题讨论】:

    标签: python opencv


    【解决方案1】:

    Path.glob 返回Path 对象的列表(Windows 中的WindowsPath 或Linux 中的PosixPath)。和 cv2.imread 期望 字符串

    您可以使用str 类(str(path_object))从Path 对象获取路径字符串,或者从Path 对象(path_object.__str__())构建魔术方法

    def image_descriptors(file):
      img = cv2.imread(file,0)
      img = cv2.resize(img, (256, 256))
      _ , descriptors = cv2.SIFT().detectAndCompute(img, None)
      return descriptors
    def folder_descriptors(folder):
      cv_img=[]
      for img in glob.glob("*.jpg"):
         # convert `Path` object to string
         n=cv2.imread(str(img))
         cv_img.append(n)
      print("Calculating descriptos. Number of images is", len(cv_img))
      return np.concatenate([image_descriptors(file) for file in cv_img])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-14
      • 2021-04-05
      • 2020-12-13
      • 2017-09-20
      • 1970-01-01
      • 1970-01-01
      • 2021-04-08
      • 1970-01-01
      相关资源
      最近更新 更多