def seamlessCloningPoisson(sourceImg, targetImg, mask, offsetX, offsetY): targetH, targetW = targetImg.shape[:2] indexes = getIndexes(mask, targetH, targetW, offsetX, offsetY) coeffA = getCoefficientMatrix(indexes) print(coeffA.shape) SolVectorb_r = getSolutionVect(indexes, I_Source[:,:,0], I_Target[:,:,0], offsetX, offsetY) print("hi " , SolVectorb_r.shape) SolVectorb_g = getSolutionVect(indexes, I_Source[:,:,1], I_Target[:,:,1], offsetX, offsetY) SolVectorb_b = getSolutionVect(indexes, I_Source[:,:,2], I_Target[:,:,2], offsetX, offsetY) inv_coeffA = inv(coeffA) f_r = np.dot(SolVectorb_r, inv_coeffA) f_g = np.dot(SolVectorb_g, inv_coeffA) f_b = np.dot(SolVectorb_b, inv_coeffA) plt.figure(num = 'source') plt.imshow(I_Source) resultImg = reconstructImg(indexes, f_r, f_g, f_b, I_Target) #plt.imshow(resultImg) #plt.show() return resultImg
def seamlessCloningPoisson(sourceImg, targetImg, mask, offsetX, offsetY): indexes = getIndexes(mask,targetImg.shape[0],targetImg.shape[1],offsetX,offsetY) A = getCoefficientMatrix(indexes) b_r = getSolutionVect(indexes,sourceImg[:, :, 0],targetImg[:, :, 0],offsetX,offsetY) b_g = getSolutionVect(indexes,sourceImg[:, :, 1],targetImg[:, :, 1],offsetX,offsetY) b_b = getSolutionVect(indexes,sourceImg[:, :, 2],targetImg[:, :, 2],offsetX,offsetY) x_r = np.linalg.solve(A,b_r) x_g = np.linalg.solve(A,b_g) x_b = np.linalg.solve(A,b_b) resultImg = reconstructImg(indexes,x_r,x_g,x_b,targetImg) return resultImg
def seamlessCloningPoisson(sourceImg, targetImg, mask, offsetX, offsetY): ''' :param sourceImg: h*w*3 matrix representing the source image. :param targetImg: h'*w'*3 matrix representing the target image. :param mask: The logical matrix hw representing the replacement region :param offsetX: The x-axis offset of the source image with respect to the target image. :param offsetY: The y-axis offset of the source image with respect to the target image. :return: resultImg: h'*w'*3 matrix representing the resulting cloned image. ''' H = len(targetImg) #height of target image W = len(targetImg[0]) #width of target image ch = len(sourceImg[0, 0]) # channel of source image indexes = getIndexes(mask, H, W, offsetX, offsetY) N = np.amax(indexes) #number of pixel to be replaced coeffA = getCoefficientMatrix(indexes) #get coeffA b = np.zeros((N, ch)) # preallocate a matrix for b for i in range(ch): b[:, i] = getSolutionVect(indexes, sourceImg[:, :, i], targetImg[:, :, i], offsetX, offsetY) f = linalg.spsolve(coeffA, b) # solve the equation for blended pixel value resultImg = reconstructImg(indexes, f[:, 0], f[:, 1], f[:, 2], targetImg) return resultImg
def seamlessCloningPoisson(sourceImg, targetImg, mask, offsetX, offsetY): # create index mask and coeffA indexes = getIndexes.getIndexes(mask, targetImg.shape[0], targetImg.shape[1], offsetX, offsetY) coeffA = getCoefficientMatrix.getCoefficientMatrix(indexes) # for Red channel print('r channel') sourceImg_R = sourceImg[:, :, 0] targetImg_R = targetImg[:, :, 0] solVectorb_R = getSolutionVect.getSolutionVect(indexes, sourceImg_R, targetImg_R, offsetX, offsetY) x_R = np.linalg.solve(coeffA, solVectorb_R) x_R = np.clip(x_R, 0, 255) # print(len(x_R)) # for Green channel print('g channel') sourceImg_G = sourceImg[:, :, 1] targetImg_G = targetImg[:, :, 1] solVectorb_G = getSolutionVect.getSolutionVect(indexes, sourceImg_G, targetImg_G, offsetX, offsetY) x_G = np.linalg.solve(coeffA, solVectorb_G) x_G = np.clip(x_G, 0, 255) # for Blue channel print('b channel') sourceImg_B = sourceImg[:, :, 2] targetImg_B = targetImg[:, :, 2] solVectorb_B = getSolutionVect.getSolutionVect(indexes, sourceImg_B, targetImg_B, offsetX, offsetY) x_B = np.linalg.solve(coeffA, solVectorb_B) x_B = np.clip(x_B, 0, 255) print('reconstructing') resultImg = reconstructImg.reconstructImg(indexes, x_R, x_G, x_B, targetImg) return resultImg
def seamlessCloningPoisson(sourceImg, targetImg, mask, offsetX, offsetY): indexes = getIndexes(mask, targetImg.shape[0], targetImg.shape[1], offsetX, offsetY) coeffA = getCoefficientMatrix(indexes) SolVectorb = getSolutionVect(indexes, sourceImg, targetImg, offsetX, offsetY) coeffA = csc_matrix(coeffA) f = spsolve(coeffA, SolVectorb) f[f <= 0] = 0 f[f > 255] = 255 f = np.array(f, dtype=np.uint8) return f
def seamlessCloningPoisson(sourceImg, targetImg, mask, offsetX, offsetY): targetH = targetImg.shape[0] targetW = targetImg.shape[1] indexes = getIndexes(mask, targetH, targetW, offsetX, offsetY) coeffA = getCoefficientMatrix(indexes) solVectorb_R = getSolutionVect(indexes, sourceImg[:, :, 0], targetImg[:, :, 0], offsetX, offsetY) solVectorb_G = getSolutionVect(indexes, sourceImg[:, :, 1], targetImg[:, :, 1], offsetX, offsetY) solVectorb_B = getSolutionVect(indexes, sourceImg[:, :, 2], targetImg[:, :, 2], offsetX, offsetY) x_R = spla.linalg.spsolve(coeffA, solVectorb_R.T) x_G = spla.linalg.spsolve(coeffA, solVectorb_G.T) x_B = spla.linalg.spsolve(coeffA, solVectorb_B.T) x_R = np.clip(x_R, 0, 255) x_G = np.clip(x_G, 0, 255) x_B = np.clip(x_B, 0, 255) resultImg = reconstructImg(indexes, x_R, x_G, x_B, targetImg) return resultImg
def seamlessCloningPoisson(simg, timg, mask, offsetX, offsetY): [h, w, z] = np.shape(simg) [targetH, targetW, zz] = np.shape(timg) ''' mask=maskImage(sim) print(mask) sio.savemat('mask.mat', {'mask':mask}) ''' # mask_load = sio.loadmat('mask') # mask = mask_load['mask'] indexes = getIndexes(mask, targetH, targetW, offsetX, offsetY) red = getSolutionVect(indexes, simg[:, :, 0], timg[:, :, 0], offsetX, offsetY) green = getSolutionVect(indexes, simg[:, :, 1], timg[:, :, 1], offsetX, offsetY) blue = getSolutionVect(indexes, simg[:, :, 2], timg[:, :, 2], offsetX, offsetY) resultImg = reconstructImg(indexes, red, green, blue, timg) return resultImg
def seamlessCloningPoisson(sourceImg, targetImg, mask, offsetX, offsetY): # Generate indexes indexes = getIndexes(mask, targetImg.shape[0], targetImg.shape[1], offsetX, offsetY) num_replacement_pixels = indexes[indexes != 0].size # Get A matrix coeffA = csr_matrix(getCoefficientMatrix(indexes), dtype=float) # Get b vector and solve for x x = np.zeros((3, num_replacement_pixels)) for i in range(3): b_i = csr_matrix( getSolutionVect(indexes, sourceImg[:, :, i], targetImg[:, :, i], offsetX, offsetY).reshape(num_replacement_pixels, 1)) x[i, :] = spsolve(coeffA, b_i) x[x < 0] = 0 x[x > 255] = 255 # Combine the three channels resultImg = reconstructImg(indexes, x[0, :], x[1, :], x[2, :], targetImg) return resultImg
def seamlessCloningPoisson(sourceImg, targetImg, mask, offsetX, offsetY): targetH, targetW, _ = targetImg.shape # get index matrix indexes = getIndexes(mask, targetH, targetW, offsetX, offsetY) print('Indexes size is %d by %d' % indexes.shape) # create coefficient matrix coeffA = getCoefficientMatrix(indexes) print('CoeffA size is %d by %d' % coeffA.shape) # create solution vector (RGB) # number of the replacement pixel N = mask.sum() SolutionVect = np.zeros((N, 3)) for i in range(3): # to make sure the shape are consistent SolutionVect[:, [i]] = getSolutionVect(indexes, sourceImg[:, :, i], targetImg[:, :, i], offsetX, offsetY) print('SolutionVect size is %d by %d' % SolutionVect.shape) # seperate into RGB red = SolutionVect[:, 0] green = SolutionVect[:, 1] blue = SolutionVect[:, 2] # solve solution r = solve(coeffA, red) g = solve(coeffA, green) b = solve(coeffA, blue) # construct final image resultImg = reconstructImg(indexes, r, g, b, targetImg) print('Image reconstructed') return resultImg
def seamlessCloningPoisson(sourceImg, targetImg, mask, offsetX, offsetY): targetH, targetW, k = targetImg.shape indexes = getIndexes.getIndexes(mask, targetH, targetW, offsetX, offsetY) coeffA = getCoefficientMatrix.getCoefficientMatrix(indexes) for i in range(3): target = targetImg[:, :, i] source = sourceImg[:, :, i] b = getSolutionVect.getSolutionVect(indexes, source, target, offsetX, offsetY) if i == 0: red = sparse.linalg.spsolve(coeffA, b) red = np.clip(red, 0, 255) elif i == 1: green = sparse.linalg.spsolve(coeffA, b) green = np.clip(green, 0, 255) else: blue = sparse.linalg.spsolve(coeffA, b) blue = np.clip(blue, 0, 255) resultImg = reconstructImg.reconstructImg(indexes, red, green, blue, targetImg) return resultImg
from reconstructImg import reconstructImg if __name__=="__main__": sourceImage = Image.open('source.png').convert('RGB') targetImage = Image.open('target.png').convert('RGB') #mask = mask_creator(sourceImage.convert('L')) offsetX = 0 offsetY = 0 #mask = np.array(Image.open('mask1.png').convert('L')) mask = mask_creator(sourceImage.convert('L')) plt.imshow(mask) plt.show() sourceImage = np.array(sourceImage) targetImage = np.array(targetImage) #print(targetImage.shape) #print(sourceImage.shape) indexes = getIndexes(mask, targetImage.shape[0],targetImage.shape[1],offsetX,offsetY) coeffA = getCoefficientMatrix(indexes) redChannelSource, redChannelTarget = sourceImage[:,:,0], targetImage[:,:,0] blueChannelSource, blueChannelTarget = sourceImage[:,:,2], targetImage[:,:,2] greenChannelSource, greenChannelTarget = sourceImage[:,:,1], targetImage[:,:,1] redSolVectorb = getSolutionVect(indexes, redChannelSource, redChannelTarget, offsetX,offsetY) redFinalimg = seamlessCloningPoisson(redChannelSource, redChannelTarget, mask, offsetX,offsetY) blueSolVectorb = getSolutionVect(indexes, blueChannelSource, blueChannelTarget, offsetX,offsetY) blueFinalimg = seamlessCloningPoisson(blueChannelSource, blueChannelTarget, mask, offsetX,offsetY) greenSolVectorb = getSolutionVect(indexes, greenChannelSource, greenChannelTarget, offsetX,offsetY) greenFinalimg = seamlessCloningPoisson(greenChannelSource, greenChannelTarget, mask, offsetX,offsetY) finalimg = reconstructImg(indexes, redFinalimg, greenFinalimg, blueFinalimg, targetImage) plt.imshow(finalimg) plt.show()
print(targetH,targetW) offsetX = int(targetW / 2) offsetY = int(targetH / 2) #offsetX = int(1) #offsetY = int(1) #print(offsetY,offsetX) indexes = getIndexes(mask, targetH, targetW, offsetX, offsetY) coeffA = getCoefficientMatrix(indexes) print(coeffA.shape) SolVectorb_r = getSolutionVect(indexes, I_Source[:,:,0], I_Target[:,:,0], offsetX, offsetY) print(SolVectorb_r.shape) SolVectorb_g = getSolutionVect(indexes, I_Source[:,:,1], I_Target[:,:,1], offsetX, offsetY) SolVectorb_b = getSolutionVect(indexes, I_Source[:,:,2], I_Target[:,:,2], offsetX, offsetY) inv_coeffA = inv(coeffA) f_r = np.dot(SolVectorb_r, inv_coeffA) f_g = np.dot(SolVectorb_g, inv_coeffA) f_b = np.dot(SolVectorb_b, inv_coeffA) plt.figure(num = 'source') plt.imshow(I_Source) resultImg = reconstructImg(indexes, f_r, f_g, f_b, I_Target)