와핑기법과 원근 변환

BERT·2023년 4월 11일
0

OpenCV

목록 보기
2/2

Warping

영상을 이동, 회전, 크기변환 등을 이용해 이미지를 왜곡하거나 왜곡된 이미지를 복원하기 위한 처리 기법

다양한 이미지 변형 방법

변환 (Transformations)
새로운 좌표(xxx \rarr x')로 변환하는 함수

  • 강체변환
    크기 각도가 보존되는 변환
  • 유사변환
    크기는 변하고 각도는 보존되는 변환
  • 선형변환
    Vector 공간에서의 이동
  • Affine
    선형변환과 이동변환가지 포함
    선의 수평성 유지
  • Perspective
    수평성 유지되지 않음
    원근 변환

변환행렬을 사용하는 OpenCV 함수

dst=cv2.warpAffine(src, matrix, dsize, dst, flags, borderMode, borderValue)
src원본 이미지numpy
matrix2x3변환행렬
dsize결과 이미지 크기(width, height)
dst결과 이미지optional
flags보간법 알고리즘optional
borderMode외곽영역 보정 플래그optional
borderValue외곽영역 보정 플래그
cv2.BORDER_CONSTANT
색상 값 (default=0)
optional
  • flags
    cv2.INTER_LINEAR
    cv2.INTER_NEAREST
    cv2.INTER_AREA
    cv2.INTER_CUBIC

  • borderMode
    cv2.BORDER_CONSTANT
    cv2.BORDER_REPLICATE
    cv2.BORDER_WARP
    cv2.BORDER_REFLECT

xycar_ws
├ src
│  └ sliding_drive
│    └ src
│      ├ translation.py
│      ├ scaling.py
│      ├ Lenna.png
│      └ chess.png
│     
├ build
└ devel

python translation.py

(10d101d2)\begin{pmatrix} 1 & 0 & d_1\\ 0 & 1 & d_2 \end{pmatrix}(xy1)\begin{pmatrix} x\\ y\\ 1 \end{pmatrix}==(d1+xd2+y)\begin{pmatrix} d_1+x\\ d_2+y \end{pmatrix}
단순 이동

외곽 픽셀 파란색으로 보정

외곽 픽셀 원본 반사 보정


python scaling.py

(a1000a20)\begin{pmatrix} a_1 & 0 & 0\\ 0 & a_2 & 0 \end{pmatrix}(xy1)\begin{pmatrix} x\\ y\\ 1 \end{pmatrix}==(a1xa2y)\begin{pmatrix} a_1x\\ a_2y \end{pmatrix}

보간법 없이 축소

보간법 적용 축소

보간법 없이 확대

보간법 적용 확대

크기 조정 OpenCV 함수

cv2.resize(src, dsize, dst, fx, fy, interpolation)
python resizing.py

src원본 이미지
dsize출력 영상 크기, 생략 시 fx,fy 적용
fx,fy크기 배율
interpolation보간법 알고리즘 선택 플래그
dst결과 이미지

크기 지정으로 축소

배율 지정으로 축소

회전

python rotation1.py
(xy)\begin{pmatrix} x'\\ y' \end{pmatrix}==(cosθsinθsinθcosθ)\begin{pmatrix} \cos\theta & -\sin\theta\\ \sin\theta & \cos\theta \end{pmatrix}(xy)\begin{pmatrix} x\\ y \end{pmatrix}

OpenCV에서는 y축이 반대방향
(cosθsinθ(1cosθ)xsinθysinθcosθsinθx(1cosθ)y)\begin{pmatrix} \cos\theta & \sin\theta & (1-\cos\theta)x-\sin\theta y\\ -\sin\theta & \cos\theta & \sin\theta x-(1-\cos\theta)y \end{pmatrix}

행렬 3열 : anchor가 원점(왼쪽상단)이므로 회전한 이미지를 중앙으로 이동

45°45\degree

90°90\degree

python rotation2.py
cv2.getRotationMatrix2D() 사용

45°45\degree 0.5배

90°90\degree 1.5배

Affine 변환

크기 이동 회전에서 원래 평행 특성 그대로 유지
python affine.py

pts1 \rarr pts2

pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
pts2 = np.float32([[10, 100], [200, 50], [100, 250]])

Perspective

python perspective.py

도로 이미지에 대한 영상처리

도로 이미지 Bird Eye View 변형 처리
차선 검출
원본 이미지에 오버레이

0개의 댓글