mirror of https://github.com/opencv/opencv.git
Open Source Computer Vision Library
https://opencv.org/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.2 KiB
79 lines
2.2 KiB
12 years ago
|
#!/usr/bin/env python
|
||
12 years ago
|
|
||
12 years ago
|
'''
|
||
|
Simple example of stereo image matching and point cloud generation.
|
||
|
|
||
|
Resulting .ply file cam be easily viewed using MeshLab ( http://meshlab.sourceforge.net/ )
|
||
|
'''
|
||
|
|
||
9 years ago
|
# Python 2/3 compatibility
|
||
|
from __future__ import print_function
|
||
|
|
||
12 years ago
|
import numpy as np
|
||
|
import cv2
|
||
|
|
||
|
ply_header = '''ply
|
||
|
format ascii 1.0
|
||
|
element vertex %(vert_num)d
|
||
|
property float x
|
||
|
property float y
|
||
|
property float z
|
||
|
property uchar red
|
||
|
property uchar green
|
||
|
property uchar blue
|
||
|
end_header
|
||
|
'''
|
||
|
|
||
|
def write_ply(fn, verts, colors):
|
||
|
verts = verts.reshape(-1, 3)
|
||
|
colors = colors.reshape(-1, 3)
|
||
|
verts = np.hstack([verts, colors])
|
||
9 years ago
|
with open(fn, 'wb') as f:
|
||
|
f.write((ply_header % dict(vert_num=len(verts))).encode('utf-8'))
|
||
|
np.savetxt(f, verts, fmt='%f %f %f %d %d %d ')
|
||
12 years ago
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
9 years ago
|
print('loading images...')
|
||
10 years ago
|
imgL = cv2.pyrDown( cv2.imread('../data/aloeL.jpg') ) # downscale images for faster processing
|
||
|
imgR = cv2.pyrDown( cv2.imread('../data/aloeR.jpg') )
|
||
12 years ago
|
|
||
|
# disparity range is tuned for 'aloe' image pair
|
||
|
window_size = 3
|
||
|
min_disp = 16
|
||
|
num_disp = 112-min_disp
|
||
10 years ago
|
stereo = cv2.StereoSGBM_create(minDisparity = min_disp,
|
||
12 years ago
|
numDisparities = num_disp,
|
||
10 years ago
|
blockSize = 16,
|
||
12 years ago
|
P1 = 8*3*window_size**2,
|
||
|
P2 = 32*3*window_size**2,
|
||
10 years ago
|
disp12MaxDiff = 1,
|
||
|
uniquenessRatio = 10,
|
||
|
speckleWindowSize = 100,
|
||
|
speckleRange = 32
|
||
12 years ago
|
)
|
||
|
|
||
9 years ago
|
print('computing disparity...')
|
||
12 years ago
|
disp = stereo.compute(imgL, imgR).astype(np.float32) / 16.0
|
||
|
|
||
9 years ago
|
print('generating 3d point cloud...',)
|
||
12 years ago
|
h, w = imgL.shape[:2]
|
||
|
f = 0.8*w # guess for focal length
|
||
|
Q = np.float32([[1, 0, 0, -0.5*w],
|
||
|
[0,-1, 0, 0.5*h], # turn points 180 deg around x-axis,
|
||
|
[0, 0, 0, -f], # so that y-axis looks up
|
||
|
[0, 0, 1, 0]])
|
||
|
points = cv2.reprojectImageTo3D(disp, Q)
|
||
|
colors = cv2.cvtColor(imgL, cv2.COLOR_BGR2RGB)
|
||
|
mask = disp > disp.min()
|
||
|
out_points = points[mask]
|
||
|
out_colors = colors[mask]
|
||
|
out_fn = 'out.ply'
|
||
|
write_ply('out.ply', out_points, out_colors)
|
||
9 years ago
|
print('%s saved' % 'out.ply')
|
||
12 years ago
|
|
||
|
cv2.imshow('left', imgL)
|
||
|
cv2.imshow('disparity', (disp-min_disp)/num_disp)
|
||
|
cv2.waitKey()
|
||
|
cv2.destroyAllWindows()
|