简体   繁体   English

在图片opencv/python中查找簇

[英]finding clusters in a picture opencv/python

looking at trying to find the center point of "clusters" / "circles" / "ellipses" ect in an image.看着试图在图像中找到“簇”/“圆”/“椭圆”等的中心点。

Example image:示例图片:

有 3 个集群的图片

It is clear by eye that there are 3ish clusters.肉眼可以清楚地看到有 3 个集群。 I am looking to find the center point of the clusters, and the defining - rectangle, or circle (with radius) or ellipse, however to describe it.我正在寻找集群的中心点,以及定义 - 矩形、圆(带半径)或椭圆,但是要描述它。

Ideally, it is expanded to the following cases:理想情况下,它扩展到以下情况:

  1. Unknown number of clusters (assume min 1, max 10) for simplicity if needed.如果需要,为简单起见,未知数量的集群(假设最小 1,最大 10)。

This picture is part of a larger picture that made use of canny-edge detection, thresholding and then contours to find me this area of interest.这张图片是一张大图的一部分,它利用精明的边缘检测、阈值处理和轮廓来找到我感兴趣的区域。 Unfortunately, I need it to be...more refined (or better parameters but I couldn't find anything that worked)不幸的是,我需要它......更精致(或更好的参数,但我找不到任何有用的东西)

Things I have tried:我尝试过的事情:

I have tried using kmeans segmentation, but they are mainly for color segments.我曾尝试使用 kmeans 分割,但它们主要用于颜色分割。 And this image could be black/white for all the information the colors give us.对于 colors 给我们的所有信息,此图像可能是黑白的。 HughCircles in opencv are not really the circles I want in that they give me way too many circles that matches "edges" Template matching via opencv also didnt work since it was too constrained and these cant be exact matches. opencv 中的 HughCircles 并不是我真正想要的圆圈,因为它们给了我太多与“边缘”匹配的圆圈 通过 opencv 匹配的模板也不起作用,因为它太受约束而且这些不能完全匹配。

Any suggestions/avenues to examine would be welcome!欢迎任何建议/检查途径!
I have also attempted some basic scatterplot k-means clustering (as if this is data) but have not had good results so far.我还尝试了一些基本的散点图 k 均值聚类(好像这是数据),但到目前为止还没有得到好的结果。

Language of choice: python, but adaptable.选择的语言:python,但适应性强。

I had an attempt at this and it may give you some ideas on how to proceed - even if only by seeing where it fails to work.我对此进行了尝试,它可能会给您一些关于如何进行的想法 - 即使只是通过查看它无法工作的地方。 I wanted to post it before the question gets a third close vote.我想在问题获得第三次近距离投票之前发布它。

#!/usr/bin/env python3

import cv2
import numpy as np

# Load image and make greyscale version too
im = cv2.imread('dl9Vx.png')
grey = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

# Threshold (in case we do morphology) and invert
_, thresh = cv2.threshold(grey, 254, 255, cv2.THRESH_BINARY_INV)
cv2.imwrite('DEBUG-thresh.png', thresh)

# Prepare to do some K-means
# https://docs.opencv.org/4.x/d1/d5c/tutorial_py_kmeans_opencv.html
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
# Find x,y coordinates of all non-white pixels in original image
Y, X = np.where(thresh==255)
Z = np.column_stack((X,Y)).astype(np.float32)

nClusters = 3
ret,label,center=cv2.kmeans(Z,nClusters,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)

# Mark and display cluster centres 
for x,y in center:
    print(f'Cluster centre: [{int(x)},{int(y)}]')
    cv2.drawMarker(im, (int(x), int(y)), [0,0,255])

cv2.imwrite('result.png', im)

Output Output

Cluster centre: [103,65]
Cluster centre: [50,93]
Cluster centre: [60,29]

在此处输入图像描述


Notes:笔记:

Note 1 : I asked it to search for 3 clusters as that was the number you suggested.注 1 :我要求它搜索 3 个集群,因为那是您建议的数字。 There are ways of automagically determining the number of clusters - search for "Elbow Method" for example.有一些方法可以自动确定集群的数量——例如搜索“Elbow Method”

Note 2 : If you wanted to do a morphological closing of the gaps, to make the clusters more solid, you could add in this code after cv2.threshold() :注意 2 :如果您想对间隙进行形态闭合,以使簇更坚固,您可以在cv2.threshold()之后添加此代码:

kernel = np.ones((3,3),np.uint8)
thresh = cv2.dilate(thresh, kernel, iterations=1)

and it will make the thresholded image look like this:它会使阈值图像看起来像这样:

在此处输入图像描述

Note 3 :注3

You could remove the thresholding call and change the np.where() line to:您可以删除阈值调用并将np.where()行更改为:

Y, X = np.where(im!=255)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM