0

cv.InitUndistortMap opencv 関数が機能していません。固有行列と歪み行列を取得しましたが、この値を使用して画像を修正することはできません。次のエラーが表示されます:
cv.InitUndistortMap(camera_matrix,distortion_coefs,mapx,mapy) TypeError: Argument 'cameraMatrix' must be CvMat. fromarray() を使用して、numpy 配列を 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)
4

1 に答える 1

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()を付けると、そのエラーメッセージが再度表示されることはありません。

一言で言えば:calibration関数を修正する

于 2012-11-20T21:00:41.420 に答える