Example #1
0
def imageTransform(image, maskImage, T):    
    height, width = len(image), len(image[0])
    centreX, centreY = width/2, height/2
    
    sImage = createImageRGB(width, height)
    for y, x in itertools.product(range(0, height-1), range(0, width-1)):
        # Alpha and colour   
        alpha = maskImage[y,x]/256.0 
        if alpha == 0: 
            continue
        rgb = (image[y,x]/4.0 + image[y+1,x+1]/4.0 + image[y+1,x]/4.0 +  \
               image[y,x+1]/4.0) * alpha
        
        # Transform
        cx, cy = x - centreX, y - centreY
        p0z = T[2][0] * cx + T[2][1] * cy + T[2][2] 
        p1z = T[2][0] * (cx+1) + T[2][1] * cy + T[2][2] 
        p2z = T[2][0] * (cx+1) + T[2][1] * (cy+1) + T[2][2] 
        
        if p0z != 0 and p1z != 0 and p2z !=0:
    
            p0x = int((T[0][0] * cx + T[0][1] * cy + T[0][2]) / p0z + centreX)
            p0y = int((T[1][0] * cx + T[1][1] * cy + T[1][2]) / p0z + centreY) 
        
            p1x = int((T[0][0] * (cx+1) + T[0][1] * cy + T[0][2]) / p1z + centreX)
            p1y = int((T[1][0] * (cx+1) + T[1][1] * cy + T[1][2]) / p1z + centreY) 
        
            p2x = int((T[0][0] * (cx+1) + T[0][1] * (cy+1) + T[0][2]) / p2z + centreX)
            p2y = int((T[1][0] * (cx+1) + T[1][1] * (cy+1) + T[1][2]) / p2z + centreY) 
            
            # Fill output image
            v1, v2 = [p1x - p0x, p1y - p0y], [p2x - p0x, p2y - p0y]
                    
            lv1 = max(.001,sqrt(v1[0]*v1[0] + v1[1]*v1[1]))
            lv2 = max(.001,sqrt(v2[0]*v2[0] + v2[1]*v2[1]))
            v1N = [v1[0]/lv1, v1[1]/lv1]
            v2N = [v2[0]/lv2, v2[1]/lv2]
    
            for dV1, dV2 in itertools.product(range(0, int(lv1)+1), range(0, int(lv2)+1)):
                a,b = int(p0x + dV1 * v1N[0] + dV2 * v2N[0]), int(p0y + dV1 * v1N[1] + dV2 * v2N[1])
                if a>0 and a < width and b > 0 and b < height:
                    sImage[b,a] = rgb
    return sImage   
Example #2
0
def getPointColours(xy, mask, image):
    nfaces = len(xy)
    
    height = len(image)
    width = len(image[0]) 
    
    colourImages = [ ]
    for faceNum in range(0, nfaces):
    
        face = xy[faceNum]
        npts = len(face)
        colours = createImageRGB(npts, npts)

        for a,b in itertools.product(range(0, npts-1), range(0, npts-1)):
            y,x = face[a][b][1], face[a][b][0]
            if y>0 and y<height and x>0 and x<width:
                alpha = mask[y,x] / 256.0 
                if alpha > 0.0:
                    c = image[y,x]
                    colours[a,b] = [alpha*c[0],alpha*c[1],alpha*c[2]]
                
        colourImages.append(colours)

    return colourImages
Example #3
0
                D = dc / 255.0 + (m / regionSide) * ds
                if D < minD[0]:
                    minD = [D, wy, wx]
        [_, minY, minX] = minD
        newRegionColour[minY, minX] += colour
        newRegionPos[minY, minX] += [y, x]
        newRegionSize[minY, minX] += 1
        regionsID[y, x] = [minY, minX]

    # Update regions
    for x, y in itertools.product(range(0, regW), range(0, regH)):
        if newRegionSize[y, x] > 0:
            regionPos[y, x] = newRegionPos[y, x] / newRegionSize[y, x]
            regionColour[y, x] = newRegionColour[y, x] / newRegionSize[y, x]

# Show regions
resultRegions = createImageRGB(width, height)
for x, y in itertools.product(range(0, width), range(0, height)):
    border = False
    for wx, wy in itertools.product(range(x, x + 2), range(y, y + 2)):
        if wy >= 0 and wy < height and wx >= 0 and wx < width:
            if regionsID[y, x, 0] != regionsID[wy, wx, 0] or regionsID[
                    y, x, 1] != regionsID[wy, wx, 1]:
                border = True
    if border:
        resultRegions[y, x] = [255, 255, 255]
    else:
        [h, w] = regionsID[y, x]
        resultRegions[y, x] = regionColour[h, w]
showImageRGB(resultRegions)
sigma = 4.0

# Read image into array
inputImage, width, height = imageReadRGB(pathToDir + imageName)

# Show input image
showImageRGB(inputImage)

# Three float array to store colors to be used in the surface plot
colorsRGB = createImageF(histSize, histSize, 3)

# Quantization scale
colourScale = 256.0 / histSize

# Create region image and histogram
regionImage = createImageRGB(2 * regionRadius[0], 2 * regionRadius[1])
histogram = createImageF(histSize, histSize)
sumValue = 0
for deltaX, deltaY in itertools.product(
        range(-regionRadius[0], regionRadius[0]),
        range(-regionRadius[1], regionRadius[1])):

    x, y = position[0] + deltaX, position[1] + deltaY
    px, py = deltaX + regionRadius[0], deltaY + regionRadius[1]

    if x > 0 and y > 0 and x < width and y < height:

        regionImage[py, px] = inputImage[y, x]
        w = exp(-(deltaX * deltaX + deltaY * deltaY) / (2 * sigma * sigma))

        rgb = inputImage[y, x] / 256.0
Example #5
0
    s = [.4, 0.8, 0.8, 100.0, 0.0]  # Angle, scaleXY, translationXY
    T = [[ s[1]*cos(s[0]), s[1]*sin(s[0]), s[3]],                        \
         [ -s[2]*sin(s[0]), s[2]*cos(s[0]), s[4]],                       \
         [0 ,0, 1]]
if transformationType == "Affine":
    # Affine transformation
    T = [[ .8, .1, 100],                                                  \
         [ -.2, 1, 0],                                                    \
         [0 ,0, 1]]
if transformationType == "Homography":
    # Homography
    T = [[ .8, 0, 100],                                                    \
         [ .2,1, 0],                                                       \
         [.0005 ,-0.0005 , 1.2]]

tImage = createImageRGB(width, height)
for y, x in itertools.product(range(0, height - 1), range(0, width - 1)):
    # Alpha and colour
    alpha = maskImage[y, x] / 256.0
    if alpha == 0:
        continue
    rgb = (inputImage[y,x]/4.0   + inputImage[y+1,x+1]/4.0 +                \
           inputImage[y+1,x]/4.0 + inputImage[y,x+1]/4.0) * alpha

    # Transform
    cx, cy = x - centreX, y - centreY
    p0z = T[2][0] * cx + T[2][1] * cy + T[2][2]
    p1z = T[2][0] * (cx + 1) + T[2][1] * cy + T[2][2]
    p2z = T[2][0] * (cx + 1) + T[2][1] * (cy + 1) + T[2][2]

    if p0z != 0 and p1z != 0 and p2z != 0: