Python 的 cv2 给出消息 "returned NULL without setting an error"
Python's cv2 gives the message "returned NULL without setting an error"
- 我正在尝试编写一个程序来计算图像中斑点的数量。
- 我已从文件中读取图像并使用
PIL
将其转换为灰度。
- 然后我将它变成一个
numpy
数组,并通过一个 for 循环传递该数组,该循环将所有高于特定光强度的像素转换为白色,将所有其他像素转换为黑色。
- 然后我使用
PIL
从该数组创建了一个新图像,一切似乎都运行良好:
- 然后我应用了 cv2 中包含的
SimpleBlobDetector
,但它给了我这个非常不明确的错误消息:
<method 'detect' of 'cv2.Feature2D' objects> returned NULL without setting an error
有谁知道这意味着什么,或者我能做些什么?如果这样可以使事情更清楚,我将包括我目前的代码。
#Reading image from file and converting to grayscale
filename = 'C:/Users/Windows/Desktop/Jobb/Np concentration/Microscopy images/EJ01057wellA10x.tif'
myimg = Image.open(filename).convert('L')
#Turning the pixels of the image into an array, and making that array editable
imgdata = asarray(myimg)
#print(imgdata)
imgdata = imgdata.astype(np.float64)
#Changing all pixels below the light intensity of 200 to black pixels, and all
#above to white
for x in imgdata:
x[x < 200] = 0
x[x >= 200] = 255
#print(imgdata.shape)
#Creating a new image from the edited array, and displaying it
newImg = Image.fromarray(imgdata)
newImg.show()
#TODO: Count white blobs in newImg using a simple blob detector
detector = cv.SimpleBlobDetector()
keypoints = detector.detect(newImg)
print(keypoints)
您正在将 PIL Image 对象传递给 OpenCV 函数。这是不正确的用法,不是 OpenCV 中的错误。 OpenCV(在 python 中)期望图像是 numpy 数组。 编辑: OpenCV 处理这种情况很糟糕,应该引发适当的 python 异常 (TypeError)。这是一个错误,您应该在 OpenCV 上提出一个问题 github 关于这个。
其次,使用 the SimpleBlobDetector_create
method 创建了一个实例 SimpleBlobDetector。它会像你那样失败。
您还应该像这样创建参数:
params = cv.SimpleBlobDetector_Params()
params.filterByArea = ... # True/False, if you need it
params.maxArea = ...
# and other attributes. see docs.
detector = cv.SimpleBlobDetector_create(params)
您根本不需要 PIL。坚持使用 OpenCV 函数来读取和写入图像文件 (imread/imwrite) 以及灰度转换 (cvtColor)。
- 我正在尝试编写一个程序来计算图像中斑点的数量。
- 我已从文件中读取图像并使用
PIL
将其转换为灰度。 - 然后我将它变成一个
numpy
数组,并通过一个 for 循环传递该数组,该循环将所有高于特定光强度的像素转换为白色,将所有其他像素转换为黑色。 - 然后我使用
PIL
从该数组创建了一个新图像,一切似乎都运行良好:
- 然后我应用了 cv2 中包含的
SimpleBlobDetector
,但它给了我这个非常不明确的错误消息:
<method 'detect' of 'cv2.Feature2D' objects> returned NULL without setting an error
有谁知道这意味着什么,或者我能做些什么?如果这样可以使事情更清楚,我将包括我目前的代码。
#Reading image from file and converting to grayscale
filename = 'C:/Users/Windows/Desktop/Jobb/Np concentration/Microscopy images/EJ01057wellA10x.tif'
myimg = Image.open(filename).convert('L')
#Turning the pixels of the image into an array, and making that array editable
imgdata = asarray(myimg)
#print(imgdata)
imgdata = imgdata.astype(np.float64)
#Changing all pixels below the light intensity of 200 to black pixels, and all
#above to white
for x in imgdata:
x[x < 200] = 0
x[x >= 200] = 255
#print(imgdata.shape)
#Creating a new image from the edited array, and displaying it
newImg = Image.fromarray(imgdata)
newImg.show()
#TODO: Count white blobs in newImg using a simple blob detector
detector = cv.SimpleBlobDetector()
keypoints = detector.detect(newImg)
print(keypoints)
您正在将 PIL Image 对象传递给 OpenCV 函数。这是不正确的用法,不是 OpenCV 中的错误。 OpenCV(在 python 中)期望图像是 numpy 数组。 编辑: OpenCV 处理这种情况很糟糕,应该引发适当的 python 异常 (TypeError)。这是一个错误,您应该在 OpenCV 上提出一个问题 github 关于这个。
其次,使用 the SimpleBlobDetector_create
method 创建了一个实例 SimpleBlobDetector。它会像你那样失败。
您还应该像这样创建参数:
params = cv.SimpleBlobDetector_Params()
params.filterByArea = ... # True/False, if you need it
params.maxArea = ...
# and other attributes. see docs.
detector = cv.SimpleBlobDetector_create(params)
您根本不需要 PIL。坚持使用 OpenCV 函数来读取和写入图像文件 (imread/imwrite) 以及灰度转换 (cvtColor)。