计算对象的数量OpenCV - Python

时间:2016-09-01 01:26:32

标签: python opencv

这是我在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))

任何帮助都会非常感激!提前致谢

3 个答案:

答案 0 :(得分:2)

如果继续处理此图像,可能会导致错误计数。执行一些预处理操作(例如形态学操作)以消除噪声并且还将对象彼此分离。在此之后使用&#34; findcontours&#34;内置的opencv功能。然后阅读&#34; findcontours&#34;的大小。这将给出对象的计数。

答案 1 :(得分:1)

我非常接近答案,我相信我只需要根据图像更改一些参数。 如果有人需要这方面的东西,这是我的代码:

# Standard imports
import cv2
import numpy as np;

# Read image
im = cv2.imread("C:/opencvTests/original.jpg", cv2.IMREAD_GRAYSCALE)

#Apply treshold
ret,im = cv2.threshold(im,240,255,cv2.THRESH_BINARY)

kernel = np.ones((6,6),np.uint8)
erosion = cv2.erode(im,kernel,iterations = 1)
opening = cv2.morphologyEx(im, cv2.MORPH_OPEN, kernel)
im = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel)


# 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/keypoints.jpg",im_with_keypoints)
print("Total of objects")
print(len(keypoints))

非常感谢!

答案 2 :(得分:-1)

我需要实现相同的逻辑。任务是从谷歌地图视图中检测房屋数量。

读取灰度图像 应用阈值 应用形态学操作进行降噪 使用 cv2.findContour() 函数,对 blob 进行计数

这是我写的代码的一部分:

img = cv2.imread("images/map_snippet2.png", 0)

ret, thresh = cv2.threshold(img, 242, 255, cv2.THRESH_TOZERO_INV)   # every pixel below the threshold is turned white
ret, thresh_final = cv2.threshold(thresh, 240, 255, cv2.THRESH_TOZERO)  # every pixel below the threshold is turned black
# using the above 2 lines, we only took values in range [240, 242]

# applying morphological operaations to reduce noise
kernel = np.ones((3,3), np.uint8)
morph_img = cv2.erode(thresh_final, kernel, iterations=1)

# using findContours() function to count the white blobs. 
# using this function on morph_img is better than using it on canny_img

blob, hierarchy = cv2.findContours(morph_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
blob_count = len(blob)

plt.imshow(morph_img, 'gray')   # this will count the number of white blobs in the images
plt.xlabel("Count = " + str(blob_count))
plt.ylabel("Detecting # of blobs")

plt.show()

希望能帮到你