相机校准(cv.calibrateCamera和cv.InitUndistortMap) [英] Camera Calibration (cv.calibrateCamera and cv.InitUndistortMap)

查看:203
本文介绍了相机校准(cv.calibrateCamera和cv.InitUndistortMap)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cv.InitUndistortMap opencv函数不起作用. 我获得了固有矩阵和失真矩阵,但是无法使用此值来修复图像. 我收到此错误:
cv.InitUndistortMap(camera_matrix,distortion_coefs,mapx,mapy) TypeError:参数'cameraMatrix'必须是CvMat.使用fromarray()将numpy数组转换为CvMat

The cv.InitUndistortMap opencv function is not working. I get the intrinsic and distortion matrix, but I can not use this values to fix my image. I get this error:
cv.InitUndistortMap(camera_matrix,distortion_coefs,mapx,mapy) TypeError: Argument 'cameraMatrix' must be CvMat. Use fromarray() to convert numpy arrays to CvMat

import numpy as np
import cv2
import cv
import os
import sys, getopt
from glob import glob

def calibracion():

  args, img_mask = getopt.getopt(sys.argv[1:], '', ['save=', 'debug=', 'square_size='])
  args = dict(args)
  img_mask = 'left*.jpg'
  img_names = glob(img_mask)
  debug_dir = args.get('--debug')
  square_size = float(args.get('--square_size', 1.0))

  pattern_size = (9, 6)
  pattern_points = np.zeros( (np.prod(pattern_size), 3), np.float32 )
  pattern_points[:,:2] = np.indices(pattern_size).T.reshape(-1, 2)
  pattern_points *= square_size

  obj_points = []
  img_points = []
  h, w = 0, 0
  for fn in img_names:
      print 'processing %s...' % fn,
      img = cv2.imread(fn, 0)
      h, w = img.shape[:2]
      found, corners = cv2.findChessboardCorners(img, pattern_size)
      if found:
      term = ( cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1 )
      cv2.cornerSubPix(img, corners, (5, 5), (-1, -1), term)
      if debug_dir:
      vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
      cv2.drawChessboardCorners(vis, pattern_size, corners, found)
      path, name, ext = splitfn(fn)
      cv2.imwrite('%s/%s_chess.bmp' % (debug_dir, name), vis)
      if not found:
      print 'chessboard not found'
      continue
      img_points.append(corners.reshape(-1, 2))
      obj_points.append(pattern_points)

      print 'ok'

  rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, (w, h))
  cv2.destroyAllWindows() 
  return camera_matrix, dist_coefs


if __name__ == '__main__':

  camera_matrix = cv.CreateMat(3, 3, cv.CV_32FC1)
  dist_coefs = cv.CreateMat(4, 1, cv.CV_32FC1)

  camera_matrix , dist_coefs = calibracion()
  print "camera matrix:\n", camera_matrix       # intrinsic
  print "distortion coefficients: ", dist_coefs     # distortion

  image=cv.LoadImage('dog.jpeg')
  mapx = cv.CreateImage( cv.GetSize(image), cv.IPL_DEPTH_8U, 1 )
  mapy = cv.CreateImage( cv.GetSize(image), cv.IPL_DEPTH_8U, 1 )

  cv.InitUndistortMap(camera_matrix,dist_coefs,mapx,mapy)
  t = cv.CloneImage(image)
  cv.Remap( t, image, mapx, mapy )

  cv.ShowImage('FOTO', t )
  cv.WaitKey(0)

推荐答案

我无法运行calibration函数,但是我认为错误是您要在此处替换变量camera_matrix的值:camera_matrix , dist_coefs = calibracion(). camera_matrix必须是CvMat,如下所示:camera_matrix = cv.CreateMat(3, 3, cv.CV_32FC1).如果您注释以下代码行:camera_matrix , dist_coefs = calibracion(),则不会再次显示该错误消息.

I couldn't run the calibration function, but the error, i think, is that you're replacing the value of the variable camera_matrix here: camera_matrix , dist_coefs = calibracion(). camera_matrix needs to be a CvMat like here: camera_matrix = cv.CreateMat(3, 3, cv.CV_32FC1). If you comment this line of code: camera_matrix , dist_coefs = calibracion() you won't have that error message displayed again.

在坚果壳中:修复calibration功能

这篇关于相机校准(cv.calibrateCamera和cv.InitUndistortMap)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆