【问题标题】:Finding the centre coordinates in an image of colored points在彩色点的图像中查找中心坐标
【发布时间】:2022-01-26 01:56:49
【问题描述】:

我正在使用颜色查找坐标,在我的例子中假设为蓝色,并给出图像中具有该颜色的那个点的坐标。

我正在使用此代码:

#!/usr/bin/env python3

import cv2
import numpy as np

#Load image
img = cv2.imread('image.png')

#Define the blue color we want to find 
blue = [255,0,0]

#Get X and Y cooridinates of ALL blue pixels
X,Y = np.where(np.all(img==blue, axis=2))
zipped = np.column_stack((X,Y))

#Get the number of coordinates founded
print(len(zipped))

我将蓝色区域中的所有像素都涂上颜色,而我只需要该特定位置的一个坐标。

此图像包含image_with_blue_coordinates 蓝色坐标(但每个蓝色点至少包含 6 到 8 个蓝色像素),所以我得到了所有坐标,而我只需要中心像素。

关于如何处理这个问题并且只获得 36 个 x,y 坐标而不是 1342 个的任何想法?

提前致谢

参考:Find the coordinates in an image where a specified colour is detected

【问题讨论】:

  • 连接组件标记,或 findContours,或 SimpleBlobDetector

标签: python opencv image-processing colors coordinates


【解决方案1】:

下面是使用 DFS 的更快更好的方法:

import cv2
import numpy as np

dx=[0,0,1,1,-1,-1]
dy=[1,-1,1,-1,1,-1]
visited={}
def dfs(x, y):
    visited[(x,y)]=2
    i=0
    while i<6:
        new_x=x+dx[i]
        new_y=y+dy[i]
        if(not((new_x,new_y)in visited)):
            i+=1
            continue
        if(visited[(new_x,new_y)]==2):
            i+=1
            continue 
        dfs(new_x,new_y)
        i+=1
# Load image
im = cv2.imread('image.png')
# Define the blue colour we want to find - remember OpenCV uses BGR ordering
blue = [255,0,0]

# Get X and Y coordinates of all blue pixels

X,Y = np.where(np.all(im==blue,axis=2))
zipped = np.column_stack((X,Y))
for pixel in zipped:
    x=pixel[0]
    y=pixel[1]
    visited[(x,y)]=1
result=[]
for pixel in zipped:
    x=pixel[0]
    y=pixel[1]
    if visited[(x,y)]==1:
        result.append((x,y))
        dfs(x,y)
print(result)

【讨论】:

    【解决方案2】:

    我找到了另一个使用 Kmeans 的解决方案,以防万一有人需要,这里是代码

    import cv2
    import numpy as np
    from sklearn.cluster import KMeans
    from sklearn.metrics import silhouette_score
    
    # Load image
    im = cv2.imread('image.png')
    
    # Define the blue colour we want to find - remember OpenCV uses BGR ordering
    blue = [255,0,0]
    
    # Get X and Y coordinates of all blue pixels
    
    X,Y = np.where(np.all(im==blue,axis=2))
    zipped = np.column_stack((X,Y))
    print(len(zipped))
    
    sil = []
    kmax = 40
    
    # dissimilarity would not be defined for a single cluster, thus, minimum number of clusters should be 2    
    for k in range(2, kmax+1):
      kmeans = KMeans(n_clusters = k).fit(zipped)
      labels = kmeans.labels_
      sil.append(silhouette_score(zipped, labels, metric = 'euclidean'))
    
    ind = sil.index(max(sil))
    n_points = list(range(2, kmax+1))[ind]
    print(n_points)
    
    kmeans = KMeans(n_clusters=n_points).fit(zipped)
    points = kmeans.cluster_centers_
    print(len(points))
    

    【讨论】:

      猜你喜欢
      • 2015-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-05
      • 1970-01-01
      • 2015-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多