Demo entry 6350726

slic_code

   

Submitted by anonymous on Mar 14, 2017 at 06:13
Language: Python. Code size: 756 Bytes.

def slic(img, k, compactness, maxInteration):
    h, w, nchannels = img.shape
    # 获取初始聚类中心并记录与所有像素与其类中心的距离
    centroids, curDis = getInitialCentroid(img, k)
    iter = 0
    s = h * w / k
    while iter < maxInteration:
        # 遍历所有像素位置
        for i in range(0, h):
            for j in range(0, w):

                # 遍历所有与当前像素空间距离小于S的聚类中心,
                # 获取与当前像素距离最小的聚类中心
                centroidsWithInS = getCentroidsWithInS(i, j, centroids)
                for centroid in centroidsWithInS:
                    dis = getDistance(i, j, img, centroid, compactness)
                    #找到更近的聚类中心,更新当前像素的聚类中心
                    if dis < curDis[i, j]:
                        updateCentroid(i, j, centroid)
        iter += 1

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).