3d reconstruction python and opencv

asked 2014-04-25 07:29:44 -0600

La Rosa gravatar image

updated 2014-04-25 07:46:06 -0600

hi guys i have been working on a small program in python using the opencv lib and tow webcams so that i can measure the distance between this last tow and the object right in front of them(using the disparity map) ,so when i run the program at the end i normally i get the result in a matrix but what i get is not only one number(which is supposed to be the distance) but many different numbers,besides i always get one number which doesn't change even if i change the view can any body tell me why?!

here is the code:

import numpy as np
import cv2 as cv
import cv2.cv as cv1
from VideoCapture import Device
import os


def caliLeftCam():    
args, img_mask = getopt.getopt(sys.argv[1:], '', ['save=', 'debug=', 'square_size='])
args = dict(args)
try: img_mask = img_mask[0]
except: img_mask = '../cpp/img*.jpg'
img_names = glob(img_mask)
debug_dir = args.get('--debug')
square_size = float(args.get('--square_size', 1.0))

pattern_size = (7, 5)
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_pointsL = []
h, w = 0, 0
for fn in img_names:
    print "processing %s..." % fn,
    imgL = cv.imread(fn, 0)
    h, w = imgL.shape[:2]
    found, corners = cv.findChessboardCorners(imgL, pattern_size)
    if found:
        term = ( cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_COUNT, 30, 0.1 )
        cv.cornerSubPix(imgL, corners, (5, 5), (-1, -1), term)
    if debug_dir:
        vis = cv.cvtColor(imgL, cv.COLOR_GRAY2BGR)
        cv.drawChessboardCorners(vis, pattern_size, corners, found)
        path, name, ext = splitfn(fn)
        cv.imwrite('%s/%s_chess.bmp' % (debug_dir, name), vis)
    if not found:
        print "chessboard not found"
        continue
    img_pointsL.append(corners.reshape(-1, 2))
    obj_points.append(pattern_points)

    print 'ok'

rmsL, cameraL_matrix, dist_coefsL, rvecsL, tvecsL = cv.calibrateCamera(obj_points, img_pointsL, (w, h))
print "RMSL:", rmsL
print "Left camera matrix:\n", cameraL_matrix
print "distortion coefficients: ", dist_coefsL.ravel()

newcameramtxL, roi=cv.getOptimalNewCameraMatrix(cameraL_matrix,dist_coefsL,(w,h),1,(w,h))
#undistort
mapxL,mapyL = cv.initUndistortRectifyMap(cameraL_matrix,dist_coefsL,None,newcameramtxL,(w,h),5)
dstL = cv.remap(imgL,mapxL,mapyL,cv.INTER_LINEAR)
return img_pointsL, cameraL_matrix, dist_coefsL
def caliRightCam():

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

pattern_size = (7, 5)
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_pointsR = []
h, w = 0, 0
for fn in img_names:
    print "processing %s..." % fn,
    imgR = cv.imread(fn, 0)
    h, w = imgR.shape[:2]
    found, corners = cv.findChessboardCorners(imgR, pattern_size)
    if found:
        term = ( cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_COUNT, 30, 0.1 )
        cv.cornerSubPix(imgR, corners, (5, 5), (-1, -1), term)
    if debug_dir:
        vis = cv.cvtColor(img, cv2.COLOR_GRAY2BGR)
        cv.drawChessboardCorners(vis, pattern_size, corners, found)
        path, name, ext = splitfn(fn)
        cv.imwrite('%s/%s_chess.bmp' % (debug_dir, name), vis)

    if not found:
        print "chessboard not found"
        continue
    img_pointsR.append(corners.reshape(-1, 2))
    obj_points.append(pattern_points)

    print 'ok'

rmsR, cameraR_matrix, dist_coefsR ...
(more)
edit retag flag offensive close merge delete