def main(): inputim = imread("tiananmen.jpg") plt.figure() plt.imshow(inputim) row, col, _ = inputim.shape t1 = np.load("tiananmen.npy") maxX = np.max(t1[0, :]) maxY = np.max(t1[1, :]) minX = np.min(t1[0, :]) minY = np.min(t1[1, :]) t2 = np.array([[0, maxX - minX, 0, maxX - minX], [0, 0, maxY - minY, maxY - minY]]) row, col = t1.shape t1_1 = np.ones((row + 1, col)) t2_1 = np.ones((row + 1, col)) t1_1[:-1, :] = t1 t2_1[:-1, :] = t2 H = computeH.computeH(t1, t2) warped = np.dot(H, t1_1) warped = normalizeHomogeneous.normalizeHomogeneous(warped) rectifyIm = rectifyImage(inputim, t1_1, H) print(H) plt.figure() plt.title("rectify image_tiananmen") rectifyIm = np.array(rectifyIm, np.int32) plt.imshow(rectifyIm) plt.savefig("rectify_tiananmen.png") plt.show()
def main(): #for wdc #t1=np.load("points1.npy") #t2=np.load("points2.npy") #print(t1,"\n",t2) #for crops t1 = np.load("cc1.npy") t2 = np.load("cc2.npy") #for keble #t1=np.load("points1_keble_1.npy") #t2=np.load("points2_keble_2.npy") #t1=t1.T #t2=t2.T row, col = t1.shape # 2*N # homogeneous coordinate t1_1 = np.ones((row + 1, col)) t2_1 = np.ones((row + 1, col)) t1_1[:-1, :] = t1 t2_1[:-1, :] = t2 H = computeH(t1, t2) t1_result = np.dot(H, t1_1) t1_result = normalizeHomogeneous.normalizeHomogeneous(t1_result) im2 = imread("crop2.jpg") plt.imshow(im2) plt.title("Pairs in wdc") plt.scatter(t1_result[0, :], t1_result[1, :], s=1, c='b') #plt.figure() #im1=imread("wdc1.jpg") #plt.imshow(im1) plt.scatter(t2[0], t2[1], s=1, c='r') #plt.savefig("pairs_keble.png") plt.show() print(H)
def warpImage(inputIm, refIm, H): hInput, wInput, _ = inputIm.shape hRef, wRef, _ = refIm.shape corners = np.array([[1, 1, wInput, wInput], [1, hInput, 1, hInput], [1, 1, 1, 1]]) cornersWarped = np.dot(H, corners) cornersWarped = normalizeHomogeneous.normalizeHomogeneous(cornersWarped) x_min = np.min(cornersWarped[0, :]) x_max = np.max(cornersWarped[0, :]) y_min = np.min(cornersWarped[1, :]) y_max = np.max(cornersWarped[1, :]) width = np.int(np.ceil(x_max - x_min)) hight = np.int(np.ceil(y_max - y_min)) warpIm = np.zeros((hight * width, 3)) Hinv = np.linalg.inv(H) xx = np.arange(x_min, x_max) yy = np.arange(y_min, y_max) [X, Y] = np.meshgrid(xx, yy) tmp = np.ones((1, X.shape[0] * X.shape[1])) #pointsProjected=np.vstack(X.T) #tmp1=np.append(X.T,Y.T) #pointsProjected=np.append(tmp1,tmp) tmpX = X.reshape(1, -1) pointsProjected = np.vstack((X.reshape(1, -1), Y.reshape(1, -1), tmp)) pointsSorce = np.dot(Hinv, pointsProjected) pointsSorce = normalizeHomogeneous.normalizeHomogeneous(pointsSorce) xr = pointsSorce[0, :] xr = xr.astype(np.int) yr = pointsSorce[1, :] yr = yr.astype(np.int) for i in range(0, width * hight): #print("1") if (xr[i] >= 1 and yr[i] >= 1 and xr[i] < wInput and yr[i] < hInput): for c in range(0, 3): warpIm[i, c] = inputIm[yr[i], xr[i], c] warpIm = np.reshape(warpIm, (hight, width, 3)) offsetX = 1 offsetY = 1 if (y_min < 1): offsetY = np.int(round(np.abs(y_min))) if (x_min < 1): offsetX = np.int(round(np.abs(x_min))) canvas = np.zeros((np.int(hRef + offsetY), np.int(wRef + offsetX), 3)) canvas = canvas.astype(np.int32) #print( canvas[offsetY:,offsetX:,:].shape) canvas[offsetY:, offsetX:, :] = refIm plt.figure() plt.imshow(canvas) #C=np.dstack((warpIm,canvas*1.8)) #cv2.imshow("imfuse",C) #mergeIm=Image.blend(warpIm,canvas*1.8,alpha=0.5) #mergeIm=pymg.imfuse(warpIm,canvas*1.8) mergeIm = warpIm.copy() r, c, _ = canvas.shape for i in range(0, r): for j in range(0, c): if (i < hight and j < width): if (canvas[i, j, 1] != 0 and canvas[i, j, 2] != 0 and canvas[i, j, 0] != 0): mergeIm[i, j, :] = canvas[i, j, :] #mergeIm = np.array(mergeIm, np.int32) #plt.imshow(mergeIm) return warpIm, mergeIm
def rectifyImage(inputIm, corners, H): input_x = inputIm.shape[0] input_y = inputIm.shape[1] #ref_x = refIm.shape[0] #ref_y = refIm.shape[1] #corners = np.array([[0, input_y - 1, 0, input_y - 1], [0, 0, input_x - 1, input_x - 1], [1, 1, 1, 1]]) cornersWarped = np.dot(H, corners) cornersWarped = normalizeHomogeneous.normalizeHomogeneous(cornersWarped) x_min = min(cornersWarped[0, :]) x_max = max(cornersWarped[0, :]) y_min = min(cornersWarped[1, :]) y_max = max(cornersWarped[1, :]) print(x_min, y_min, x_max, y_max) # boundary points point1 = min(x_min, 0) point2 = x_max point3 = min(y_min, 0) point4 = y_max Hinv = np.linalg.inv(H) # initial width = np.int(math.ceil(x_max - point1) - 1) height = np.int(math.ceil(y_max - point3) - 1) warpIm = np.zeros((height, width, 3)) mergeIm = warpIm.copy() row = [] col = [] k = 0 for i in range(np.int(point1), np.int(point2)): row.append(i) for j in range(np.int(point3), np.int(point4)): col.append(j) X, Y = np.meshgrid(row, col) box = np.column_stack((X.ravel(), Y.ravel())) result_box = (np.append(box, np.ones((len(box), 1)), axis=1)).T print(result_box) for i in range(np.int(point3), np.int(point4)): for j in range(np.int(point1), np.int(point2)): if (len(result_box.T) > k): tmp = result_box.T[k] inv = np.matmul(Hinv, tmp) input_col = inv[0] / inv[2] input_row = inv[1] / inv[2] k = k + 1 if (0 < input_row and 0 < input_col and input_row < input_x - 1 and input_col < input_y - 1): r = np.int(input_row) c = np.int(input_col) tmp1 = input_row - r tmp2 = input_col - c warpIm_r = np.int(i - point3) warpIm_c = np.int(j - point1) mergeIm_r = np.int(i - point3) mergeIm_c = np.int(j - point1) # mapping warp warpIm[warpIm_r, warpIm_c] = (1 - tmp1) * (1 - tmp2) * inputIm[r][c] + tmp1 * (1 - tmp2) * inputIm[r][ c + 1] \ + tmp1 * tmp2 * inputIm[r + 1][c + 1] + (1 - tmp1) * tmp2 * inputIm[r + 1][ c] # mapping merge # mergeIm[mergeIm_r, mergeIm_c] = (1 - tmp1) * (1 - tmp2) * inputIm[r][c] + tmp1 * (1 - tmp2) * \ # inputIm[r][c + 1] \ # + tmp1 * tmp2 * inputIm[r + 1][c + 1] + (1 - tmp1) * tmp2 * \ # inputIm[r + 1][c] return warpIm