self.calibrate_button = QPushButton("Calibrate")
self.calibrate_button.clicked.connect(self.on_calibrate_button_clicked)
layout.addWidget(self.calibrate_button)
self.exit_button = QPushButton("Exit")
self.exit_button.clicked.connect(self.on_exit_button_clicked)
layout.addWidget(self.exit_button)
self.objpoints = [] # 3D points in real world space
self.imgpoints = [] # 2D points in image plane
self.grid_size = tuple(int(x) for x in args.grid.split("x"))
self.grid_points = np.zeros((1, np.prod(self.grid_size), 3), np.float32)
self.grid_points[0, :, :2] = np.indices(self.grid_size).T.reshape(-1, 2)
self.device = args.input
self.args = args
self.img = None
self.frame_count = 0 # Initialize frame counter
def update_image(self):
# Get the latest image from the camera
img = buffer_manager.get_device(self.device).get().image
if img is None:
return # Skip if there's no image
self.img = img # Store the latest image
# Convert img to QPixmap and set it on the label
pixmap = cvimage_to_pixmap(img)
self.label.setPixmap(pixmap)
# Detect chessboard corners
self.detect_chessboard_corners()
def detect_chessboard_corners(self):
if self.img is None:
return
# Increment frame counter
self.frame_count += 1
if self.frame_count % self.args.framestep != 0: # Check if it's the framestep-th frame
return
gray = cv2.cvtColor(self.img, cv2.COLOR_BGR2GRAY)
found, corners = cv2.findChessboardCorners(
gray,
self.grid_size,
cv2.CALIB_CB_ADAPTIVE_THRESH +
cv2.CALIB_CB_NORMALIZE_IMAGE +
cv2.CALIB_CB_FILTER_QUADS
)
if found:
term = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.01)
cv2.cornerSubPix(gray, corners, (5, 5), (-1, -1), term)
self.imgpoints.append(corners) # Append corners to imgpoints
self.objpoints.append(self.grid_points) # Append grid points
cv2.drawChessboardCorners(self.img, self.grid_size, corners, found)
def on_calibrate_button_clicked(self):
self.calibrate_camera()
def calibrate_camera(self):
N_OK = len(self.objpoints)
if N_OK < 12:
print("Less than 12 corners (%d) detected, calibration failed" % N_OK)
return
K = np.zeros((3, 3))
D = np.zeros((4, 1))
rvecs = [np.zeros((1, 1, 3), dtype=np.float64) for _ in range(N_OK)]
tvecs = [np.zeros((1, 1, 3), dtype=np.float64) for _ in range(N_OK)]
calibration_flags = (cv2.fisheye.CALIB_RECOMPUTE_EXTRINSIC +
cv2.fisheye.CALIB_CHECK_COND +
cv2.fisheye.CALIB_FIX_SKEW)
if self.args.fisheye:
ret, mtx, dist, rvecs, tvecs = cv2.fisheye.calibrate(
self.objpoints,
self.imgpoints,
(640, 480), # Change according to your actual resolution
K,
D,
rvecs,
tvecs,
calibration_flags,
(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 1e-6)
)
else:
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(
self.objpoints,
self.imgpoints,
(640, 480), # Change according to your actual resolution
None,
None)
if ret:
fs = cv2.FileStorage(self.args.output, cv2.FILE_STORAGE_WRITE)
fs.write("resolution", np.int32([640, 480])) # Change according to your actual resolution
fs.write("camera_matrix", K)
fs.write("dist_coeffs", D)
fs.release()
print("Successfully saved camera data")
else:
print("Calibration failed")
def on_exit_button_clicked(self):
print("Quitting...")
QApplication.quit()
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input", type=int, default=0, help="input camera device")
parser.add_argument("-grid", "--grid", default="9x6", help="size of the calibrate grid pattern")
parser.add_argument("-r", "--resolution", default="640x480", help="resolution of the camera image")
parser.add_argument("-framestep", type=int, default=20, help="use every nth frame in the video")
parser.add_argument("-o", "--output", default=DEFAULT_PARAM_FILE, help="path to output yaml file")
parser.add_argument("-fisheye", "--fisheye", action="store_true", help="set true if this is a fisheye camera")
parser.add_argument("-flip", "--flip", default=0, type=int, help="flip method of the camera")
parser.add_argument("--no_gst", action="store_true", help="set true if not use gstreamer for camera capture")