2015-01-17 4 views
1

이것은 opencv python 문서의 카메라 보정 코드입니다. objp [:, : 2] = np.mgrid [0 : 7,0 : 6] .T.reshape (-1,2)가 어떻게 작동하는지 알고 싶습니다. 재구성 (-1,2)은 무엇을합니까? 이 코드 줄에서 값을 변경하려고 시도했지만 오류가 발생했습니다. 누군가가 어떻게 작동하는지, 왜이 숫자 만 작동하는지 설명 할 수 있습니까?Opencv python 카메라 보정 : objp 매트릭스

import numpy as np 
import cv2 
import glob 

# termination criteria 
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001) 

# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0) 
objp = np.zeros((6*7,3), np.float32) 
print "objp: ",objp 
objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2) 

print "objp: ",objp 
# 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('left*.jpg') 

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

    # Find the chess board corners 
    ret, corners = cv2.findChessboardCorners(gray, (7,6),None) 

    # If found, add object points, image points (after refining them) 
    if ret == True: 
     objpoints.append(objp) 

     cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria) 
     imgpoints.append(corners) 

     # Draw and display the corners 
     cv2.drawChessboardCorners(img, (7,6), corners,ret) 
     cv2.imshow('img',img) 
     cv2.waitKey(500) 

cv2.destroyAllWindows() 

ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None) # camera matrix, distortion coefficients, rotation and translation vectors 

또한, objpoints는 3D 실제 좌표입니다. 이것은 수동으로 올바르게 측정해야합니까? 왜 (0,0,0), (1,0,0), (2,0,0) ...., (6,5,0) ?? 감사합니다. . 어떤 도움을 주시면 감사하겠습니다.

답변

2

오류 코드와 예외 사항을 게시하여 해결하도록 도와주십시오. 다음과 같은 코드를 분할 할 수 있습니다

np.mgrid[0:7,0:6].T.reshape(-1,2) 

:

-1 총 요소 번호에서 실제 길이를 계산 의미

a = np.mgrid[0:7, 0:6] 
b = a.T 
c = b.reshape(-1, 2) 

print a.shape, b.shape, c.shape 

출력은 다음과 같습니다

(2, 7, 6) (6, 7, 2) (42, 2) 

코드를 이해하는 것이 어려울 경우

과 같습니다.
x, y = np.mgrid[0:7, 0:6] 
np.c_[x.ravel(), y.ravel()] 

대상은 3D 실제 좌표이지만 길이 단위는 임의적이므로 모든 상자의 가장자리 길이가 같으면 수동으로 측정 할 필요가 없습니다. 모서리의 길이가 16cm이면 objp은 16cm를 의미합니다.

+0

감사합니다. 그게 도움이 됐어! – Clive

관련 문제