Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
0dbbb89e
Commit
0dbbb89e
authored
Jun 26, 2011
by
Alexander Mordvintsev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added stereo_match.py sample
parent
65575cef
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
74 additions
and
0 deletions
+74
-0
stereo_match.py
samples/python2/stereo_match.py
+74
-0
No files found.
samples/python2/stereo_match.py
0 → 100644
View file @
0dbbb89e
'''
Simple example of stereo image matching and point cloud generation.
Resulting .ply file cam be easily viewed using MeshLab (http://meshlab.sourceforge.net/)
'''
import
numpy
as
np
import
cv2
,
cv
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
])
with
open
(
fn
,
'w'
)
as
f
:
f
.
write
(
ply_header
%
dict
(
vert_num
=
len
(
verts
)))
np
.
savetxt
(
f
,
verts
,
'
%
f
%
f
%
f
%
d
%
d
%
d'
)
if
__name__
==
'__main__'
:
print
'loading images...'
imgL
=
cv2
.
pyrDown
(
cv2
.
imread
(
'../gpu/aloeL.jpg'
)
)
# downscale images for faster processing
imgR
=
cv2
.
pyrDown
(
cv2
.
imread
(
'../gpu/aloeR.jpg'
)
)
# disparity range is tuned for 'aloe' image pair
window_size
=
3
min_disp
=
16
num_disp
=
112
-
min_disp
stereo
=
cv2
.
StereoSGBM
(
minDisparity
=
min_disp
,
numDisparities
=
num_disp
,
SADWindowSize
=
window_size
,
uniquenessRatio
=
10
,
speckleWindowSize
=
100
,
speckleRange
=
32
,
disp12MaxDiff
=
1
,
P1
=
8
*
3
*
window_size
**
2
,
P2
=
32
*
3
*
window_size
**
2
,
fullDP
=
False
)
print
'computing disparity...'
disp
=
stereo
.
compute
(
imgL
,
imgR
)
.
astype
(
np
.
float32
)
/
16.0
print
'generating 3d point cloud...'
,
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
,
cv
.
CV_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
)
print
'
%
s saved'
%
'out.ply'
cv2
.
imshow
(
'left'
,
imgL
)
cv2
.
imshow
(
'disparity'
,
(
disp
-
min_disp
)
/
num_disp
)
cv2
.
waitKey
()
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment