代码展示:
# -*- coding:utf-8 -*-
import cv2
import numpy as np
class HalconBoard:
def __init__(self, pattern_size_hw, resolution_mm):
self._pattern_size_hw = pattern_size_hw
self._resolution_mm = resolution_mm
self._object_points2d_nx2_original = np.mgrid[0:self._pattern_size_hw[1],
0:self._pattern_size_hw[0]].T.reshape(-1, 2)
self.object_points2d_nx2 = self._object_points2d_nx2_original.copy()
self.object_points2d_nx2[:, 0] = self._object_points2d_nx2_original[:, 1]
self.object_points2d_nx2[:, 1] = self._object_points2d_nx2_original[:, 0]
self.object_points3d_nx3 = np.hstack(
(self.object_points2d_nx2, np.zeros((self.object_points2d_nx2.shape[0], 1), np.float))) * resolution_mm
def get_board_picture(self,
block_distance_xy_pixel=(80, 80),
foreground_color=(0, 0, 0),
background_color=(255, 255, 255),
outer_board_xy=None,
outer_board_thickness=1,
triangular_distance=1.0):
halcon_circle_radius = int(min(block_distance_xy_pixel) * 0.618 * 0.5)
height, width = self._pattern_size_hw
if outer_board_xy is not None:
image_height = block_distance_xy_pixel[1] * (height - 1) + 2 * halcon_circle_radius + 2 * outer_board_xy[
1] + outer_board_thickness
image_width = block_distance_xy_pixel[0] * (width - 1) + 2 * halcon_circle_radius + 2 * outer_board_xy[
0] + outer_board_thickness
offset = np.array(
[outer_board_xy[0], outer_board_xy[1]]) + halcon_circle_radius + outer_board_thickness // 2
else:
image_height = block_distance_xy_pixel[1] * (height - 1) + 4 * halcon_circle_radius
image_width = block_distance_xy_pixel[0] * (width - 1) + 4 * halcon_circle_radius
offset = np.array([halcon_circle_radius * 2] * 2)
image = np.zeros((image_height, image_width, 3), dtype=np.uint8)
image[:] = background_color
image_points_nx2 = self.object_points2d_nx2.copy()
image_points_nx2[:, 0], image_points_nx2[:, 1] = self.object_points2d_nx2[:, 1], self.object_points2d_nx2[:, 0]
image_points_nx2[:, 0] *= block_distance_xy_pixel[0]
image_points_nx2[:, 1] *= block_distance_xy_pixel[1]
image_points_nx2 += offset
image_points_list = np.vsplit(image_points_nx2, image_points_nx2.shape[0])
for point in image_points_list:
x, y = point.ravel()
cv2.circle(image, (x, y), radius=halcon_circle_radius, color=foreground_color, thickness=-1,
lineType=cv2.LINE_AA)
if outer_board_xy is not None:
upper_left = image_points_nx2[0, :] - halcon_circle_radius - np.array(outer_board_xy)
bottom_right = image_points_nx2[-1, :] + halcon_circle_radius + np.array(outer_board_xy)
start = (int(upper_left[0] + halcon_circle_radius * 2 * triangular_distance), int(upper_left[1]))
end = (int(upper_left[0]), int(upper_left[1] + halcon_circle_radius * 2 * triangular_distance))
triangular = np.array([start, tuple(upper_left.astype(np.int)), end])
cv2.drawContours(image, [triangular], 0, color=0, thickness=-1, lineType=cv2.LINE_AA)
cv2.rectangle(image, pt1=tuple(upper_left.astype(np.int)), pt2=tuple(bottom_right.astype(np.int)), color=0,
thickness=outer_board_thickness, lineType=cv2.LINE_AA, shift=0)
return image, image_points_nx2
if __name__ == '__main__':
calibration_board = HalconBoard(pattern_size_hw=(7, 7), resolution_mm=2.5)
scale_cfg = 3
board_pic, board_points_nx2 = calibration_board.get_board_picture(
block_distance_xy_pixel=(71 * scale_cfg + 1, 71 * scale_cfg + 1),
outer_board_xy=(53 * scale_cfg, 53 * scale_cfg),
outer_board_thickness=66,
triangular_distance=2.0)
cv2.namedWindow('cb', 0)
cv2.imshow('cb', board_pic)
cv2.waitKey()
效果展示: