我已经安装了 opencv-python 并导入了 cv2。 但是 CV2 的所有函数都没有出现,当我运行下面的代码时,我收到一条错误消息:“AttributeError: 'NoneType' object has no attribute 'shape'”,这是不对的。
import argparse
import cv2
# =============================================
# This section is just a path to the image
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="Path to the image")
args = vars(ap.parse_args())
# =============================================
image = cv2.imread(args["image"])
print("width: {} pixels".format(image.shape[1]))
print("height: {} pixels".format(image.shape[0]))
print("channels: {}".format(image.shape[2]))
cv2.imshow("Image", image)
cv2.waitKey(0)
了解更多:
AttributeError: 'NoneType' object has no attribute 'shape'
并不意味着 CV2 未被识别,事实上,它是,否则,您将在该行收到错误消息
cv2.imread(args["image"])
你会得到一个
Cannot find module cv2
错误
AttributeError: 'NoneType' object has no attribute 'shape'
表示您在其上使用 .shape 函数的对象是 Nonetype(ig no object)
因此,这意味着你的图像对象(
image = cv2.imread(args["image"])
)是Nonetype(你应该打印它来验证)
一个可能的原因是
args["image"]
没有返回你所期望的(在你的情况下是图像的路径),你应该验证你的图像路径是正确的