Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags
more
Archives
Today
Total
관리 메뉴

what i learned

[OpenCV] Camera Calibration - Python, ROS 본문

Image Processing

[OpenCV] Camera Calibration - Python, ROS

햄식이111 2023. 6. 20. 01:50

카메라에 calibration이 필요한 이유: 링크

 

calibration code

import cv2
import numpy as np

image_width, image_height = 640, 480

capture = cv2.VideoCapture(1)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

def calibrate_image(src):
    global image_width, image_height

    # calibrate_mtx와 dist 각자 카메라에 맞는 mtx로 변경해 줘야 한다.
    calibrate_mtx = np.array([
        [ 350.354184, 0.0, 328.104147],
        [0.0, 350.652653, 236.540676],
        [0.0, 0.0, 1.0]
    ])
    dist = np.array([-0.289296, 0.061035, 0.001786, 0.015238, 0.0])

    cal_mtx, cal_roi = cv2.getOptimalNewCameraMatrix(calibrate_mtx, dist,
                            (image_width, image_height), 1, (image_width, image_height))

    dst = cv2.undistort(src, calibrate_mtx, dist, None, cal_mtx)
    x, y, w, h = cal_roi
    calibrated_image = dst[y : y + h, x : x + w]

    return cv2.resize(calibrated_image, (image_width, image_height))

while True:
    _, src = capture.read()
    cal = calibrate_image(src)

    cv2.imshow("calibrated image 1", cal)

    if cv2.waitKey(10) == 27:
        break

 

ROS에서 calibraion mtx와 dist 값 얻기

ROS에서 지원하는 camera_calibration 패키지를 사용하면 쉽게 얻을 수 있다.

💡 터미널 창 3개 필요 - 하단의 명령어를 한 개당 터미널 한 개에서 진행

# ros 구동
$ roscore

# ros로 camera 켜기
$ roslaunch usb_cam usb_cam-test.launch video_device:=/usb_cam
# roslaunch [packagename] [launch파일명] video_device:=/[카메라토픽]

# calibration mtx 찾아주는 패키지 실행
$ rosrun camera_calibration cameracalibrator.py --size 8x6 --square 0.02 image:=/my_camera/image camera:=/my_camera
# 8x6: calibration에 사용할 체커보드 사이즈
# 0.02: 체커보드의 사각형 길이 (단위:m)
# /my_camera/image: 이미지 토픽
# /my_camera: 카메라 토픽

ROS 공식 사이트에 더 상세하게 나와있다. [링크] ROS camera_calibration

 

+ 체커보드의 size와 square 측정법

4x2 size 체커보드


공부한 내용을 기록하는 공간입니다.

잘못된 정보가 있을 경우 댓글로 피드백 주세요.

올바른 지식을 알려주셔서 감사합니다:)

'Image Processing' 카테고리의 다른 글

[VSLAM] Ubuntu18.04에서 ORB_SLAM3 build후 실행  (0) 2023.07.27
[VSLAM] SLAM이란?  (0) 2023.07.20
[OpenCV] keypoint detector  (0) 2023.07.13