使用两个点从单个相机进行图像校正

时间:2017-01-18 18:08:19

标签: python opencv camera homography perspectivecamera

我正在尝试实施图像校正。我正在使用一种不再可用的软件。为了校正图像,软件使用摄像机的高度(h),摄像机两点(d1,d2)的距离以及图像中的对应线到参考点(Line1,Line2)。 所以变量是:

  • h(摄像机高程);
  • 第1行,第2行(行像素)
  • d1,d2(距离相机的距离,以米为单位)

配置:

我尝试使用OpenCV(Python)实现少量代码,但最终结果与软件不同。我编写了一个代码来校准相机,第二个代码是为了使图像失真,然后我想应用整改。 问题是我正在使用固定焦距和焦距固定的单个相机(拍摄风景照片),我不能再改变了。 有人能告诉我使用相同的软件方式或其他有效的解决方案来执行整改的好方法吗?

我的校准代码是

# Numbers of corners
n_w = 9
n_h = 6
patternSize = (n_w, n_h)
# SIZE OF THE WINDOW TO IMPROVE THE COORDINATES OF CORNERS
windowSize = (11, 11)
# TERMINATION CRITERIA
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
objp = np.zeros((n_h * n_w, 3), dtype=np.float32)
objp[:, :2] = np.mgrid[0:n_w, 0:n_h].T.reshape(-1, 2)
# LIST OF POINT
objpoints = []
imgpoints = []
# GET ALL IMAGES
images = glob.glob('*.jpg')

for fname in images:
img = cv2.imread(fname)

# IMAGE ON GRAY SACLE
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# fIND CORNERS
retval, corners = cv2.findChessboardCorners(gray_img, patternSize, None)

if retval == True:
    print 'Looping through image %s' % fname
    objpoints.append(objp)
    cv2.cornerSubPix(gray_img, corners, windowSize, (-1, -1), criteria)
    imgpoints.append(corners)
    cv2.drawChessboardCorners(img, patternSize, corners, retval)
    cv2.imshow('ChessBoard Image %s' % fname, img)
    cv2.waitKey(500)
    cv2.destroyAllWindows()

    print "------START CALIBRATION....."
    ret, cameraMatrix, distCoeffs, revcs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray_img.shape[::-1],
                                                                      None, None)
    print ret
    print cameraMatrix
    print distCoeffs
    print '---SAVING CALIBRATION DATA'
    np.savez('calibration_data', RMS=ret, distCoeffs=distCoeffs, cameraMatrix=cameraMatrix)
    if ret <= 1.0:
        print '''-----GOOD CALIBRATION'''

删除失真的代码是:

 # LOAD CALIBRATION DATA
 calibrationData = np.load('calibration_data.npz')

 distCoeffs = calibrationData['distCoeffs']
 cameraMatrix = calibrationData['cameraMatrix']

 calibrationData.close()

 # LOAD IMAGES
 images = glob.glob('/*.jpg')

 for i, fname in enumerate(images):
     img = cv2.imread(fname)
     # UNDISTORT
     undistorted_img = cv2.undistort(img, cameraMatrix, distCoeffs, None)
     # SAVE IMAGE
     cv2.imwrite(os.path.join(dirname, 'Undistorted_%05d.jpg' % i), undistorted_img)
     cv2.imshow('Undistorted Image %s' % fname, undistorted_img)

纠正图像的第一个想法是找到梯形( A4纸)的真实世界图像中的4个角,并计算给定4个矩形点的变换矩阵(实际尺寸为 A4 )。但我认为这是一个错误的问题。 为此,我写了这段代码:

#load image
img_Trap = cv2.imread('image.png', cv2.IMREAD_GRAYSCALE)
#points on the image (corners of an A4 paper)
ptsTrap = np.array(((1556, 1050), (1556, 1050), (2189, 1677), (1425, 1723)), dtype=np.float32)

img_Rect = cv2.imread('image2.png', cv2.IMREAD_GRAYSCALE)
# corner of a A4 (saving the aspect ratio)
ptsRect = np.array(((1980, 1381), (2189, 1381), (2189, 1677), (1980, 1677)), dtype=np.float32)
#transformation matrix
T = cv2.getPerspectiveTransform(ptsTrap, ptsRect)
print T
# warp perspective
warp = cv2.warpPerspective(img_Trap, T, img_Rect.shape[:2])
cv2.imwrite('warpimage.png', warp)

0 个答案:

没有答案