# find all of the connected components (white blobs in your image).
# im_with_separated_blobs is an image where each detected blob has a different pixel value ranging from 1 to nb_blobs - 1.
nb_blobs, im_with_separated_blobs, stats, _ = cv2.connectedComponentsWithStats(im)
# stats (and the silenced output centroids) gives some information about the blobs. See the docs for more information.
# here, we're interested only in the size of the blobs, contained in the last column of stats.
sizes = stats[:, -1]
# the following lines result in taking out the background which is also considered a component, which I find for most applications to not be the expected output.
# you may also keep the results as they are by commenting out the following lines. You'll have to update the ranges in the for loop below.
sizes = sizes[1:]
nb_blobs -= 1
# minimum size of particles we want to keep (number of pixels).
# here, it's a fixed value, but you can set it as you want, eg the mean of the sizes or whatever.
min_size = 150
# output image with only the kept components
im_result = np.zeros_like(im_with_separated_blobs)
# for every component in the image, keep it only if it's above min_size
for blob in range(nb_blobs):
if sizes[blob] >= min_size:
# see description of im_with_separated_blobs above
im_result[im_with_separated_blobs == blob + 1] = 255
mask = np.zeros_like(img)
for contour in contours:
area = cv2.contourArea(contour)
if area > noise_removal_threshold:
cv2.fillPoly(mask, [contour], 255)
4条答案
按热度按时间1tu0hz3e1#
使用
connectedComponentsWithStats
(doc)如何:输出:
bnlyeluc2#
为了自动删除对象,您需要在图像中找到它们。从您提供的图像中,我看不到任何可以区分7个突出显示的项目的内容。您必须告诉您的计算机如何识别您不想要的对象。如果它们看起来相同,这是不可能的。
如果你有多个图像,其中的对象总是看起来像你可以使用模板匹配技术。
此外,关闭操作对我来说没有多大意义。
dhxwm5r43#
对于孤立或未连接的Blob:试试这个(你可以设置noise_removal_threshold为你喜欢的任何值,并使它相对于最大的轮廓,例如,或者像100或25这样的标称值)。
niknxzdl4#
按区域去除小连通分量称为区域打开,OpenCV没有这个功能,可以按其他答案实现,但大多数其他图像处理包都有区域打开功能。
例如,使用scikit-image:
例如,使用DIPlib:
PS:DIPlib的实现明显更快。免责声明:我是DIPlib的作者。