def convertImage(rowsOut, colsOut, superpixels):
    result = support.makeVecMatrix(rowsOut * scale, colsOut * scale, 3)
    for sp in superpixels:
        r, c = sp.outPos[0], sp.outPos[1]
        result[r * scale:(r + 1) * scale, c * scale:(c + 1) * scale] = sp.ms

    return support.lab2rgb(result)
def pixelart(imageRGB, rowsOut, colsOut, K):
    rowsIn, colsIn = support.getSize(imageRGB)
    N = rowsOut * colsOut  # Number of superpixels
    M = rowsIn * colsIn  # Number of input pixels

    # Convert the input image to lab space
    imageLAB = support.rgb2lab(imageRGB)

    # Initialize superpixels, palette, and temperature
    superpixels = initSuperPixels(imageLAB, rowsOut, colsOut)
    palette = initPalette(imageLAB)
    T = initT(imageLAB)

    # While (T > Tf)
    i = 0
    while T > Tf:
        if printProgress:
            print "Iteration: %d T: %d" % (i, T)

    #   REFINE superpixels with 1 step of modified SLIC
        refineSuperPixels(imageLAB, superpixels)

        #   ASSOCIATE superpixels to colors in the palette
        for cluster in palette:
            cluster.sub1.associate(superpixels, T, palette)
            cluster.sub2.associate(superpixels, T, palette)

    #   REFINE colors in the palette
        totalChange = refinePalette(superpixels, palette)
        if printProgress:
            print "totalChange", totalChange

    #   If (palette converged)
        print "Palette size: %d" % len(palette)
        if totalChange < ep_palette:

            #     REDUCE temperature T = aT
            T = alpha * T

            #     EXPAND palette
            expandPalette(palette, K)

        i += 1

    # convert SuperPixels to matrix image
    result = support.makeVecMatrix(rowsOut * scale, colsOut * scale, 3)
    for sp in superpixels:
        r, c = int(sp.outPos[0]), int(sp.outPos[1])
        result[r * scale:(r + 1) * scale, c * scale:(c + 1) * scale] = sp.ms

    # Post-process
    result[:, :, 1] = result[:, :, 1] * beta
    result[:, :, 2] = result[:, :, 2] * beta

    # Convert LAB image to RGB
    result = support.lab2rgb(result)

    return result
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
def assignColors(image, drawing):
    #print "assign"
    rows, cols = support.getSize(drawing)
    rgbImage = support.makeVecMatrix(rows, cols, 3)
    for r in range(0, rows, 1):
        for c in range(0, cols, 1):
            if drawing[r, c] == -1:
                total, numPixels = computeColor(r, c, image, drawing)
                rgbColor = total / numPixels
                #print(rgbColor * 255)
                floodRegion(r, c, rgbColor, drawing, rgbImage)
    return rgbImage * 255
Esempio n. 5
0
def sphereToRgb(r_mat, t_mat, p_mat):
    rows, cols = support.getSize(r_mat)
    x = r_mat * np.sin(t_mat) * np.cos(p_mat)
    y = r_mat * np.sin(t_mat) * np.sin(p_mat)
    z = r_mat * np.cos(t_mat)

    # load x, y, z into new matrix
    rgb_image = support.makeVecMatrix(rows, cols, 3)
    rgb_image[:, :, 0] = x
    rgb_image[:, :, 1] = y
    rgb_image[:, :, 2] = z

    return rgb_image
def convertImage(rowsOut, colsOut, superpixels):

  if not shouldScale:
    global scale
    scale = 1
  
  # convert SuperPixels to matrix image
  result = support.makeVecMatrix(rowsOut*scale, colsOut*scale, 3)
  for sp in superpixels.itervalues():
    r,c = int(sp.outPos[0]), int(sp.outPos[1])
    result[r*scale:(r+1)*scale,c*scale:(c+1)*scale] = sp.ms

  # Post-process
  result[:,:,1] = result[:,:,1] * beta
  result[:,:,2] = result[:,:,2] * beta

  # Convert LAB image to RGB
  return support.lab2rgb(result)
def computeNormals(heightField):
    print "Computing normals..."
    rows, cols = support.getSize(heightField)
    normalField = support.makeVecMatrix(rows, cols, 3)

    # For each pixel that's not a boundary pixel
    for r in range(1, rows - 1, 1):
        for c in range(1, cols - 1, 1):
            v = support.makeVector(r, c, heightField[r, c])
            # list of neighbor pixels of v (top, left, bottom, right)
            verts = support.makeVector(support.makeVector(r-1,c, heightField[r-1,c]),\
                                       support.makeVector(r,c-1, heightField[r,c-1]),\
                                       support.makeVector(r+1,c, heightField[r+1,c]),\
                                       support.makeVector(r,c+1, heightField[r,c+1]))
            vects = verts - v  #vector from v to each vertex

            norms = [cross(vects[0],vects[1]), cross(vects[1],vects[2]),\
                     cross(vects[2],vects[3]), cross(vects[3],vects[0])]

            normalField[r, c] = normalizeVector3D(sum(norms))

    return normalField