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): name = "%d,%d" % (out_r, out_c) # ex. 4,3 for row 4 col 3 sp = SuperPixel(support.makeVector(out_r,out_c),\ support.makeVector(cur_r,cur_c), N) superpixels[name] = sp cur_c += c_step cur_r += r_step cur_c = c_init # Run SLIC refineSuperPixels(imageLAB, superpixels) return superpixels
def updateGradients(Gr, Gc, G): if useFile: Tr = support.loadMatrix(Tr_file) Tc = support.loadMatrix(Tc_file) else: rows, cols = support.getSize(G) # 1. Compute the median of G and the max of G medVal_G = np.median(G) max_G = np.max(G) # 2. For each G[r,c] below a threshold*max for r in range(0, rows, 1): for c in range(0, cols, 1): val = G[r, c] if val < threshold * max_G: #compute unit vector from center to r,c v = normalizeVector( support.makeVector(r, c) - support.makeVector(int(rows / 2), int(cols / 2))) # scale vector by the median v = v * medVal_G # set Gr, Gc, G at (r,c to the components and magnitude of the vector) Gr[r, c] = v[0] Gc[r, c] = v[1] G[r, c] = math.sqrt(v[0] * v[0] + v[1] * v[1]) Tr, Tc = linedrawing.gradientTangents(Gr, Gc) if useEtf: Tr, Tc = linedrawing.computeETF(Tr, Tc, G) return Tr, Tc
def phi(x, y): T_at_x = support.makeVector(Tx[x[0], x[1]], Ty[x[0], x[1]]) T_at_y = support.makeVector(Tx[y[0], y[1]], Ty[y[0], y[1]]) result = getDotProduct(T_at_x, T_at_y) if (result > 0): return 1 return -1
def phi(x, y, Tx_cur, Ty_cur): T_at_x = support.makeVector(Tx_cur[x[0], x[1]], Ty_cur[x[0], x[1]]) T_at_y = support.makeVector(Tx_cur[y[0], y[1]], Ty_cur[y[0], y[1]]) result = getDotProduct(T_at_x, T_at_y) if (result < 0): return 1 return -1
def __init__(self, outPos, inPos, N): self.outPos = outPos self.inPos = inPos self.ms = 0 self.ps = 1.0/N self.size = 0 self.totalWeight = support.makeVector(0,0,0) self.color = support.makeVector(0,0,0)
def getTangentVector(Tx, Ty, z): rows, cols = support.getSize(Tx) T = support.makeVector(Tx[z[0], z[1]], Ty[z[0], z[1]]) T_tan = rotateCW(T) # round answers to integers Tx_tan = int(T_tan[0]) Ty_tan = int(T_tan[1]) return support.makeVector(Tx_tan, Ty_tan)
def normalizeVector(v): r = v[0] c = v[1] length = math.sqrt(v[0] * v[0] + v[1] * v[1]) if length < .00001 and length > -.00001: # divide by 0 (ignore) return support.makeVector(0, 0) else: return support.makeVector(r / length, c / length)
def rotateCCW(v): r = v[0] c = v[1] # if r and c are the same sign # return -r +c if (r > 0 and c > 0) or (r < 0) and (c < 0): return support.makeVector(-v[0], v[1]) # else return +r -c return support.makeVector(v[0], -v[1])
def normalizeVector(v): r = v[0] c = v[1] length = getVectorLength(v) if length < .00001 and length > -.00001: # divide by 0 (ignore) return support.makeVector(0, 0) else: return support.makeVector(r / length, c / length)
def dWeight(x, y, Tx_cur, Ty_cur): T_at_x = support.makeVector(Tx_cur[x[0], x[1]], Ty_cur[x[0], x[1]]) T_at_y = support.makeVector(Tx_cur[y[0], y[1]], Ty_cur[y[0], y[1]]) result = getDotProduct(T_at_x, T_at_y) # absolute value of result (always 0 or positive) if (result < 0): result = -result #print(tcur_x, tcur_y) #print(result) return result
def __init__(self, lF, rF, tpr, tpc, bpr, bpc, a, b, c): self.lF = int(lF) # Left Face self.rF = int(rF) # Right Face self.top = support.makeVector(tpr, tpc) self.top = np.round(self.top) self.bot = support.makeVector(bpr, bpc) self.bot = np.round(self.bot) self.a = a self.b = b self.c = c self.sect = 0
def draw(self, drawing, G, Tr, Tc): pix_map = drawing.load() draw = ImageDraw.Draw(drawing) rows, cols = support.getSize(G) p = np.around(self.p) # Go forward x = p x_prev = x i = -1 lastSample = 1000 while not isOutOfBounds(rows, cols, x) and i < self.length / 2: drawStroke(draw, x_prev * antialiased_sf, x * antialiased_sf, self.width, self.color) newSample = interpolate(x[0], x[1], G) if (clipping and newSample > lastSample): #print "clip" break x_prev = x if fixedOrientation: x = x + support.makeVector(-1, 1) #45 degrees else: x = x + getNewDir(Tr[x[0], x[1]], Tc[x[0], x[1]], self.thetap) lastSample = newSample i = i + 1 # Go backwards points = [] x = p x_prev = x i = 0 lastSample = 1000 while not isOutOfBounds(rows, cols, x) and i < self.length / 2: if not i == 0: drawStroke(draw, x_prev * antialiased_sf, x * antialiased_sf, self.width, self.color) newSample = interpolate(x[0], x[1], G) if (clipping and newSample > lastSample): #print "clip" break x_prev = x if fixedOrientation: x = x + support.makeVector(1, -1) #45 degrees else: x = x - getNewDir(Tr[x[0], x[1]], Tc[x[0], x[1]], self.thetap) lastSample = newSample i = i + 1
def __init__(self, p, color_img): self.p = p self.length = random.randint(r_strokel[0], r_strokel[1] + 1) self.width = random.randint(r_strokew[0], r_strokew[1] + 1) self.thetap = getRandPerturb(r_theta) self.dir = support.makeVector(Tr[p[0], p[1]], Tc[p[0], p[1]]) self.end1 = (0, 0) self.end2 = (0, 0) val = interpolate(p[0], p[1], color_img) r = clamp(val[0] + getRandPerturb(r_color)) g = clamp(val[1] + getRandPerturb(r_color)) b = clamp(val[2] + getRandPerturb(r_color)) self.color = support.makeVector(r, g, b) # is [0..1]
def __init__(self, r, c, ETFr, ETFc, direction): self.ETFr = ETFr self.ETFc = ETFc self.rows, self.cols = support.getSize(ETFr) self.F = support.makeVector(ETFr[r,c], ETFc[r,c]) if self.F[0] == 0 and self.F[1] == 0: self.F = getVectorCP(self.rows,self.cols,r,c) if direction == 'neg': self.F = -self.F self.v = support.makeVector(0,0) self.x = support.makeVector(r,c) self.prev_x = self.x self.a = self.F / m
def gradientTangents(Gx, Gy): rows, cols = support.getSize(Gx) zero_matrix = support.makeMatrix(rows, cols) Tx, Ty = zero_matrix, zero_matrix for r in range(0, rows, 1): for c in range(0, cols, 1): vec = support.makeVector(Gx[r, c], Gy[r, c]) tangent = rotateCCW(vec) #print(tangent) normaltangent = normalizeVector( support.makeVector(tangent[0], tangent[1])) #print(normaltangent) Tx[r, c], Ty[r, c] = normaltangent[0], normaltangent[1] return Tx, Ty
def normalizeVector3D(v): length = math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]) if length < .00001 and length > -.00001: # divide by 0 (ignore) return support.makeVector(0, 0, 0) else: return v / length
def filteringFramework(x, Tx, Ty): r = x[0] # row of x c = x[1] # col of x end = int(q / 2) total_sum = 0 # Calculate Direction tan = support.makeVector(Tx[r, c], Ty[r, c]) direct = rotateCW(tan) # Loop forwards (t=1,2,3,4) init_x = x for t in range(1, end, 1): z = np.add(x, delta_n * direct) z = z.astype(int) total_sum = total_sum + image[z[0], z[1]] * filter1d(t) x = z # Loop backwards (t=1,2,3,4) x = init_x for t in range(1, end, 1): z = np.subtract(x, delta_n * direct) z = z.astype(int) total_sum = total_sum + image[z[0], z[1]] * filter1d(t) x = z # t = 0 return total_sum + image[r, c] * filter1d(0)
def updateCentroid(self): prev_center = self.centroid ## if self.denom == 0: ## self.denom = 1 self.centroid = support.makeVector(round(self.rNum / self.denom), round(self.cNum / self.denom)) return getPointDist(prev_center, self.centroid)
def initPalette(imageLAB): # Calculate the average of each channel to get the average color L = imageLAB[:,:,0].mean() A = imageLAB[:,:,1].mean() B = imageLAB[:,:,2].mean() color = support.makeVector(L,A,B) return [Cluster(SubCluster(color, 0.5), SubCluster(color+perturb, 0.5))]
def getNewDir(self, x, perturb): x = x.astype(int) Tr_val = Tr[x[0], x[1]] Tc_val = Tc[x[0], x[1]] if fixedDir: return self.dir elif wobble: theta = math.atan2(Tr_val, Tc_val) perturb = math.radians(perturb) theta = theta + perturb return support.makeVector(math.sin(theta), math.cos(theta)) else: return support.makeVector(Tr_val, Tc_val)
def advance(self): q = s * curveF(self.t) qv = q * self.v F = support.makeVector(-qv[1], qv[0]) a = F/m self.v = self.v + a*deltaT self.prev_x = self.x self.x = self.x + self.v*deltaT self.t = self.t + deltaT
def __init__(self,r, c, rows, cols, direction): self.t = 0 self.v = getVectorCP(rows, cols, r, c) if direction == 'neg': self.v = -self.v self.x = support.makeVector(r,c) self.prev_x = self.x self.rows = rows self.cols = cols self.dir = 1
def updateForce(self): r, c = int(self.x[0]), int(self.x[1]) newF = support.makeVector(self.ETFr[r,c], self.ETFc[r,c]) # if force is now 0,0 continue using old force, otherwise if newF[0] != 0 or newF[1] != 0: # if obtuse angle, flip the force to make it acute if np.dot(self.F, newF) < 0: newF = -newF self.F = newF self.a = self.F / m
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 getNewDir(Tr_val, Tc_val, perturb): perturb = math.radians(perturb) if Tc_val == 0: #print('zero!') theta = 45 else: theta = math.atan(Tr_val / Tc_val) if wobble: theta = theta + perturb return support.makeVector(math.sin(theta), math.cos(theta))
def normalizeVector(v): r = v[0] c = v[1] length = getVectorLength(v) #print("length: %d" % length) if length < .000001 and length > -.00001: # divide by 0 (ignore) #print("Ignored") return r, c else: return support.makeVector(r / length, c / length)
def filterAccumulated(x, Tx, Ty): r = x[0] # row of x c = x[1] # col of x rows, cols = support.getSize(Tx) end = int(p / 2) total_sum = 0 # Loop forwards (s=1,2,3,4) for s in range(1, end, 1): tan = getTangentVector(Tx, Ty, x) if isZeroVector(tan): break #else: #print(r,s) z = np.add(x, delta_m * tan) z = z.astype(int) if isOutOfBounds(rows, cols, z): break total_sum = total_sum + gaussian(s, sig_m) * filteringFramework( z, Tx, Ty) x = z # Loop backwards x = support.makeVector(r, c) # reset x to s=0 for s in range(1, end, 1): tan = getTangentVector(Tx, Ty, x) if isZeroVector(tan): break #else: #print(r,s) z = np.subtract(x, delta_m * tan) z.astype(int) if isOutOfBounds(rows, cols, z): break total_sum = total_sum + gaussian(s, sig_m) * filteringFramework( z, Tx, Ty) x = z # s = 0 x = support.makeVector(r, c) # reset to s = 0 return gaussian(0, sig_m) * filteringFramework(x, Tx, Ty)
def computeCentroid(P, Q, r1, c1, r2, c2): c1 = c1 - 1 # Inclusive rather than exclusive denom = 0 rNum = 0 cNum = 0 for r in range(r1, r2 + 1, 1): denom = denom + P[r, c2] - P[r, c1] rNum = rNum + (r * (P[r, c2] - P[r, c1])) cNum = cNum + (c2 * P[r, c2] - Q[r, c2]) - (c1 * P[r, c1] - Q[r, c1]) return support.makeVector(rNum / denom, cNum / denom)
def generatePoints(image): rows, cols = support.getSize(image) if pointsGen == 'grid': size = math.sqrt(N) r_step = int(rows / size) r_init = r_step / 2 c_step = int(cols / size) c_init = c_step / 2 # Find points pointsList = [] for r in range(r_init, rows - 1, r_step): for c in range(c_init, cols - 1, c_step): pointsList.append([r, c]) return pointsList elif pointsGen == 'random': chosenPoints = {} while len(chosenPoints) < N: r = random.randint(10, rows - 10) c = random.randint(10, cols - 10) # if r,c is not already a chosen point value = (r, c) if value not in chosenPoints: chosenPoints[r, c] = support.makeVector(r, c) pointsList = [] for key, value in chosenPoints.iteritems(): pointsList.append(value) pointsList.sort(cmp=comparePoints) return pointsList elif pointsGen == 'file': matrix = support.loadMatrix(npy_file) rows, cols = support.getSize(matrix) pointsList = [] for r in range(0, rows, 1): value = matrix[r, 0] pointsList.append(value) return pointsList
def refineSuperPixels(imageLAB, superpixels): rowsIn, colsIn = support.getSize(imageLAB) N = len(superpixels) M = rowsIn * colsIn # Reset superpixels for sp in superpixels: sp.clear() # For each pixel in the input image for r in range(0, rowsIn, 1): for c in range(0, colsIn, 1): pixPos = support.makeVector(r, c) minDiff = 1000 #Initially a large number cur_sp = None for sp in superpixels: diff = sp.calcDiff(pixPos, imageLAB, N, M) if diff < minDiff: minDiff = diff cur_sp = sp cur_sp.addPixel(imageLAB, pixPos)