딥러닝

자율주행 perception aplication

Jagbbum 2024. 1. 5. 14:45

Camera model

- 카메라 종류에 따라 이미지 형태 달라짐

- Pinhole Camera model

  - intrinsic : 카메라 내부적인 특성

  - extrinsic : 카메라와 대상과의 위치와 자세에 관한 특성

 

Camera Intrinsic Calibration

Camera Calibration : 카메라가 가진 고유 특성을 파악하는 과정

 

intrinsic 과 extrinsic 으로 나누는 이유

- Intrinsic : 동일한 위치에서 서로 다른  카메라로 동일한 피사체를 촬형하면 결과가 다름

- extrinsic : 같은 카메라로 다른 위치에서 동일한 피사체를 촬영하면 결과가 다름

 

instrinsic calibration은 초점거리와 주점을 의미

- 초점 거리 :  렌즈부터 이미지 센서까지의 거리

Computer Vision에서는 이미지 센서의 cell 크기에 대한 상대적인 Pixel 단위로 표현

-> 이미지 해상도에 따라 초점거리가 변화

 

- 주점 : Pinhole과 이미지 센서가 직교하는 위치

 

Computer Vision의 4개의 좌표계

- 월드 좌표계 :  3차원 공간에 존재하는 좌표계

- 카메라 좌표계 : 카메라를 기준으로 설정하는 좌표계

  - 카메라 렌즈가 바라보는 방향 Zc

  - 카메라 아래쪽 방향 Xc

  - 카메라 오른쪽 방향 Yc

- 이미지 좌표계 : 실제 이미지로 표출되는 데이터를 표현한 좌표계

- 평균 이미지 좌표계 : 실제하지 않는 좌표계, Computer Vision에서 해석을 위해 정의한 좌표계

 

Distortion

렌즈 형상이 곡률을 가지는 구면 형태이기 때문에 왜곡 발생

- 방사 왜곡

  - Barrel Distortion : 중심부가 외각부보다 큰 형태

  - Pincushion Distortion : 중심부가 외각부보다 작은 형태

- 접선 왜곡 : 카메라 제조 공정에서 발생하는 왜곡, 렌즈와 이미지 센서와의 수평이 맞지않는 경우 발생

- 원근 왜곡 : 3차원 공간이 2차원 공간으로 투영괴면서 발생하는 왜곡

 

 

Calibration : 3차원 공간에 존재하는 물체가 2차원 이미지 공간에 투영되는 과정을 통해 카메라 고유의 특성을 파악

import numpy as np
import cv2 as cv
import glob
# termination criteria
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001) # constant 

# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((6*7,3), np.float32) # chess board size , 3(x,y,z)
objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2) * 0.025 # 0.025 == 1 grid size (m unit)

# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.

images = glob.glob('*.jpg') # image file list

for fname in images:
    img = cv.imread(fname)
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

    # Find the chess board corners
    ret, corners = cv.findChessboardCorners(gray, (7,6), None) # gray image, grid size ->  bool value about finding corners, corner points of image 
    
    # If found, add object points, image points (after refining them)
    if ret == True:
        objpoints.append(objp)

        # refines corner locations using gradient direction
        # image,corners, window size for search,zero zone(no size of -1,-1),end point after max count or value under epsilon to move
        corners2 = cv.cornerSubPix(gray,corners, (11,11), (-1,-1), criteria) 

        imgpoints.append(corners)
        # Draw and display the corners
        cv.drawChessboardCorners(img, (7,6), corners2, ret) 
        cv.imshow('img', img)
        cv.waitKey(500)
cv.destroyAllWindows()

- 3차원 월드 공간에 존재하는 object의 위치 정보 (objpoints)

- 2차원 이미지 공간에 존재하는 object의 이미지 픽셀 위치 정보 (imgpoints)

-> 이 두 개의 쌍으로 파라미터 조정

 

Camera Calibrate

ret, mtx, dist, revecs, tvecs = cv.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)

 

ret : Calibrate 결과

<Intrinsic Calibration>

mtx : intrinsic Matrix

dist : Distortion Matrix

 

<Extrinsic Calibration> 

revecs, tvecs

 

Undistortion

# undistort 1.
dst = cv.undistort(img, mtx, dist, None, mtx)

# undistort 2.
mapx, mapy = cv.initUndistortRectifyMap(mtx, dist, None, None, (w,h), 5)
remap = cv.remap(img, mapx, mapy, cv.INTER_LINEAR)

 

cv.undistort()

- initUndistortRectifyMap(), remap() 함수를 내부적으로 호출

- 하지만 RectifyMap을 계산하는 것은 camera matric와 distortion coefficients를 알면 한 번만 계산하면 됨

두 함수를 분리하여 카메라로부터 이미지를 받을 때 cv.remap()을 적용하는 것이 효율적

 

Extrinsic Calibration

- 월드 좌표계 -> 카메라 좌표계, 카메라 좌표계 -> 월드 좌표계를 이해하기 위한 과정

- 카메라가 실제로 존재하는 3차원 공간에 대한 정보를 다룬다

- computer vision을 활용한 extrinsic calibration

  - sensor fusion을 위한 정보 활용

  - perception aplication을 위한 정보로 활용

- 3차원 데이터를 출력하는 LiDAR 센서는 Camer와의 Data Fusion을 통해 Depth 정보를 획득

 

'딥러닝' 카테고리의 다른 글

Lidar - 3D perception  (1) 2024.01.10
딥러닝 : YOLO  (0) 2023.12.19
딥러닝 : object detection  (0) 2023.12.18
딥러닝 : pytorch  (0) 2023.12.14
딥러닝: 신경망 기초  (0) 2023.12.13