【发布时间】:2021-04-08 06:31:51
【问题描述】:
- 我正在尝试编写一个程序来计算图像中的 blob 数量。
- 我已从文件中读取图像并使用
PIL将其转换为灰度。 - 然后我将其转换为
numpy数组,并通过一个 for 循环传递该数组,该循环将高于特定光强度的所有像素转换为白色,将所有其他像素转换为黑色。 - 然后我使用
PIL从该数组创建了一个新图像,一切似乎都运行良好:
- 然后我应用了 cv2 中包含的
SimpleBlobDetector,但它给了我这个非常不具体的错误消息:
返回 NULL 且未设置错误
有谁知道这意味着什么,或者我能做些什么?到目前为止,我将包含我的代码,如果这能让事情更清楚的话。
#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)
【问题讨论】:
-
这是一个 cv2 错误。检查他们的错误跟踪器以获取现有报告,如果没有,请报告。 (确保包含足够的细节来重现错误。)
-
(您的代码也可能有误 - 我没有仔细检查过 - 但您收到的错误消息来自 cv2 错误。)
-
@EmmaJo 而不是
for-loop,最好使用矢量化:imgdata[imgdata< 200] = 0, imgdata[imgdata>200]=255
标签: python arrays opencv image-processing cv2