레이블이 3D Scanner인 게시물을 표시합니다. 모든 게시물 표시
레이블이 3D Scanner인 게시물을 표시합니다. 모든 게시물 표시

2017년 7월 11일 화요일

나만의 3차원 스캐너 만들기 (2)

- 주제 : 카메라 2개와 Line 레이저를 사용한 광학식 3차원 스캐너

- 개발 범위 : 카메라로 데이터 획득 --> Reconstruction (3D point cloud)

  앞에서 레이저 포인터를 이용하여 3차원 데이터를 획득 하였을 때의 단점을 계선하기 위해 
  레이저 포인터를 Line 레이저로 바꿔본다.

  레이저 포인터를 사용하면 대응점 검출을 매우 간단한 방법으로 할 수 있었다.
  

  Line 레이저를 사용하면 왼쪽 카메라에서 대응되는 점을 오른쪽에서 어떻게 찾을 수 있을까?
  
  두 카메라의 위치를 알고있다면 왼쪽 카메라에서의 한 점은 두 카메라의 위치에 따른 제약에 따라 오른쪽 카메라에서 선으로 투영이 된다.
  그 내용은 Epipolar Geometry에 대하여 검색을 해보면 알 수 있다.
Epipolar geometry is the geometry of stereo vision. When two cameras view a 3D scene from two distinct positions, there are a number of geometric relations between the 3D points and their projections onto the 2D images that lead to constraints between the image points. These relations are derived based on the assumption that the cameras can be approximated by the pinhole camera model.

출처 : Wikipedia https://en.wikipedia.org/wiki/Epipolar_geometry
  
  위키피디아에서 에피폴라기하에 대하여 검색을 해보면 위와같이 그림이 잘 나와있다. 
  왼쪽 뷰에서 3차원 객체 X를 바라볼때 X가 OL로 이어지는 선으로 객체가 투영이 되는데 이 선은 오른쪽 뷰에서 빨간선으로 보이게된다.
  왼쪽 뷰의 한 픽셀에 대한 오른쪽 뷰에서의 선을 계산하기 위해서는 Fundamental matrix를 사용하는 것이 편하다.
  
  F는 기존에 수행했던 cv::stereoCalibration에서 결과 값으로 계산을 해준다.
C++: double stereoCalibrate(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints1, InputArrayOfArrays imagePoints2, InputOutputArray cameraMatrix1, InputOutputArray distCoeffs1, InputOutputArray cameraMatrix2, InputOutputArray distCoeffs2, Size imageSize, OutputArray R, OutputArray T, OutputArray E, OutputArray F, TermCriteria criteria=TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 1e-6), int flags=CALIB_FIX_INTRINSIC )
  
  위의 이미지에서. 왼쪽 뷰에서 Line 레이저에 의해 인식된(빨간색) 점 중 하나를 고르면 오른쪽 뷰에서 주황색 선을 따라 투영이 된다. 
  이때 Line 레이저를 통해 인식된 빨간 선과 교차하는 부분을 계산하면 대응점을 쉽게 구할 수 있다.

  다시 Line 스캔의 순서를 정리해보면
  1. Line을 인식한다.(빨간색)
  2. Epipolar 제약에 따른 선을 계산하여 교점을 계산한다. (=대응점 계산) 

  1. 대응점에 대한 Ray를 계산한다. 
  1. 두 Ray의 교점을 계산한다.(triangulation)
  2. 위 [2~5]과정을 모든 Pixel을 반복한다.
  

- 결과
  레이저 Line에 대한 3차원 데이터를 매우 빠르게 얻을 수 있다.
 

 

나만의 3차원 스캐너 만들기 (1)

- 주제 : 카메라 2개와 레이저 포인터를 사용한 광학식 3차원 스캐너

- 개발 범위 : 카메라로 데이터 획득 --> Reconstruction (3D point cloud)

- 구성품 : 카메라와 렌즈 2 set, 레이저 포인터, 카메라를 고정할 지지대
  

  1. Calibration (cv::stereoCalibrate )
     두 개의 카메라에서 활영한 영상에서 각각의 대응점을 찾고, 두 카메라 사이의 정보가 있으면 객체의 실제 3차원 좌표를 계산 할 수 있다.
     일단 카메라 사이의 정보가 필요하다. OpenCV의 Streo Calibration을 수행하여 두 카메라 간의 기하학적 정보 R, T를 계산한다.
     

  1. 대응점(corresponding points) 검출
     왼쪽 카메라에서 보이는 한 점이 오른쪽 카메라에서 어떤 점일까?
     사람은 매우 똑똑하기 때문에 쉽게 대응점을 찾지만 컴퓨터가 카메라 영상에서 일치하는 점을 찾기는 어렵다.
     

     그래서 3차원 스캐너에서는 보통 여러종류의 패턴을 사용한다. 
     일단 주위에서 쉽게 구할 수 있는 레이저 포인터를 사용해보자.
     

     레이저 포인터를 사용하여 손쉽게 대응점을 찾을 수 있었다.
     각 카메라의 영상에서 레이저 포인터의 중심점(x, y)를 얻는다.
     여러가지 방법이 있을 수 있겠지만.. 나는 cv::Canny --> cv::findContours --> cv::fitEllipse 를 사용하여 중점을 계산하였다.

  1. 대응점에 대한 Ray 계산
     초점거리(f)와 주점(cx, cy) 그리고 전 단계에서 알아낸 대응점 좌표를 사용하여 카메라의 중심에서부터의 Ray를 계산한다.
     초점거리와 주점은 카메라 calibration을 수행하면 얻을 수 있다. (cv::calibrateCamera 또는 cv::stereoCalibrate)
     calibration을 하지 않는다면 렌즈의 초점거리를 초점거리(f)로, 이미지 센서의 중점을 주점(cx, cy)로 사용 할 수 있다.
     정밀도는 매우 떨어진다.
     오른쪽 카메라도 똑같이 계산을 한 후 기존에 계산된 R|t로 회전,이동을 시켜준다.

  1. 두 Ray의 교점 계산 (triangulation)
     우리는 한 변과 양 끝의 각을 알면 유일한 삼각형을 만들 수 있다는것을 알고있다.
     두 카메라의 기하학적인 관계(R|t)를 알고 있으며 양 끝에서의 Ray(크기와 방향을 가지는 vector)를 알고 있으므로 유일한 삼각형을 만들어 낼 수 있다.
     두 벡터의 교점을 구하면 레이저 포인터가 가리키는 한 점에 대한 3차원 좌표를 계산 할 수 있다.     
     

- 결과
  레이저 포인터가 가리키는 한 점에 대한 3차원 데이터를 매우 빠르게 얻을 수 있다.
  하지만 넓은 영역을 측정하기 힘들다.
   



2016년 12월 30일 금요일

Essential matrix 위키피디아 정리

Essential matrix  From Wikipedia, the free encyclopedia

if y and y' are homogeneous normalized image coordinates in image 1 and 2, respectively, then

만약 y 와 y'이 각각 이미지 1과 2에서 homogeneous한 정규화된 좌표라면
(y')TE y = 0 의 식을 만족한다

if y and y' correspond to the same 3D point in the scene.

The above relation which defines the essential matrix was published in 1981 by Longuet-Higgins, introducing the concept to the computer vision community. Longuet-Higgins' paper includes an algorithm for estimating E from a set of corresponding normalized image coordinates as well as an algorithm for determining the relative position and orientation of the two cameras given that E is known. Finally, it shows how the 3D coordinates of the image points can be determined with the aid of the essential matrix.

만약 y, y'이 같은 3차원 점이라면.
essential matrix를 정의하는 위의 관계는 1981년 컴퓨터 비전 커뮤니티에서 소개가 되었다.
Longuet-Higgins' 의 논문에서 일치하는 정규화된 이미지의 좌표 셋으로 부터 E 를 추정하기 위한 알고리즘 뿐 아니라 E를 사용하여 두 카메라의 상대적인 위치 및 방향을 계산할 수 있는 알고리즘을 포함한다.
마지막으로 essential matrix를 사용하여 이미지 포인트의 3차원 좌표를 구할 수 있는 방법을 보여준다.




참고
 
3x3 skew symmetric matrices can be used to represent cross products as matrix multiplications. Consider vectors a = ( a 1   a 2   a 3 ) T {\displaystyle \mathbf {a} =(a_{1}\ a_{2}\ a_{3})^{\mathrm {T} }} \mathbf {a} =(a_{1}\ a_{2}\ a_{3})^{\mathrm {T} } and b = ( b 1   b 2   b 3 ) T {\displaystyle \mathbf {b} =(b_{1}\ b_{2}\ b_{3})^{\mathrm {T} }} \mathbf {b} =(b_{1}\ b_{2}\ b_{3})^{\mathrm {T} }. Then, defining matrix:
[ a ] × = [ 0 − a 3 a 2 a 3 0 − a 1 − a 2 a 1 0 ] {\displaystyle [\mathbf {a} ]_{\times }={\begin{bmatrix}\,\,0&\!-a_{3}&\,\,\,a_{2}\\\,\,\,a_{3}&0&\!-a_{1}\\\!-a_{2}&\,\,a_{1}&\,\,0\end{bmatrix}}} [\mathbf {a} ]_{\times }={\begin{bmatrix}\,\,0&\!-a_{3}&\,\,\,a_{2}\\\,\,\,a_{3}&0&\!-a_{1}\\\!-a_{2}&\,\,a_{1}&\,\,0\end{bmatrix}}
the cross product can be written as
a × b = [ a ] × b . {\displaystyle \mathbf {a} \times \mathbf {b} =[\mathbf {a} ]_{\times }\mathbf {b} .} {\displaystyle \mathbf {a} \times \mathbf {b} =[\mathbf {a} ]_{\times }\mathbf {b} .}
This can be immediately verified by computing both sides of the previous equation and comparing each corresponding element of the results. See also: Plücker matrix

3x3 반대칭행렬(skew-symmetric matrix)는 행렬의 외적을 행렬의 곱셈으로 표현하는데 사용 할 수 있다.

a = (1,2,3) , b = (1,1,0)

외적
1, 1, 1    
1, 2, 3  --> (-3, -1*-3, -1) = (-3, 3, -1)
1, 1, 0

반대칭행렬
0, -3, 2      1
3,  0, -1  *  1  = (-3, 3, -1)
-2,  1,  0     0




Fundamental matrix 위키피디아 정리

Fundamental matrix (computer vision)  From Wikipedia, the free encyclopedia


In computer vision, the fundamental matrix F is a 3×3 matrix which relates corresponding points in stereo images. In epipolar geometry, with homogeneous image coordinates, x and x′, of corresponding points in a stereo image pair, Fx describes a line (an epipolar line) on which the corresponding point x′ on the other image must lie. That means, for all pairs of corresponding points holds

컴퓨터 비전에서 fundamental matrix F는 스테레오 이미지에서 상응하는 점과 관련된 3x3 matrix 이다.
스테레오 이미지 쌍에 대응하는 점들 x, x' 은 Epipolar geometry 에서 Fx로 계산된 선 위에 존재한다.


fundamental matrix는 두 이미지의 실제 픽셀 좌표 사이의 기하학적 관계를 표현한다.
임의의 두 이미지 A,B에 대하여 매칭되는 픽셀좌표 x, x' 사이에는 항상 다음과 같은 관계를 만족하는 행렬 F가 존재한다.

This is captured mathematically by the relationship between a fundamental matrix F and its corresponding essential matrix E, which is


이 식은 F와 E 사이의 관계를 수학식으로 표현한 것이다. K, K'은 두 이미지의 카메라 내부 파라미터 행렬이다.
fundamental matrix는 exssential matrix에 카메라 내부 파라미터 행렬인 K를 포함한 matrix 임을 알 수 있다.





2016년 12월 29일 목요일

Epipolar geometry 위키피디아 정리

Epipolar geometry  From Wikipedia, the free encyclopedia

Epipolar geometry is the geometry of stereo vision. When two cameras view a 3D scene from two distinct positions, there are a number of geometric relations between the 3D points and their projections onto the 2D images that lead to constraints between the image points. These relations are derived based on the assumption that the cameras can be approximated by the pinhole camera model.

스테레오 비전의 Geometry 이다. 두 카메라가 두개의 다른 위치에서  3차원 장면을 볼때, 3차원 포인트와 그것이 투영된 2차원 이미지 상에는 이미지 포인트 사이에서 제약을 야기하는 많은 기하학적 관계가 있다. 이러한 관계는 카메라가 핀홀 카메라 모델이라는 가정에 기초하여 유도된다.
즉 Epipolar geometry는 동일한 3차원 점에대한 영상을 서로 다른 두 지점에서 획득했을 때 두 이미지에서의 매칭쌍들 사이의 기하학적 관계를 다룬다.


Epipolar geometry

The figure below depicts two pinhole cameras looking at point X. In real cameras, the image plane is actually behind the focal center, and produces an image that is the symmetry about the focal center of the lens. Here, however, the problem is simplified by placing a virtual image plane in front of the focal center i.e optical center of each camera lens to produce an image not transformed by the symmetry. OL and OR represent the centers of symmetry of the two cameras lenses. X represents the point of interest in both cameras. Points xL and xR are the projections of point X onto the image planes.

아래 그림은 점 X를 바라보는 두개의 핀홀 카메라를 보여준다. 실제 카메라에서 이미지면(하늘색 사각형)은 초점 중심 뒤에 있으며 렌즈의 초점 중심에 대한 대칭 이미지를 생성한다. 하지만 그 문제는 아래 그림과 같이 이미지면을 초점 중심 앞에 배치함으로써 단순화 할 수 있다. OL과 OR은 두 카메라 렌즈 각각의 대칭 중심을 나타내며 X는 두 카메라의 관심지점을 나타낸다. 점 XL 과 XR은 점 X의 각 이미지평면으로의 투영을 나타낸다.

Each camera captures a 2D image of the 3D world. This conversion from 3D to 2D is referred to as a perspective projection and is described by the pinhole camera model. It is common to model this projection operation by rays that emanate from the camera, passing through its focal center. Note that each emanating ray corresponds to a single point in the image.

각 카메라는 3차원 세계의 2차원 이미지를 캡쳐한다. 이 3차원에서 2차원으로의 변환은 원근 투영(perspective projection)이라고 하며 핀홀 카메라 모델에서 설명이 된다. 이런 투영 모델은 보통 초점 중심을 통과하는 ray로 아래와 같이 표현을 한다. 각 ray는 하나의 점에 대응한다.


Epipole or epipolar point

Since the optical centers of the cameras lenses are distinct, each center projects onto a distinct point into the other camera's image plane. These two image points are denoted by eL and eR and are called epipoles or epipolar points. Both epipoles eL and eR in their respective image planes and both optical centers OL and OR lie on a single 3D line.

카메라 렌즈의 광학 중심이 다르기 때문에 각 중심은 각 이미지 평면의 다른 점으로 투영된다. 두 이미지 포인트는 eL, eR로 표시되며 epipoles 또는 epipolar points라고 부른다.
각 이미지 평면에서의 epipole(eL, eR)과 초점 중심(oL, oR)은 모두 3차원 상에서 같은 선 위에 놓여있다.

Epipolar line

The line OL–X is seen by the left camera as a point because it is directly in line with that camera's lens optical center. However, the right camera sees this line as a line in its image plane. That line (eR–xR) in the right camera is called an epipolar line. Symmetrically, the line OR–X seen by the right camera as a point is seen as epipolar line eL–xLby the left camera.

선 oL - X은 왼쪽 카메라의 optical canter와 연결이 되어있기 때문에 점으로 보인다. 하지만 오른쪽 카메라는 이미지 평면에서 선으로 보인다.
여기서 오른쪽 카메라의 eR - Xr을 epipolar line이라고 부른다. 대칭적으로 오른쪽 카메라에서 점으로 보이는 oR - X는 왼쪽 카메라에 의해 epipolar line eL - XL으로 보여진다.

An epipolar line is a function of the position of point X in the 3D space, i.e. as X varies a set of epipolar lines is generated in both images.
Since the 3D line OL–X passes through the optical center of the lens OL, the corresponding epipolar line in the right image must pass through the epipole eR (and correspondingly for epipolar lines in the left image). All epipolar lines in one image contains the epipolar point of that image. In fact, any line which contains the epipolar point is an epipolar line since it can be derived from some 3D point X.

Epipolar line은 점 X의 위치와 상관관계에 있으며  X가 변하면 epipolar line set이 다시 계산이 되어야 한다.
3차원 선 oL - X 는 oL의 optical center를 통과하기 때문에 오른쪽 이미지 평면에서의 대응되는 epipolar line 은 epipole eR을 통과하여야 한다. 
모든 epipolar line은 해당 이미지 평면의 epipolar point를 포함한다.

Epipolar plane

As an alternative visualization, consider the points X, OL & OR that form a plane called the epipolar plane. The epipolar plane intersects each camera's image plane where it forms lines—the epipolar lines. All epipolar planes and epipolar lines intersect the epipole regardless of where X is located.

점 X, oL과 oR를 가지는 평면을 epipolar plane이라 한다. epipolar plane은 각 카메라의 이미지 평면과 epipolar line에서 교차한다.

Epipolar constraint and triangulation

두 카메라간의 위치관계를 알고있다면 두가지 중요한 사실을 알 수 있다.
- If the projection point xL is known, then the epipolar line eR–xR is known and the point X projects into the right image, on a point xR which must lie on this particular epipolar line. This means that for each point observed in one image the same point must be observed in the other image on a known epipolar line. This provides an epipolar constraint: the projection of X on the right camera plane xR must be contained in the eR–xR epipolar line. Note also that all points X e.g X1, X2, X3 on the OL–XL line will verify that constraint. It means that it is possible to test if two points correspond to the same 3D point. Epipolar constraints can also be described by the essential matrix or the fundamental matrix between the two cameras.

- If the points xL and xR are known, their projection lines are also known. If the two image points correspond to the same 3D point X the projection lines must intersect precisely at X. This means that X can be calculated from the coordinates of the two image points, a process called triangulation.

투영점 xL을 알고있으므로 epipolar line eR - xR 을 알 수 있으며 점 X는 오른쪽 이미지 평면으로 투영이 되고 이 특정 epipolar line위에 놓여야 한다. 이 부분이 첫번째 epipolar constraint 이다.
다시 정리하면 오른쪽 카메라 이미지 평면의 xR로 투영되는 X는 eR - xR epipolar line 에 포함되어야 한다.
epipolar constraint는 두 카메라간의 essential matrix, fundamental matrix 로도 기술 할 수있다.

만약 점 xL 과 xR을 알고 있다면 투영된 선도 알 수 있다. 두 이미지에서의 점이 동일한 3차원 점이라면 투영된 선은 정확하게 X에서 교차한다. 이것은 두 이미지상의 점과 triangulation 을 이용하여 X를 계산 할 수 있다는 것을 의미한다.

정리하면..
왼쪽 이미지 평면의 점으로부터 오른쪽 이미지 평면에 대응되는 점을 유일하게 결정할 수는 없지만 그 점이 지나는 직선인 epipolar line은 유일하게 결정할 수 있다.
또 양쪽의 이미지 평면에서 대응되는 점을 알고있다면 삼각법을 통해서 3차원 좌표를 정확하게 계산 할 수 있다.

Simplified cases

The epipolar geometry is simplified if the two camera image planes coincide. In this case, the epipolar lines also coincide (EL–PL = ER–PR). Furthermore, the epipolar lines are parallel to the line OL–OR between the centers of projection, and can in practice be aligned with the horizontal axes of the two images. This means that for each point in one image, its corresponding point in the other image can be found by looking only along a horizontal line. If the cameras cannot be positioned in this way, the image coordinates from the cameras may be transformed to emulate having a common image plane. This process is called image rectification.

두 카메라 이미지 평면이 일치하면 epipolar geometry또한 단순화 할 수 있다.(두 카메라가 나란히 있는 경우 같음)
이 경우 epipolar line이 일치하게 되고 epipolar line은 중심점 oL - oR 선과도 평행하게 된다. 즉, 한 이미지의 각 점에 대하여 다른 이미지의 일치하는 점을 가로선만 보면서 찾을 수 있다.
카메라가 이렇게 배치될 수 없다면 이미지 평면을 변형 시켜서 위와 같은 방법으로 계산을 할 수 있는데 이 프로세스를 rectification 이라 한다. 







홈