【发布时间】:2015-06-03 23:45:17
【问题描述】:
我需要将依赖于 scipy.cluster.vq 模块的代码库转换为不使用 scipy,以便我可以在 C++ 中实现它。
首先,我尝试仅使用 numpy 来复制结果。
从尺寸为 MxNx3 的图像开始,我使用带有 opencv 的 kmeans 创建了一个“质心”Kx3 数组。
我需要将原始图像的每个像素映射到centroids数组中最接近原始像素的像素值。
我可以正常工作,但性能很糟糕。我确信必须有更高级的方法来计算它,我怀疑它与最近邻搜索有关(也许?)但不确定。
这是我目前正在做的事情:我认为这可能被称为“蛮力”方法
- 遍历图像中的每个像素
- 计算该像素与质心列表中每个像素之间的欧式距离
- 从第 2 步生成的列表中返回最小值
-
将原始图像像素分配给返回最小距离的质心列表的值。
def vq(self,image,centroids): x,y,z = image.shape Z=np.reshape(image,(x*y,z)) counts = np.zeros(len(centroids)) clusterMap = np.zeros(Z.shape,np.uint8) for i in range(Z.shape[0]): color = Z[i] closestIndex = self.getClosestCenter(color, centroids) counts[closestIndex]+=1# tracking how often each color occurs clusterMap[i] = centroids[closestIndex] return clusterMap,counts def getClosestCenter(self,color,centers): distances = [0 for i in range(len(centers))] for i,center in enumerate(centers): distances[i] = self.getDistance(color, center) return distances.index(min(distances)) def getDistance(self,value1,value2): if len(value1) !=len(value2): return None #error sum = 0 for i in range(len(value1)): sum+=(value1[i]-value2[i])**2 return sum**(0.5)
【问题讨论】:
标签: python opencv scipy cluster-analysis k-means