def calcEquation(sp, cluster, T):
     #print sp.ms, cluster.ck
     cDist = utils.getVectorLength(sp.ms - cluster.ck)
     cDist = cDist * cDist
     #print "cdist", cDist
     eEq = math.exp(-(cDist / T))
     #print "eq", eEq
     return float(cluster.pck * eEq)
  def refine(self, superpixels):
    totalChange = 0
    topValue = 0
    for sp in superpixels.itervalues():
      name = "%d,%d" % (sp.outPos[0], sp.outPos[1]) # ex. 4,3 for row 4 col 3
      topValue += (sp.ms * self.pckps[name] * sp.ps)

    newCk = topValue / self.pck
    totalChange = utils.getVectorLength(self.ck - newCk)
    self.ck = newCk
    return totalChange
    def refine(self, superpixels):
        totalChange = 0
        topValue = 0
        for sp in superpixels:
            name = "%d,%d" % (sp.outPos[0], sp.outPos[1]
                              )  # ex. 4,3 for row 4 col 3
            topValue += (sp.ms * self.pckps[name] * sp.ps)
            #print self.pckps[name]
            #print ("Update top", topValue)

        newCk = topValue / self.pck
        #print topValue, self.pck
        #print self.ck, newCk
        totalChange = utils.getVectorLength(self.ck - newCk)
        #print totalChange
        self.ck = newCk
        return totalChange
def initSuperPixels(imageLAB, rowsOut, colsOut):
    rowsIn, colsIn = support.getSize(imageLAB)
    N = rowsOut * colsOut
    superpixels = []

    r_step = rowsIn / rowsOut
    r_init = r_step / 2
    c_step = colsIn / colsOut
    c_init = c_step / 2

    cur_r = r_init
    cur_c = c_init

    # For each superpixel
    # Initialize outPos and inPos
    for out_r in range(0, rowsOut, 1):
        for out_c in range(0, colsOut, 1):
            sp = SuperPixel(support.makeVector(out_r,out_c),\
                            support.makeVector(cur_r,cur_c), N)
            superpixels.append(sp)

            cur_c += c_step
        cur_r += r_step
        cur_c = c_init

    # For each input pixel
    # Inialize what pixels are associated with what superpixels
    # Input pixels are assigned to the nearest superpixel in (x,y) space

    for r in range(0, rowsIn, 1):
        for c in range(0, colsIn, 1):
            pixPos = support.makeVector(r, c)
            minDist = 1000  #Initially a large number
            cur_sp = None
            for sp in superpixels:
                dist = utils.getVectorLength(pixPos - sp.inPos)
                if dist < minDist:
                    minDist = dist
                    cur_sp = sp
            cur_sp.addPixel(imageLAB, pixPos)

    return superpixels
 def calcDiff(self, pixelPos, imageLAB, N, M):
   dc = utils.getVectorLength(self.ms - imageLAB[int(pixelPos[0]),\
                                                 int(pixelPos[1])]) # Color difference
   dp = utils.getVectorLength(pixelPos - self.inPos)# Positional difference
   return dc + m * math.sqrt(N/M) * dp
 def calcEquation(sp, cluster, T):
   cDist = utils.getVectorLength(sp.ms - cluster.ck)
   cDist = cDist * cDist
   eEq = float(math.exp(-(cDist/T)))
   return cluster.pck * Eq
 def shouldSplit(self):
   return utils.getVectorLength(self.sub1.ck-self.sub2.ck) > ep_cluster