这是我在Python(3.5.1)和OpenCV(3)中的第一个项目,因此我为自己的错误感到抱歉。
我有一些像这样的图片:
https://s12.postimg.org/ox8gw5l8d/gado.jpg
我需要计算这张图片上有多少白色物体。我尝试过使用SimpleBlobDetector,但没有按预期工作。
# Standard imports
import cv2
import numpy as np;
# Read image
im = cv2.imread("C:/opencvTests/cattle.jpg", cv2.IMREAD_GRAYSCALE)
# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()
#filter by color
params.filterByColor = True
params.blobColor = 255
# Filter by Convexity
params.filterByConvexity = True
params.minConvexity = 0.87
# Filter by Inertia
params.filterByInertia = True
params.minInertiaRatio = 0.08
# Create a detector with the parameters
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3 :
detector = cv2.SimpleBlobDetector(params)
else :
detector = cv2.SimpleBlobDetector_create(params)
# Detect blobs.
keypoints = detector.detect(im)
# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle corresponds to the size of blob
im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# Show keypoints
cv2.imwrite("C:/opencvTests/blobSave.jpg",im_with_keypoints)
print("Total of objects")
print(len(keypoints))
任何帮助将不胜感激!提前致谢
最佳答案
如果继续处理此图像,则可能会导致计数错误。执行一些预处理操作(例如形态学操作)以消除噪声,并使对象彼此分离。之后,使用“findcontours”内置的opencv函数。然后读取“findcontours”的大小,这将给出对象的数量。
关于python - 计算OpenCV的对象数量-Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39261378/