Ejemplo n.º 1
0
def gradientTangents(Gx, Gy):
    rows, cols = support.getSize(Gx)
    Tx = support.makeMatrix(rows, cols)
    Ty = support.makeMatrix(rows, cols)
    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)
            normaltangent = normalizeVector(tangent)
            Tx[r, c], Ty[r, c] = normaltangent[0], normaltangent[1]

    return Tx, Ty
def renderTiles(rows, cols, rectsList):
    image = support.makeMatrix(rows, cols, 255)

    for rect in rectsList:
        rect.draw_tiles(image)

    return image
def renderDots(rows, cols, rectsList):
    image = support.makeMatrix(rows, cols, 255)

    for rect in rectsList:
        rect.draw_center(image)

    return image
def renderOutline(rows, cols, rectsList):
    image = support.makeMatrix(rows, cols, 255)

    for rect in rectsList:
        rect.draw_outline(image)

    return image
def computeP(I):
    rows, cols = support.getSize(I)
    P = support.makeMatrix(rows, cols)
    P[0:rows, 0] = I[0:rows, 0]
    for r in range(0, rows, 1):
        for c in range(1, cols, 1):
            P[r, c] = I[r, c] + P[r, c - 1]
    return P
def computeQ(P):
    rows, cols = support.getSize(P)
    Q = support.makeMatrix(rows, cols)
    Q[0:rows, 1] = P[0:rows, 0]
    for r in range(0, rows, 1):
        for c in range(2, cols, 1):
            Q[r, c] = P[r, c - 1] + Q[r, c - 1]

    return Q
def computeField(strokesList, rows, cols):
    heightMap = support.loadImage(heightMap_f) / 255.0
    opacityMap = support.loadImage(opacityMap_f) / 255.0
    mapRows, mapCols = support.getSize(heightMap)

    heightField = support.makeMatrix(rows, cols, 0)
    colorField = support.makeVecMatrix(rows, cols, 3)

    # for each stroke
    print "Total = %d" % len(strokesList)
    for index, stroke in enumerate(strokesList):
        ##    if index%20 == 0:
        ##      os.system("pkill -f display")
        ##      support.showImage( heightField )
        if index % 100 == 0:
            print index
        samples = stroke.length

        # compute corner point r and coordinate system
        dirL = stroke.end2 - stroke.end1
        dirW = rotateCW(dirL)
        dirL = impress.normalizeVector(dirL)
        dirW = impress.normalizeVector(dirW)
        r = stroke.end1 - dirW * (stroke.width / 2)

        # step sizes
        stepL = float(stroke.length) / samples
        stepW = float(stroke.width) / samples

        mapStepL = float(mapCols) / samples
        mapStepW = float(mapRows) / samples

        i = 0
        while i < samples:
            j = 0
            while j < samples:
                x = r + dirL * i * stepL + dirW * j * stepW
                if not impress.isOutOfBounds(rows, cols, x):
                    f = impress.interpolate(x[0], x[1], heightField)
                    map_c = i * mapStepL
                    map_r = j * mapStepW
                    h = impress.interpolate(map_r, map_c, heightMap)
                    t = impress.interpolate(map_r, map_c, opacityMap)
                    x = x.astype(int)
                    heightField[x[0], x[1]] = f * (1 - t) + h * t
                    heightField[x[0], x[1]] += fixedHeight
                    colorField[x[0], x[1]] = colorField[x[0], x[1]] * (
                        1 - t) + stroke.color * t
                j = j + 1
            i = i + 1

    heightField = normalizeMatrix(heightField)

    return heightField, colorField
Ejemplo n.º 8
0
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)
            Tx[r, c], Ty[r, c] = tangent[0], tangent[1]

    return Tx, Ty
Ejemplo n.º 9
0
def tessellate(image, pointsList, ETFr, ETFc):
    rows,cols = support.getSize(image)
    print("SIZE: %d x %d" % (rows,cols))
    drawing = support.makeMatrix(rows,cols,-1)
    if curveType == 'ETF':
        growCurveETF(pointsList, drawing, ETFr, ETFc)
        print("Curve grown!")
    elif curveType == 'Lorentz':
        growCurveLorentz(pointsList,drawing)

    return assignColors(image,drawing)
Ejemplo n.º 10
0
def makeKernel(*params):
    if (params[0] == 'prewitt'):
        Kx = support.makeMatrix([(-1, 0, 1), (-1, 0, 1), (-1, 0, -1)])
        Ky = support.makeMatrix([(-1, -1, -1), (0, 0, 0), (1, 1, 1)])
        return Kx, Ky
    elif (params[0] == 'sobel'):
        Kx = support.makeMatrix([(-1, 0, 1), (-2, 0, 2), (-1, 0, -1)])
        Ky = support.makeMatrix([(-1, -2, -1), (0, 0, 0), (1, 2, 1)])
        return Kx, Ky
    elif (params[0] == 'average'):
        n = params[1]
        value = 1.0 / (n * n)
        K = support.makeMatrix(n, n, value)
        return K
    elif (params[0] == 'gauss'):
        n = params[1]
        sig = params[2]
        K = support.makeMatrix(n, n)
        bound = n / 2
        r = 0
        for y in range(-bound, bound + 1, 1):
            c = 0
            for x in range(-bound, bound + 1, 1):
                value = 1 / (2 * math.pi *
                             (sig * sig)) * math.exp(-(x * x + y * y) /
                                                     (2 * sig * sig))
                K[r, c] = value
                c = c + 1
            r = r + 1

    # Normalize array
        K = K / np.sum(K)
        return K
Ejemplo n.º 11
0
def testCentroid():
    M = support.makeMatrix(10, 10, 1)
    P = stipple.computeP(M)
    print "p", P
    Q = stipple.computeQ(P)
    print "q", Q

    r, c = stipple.computeCentroid(P, Q, 2, 2, 5, 5)
    print r, c, "Expect 3.5,3.5"
    r, c = stipple.computeCentroid(P, Q, 2, 2, 6, 6)
    print r, c, "Expect 4,4"
    r, c = stipple.computeCentroid(P, Q, 2, 3, 5, 7)
    print r, c, "Expect 3.5,5"
Ejemplo n.º 12
0
def computeGauss(n, sig):
  K = support.makeMatrix(n,n)
  bound = n/2
  r = 0
  for y in range(-bound,bound+1,1):
    c = 0
    for x in range(-bound,bound+1,1):
      value = 1/(2*math.pi*(sig*sig)) * math.exp(-(x*x+y*y)/(2*sig*sig))
      K[r,c] = value
      c = c+1
    r = r+1

  # Normalize array
  K = K / np.sum(K)
  return K
def renderTSP(rows, cols, rectsList):
    image = support.makeMatrix(rows, cols, 255)

    # get vertex list of rect centers
    rect_centers = []
    for rect in rectsList:
        row, col = rect.get_center()
        rect_centers.append(Vertex(row, col, ""))

    dimension = len(rect_centers)

    # create tour.in using rect_centers
    f = file("tour.in", "w")
    print >> f, "NAME tour.in"
    print >> f, "TYPE TSP"
    print >> f, "DIMENSION %d" % dimension
    print >> f, "EDGE_WEIGHT_TYPE : EUC_2D"
    print >> f, "NODE_COORD_TYPE : TWOD_COORDS"
    print >> f, "NODE_COORD_SECTION"

    i = 0
    for v in rect_centers:
        print >> f, "%d %d %d" % (i, v.r, v.c)
        i = i + 1
    f.close()

    # run the TSP program
    os.system("tsp_solver -o tour.out tour.in")

    # read the results from tour.out
    f = file("tour.out")
    line = f.readline().strip()
    line = f.readline()  # remove first line

    pairs = []
    while line != "":
        values = line.split(" ")
        v0 = rect_centers[int(values[0])]
        pairs.append((v0.c, v0.r))
        line = f.readline()

    v1 = rect_centers[int(values[1])]
    pairs.append((v1.c, v1.r))
    f.close()
    # draw a line between all pairs
    return drawPolyLine(rows, cols, pairs)
Ejemplo n.º 14
0
def convolve(image, kernel):
    image_matrix = support.loadImage(image)
    image_rows, image_cols = support.getSize(image_matrix)
    kernel_rows = support.getRows(kernel)
    new_image_rows = image_rows - kernel_rows
    new_image_cols = image_cols - kernel_rows
    # A new matrix of zeros of final size of new image
    new_image = support.makeMatrix(new_image_rows, new_image_cols)
    # for each row in new image
    for r in range(0, new_image_rows, 1):
        end_row = r + kernel_rows
        # for each col in new image
        for c in range(0, new_image_cols, 1):
            # current matrix of image values
            end_col = c + kernel_rows
            conveq_matrix = image_matrix[r:end_row, c:end_col]
            # convolve image values and kernel
            new_image[r, c] = conveq(conveq_matrix, kernel)
    return new_image
Ejemplo n.º 15
0
def convolve(image, kernel):
    image_matrix = support.loadImage(image)
    image_rows, image_cols = support.getSize(image_matrix)
    kernel_rows, kernel_cols = support.getSize(kernel)
    kernel_rows, kernel_cols = kernel_rows / 2, kernel_cols / 2

    # A new matrix of zeros of final size of new image
    new_image = support.makeMatrix(image_rows, image_cols)
    # for each row in new image
    for r in range(kernel_rows, image_rows - kernel_rows, 1):
        # for each col in new image
        for c in range(kernel_cols, image_cols - kernel_cols, 1):
            # current matrix of image values
            conveq_matrix = image_matrix[r - kernel_rows:r + kernel_rows + 1,
                                         c - kernel_cols:c + kernel_cols +
                                         1]  # convolve image values and kernel
            new_image[r, c] = conveq(conveq_matrix, kernel)
    return new_image[kernel_rows:image_rows - kernel_rows,
                     kernel_cols:image_cols - kernel_cols]
Ejemplo n.º 16
0
def computeETF(Tx, Ty, G):

    #EQUATIONS#

    #Spatial Weight Function
    # Eq 2
    # Return 1 if ||x-y|| < r
    # 0 otherwise
    def sWeight(x, y):
        dist = distBetweenVectors(x, y)
        if dist < radius:
            return 1
        return 0

    # Magnitute Weight Function
    # Eq 3
    # Returns (1/2)(1+tanh[(g^(y)-g^(x))])
    # g^(z) - normalized gradient magnitude at z
    def mWeight(x, y, G):
        g_hat = normalizeMatrix(G)
        gx_hat = g_hat[x[0], x[1]]
        gy_hat = g_hat[y[0], y[1]]
        return .5 * (1 + math.tanh(N * (gy_hat - gx_hat)))

    # Direction Weight Function
    # Eq 4
    # Returns |tcur(x) (dot) tcur(y)|
    # tcur(z) = current normalized tangent vector at z
    def dWeight(x, y, tcur):
        tcur_x = tcur[x[0], x[1]]
        tcur_y = tcur[y[0], y[1]]
        result = getDotProduct(tcur_x, tcur_y)
        # absolute value of result (always 0 or positive)
        if (result < 0):
            result = -result
        return result

    # Phi
    # Eq 5
    # Return 1 if tcur(x) (dot) tcur(y) > 0
    # Return -1 otherwise
    def phi(x, y, tcur):
        tcur_x = tcur[x[0], x[1]]
        tcur_y = tcur[y[0], y[1]]
        result = getDotProduct(tcur_x, tcur_y)
        if (result < 0):
            return 1
        return -1

    rows, cols = support.getSize(Tx)
    Tx_cur, Ty_cur = Tx, Ty
    zero_matrix = support.makeMatrix(rows, cols)
    Tx_new, Ty_new = zero_matrix, zero_matrix

    for i in range(0, s_factor, 1):
        print("iteration: %d" % i)
        # For every pixel in T
        for r in range(0, rows, 1):
            for c in range(0, cols, 1):
                x = support.makeVector(r, c)
                # For every neighbor of x
                sum_x = 0
                sum_y = 0

                # Calculate mask for neighbors of x
                rn_init = r - radius
                rn_end = r + radius
                cn_init = c - radius
                cn_end = c + radius
                if rn_init < 0:
                    rn_init = 0
                if rn_end > rows:
                    rn_end = rows
                if cn_init < 0:
                    cn_init = 0
                if cn_end > cols:
                    cn_end = cols

                for rn in range(rn_init, rn_end, 1):
                    for cn in range(cn_init, cn_end, 1):
                        y = support.makeVector(rn, cn)
                        if sWeight(x, y) == 1:
                            mW = mWeight(x, y, G)
                            sum_x = sum_x + phi(x, y, Tx_cur) * Tx_cur[
                                y[0], y[1]] * mW * dWeight(x, y, Tx_cur)
                            sum_y = sum_y + phi(x, y, Ty_cur) * Ty_cur[
                                y[0], y[1]] * mW * dWeight(x, y, Ty_cur)

                #Normalize Vector
                v = support.makeVector(sum_x, sum_y)
                normal_v = normalizeVector(v)
                Tx_new[r, c] = normal_v[0]
                Ty_new[r, c] = normal_v[1]

        Tx_cur = Tx_new
        Ty_cur = Ty_new

    return Tx_cur, Ty_cur
Ejemplo n.º 17
0
def computeFDoG(image, Tx, Ty):

    #VARIABLES#
    p = 6 * sig_m
    q = 6 * sig_c

    #EQUATIONS#

    # Gsig(x)
    # Eq 8
    def gaussian(t, sig):
        return 1.0 / (math.sqrt(2 * math.pi) * sig) * math.exp(-(t * t) /
                                                               (2 * sig * sig))

    # f(t)
    # Eq 7
    def filter1d(t):
        return gaussian(t, sig_c) - p_noise * gaussian(t, sig_s)

    # F(s)
    # x is a vector
    # Eq 6
    def filteringFramework(x):
        r = int(x[0])  # row of x
        c = int(x[1])  # col of x
        end = int(p / 2)

        # t = 0
        total_sum = image[r, c] * filter1d(0)

        # Calculate Direction
        tan = support.makeVector(Tx[r, c], Ty[r, c])
        direct = rotateCW(tan)

        # Loop forwards (t=1,2,3,4)
        z = x
        #print "t forward"
        for t in range(1, end + 1, 1):
            z = z + delta_n * direct
            z_int = z.astype(int)
            #print(z_int)
            if isOutOfBounds(rows, cols, z_int):
                #print "out of bounds 1", z_int, t
                break
            total_sum = total_sum + image[z_int[0], z_int[1]] * filter1d(t)

        # Loop backwards (t=1,2,3,4)
        z = x
        #print "t backward"
        for t in range(1, end + 1, 1):
            z = x - delta_n * direct
            z_int = z.astype(int)
            #print(z_int)
            if isOutOfBounds(rows, cols, z_int):
                #print "out of bounds 2", z_int, t
                break
            total_sum = total_sum + image[z_int[0], z_int[1]] * filter1d(t)

        return total_sum

    # H(x)
    # Eq 9
    def filterAccumulated(x):
        rows, cols = support.getSize(Tx)
        end = int(q / 2)

        # s = 0
        total_sum = gaussian(0, sig_m) * filteringFramework(x)

        z = x
        # Loop forwards (s=1,2,3,4)
        #print "s forward"
        for s in range(1, end + 1, 1):
            tan = getTangentVector(z)
            if isZeroVector(tan):
                break
            z = z + delta_m * tan
            z_int = z.astype(int)
            #print(z_int)
            if isOutOfBounds(rows, cols, z_int):
                #print "out of bounds 3", z_int, s
                break
            total_sum = total_sum + gaussian(s, sig_m) * filteringFramework(z)

        # Loop backwards
        z = x
        #print "s backward"
        for s in range(1, end + 1, 1):
            tan = getTangentVector(z)
            if isZeroVector(tan):
                break
            z = z - delta_m * tan
            z_int = z.astype(int)
            #print(z_int)
            if isOutOfBounds(rows, cols, z_int):
                #print "out of bounds 4", z_int, s
                break
            total_sum = total_sum + gaussian(s, sig_m) * filteringFramework(z)

        return total_sum

    # OTHER FUNCTIONS #

    # Returns the tangent vector at z, or zero vector if out of bounds
    def getTangentVector(z):
        z = z.astype(int)
        return support.makeVector(Tx[z[0], z[1]], Ty[z[0], z[1]])

    rows, cols = support.getSize(image)
    line_image = support.makeMatrix(rows, cols)
    print(support.getSize(line_image))
    for r in range(0, rows, 1):
        print "Row", r
        for c in range(0, cols, 1):
            x = support.makeVector(r, c)
            Hx = filterAccumulated(x)
            #print (Hx)
            if Hx > 0:
                line_image[r, c] = 1
            else:
                line_image[r, c] = 1 + np.tanh(Hx)

##            if Hx < 0 and 1 + np.tanh(Hx) < thresh:
##                line_image[r,c] =0
##            else:
##                line_image[r,c] = 1

    return line_image
Ejemplo n.º 18
0
def computeETF(Tx, Ty, G):

    #EQUATIONS#

    #Spatial Weight Function
    # Eq 2
    # Return 1 if ||x-y|| < r
    # 0 otherwise
    def sWeight(x, y):
        dist = getVectorLength(x - y)
        if dist < radius:
            return 1
        return 0

    # Magnitute Weight Function
    # Eq 3
    # Returns (1/2)(1+tanh[(g^(y)-g^(x))])
    # g^(z) - normalized gradient magnitude at z
    def mWeight(x, y):
        gx_hat = G[x[0], x[1]]
        gy_hat = G[y[0], y[1]]
        return .5 * (1 + math.tanh(eta * (gy_hat - gx_hat)))

    # Direction Weight Function
    # Eq 4
    # Returns |tcur(x) (dot) tcur(y)|
    # tcur(z) = current normalized tangent vector at z
    def dWeight(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)
        # absolute value of result (always 0 or positive)
        if (result < 0):
            result = -result
        #print(tcur_x, tcur_y)
        #print(result)
        return result

    # Phi
    # Eq 5
    # Return 1 if tcur(x) (dot) tcur(y) > 0
    # Return -1 otherwise
    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

    rows, cols = support.getSize(Tx)
    Tx_new = support.makeMatrix(rows, cols)
    Ty_new = support.makeMatrix(rows, cols)
    print "ETF start"
    for i in range(0, s_factor, 1):
        print("iteration: %d" % i)
        # For every pixel in T
        for r in range(0, rows, 1):
            print "Row", r
            for c in range(0, cols, 1):
                x = support.makeVector(r, c)
                sum_x = 0
                sum_y = 0

                # Calculate mask for neighbors of x
                rn_init = r - radius
                rn_end = r + radius
                cn_init = c - radius
                cn_end = c + radius

                # For each neighbor within radius
                for rn in range(rn_init, rn_end + 1, 1):
                    for cn in range(cn_init, cn_end + 1, 1):
                        y = support.makeVector(rn, cn)
                        if isOutOfBounds(rows, cols, y):
                            break
                        if sWeight(x, y) == 1:
                            mW = mWeight(x, y)
                            dW = dWeight(x, y)
                            phi_eq = phi(x, y)
                            #print(x, y)
                            sum_x = sum_x + phi_eq * Tx[y[0], y[1]] * mW * dW
                            sum_y = sum_y + phi_eq * Ty[y[0], y[1]] * mW * dW

                #Normalize Vector
                v = support.makeVector(sum_x, sum_y)
                normal_v = normalizeVector(v)
                Tx_new[r, c] = normal_v[0]
                Ty_new[r, c] = normal_v[1]

        Tx = Tx_new
        Ty = Ty_new

    return Tx, Ty
Ejemplo n.º 19
0
def computeFDoG(image, Tx, Ty):

    #VARIABLES#
    p = 6 * sig_m
    q = 6 * sig_c

    #EQUATIONS#

    # Gsig(x)
    # Eq 8
    def gaussian(t, sig):
        return 1 / (math.sqrt(2 * math.pi) * sig) * math.exp(-(t * t) /
                                                             (2 * sig * sig))

    # f(t)
    # Eq 7
    def filter1d(t):
        return gaussian(t, sig_c) - p_noise * gaussian(t, sig_s)

    # F(s)
    # x is a vector
    # Eq 6
    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)

    # H(x)
    # Eq 9
    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
            print("Not Zero Vector: loop 1")
            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
            print("Not Zero Vector: loop 2")
            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)

    # OTHER FUNCTIONS #

    # Returns the tangent vector at z, or zero vector if out of bounds
    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)

    rows, cols = support.getSize(image)
    line_image = support.makeMatrix(rows, cols)
    print(support.getSize(line_image))
    for r in range(0, rows, 1):
        print("Row", r)
        for c in range(0, cols, 1):
            x = support.makeVector(r, c)
            Hx = filterAccumulated(x, Tx, Ty)
            if Hx < 0:
                line_image[r, c] = 0
            else:
                line_image[r, c] = 1 + np.tanh(Hx)

##            if Hx < 0 and 1 + np.tanh(Hx) < thresh:
##                line_image[r,c] = 0
##            else:
##                line_image[r,c] = 1

    return line_image