手动Homography对齐RGB与热成像图像失败,求问题排查
手动图像校准对齐失败问题排查
我正在开发一款采用手动校准方式对齐两张图像的应用,因自动校准在该场景下效果不佳,故选择手动选取像素点实现近乎像素级的对齐。但最终结果未达预期——按计算出的点应该能将图像精准重叠,却未实现。
单应矩阵结果
[[ 7.43200521e-01 -1.79170744e-02 -1.76782990e+02] [ 1.00046389e-02 7.84106136e-01 -3.22549155e+01] [ 5.10695284e-05 -8.48641135e-05 1.00000000e+00]]
手动选取的点
RGB图像点
[[ 277 708] [1108 654] [ 632 545] [ 922 439] [ 874 403] [ 398 376] [ 409 645] [ 445 593] [ 693 342] [ 739 244] [ 505 234] [ 408 275] [ 915 162] [1094 126] [ 483 115] [ 951 366] [ 517 355]]
热成像图像点
[[ 8 549] [634 491] [282 397] [496 318] [461 289] [113 269] [122 479] [148 438] [325 236] [360 162] [194 156] [121 188] [484 106] [621 67] [178 62] [515 261] [203 253]]
实现代码
def manual_calibration(self, rgb: cv2.UMat, thermal: cv2.UMat) -> Tuple[cv2.UMat, Tuple[int, int, int, int]]: rgb_gray = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY) thermal_gray = cv2.cvtColor(thermal, cv2.COLOR_BGR2GRAY) h_rgb, w_rgb = rgb_gray.shape h_th, w_th = thermal_gray.shape thermal_gray = cv2.copyMakeBorder(thermal_gray, 0, h_rgb - h_th, 0, 0, cv2.BORDER_CONSTANT, value=[0, 0, 0]) merged = cv2.hconcat((rgb_gray, thermal_gray)) self.merged = cv2.cvtColor(merged, cv2.COLOR_GRAY2RGB) def point_validation(ix, iy): if ix > w_rgb: ix -= w_rgb return ix, iy self.points_left = np.array([]) self.points_right = np.array([]) self.label = True def select_point(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: # captures left button double-click ix, iy = x, y cv2.circle(img=self.merged, center=(x,y), radius=5, color=(0,255,0),thickness=-1) ix, iy = point_validation(ix, iy) pt = np.array([ix, iy]) if self.label: # self.points_left = np.vstack((self.points_left, pt)) self.points_left = np.vstack((self.points_left, pt)) if self.points_left.size else pt self.label = False else: # self.points_right = np.vstack((self.points_right, pt)) self.points_right = np.vstack((self.points_right, pt)) if self.points_right.size else pt self.label = True print(ix, iy) cv2.namedWindow('calibration') cv2.setMouseCallback('calibration', select_point) while True: cv2.imshow("calibration", self.merged) if cv2.waitKey(20) & 0xFF == 27: break cv2.destroyAllWindows() print(self.points_left) print(self.points_right) ### EDIT NEW POINT VALIDATION rgb_gray_check = rgb_gray thermal_gray_check = thermal_gray for point in self.points_left: cv2.circle(img=rgb_gray_check, center=point, radius=5, color=(0,255,0),thickness=-1) for point in self.points_right: cv2.circle(img=thermal_gray_check, center=point, radius=5, color=(0,255,0),thickness=-1) cv2.imshow('rgb', rgb_gray_check) cv2.imshow('thermal', thermal_gray_check) cv2.waitKey(0) ### EDIT NEW POINT VALIDATION # Compute homography # 0 - a regular method using all the points # CV_RANSAC - RANSAC-based robust method # CV_LMEDS - Least-Median robust method matrix, mask = cv2.findHomography(self.points_left, self.points_right, 0) print(matrix) # matrix[0][3] += (w_th/2) # matrix[1][3] += (h_th/2) warp_src = cv2.warpPerspective(thermal, matrix, (rgb.shape[1], rgb.shape[0])) alpha = 0.5 beta = (1.0 - alpha) dst_warp_blended = cv2.addWeighted(rgb, alpha, warp_src, beta, 0.0) cv2.imshow('Blended destination and warped image', dst_warp_blended) cv2.waitKey(0)
原始图像
- RGB图像:

- 热成像图像:

当前结果

请问我哪里出错了?
内容的提问来源于stack exchange,提问作者Blank
相关产品推荐
相关产品推荐

