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
f2a3e7e3
Commit
f2a3e7e3
authored
Nov 28, 2010
by
Vadim Pisarevsky
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
converted some more samples to C++
parent
4c160acc
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
432 additions
and
578 deletions
+432
-578
3calibration.cpp
samples/cpp/3calibration.cpp
+0
-16
CMakeLists.txt
samples/cpp/CMakeLists.txt
+0
-0
calibration_artificial.cpp
samples/cpp/calibration_artificial.cpp
+0
-0
camshiftdemo.cpp
samples/cpp/camshiftdemo.cpp
+93
-132
fitellipse.cpp
samples/cpp/fitellipse.cpp
+0
-0
image.cpp
samples/cpp/image.cpp
+0
-0
inpaint.cpp
samples/cpp/inpaint.cpp
+40
-42
lkdemo.cpp
samples/cpp/lkdemo.cpp
+73
-116
stereo_calib.cpp
samples/cpp/stereo_calib.cpp
+226
-272
No files found.
samples/cpp/3calibration.cpp
View file @
f2a3e7e3
...
@@ -17,7 +17,6 @@ enum { DETECTION = 0, CAPTURING = 1, CALIBRATED = 2 };
...
@@ -17,7 +17,6 @@ enum { DETECTION = 0, CAPTURING = 1, CALIBRATED = 2 };
void
help
()
void
help
()
{
{
printf
(
"This is a camera calibration sample that calibrates 3 horizontally placed cameras together.
\n
"
printf
(
"This is a camera calibration sample that calibrates 3 horizontally placed cameras together.
\n
"
"Usage: 3calibration
\n
"
"Usage: 3calibration
\n
"
" -w <board_width> # the number of inner corners per one of board dimension
\n
"
" -w <board_width> # the number of inner corners per one of board dimension
\n
"
...
@@ -32,20 +31,6 @@ void help()
...
@@ -32,20 +31,6 @@ void help()
}
}
static
void
calcChessboardCorners
(
Size
boardSize
,
float
squareSize
,
vector
<
Point3f
>&
corners
)
static
void
calcChessboardCorners
(
Size
boardSize
,
float
squareSize
,
vector
<
Point3f
>&
corners
)
{
{
corners
.
resize
(
0
);
corners
.
resize
(
0
);
...
@@ -56,7 +41,6 @@ static void calcChessboardCorners(Size boardSize, float squareSize, vector<Point
...
@@ -56,7 +41,6 @@ static void calcChessboardCorners(Size boardSize, float squareSize, vector<Point
float
(
i
*
squareSize
),
0
));
float
(
i
*
squareSize
),
0
));
}
}
static
bool
run3Calibration
(
vector
<
vector
<
Point2f
>
>
imagePoints1
,
static
bool
run3Calibration
(
vector
<
vector
<
Point2f
>
>
imagePoints1
,
vector
<
vector
<
Point2f
>
>
imagePoints2
,
vector
<
vector
<
Point2f
>
>
imagePoints2
,
vector
<
vector
<
Point2f
>
>
imagePoints3
,
vector
<
vector
<
Point2f
>
>
imagePoints3
,
...
...
samples/cpp/CMakeLists.txt
View file @
f2a3e7e3
samples/c/calibration_artificial.cpp
→
samples/c
pp
/calibration_artificial.cpp
View file @
f2a3e7e3
samples/cpp/camshiftdemo.cpp
View file @
f2a3e7e3
#include <opencv2/video/tracking.hpp>
#include <opencv2/video/tracking.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <iostream>
#include <ctype.h>
#include <ctype.h>
IplImage
*
image
=
0
,
*
hsv
=
0
,
*
hue
=
0
,
*
mask
=
0
,
*
backproject
=
0
,
*
histimg
=
0
;
using
namespace
cv
;
CvHistogram
*
hist
=
0
;
using
namespace
std
;
int
backproject_mode
=
0
;
Mat
image
;
int
select_object
=
0
;
int
track_object
=
0
;
bool
backprojMode
=
false
;
int
show_hist
=
1
;
bool
selectObject
=
false
;
CvPoint
origin
;
int
trackObject
=
0
;
CvRect
selection
;
bool
showHist
=
true
;
CvRect
track_window
;
Point
origin
;
CvBox2D
track_box
;
Rect
selection
;
CvConnectedComp
track_comp
;
int
hdims
=
16
;
float
hranges_arr
[]
=
{
0
,
180
};
float
*
hranges
=
hranges_arr
;
int
vmin
=
10
,
vmax
=
256
,
smin
=
30
;
int
vmin
=
10
,
vmax
=
256
,
smin
=
30
;
void
onMouse
(
int
event
,
int
x
,
int
y
,
int
flags
,
void
*
param
)
void
onMouse
(
int
event
,
int
x
,
int
y
,
int
flags
,
void
*
param
)
...
@@ -27,15 +24,10 @@ void onMouse( int event, int x, int y, int flags, void* param )
...
@@ -27,15 +24,10 @@ void onMouse( int event, int x, int y, int flags, void* param )
{
{
selection
.
x
=
MIN
(
x
,
origin
.
x
);
selection
.
x
=
MIN
(
x
,
origin
.
x
);
selection
.
y
=
MIN
(
y
,
origin
.
y
);
selection
.
y
=
MIN
(
y
,
origin
.
y
);
selection
.
width
=
selection
.
x
+
std
::
abs
(
x
-
origin
.
x
);
selection
.
width
=
std
::
abs
(
x
-
origin
.
x
);
selection
.
height
=
selection
.
y
+
std
::
abs
(
y
-
origin
.
y
);
selection
.
height
=
std
::
abs
(
y
-
origin
.
y
);
selection
.
x
=
MAX
(
selection
.
x
,
0
);
selection
&=
Rect
(
0
,
0
,
image
.
cols
,
image
.
rows
);
selection
.
y
=
MAX
(
selection
.
y
,
0
);
selection
.
width
=
MIN
(
selection
.
width
,
image
.
cols
);
selection
.
height
=
MIN
(
selection
.
height
,
image
.
rows
);
selection
.
width
-=
selection
.
x
;
selection
.
height
-=
selection
.
y
;
}
}
switch
(
event
)
switch
(
event
)
...
@@ -53,162 +45,131 @@ void onMouse( int event, int x, int y, int flags, void* param )
...
@@ -53,162 +45,131 @@ void onMouse( int event, int x, int y, int flags, void* param )
}
}
}
}
Scalar
hsv2rgb
(
float
hue
)
{
int
rgb
[
3
],
p
,
sector
;
static
const
int
sectorData
[][
3
]
=
{{
0
,
2
,
1
},
{
1
,
2
,
0
},
{
1
,
0
,
2
},
{
2
,
0
,
1
},
{
2
,
1
,
0
},
{
0
,
1
,
2
}};
hue
*=
0.033333333333333333333333333333333
f
;
sector
=
cvFloor
(
hue
);
p
=
cvRound
(
255
*
(
hue
-
sector
));
p
^=
sector
&
1
?
255
:
0
;
rgb
[
sector_data
[
sector
][
0
]]
=
255
;
rgb
[
sector_data
[
sector
][
1
]]
=
0
;
rgb
[
sector_data
[
sector
][
2
]]
=
p
;
return
cvScalar
(
rgb
[
2
],
rgb
[
1
],
rgb
[
0
],
0
);
}
int
main
(
int
argc
,
char
**
argv
)
int
main
(
int
argc
,
char
**
argv
)
{
{
CvCapture
*
capture
=
0
;
VideoCapture
cap
;
Rect
trackWindow
;
RotatedRect
trackBox
;
CvConnectedComp
trackComp
;
int
hsize
=
16
;
float
hranges
[]
=
{
0
,
180
};
const
float
*
phranges
=
hranges
;
if
(
argc
==
1
||
(
argc
==
2
&&
strlen
(
argv
[
1
])
==
1
&&
isdigit
(
argv
[
1
][
0
])))
if
(
argc
==
1
||
(
argc
==
2
&&
strlen
(
argv
[
1
])
==
1
&&
isdigit
(
argv
[
1
][
0
])))
cap
ture
=
cvCaptureFromCAM
(
argc
==
2
?
argv
[
1
][
0
]
-
'0'
:
0
);
cap
.
open
(
argc
==
2
?
argv
[
1
][
0
]
-
'0'
:
0
);
else
if
(
argc
==
2
)
else
if
(
argc
==
2
)
cap
ture
=
cvCaptureFromAVI
(
argv
[
1
]
);
cap
.
open
(
argv
[
1
]
);
if
(
!
cap
ture
)
if
(
!
cap
.
isOpened
()
)
{
{
fprintf
(
stderr
,
"Could not initialize capturing...
\n
"
)
;
cout
<<
"Could not initialize capturing...
\n
"
;
return
-
1
;
return
0
;
}
}
printf
(
"Hot keys:
\n
"
cout
<<
"Hot keys:
\n
"
"
\t
ESC - quit the program
\n
"
"
\t
ESC - quit the program
\n
"
"
\t
c - stop the tracking
\n
"
"
\t
c - stop the tracking
\n
"
"
\t
b - switch to/from backprojection view
\n
"
"
\t
b - switch to/from backprojection view
\n
"
"
\t
h - show/hide object histogram
\n
"
"
\t
h - show/hide object histogram
\n
"
"To initialize tracking, select the object with mouse
\n
"
);
"To initialize tracking, select the object with mouse
\n
"
;
namedWindow
(
"Histogram"
,
1
);
namedWindow
(
"CamShift Demo"
,
1
);
setMouseCallback
(
"CamShift Demo"
,
onMouse
,
0
);
createTrackbar
(
"Vmin"
,
"CamShift Demo"
,
&
vmin
,
256
,
0
);
createTrackbar
(
"Vmax"
,
"CamShift Demo"
,
&
vmax
,
256
,
0
);
createTrackbar
(
"Smin"
,
"CamShift Demo"
,
&
smin
,
256
,
0
);
cvNamedWindow
(
"Histogram"
,
1
);
Mat
hsv
,
hue
,
mask
,
hist
,
histimg
=
Mat
::
zeros
(
200
,
320
,
CV_8UC3
),
backproj
;
cvNamedWindow
(
"CamShiftDemo"
,
1
);
cvSetMouseCallback
(
"CamShiftDemo"
,
on_mouse
,
0
);
cvCreateTrackbar
(
"Vmin"
,
"CamShiftDemo"
,
&
vmin
,
256
,
0
);
cvCreateTrackbar
(
"Vmax"
,
"CamShiftDemo"
,
&
vmax
,
256
,
0
);
cvCreateTrackbar
(
"Smin"
,
"CamShiftDemo"
,
&
smin
,
256
,
0
);
for
(;;)
for
(;;)
{
{
IplImage
*
frame
=
0
;
Mat
frame
;
int
i
,
bin_w
,
c
;
cap
>>
frame
;
if
(
frame
.
empty
()
)
frame
=
cvQueryFrame
(
capture
);
if
(
!
frame
)
break
;
break
;
if
(
!
image
)
frame
.
copyTo
(
image
);
{
cvtColor
(
image
,
hsv
,
CV_BGR2HSV
);
/* allocate all the buffers */
image
=
cvCreateImage
(
cvGetSize
(
frame
),
8
,
3
);
image
->
origin
=
frame
->
origin
;
hsv
=
cvCreateImage
(
cvGetSize
(
frame
),
8
,
3
);
hue
=
cvCreateImage
(
cvGetSize
(
frame
),
8
,
1
);
mask
=
cvCreateImage
(
cvGetSize
(
frame
),
8
,
1
);
backproject
=
cvCreateImage
(
cvGetSize
(
frame
),
8
,
1
);
hist
=
cvCreateHist
(
1
,
&
hdims
,
CV_HIST_ARRAY
,
&
hranges
,
1
);
histimg
=
cvCreateImage
(
cvSize
(
320
,
200
),
8
,
3
);
cvZero
(
histimg
);
}
cvCopy
(
frame
,
image
,
0
);
if
(
trackObject
)
cvCvtColor
(
image
,
hsv
,
CV_BGR2HSV
);
if
(
track_object
)
{
{
int
_vmin
=
vmin
,
_vmax
=
vmax
;
int
_vmin
=
vmin
,
_vmax
=
vmax
;
cvInRangeS
(
hsv
,
cvScalar
(
0
,
smin
,
MIN
(
_vmin
,
_vmax
),
0
),
inRange
(
hsv
,
Scalar
(
0
,
smin
,
MIN
(
_vmin
,
_vmax
)),
cvScalar
(
180
,
256
,
MAX
(
_vmin
,
_vmax
),
0
),
mask
);
Scalar
(
180
,
256
,
MAX
(
_vmin
,
_vmax
)),
mask
);
cvSplit
(
hsv
,
hue
,
0
,
0
,
0
);
int
ch
[]
=
{
0
,
0
};
hue
.
create
(
hsv
.
size
(),
hsv
.
depth
());
mixChannels
(
&
hsv
,
1
,
&
hue
,
1
,
ch
,
1
);
if
(
track
_o
bject
<
0
)
if
(
track
O
bject
<
0
)
{
{
float
max_val
=
0.
f
;
Mat
roi
(
hue
,
selection
),
maskroi
(
mask
,
selection
);
cvSetImageROI
(
hue
,
selection
);
calcHist
(
&
roi
,
1
,
0
,
maskroi
,
hist
,
1
,
&
hsize
,
&
phranges
);
cvSetImageROI
(
mask
,
selection
);
normalize
(
hist
,
hist
,
0
,
255
,
CV_MINMAX
);
cvCalcHist
(
&
hue
,
hist
,
0
,
mask
);
cvGetMinMaxHistValue
(
hist
,
0
,
&
max_val
,
0
,
0
);
trackWindow
=
selection
;
cvConvertScale
(
hist
->
bins
,
hist
->
bins
,
max_val
?
255.
/
max_val
:
0.
,
0
);
trackObject
=
1
;
cvResetImageROI
(
hue
);
cvResetImageROI
(
mask
);
histimg
=
Scalar
::
all
(
0
);
track_window
=
selection
;
int
binW
=
histimg
.
cols
/
hsize
;
track_object
=
1
;
Mat
buf
(
1
,
hsize
,
CV_8UC3
);
for
(
int
i
=
0
;
i
<
hsize
;
i
++
)
cvZero
(
histimg
);
buf
.
at
<
Vec3b
>
(
i
)
=
Vec3b
(
saturate_cast
<
uchar
>
(
i
*
180.
/
hsize
),
255
,
255
);
bin_w
=
histimg
->
width
/
hdims
;
cvtColor
(
buf
,
buf
,
CV_HSV2BGR
);
for
(
i
=
0
;
i
<
hdims
;
i
++
)
for
(
int
i
=
0
;
i
<
hsize
;
i
++
)
{
{
int
val
=
cvRound
(
cvGetReal1D
(
hist
->
bins
,
i
)
*
histimg
->
height
/
255
);
int
val
=
saturate_cast
<
int
>
(
hist
.
at
<
float
>
(
i
)
*
histimg
.
rows
/
255
);
CvScalar
color
=
hsv2rgb
(
i
*
180.
f
/
hdims
);
rectangle
(
histimg
,
Point
(
i
*
binW
,
histimg
.
rows
),
cvRectangle
(
histimg
,
cvPoint
(
i
*
bin_w
,
histimg
->
height
),
Point
((
i
+
1
)
*
binW
,
histimg
.
rows
-
val
),
cvPoint
((
i
+
1
)
*
bin_w
,
histimg
->
height
-
val
),
Scalar
(
buf
.
at
<
Vec3b
>
(
i
)),
-
1
,
8
);
color
,
-
1
,
8
,
0
);
}
}
}
}
cvCalcBackProject
(
&
hue
,
backproject
,
hist
);
calcBackProject
(
&
hue
,
1
,
0
,
hist
,
backproj
,
&
phranges
);
cvAnd
(
backproject
,
mask
,
backproject
,
0
);
backproj
&=
mask
;
cvCamShift
(
backproject
,
track_window
,
RotatedRect
trackBox
=
CamShift
(
backproj
,
trackWindow
,
cvTermCriteria
(
CV_TERMCRIT_EPS
|
CV_TERMCRIT_ITER
,
10
,
1
),
TermCriteria
(
CV_TERMCRIT_EPS
|
CV_TERMCRIT_ITER
,
10
,
1
));
&
track_comp
,
&
track_box
);
trackBox
.
angle
=
90
-
trackBox
.
angle
;
track_window
=
track_comp
.
rect
;
if
(
backprojMode
)
if
(
backproject_mode
)
cvtColor
(
backproj
,
image
,
CV_GRAY2BGR
);
cvCvtColor
(
backproject
,
image
,
CV_GRAY2BGR
);
ellipse
(
image
,
trackBox
,
Scalar
(
0
,
0
,
255
),
3
,
CV_AA
);
if
(
!
image
->
origin
)
track_box
.
angle
=
-
track_box
.
angle
;
cvEllipseBox
(
image
,
track_box
,
CV_RGB
(
255
,
0
,
0
),
3
,
CV_AA
,
0
);
}
}
if
(
select
_o
bject
&&
selection
.
width
>
0
&&
selection
.
height
>
0
)
if
(
select
O
bject
&&
selection
.
width
>
0
&&
selection
.
height
>
0
)
{
{
cvSetImageROI
(
image
,
selection
);
Mat
roi
(
image
,
selection
);
cvXorS
(
image
,
cvScalarAll
(
255
),
image
,
0
);
bitwise_not
(
roi
,
roi
);
cvResetImageROI
(
image
);
}
}
cvShowImage
(
"CamShift
Demo"
,
image
);
imshow
(
"CamShift
Demo"
,
image
);
cvShowImage
(
"Histogram"
,
histimg
);
imshow
(
"Histogram"
,
histimg
);
c
=
cvW
aitKey
(
10
);
c
har
c
=
(
char
)
w
aitKey
(
10
);
if
(
(
char
)
c
==
27
)
if
(
c
==
27
)
break
;
break
;
switch
(
(
char
)
c
)
switch
(
c
)
{
{
case
'b'
:
case
'b'
:
backproj
ect_mode
^=
1
;
backproj
Mode
=
!
backprojMode
;
break
;
break
;
case
'c'
:
case
'c'
:
track
_o
bject
=
0
;
track
O
bject
=
0
;
cvZero
(
histimg
);
histimg
=
Scalar
::
all
(
0
);
break
;
break
;
case
'h'
:
case
'h'
:
show
_hist
^=
1
;
show
Hist
=
!
showHist
;
if
(
!
show
_h
ist
)
if
(
!
show
H
ist
)
cvD
estroyWindow
(
"Histogram"
);
d
estroyWindow
(
"Histogram"
);
else
else
cvN
amedWindow
(
"Histogram"
,
1
);
n
amedWindow
(
"Histogram"
,
1
);
break
;
break
;
default:
default:
;
;
}
}
}
}
cvReleaseCapture
(
&
capture
);
cvDestroyWindow
(
"CamShiftDemo"
);
return
0
;
return
0
;
}
}
samples/c/fitellipse.cpp
→
samples/c
pp
/fitellipse.cpp
View file @
f2a3e7e3
File moved
samples/c/image.cpp
→
samples/c
pp
/image.cpp
View file @
f2a3e7e3
File moved
samples/c/inpaint.cpp
→
samples/c
pp
/inpaint.cpp
View file @
f2a3e7e3
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc
_c.h
"
#include "opencv2/imgproc/imgproc
.hpp
"
#include <
stdio.h
>
#include <
iostream
>
IplImage
*
inpaint_mask
=
0
;
using
namespace
cv
;
IplImage
*
img0
=
0
,
*
img
=
0
,
*
inpainted
=
0
;
using
namespace
std
;
CvPoint
prev_pt
=
{
-
1
,
-
1
};
void
on_mouse
(
int
event
,
int
x
,
int
y
,
int
flags
,
void
*
)
Mat
img
,
inpaintMask
;
{
Point
prevPt
(
-
1
,
-
1
);
if
(
!
img
)
return
;
void
onMouse
(
int
event
,
int
x
,
int
y
,
int
flags
,
void
*
)
{
if
(
event
==
CV_EVENT_LBUTTONUP
||
!
(
flags
&
CV_EVENT_FLAG_LBUTTON
)
)
if
(
event
==
CV_EVENT_LBUTTONUP
||
!
(
flags
&
CV_EVENT_FLAG_LBUTTON
)
)
prev
_pt
=
cv
Point
(
-
1
,
-
1
);
prev
Pt
=
Point
(
-
1
,
-
1
);
else
if
(
event
==
CV_EVENT_LBUTTONDOWN
)
else
if
(
event
==
CV_EVENT_LBUTTONDOWN
)
prev
_pt
=
cv
Point
(
x
,
y
);
prev
Pt
=
Point
(
x
,
y
);
else
if
(
event
==
CV_EVENT_MOUSEMOVE
&&
(
flags
&
CV_EVENT_FLAG_LBUTTON
)
)
else
if
(
event
==
CV_EVENT_MOUSEMOVE
&&
(
flags
&
CV_EVENT_FLAG_LBUTTON
)
)
{
{
CvPoint
pt
=
cvPoin
t
(
x
,
y
);
Point
p
t
(
x
,
y
);
if
(
prev
_p
t
.
x
<
0
)
if
(
prev
P
t
.
x
<
0
)
prev
_p
t
=
pt
;
prev
P
t
=
pt
;
cvLine
(
inpaint_mask
,
prev_pt
,
pt
,
cvScalarA
ll
(
255
),
5
,
8
,
0
);
line
(
inpaintMask
,
prevPt
,
pt
,
Scalar
::
a
ll
(
255
),
5
,
8
,
0
);
cvLine
(
img
,
prev_pt
,
pt
,
cvScalarA
ll
(
255
),
5
,
8
,
0
);
line
(
img
,
prevPt
,
pt
,
Scalar
::
a
ll
(
255
),
5
,
8
,
0
);
prev
_p
t
=
pt
;
prev
P
t
=
pt
;
cvShowImage
(
"image"
,
img
);
imshow
(
"image"
,
img
);
}
}
}
}
...
@@ -32,49 +31,48 @@ void on_mouse( int event, int x, int y, int flags, void* )
...
@@ -32,49 +31,48 @@ void on_mouse( int event, int x, int y, int flags, void* )
int
main
(
int
argc
,
char
**
argv
)
int
main
(
int
argc
,
char
**
argv
)
{
{
char
*
filename
=
argc
>=
2
?
argv
[
1
]
:
(
char
*
)
"fruits.jpg"
;
char
*
filename
=
argc
>=
2
?
argv
[
1
]
:
(
char
*
)
"fruits.jpg"
;
Mat
img0
=
imread
(
filename
,
-
1
);
if
(
(
img0
=
cvLoadImage
(
filename
,
-
1
))
==
0
)
if
(
img0
.
empty
())
{
cout
<<
"Usage: inpaint <image_name>
\n
"
;
return
0
;
return
0
;
}
printf
(
"Hot keys:
\n
"
cout
<<
"Hot keys:
\n
"
"
\t
ESC - quit the program
\n
"
"
\t
ESC - quit the program
\n
"
"
\t
r - restore the original image
\n
"
"
\t
r - restore the original image
\n
"
"
\t
i or SPACE - run inpainting algorithm
\n
"
"
\t
i or SPACE - run inpainting algorithm
\n
"
"
\t\t
(before running it, paint something on the image)
\n
"
)
;
"
\t\t
(before running it, paint something on the image)
\n
"
;
cvN
amedWindow
(
"image"
,
1
);
n
amedWindow
(
"image"
,
1
);
img
=
cvCloneImage
(
img0
);
img
=
img0
.
clone
();
inpainted
=
cvCloneImage
(
img0
);
inpaintMask
=
Mat
::
zeros
(
img
.
size
(),
CV_8U
);
inpaint_mask
=
cvCreateImage
(
cvGetSize
(
img
),
8
,
1
);
cvZero
(
inpaint_mask
);
imshow
(
"image"
,
img
);
cvZero
(
inpainted
);
setMouseCallback
(
"image"
,
onMouse
,
0
);
cvShowImage
(
"image"
,
img
);
cvShowImage
(
"inpainted image"
,
inpainted
);
cvSetMouseCallback
(
"image"
,
on_mouse
,
0
);
for
(;;)
for
(;;)
{
{
int
c
=
cvWaitKey
(
0
);
char
c
=
(
char
)
waitKey
(
);
if
(
(
char
)
c
==
27
)
if
(
c
==
27
)
break
;
break
;
if
(
(
char
)
c
==
'r'
)
if
(
c
==
'r'
)
{
{
cvZero
(
inpaint_mask
);
inpaintMask
=
Scalar
::
all
(
0
);
cvCopy
(
img0
,
img
);
img0
.
copyTo
(
img
);
cvShowImage
(
"image"
,
img
);
imshow
(
"image"
,
img
);
}
}
if
(
(
char
)
c
==
'i'
||
(
char
)
c
==
' '
)
if
(
c
==
'i'
||
c
==
' '
)
{
{
cvNamedWindow
(
"inpainted image"
,
1
)
;
Mat
inpainted
;
cvInpaint
(
img
,
inpaint_mask
,
inpainted
,
3
,
CV_INPAINT_TELEA
);
inpaint
(
img
,
inpaintMask
,
inpainted
,
3
,
CV_INPAINT_TELEA
);
cvShowImage
(
"inpainted image"
,
inpainted
);
imshow
(
"inpainted image"
,
inpainted
);
}
}
}
}
return
1
;
return
0
;
}
}
samples/cpp/lkdemo.cpp
View file @
f2a3e7e3
#include "opencv2/video/tracking.hpp"
#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <ctype.h>
#include <ctype.h>
#include <stdio.h>
IplImage
*
image
=
0
,
*
grey
=
0
,
*
prev_grey
=
0
,
*
pyramid
=
0
,
*
prev_pyramid
=
0
,
*
swap_temp
;
using
namespace
cv
;
using
namespace
std
;
int
win_size
=
10
;
Point2f
pt
;
const
int
MAX_COUNT
=
500
;
bool
addRemovePt
=
false
;
CvPoint2D32f
*
points
[
2
]
=
{
0
,
0
},
*
swap_points
;
char
*
status
=
0
;
int
count
=
0
;
int
need_to_init
=
0
;
int
night_mode
=
0
;
int
flags
=
0
;
int
add_remove_pt
=
0
;
CvPoint
pt
;
void
onMouse
(
int
event
,
int
x
,
int
y
,
int
flags
,
void
*
param
)
void
on_mouse
(
int
event
,
int
x
,
int
y
,
int
flags
,
void
*
param
)
{
{
if
(
!
image
)
return
;
if
(
image
->
origin
)
y
=
image
->
height
-
y
;
if
(
event
==
CV_EVENT_LBUTTONDOWN
)
if
(
event
==
CV_EVENT_LBUTTONDOWN
)
{
{
pt
=
cvPoint
(
x
,
y
);
pt
=
Point2f
((
float
)
x
,(
float
)
y
);
add
_remove_pt
=
1
;
add
RemovePt
=
true
;
}
}
}
}
int
main
(
int
argc
,
char
**
argv
)
int
main
(
int
argc
,
char
**
argv
)
{
{
CvCapture
*
capture
=
0
;
VideoCapture
cap
;
TermCriteria
termcrit
(
CV_TERMCRIT_ITER
|
CV_TERMCRIT_EPS
,
20
,
0.03
);
Size
winSize
(
10
,
10
);
const
int
MAX_COUNT
=
500
;
bool
needToInit
=
false
;
bool
nightMode
=
false
;
if
(
argc
==
1
||
(
argc
==
2
&&
strlen
(
argv
[
1
])
==
1
&&
isdigit
(
argv
[
1
][
0
])))
if
(
argc
==
1
||
(
argc
==
2
&&
strlen
(
argv
[
1
])
==
1
&&
isdigit
(
argv
[
1
][
0
])))
cap
ture
=
cvCaptureFromCAM
(
argc
==
2
?
argv
[
1
][
0
]
-
'0'
:
0
);
cap
.
open
(
argc
==
2
?
argv
[
1
][
0
]
-
'0'
:
0
);
else
if
(
argc
==
2
)
else
if
(
argc
==
2
)
cap
ture
=
cvCaptureFromAVI
(
argv
[
1
]
);
cap
.
open
(
argv
[
1
]
);
if
(
!
cap
ture
)
if
(
!
cap
.
isOpened
()
)
{
{
fprintf
(
stderr
,
"Could not initialize capturing...
\n
"
)
;
cout
<<
"Could not initialize capturing...
\n
"
;
return
-
1
;
return
0
;
}
}
/* print a welcome message, and the OpenCV version */
// print a welcome message, and the OpenCV version
printf
(
"Welcome to lkdemo, using OpenCV version %s (%d.%d.%d)
\n
"
,
cout
<<
"Welcome to lkdemo, using OpenCV version %s
\n
"
<<
CV_VERSION
;
CV_VERSION
,
CV_MAJOR_VERSION
,
CV_MINOR_VERSION
,
CV_SUBMINOR_VERSION
);
printf
(
"
Hot keys:
\n
"
cout
<<
"
\n
Hot keys:
\n
"
"
\t
ESC - quit the program
\n
"
"
\t
ESC - quit the program
\n
"
"
\t
r - auto-initialize tracking
\n
"
"
\t
r - auto-initialize tracking
\n
"
"
\t
c - delete all the points
\n
"
"
\t
c - delete all the points
\n
"
"
\t
n - switch the
\"
night
\"
mode on/off
\n
"
"
\t
n - switch the
\"
night
\"
mode on/off
\n
"
"To add/remove a feature point click it
\n
"
)
;
"To add/remove a feature point click it
\n
"
;
cvNamedWindow
(
"LkDemo"
,
0
);
namedWindow
(
"LK Demo"
,
1
);
cvSetMouseCallback
(
"LkDemo"
,
on_mouse
,
0
);
setMouseCallback
(
"LK Demo"
,
onMouse
,
0
);
Mat
gray
,
prevGray
,
image
;
vector
<
Point2f
>
points
[
2
];
for
(;;)
for
(;;)
{
{
IplImage
*
frame
=
0
;
Mat
frame
;
int
i
,
k
,
c
;
cap
>>
frame
;
if
(
frame
.
empty
()
)
frame
=
cvQueryFrame
(
capture
);
if
(
!
frame
)
break
;
break
;
if
(
!
image
)
frame
.
copyTo
(
image
);
{
cvtColor
(
image
,
gray
,
CV_BGR2GRAY
);
/* allocate all the buffers */
image
=
cvCreateImage
(
cvGetSize
(
frame
),
8
,
3
);
image
->
origin
=
frame
->
origin
;
grey
=
cvCreateImage
(
cvGetSize
(
frame
),
8
,
1
);
prev_grey
=
cvCreateImage
(
cvGetSize
(
frame
),
8
,
1
);
pyramid
=
cvCreateImage
(
cvGetSize
(
frame
),
8
,
1
);
prev_pyramid
=
cvCreateImage
(
cvGetSize
(
frame
),
8
,
1
);
points
[
0
]
=
(
CvPoint2D32f
*
)
cvAlloc
(
MAX_COUNT
*
sizeof
(
points
[
0
][
0
]));
points
[
1
]
=
(
CvPoint2D32f
*
)
cvAlloc
(
MAX_COUNT
*
sizeof
(
points
[
0
][
0
]));
status
=
(
char
*
)
cvAlloc
(
MAX_COUNT
);
flags
=
0
;
}
cvCopy
(
frame
,
image
,
0
);
if
(
nightMode
)
cvCvtColor
(
image
,
grey
,
CV_BGR2GRAY
);
image
=
Scalar
::
all
(
0
);
if
(
night_mode
)
if
(
needToInit
)
cvZero
(
image
);
if
(
need_to_init
)
{
{
/* automatic initialization */
// automatic initialization
IplImage
*
eig
=
cvCreateImage
(
cvGetSize
(
grey
),
32
,
1
);
goodFeaturesToTrack
(
gray
,
points
[
1
],
MAX_COUNT
,
0.01
,
10
,
Mat
(),
3
,
0
,
0.04
);
IplImage
*
temp
=
cvCreateImage
(
cvGetSize
(
grey
),
32
,
1
);
cornerSubPix
(
gray
,
points
[
1
],
winSize
,
Size
(
-
1
,
-
1
),
termcrit
);
double
quality
=
0.01
;
addRemovePt
=
false
;
double
min_distance
=
10
;
count
=
MAX_COUNT
;
cvGoodFeaturesToTrack
(
grey
,
eig
,
temp
,
points
[
1
],
&
count
,
quality
,
min_distance
,
0
,
3
,
0
,
0.04
);
cvFindCornerSubPix
(
grey
,
points
[
1
],
count
,
cvSize
(
win_size
,
win_size
),
cvSize
(
-
1
,
-
1
),
cvTermCriteria
(
CV_TERMCRIT_ITER
|
CV_TERMCRIT_EPS
,
20
,
0.03
));
cvReleaseImage
(
&
eig
);
cvReleaseImage
(
&
temp
);
add_remove_pt
=
0
;
}
}
else
if
(
count
>
0
)
else
if
(
!
points
[
0
].
empty
()
)
{
{
cvCalcOpticalFlowPyrLK
(
prev_grey
,
grey
,
prev_pyramid
,
pyramid
,
vector
<
uchar
>
status
;
points
[
0
],
points
[
1
],
count
,
cvSize
(
win_size
,
win_size
),
3
,
status
,
0
,
vector
<
float
>
err
;
cvTermCriteria
(
CV_TERMCRIT_ITER
|
CV_TERMCRIT_EPS
,
20
,
0.03
),
flags
);
if
(
prevGray
.
empty
())
flags
|=
CV_LKFLOW_PYR_A_READY
;
gray
.
copyTo
(
prevGray
);
for
(
i
=
k
=
0
;
i
<
count
;
i
++
)
calcOpticalFlowPyrLK
(
prevGray
,
gray
,
points
[
0
],
points
[
1
],
status
,
err
,
winSize
,
3
,
termcrit
,
0
);
size_t
i
,
k
;
for
(
i
=
k
=
0
;
i
<
points
[
1
].
size
();
i
++
)
{
{
if
(
add
_remove_p
t
)
if
(
add
RemoveP
t
)
{
{
double
dx
=
pt
.
x
-
points
[
1
][
i
].
x
;
if
(
norm
(
pt
-
points
[
1
][
i
])
<=
5
)
double
dy
=
pt
.
y
-
points
[
1
][
i
].
y
;
if
(
dx
*
dx
+
dy
*
dy
<=
25
)
{
{
add
_remove_pt
=
0
;
add
RemovePt
=
false
;
continue
;
continue
;
}
}
}
}
...
@@ -137,51 +101,44 @@ int main( int argc, char** argv )
...
@@ -137,51 +101,44 @@ int main( int argc, char** argv )
continue
;
continue
;
points
[
1
][
k
++
]
=
points
[
1
][
i
];
points
[
1
][
k
++
]
=
points
[
1
][
i
];
c
vCircle
(
image
,
cvPointFrom32f
(
points
[
1
][
i
]),
3
,
CV_RGB
(
0
,
255
,
0
),
-
1
,
8
,
0
);
c
ircle
(
image
,
points
[
1
][
i
],
3
,
Scalar
(
0
,
255
,
0
),
-
1
,
8
);
}
}
count
=
k
;
points
[
1
].
resize
(
k
)
;
}
}
if
(
add
_remove_pt
&&
count
<
MAX_COUNT
)
if
(
add
RemovePt
&&
points
[
1
].
size
()
<
MAX_COUNT
)
{
{
points
[
1
][
count
++
]
=
cvPointTo32f
(
pt
)
;
vector
<
Point2f
>
tmp
;
cvFindCornerSubPix
(
grey
,
points
[
1
]
+
count
-
1
,
1
,
tmp
.
push_back
(
pt
);
cvSize
(
win_size
,
win_size
),
cvSize
(
-
1
,
-
1
),
cornerSubPix
(
gray
,
tmp
,
winSize
,
cvSize
(
-
1
,
-
1
),
termcrit
);
cvTermCriteria
(
CV_TERMCRIT_ITER
|
CV_TERMCRIT_EPS
,
20
,
0.03
)
);
points
[
1
].
push_back
(
tmp
[
0
]
);
add
_remove_pt
=
0
;
add
RemovePt
=
false
;
}
}
CV_SWAP
(
prev_grey
,
grey
,
swap_temp
);
needToInit
=
false
;
CV_SWAP
(
prev_pyramid
,
pyramid
,
swap_temp
);
imshow
(
"LK Demo"
,
image
);
CV_SWAP
(
points
[
0
],
points
[
1
],
swap_points
);
need_to_init
=
0
;
cvShowImage
(
"LkDemo"
,
image
);
c
=
cvW
aitKey
(
10
);
c
har
c
=
(
char
)
w
aitKey
(
10
);
if
(
(
char
)
c
==
27
)
if
(
c
==
27
)
break
;
break
;
switch
(
(
char
)
c
)
switch
(
c
)
{
{
case
'r'
:
case
'r'
:
need
_to_init
=
1
;
need
ToInit
=
true
;
break
;
break
;
case
'c'
:
case
'c'
:
count
=
0
;
points
[
1
].
clear
()
;
break
;
break
;
case
'n'
:
case
'n'
:
night
_mode
^=
1
;
night
Mode
=
!
nightMode
;
break
;
break
;
default:
default:
;
;
}
}
}
cvReleaseCapture
(
&
capture
);
std
::
swap
(
points
[
1
],
points
[
0
]);
cvDestroyWindow
(
"LkDemo"
);
swap
(
prevGray
,
gray
);
}
return
0
;
return
0
;
}
}
#ifdef _EiC
main
(
1
,
"lkdemo.c"
);
#endif
samples/cpp/stereo_calib.cpp
View file @
f2a3e7e3
...
@@ -27,14 +27,18 @@
...
@@ -27,14 +27,18 @@
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc
_c.h
"
#include "opencv2/imgproc/imgproc
.hpp
"
#include <vector>
#include <vector>
#include <string>
#include <string>
#include <algorithm>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <ctype.h>
using
namespace
cv
;
using
namespace
std
;
using
namespace
std
;
//
//
...
@@ -45,344 +49,294 @@ using namespace std;
...
@@ -45,344 +49,294 @@ using namespace std;
// rectified results along with the computed disparity images.
// rectified results along with the computed disparity images.
//
//
static
void
static
void
StereoCalib
(
const
char
*
path
,
const
char
*
imageList
,
int
useUncalibrated
)
StereoCalib
(
const
vector
<
string
>&
imagelist
,
Size
boardSize
,
bool
useCalibrated
=
true
,
bool
showRectified
=
true
)
{
{
CvRect
roi1
,
roi2
;
if
(
imagelist
.
size
()
%
2
!=
0
)
int
nx
=
0
,
ny
=
0
;
int
displayCorners
=
1
;
int
showUndistorted
=
1
;
bool
isVerticalStereo
=
false
;
//OpenCV can handle left-right
//or up-down camera arrangements
const
int
maxScale
=
1
;
const
float
squareSize
=
1.
f
;
//Set this to your actual square size
FILE
*
f
=
fopen
(
imageList
,
"rt"
);
int
i
,
j
,
lr
,
nframes
=
0
,
n
,
N
=
0
;
vector
<
string
>
imageNames
[
2
];
vector
<
CvPoint3D32f
>
objectPoints
;
vector
<
CvPoint2D32f
>
points
[
2
];
vector
<
CvPoint2D32f
>
temp_points
[
2
];
vector
<
int
>
npoints
;
// vector<uchar> active[2];
int
is_found
[
2
]
=
{
0
,
0
};
vector
<
CvPoint2D32f
>
temp
;
CvSize
imageSize
=
{
0
,
0
};
// ARRAY AND VECTOR STORAGE:
double
M1
[
3
][
3
],
M2
[
3
][
3
],
D1
[
5
],
D2
[
5
];
double
R
[
3
][
3
],
T
[
3
],
E
[
3
][
3
],
F
[
3
][
3
];
double
Q
[
4
][
4
];
CvMat
_M1
=
cvMat
(
3
,
3
,
CV_64F
,
M1
);
CvMat
_M2
=
cvMat
(
3
,
3
,
CV_64F
,
M2
);
CvMat
_D1
=
cvMat
(
1
,
5
,
CV_64F
,
D1
);
CvMat
_D2
=
cvMat
(
1
,
5
,
CV_64F
,
D2
);
CvMat
matR
=
cvMat
(
3
,
3
,
CV_64F
,
R
);
CvMat
matT
=
cvMat
(
3
,
1
,
CV_64F
,
T
);
CvMat
matE
=
cvMat
(
3
,
3
,
CV_64F
,
E
);
CvMat
matF
=
cvMat
(
3
,
3
,
CV_64F
,
F
);
CvMat
matQ
=
cvMat
(
4
,
4
,
CV_64FC1
,
Q
);
char
buf
[
1024
];
if
(
displayCorners
)
cvNamedWindow
(
"corners"
,
1
);
// READ IN THE LIST OF CHESSBOARDS:
if
(
!
f
)
{
{
fprintf
(
stderr
,
"can not open file %s
\n
"
,
imageList
)
;
cout
<<
"Error: the image list contains odd (non-even) number of elements
\n
"
;
return
;
return
;
}
}
if
(
!
fgets
(
buf
,
sizeof
(
buf
)
-
3
,
f
)
||
sscanf
(
buf
,
"%d%d"
,
&
nx
,
&
ny
)
!=
2
)
bool
displayCorners
=
true
;
return
;
const
int
maxScale
=
2
;
n
=
nx
*
ny
;
const
float
squareSize
=
1.
f
;
// Set this to your actual square size
temp
.
resize
(
n
);
// ARRAY AND VECTOR STORAGE:
temp_points
[
0
].
resize
(
n
);
temp_points
[
1
].
resize
(
n
);
for
(
i
=
0
;;
i
++
)
vector
<
vector
<
Point2f
>
>
imagePoints
[
2
];
vector
<
vector
<
Point3f
>
>
objectPoints
;
Size
imageSize
;
int
i
,
j
,
k
,
nimages
=
(
int
)
imagelist
.
size
()
/
2
;
imagePoints
[
0
].
resize
(
nimages
);
imagePoints
[
1
].
resize
(
nimages
);
vector
<
string
>
goodImageList
;
for
(
i
=
j
=
0
;
i
<
nimages
;
i
++
)
{
{
int
count
=
0
,
result
=
0
;
for
(
k
=
0
;
k
<
2
;
k
++
)
lr
=
i
%
2
;
{
vector
<
CvPoint2D32f
>&
pts
=
temp_points
[
lr
];
//points[lr];
const
string
&
filename
=
imagelist
[
i
*
2
+
k
];
if
(
!
fgets
(
buf
,
sizeof
(
buf
)
-
3
,
f
))
Mat
img
=
imread
(
filename
,
0
);
if
(
img
.
empty
())
break
;
break
;
size_t
len
=
strlen
(
buf
);
if
(
imageSize
==
Size
()
)
while
(
len
>
0
&&
isspace
(
buf
[
len
-
1
]))
imageSize
=
img
.
size
();
buf
[
--
len
]
=
'\0'
;
else
if
(
img
.
size
()
!=
imageSize
)
if
(
buf
[
0
]
==
'#'
)
continue
;
char
fullpath
[
1024
];
sprintf
(
fullpath
,
"%s/%s"
,
path
,
buf
);
IplImage
*
img
=
cvLoadImage
(
fullpath
,
0
);
if
(
!
img
)
{
{
printf
(
"Cannot read file %s
\n
"
,
fullpath
)
;
cout
<<
"The image "
<<
filename
<<
" has the size different from the first image size. Skipping the pair
\n
"
;
return
;
break
;
}
}
imageSize
=
cvGetSize
(
img
);
bool
found
=
false
;
imageNames
[
lr
].
push_back
(
buf
);
vector
<
Point2f
>&
corners
=
imagePoints
[
k
][
j
];
//FIND CHESSBOARDS AND CORNERS THEREIN:
for
(
int
scale
=
1
;
scale
<=
maxScale
;
scale
++
)
for
(
int
s
=
1
;
s
<=
maxScale
;
s
++
)
{
{
IplImage
*
timg
=
img
;
Mat
timg
;
if
(
s
>
1
)
if
(
scale
==
1
)
timg
=
img
;
else
resize
(
img
,
timg
,
Size
(),
scale
,
scale
);
found
=
findChessboardCorners
(
timg
,
boardSize
,
corners
,
CV_CALIB_CB_ADAPTIVE_THRESH
|
CV_CALIB_CB_NORMALIZE_IMAGE
);
if
(
found
)
{
{
timg
=
cvCreateImage
(
cvSize
(
img
->
width
*
s
,
img
->
height
*
s
),
if
(
scale
>
1
)
img
->
depth
,
img
->
nChannels
);
cvResize
(
img
,
timg
,
CV_INTER_CUBIC
);
}
result
=
cvFindChessboardCorners
(
timg
,
cvSize
(
nx
,
ny
),
&
temp
[
0
],
&
count
,
CV_CALIB_CB_ADAPTIVE_THRESH
|
CV_CALIB_CB_NORMALIZE_IMAGE
);
if
(
timg
!=
img
)
cvReleaseImage
(
&
timg
);
if
(
result
||
s
==
maxScale
)
for
(
j
=
0
;
j
<
count
;
j
++
)
{
{
temp
[
j
].
x
/=
s
;
Mat
cornersMat
(
corners
)
;
temp
[
j
].
y
/=
s
;
cornersMat
*=
1.
/
scale
;
}
}
if
(
result
)
break
;
break
;
}
}
}
if
(
displayCorners
)
if
(
displayCorners
)
{
{
printf
(
"%s
\n
"
,
buf
);
cout
<<
filename
<<
endl
;
IplImage
*
cimg
=
cvCreateImage
(
imageSize
,
8
,
3
);
Mat
cimg
,
cimg1
;
cvCvtColor
(
img
,
cimg
,
CV_GRAY2BGR
);
cvtColor
(
img
,
cimg
,
CV_GRAY2BGR
);
cvDrawChessboardCorners
(
cimg
,
cvSize
(
nx
,
ny
),
&
temp
[
0
],
drawChessboardCorners
(
cimg
,
boardSize
,
corners
,
found
);
count
,
result
);
double
sf
=
640.
/
MAX
(
img
.
rows
,
img
.
cols
);
IplImage
*
cimg1
=
cvCreateImage
(
cvSize
(
640
,
480
),
IPL_DEPTH_8U
,
3
);
resize
(
cimg
,
cimg1
,
Size
(),
sf
,
sf
);
cvResize
(
cimg
,
cimg1
);
imshow
(
"corners"
,
cimg1
);
cvShowImage
(
"corners"
,
cimg1
);
char
c
=
(
char
)
waitKey
(
500
);
cvReleaseImage
(
&
cimg
);
cvReleaseImage
(
&
cimg1
);
int
c
=
cvWaitKey
(
1000
);
if
(
c
==
27
||
c
==
'q'
||
c
==
'Q'
)
//Allow ESC to quit
if
(
c
==
27
||
c
==
'q'
||
c
==
'Q'
)
//Allow ESC to quit
exit
(
-
1
);
exit
(
-
1
);
}
}
else
else
putchar
(
'.'
);
putchar
(
'.'
);
//N = pts.size();
if
(
!
found
)
//pts.resize(N + n, cvPoint2D32f(0,0));
break
;
//active[lr].push_back((uchar)result);
cornerSubPix
(
img
,
corners
,
Size
(
11
,
11
),
Size
(
-
1
,
-
1
),
is_found
[
lr
]
=
result
>
0
?
1
:
0
;
TermCriteria
(
CV_TERMCRIT_ITER
+
CV_TERMCRIT_EPS
,
//assert( result != 0 );
30
,
0.01
));
if
(
result
)
{
//Calibration will suffer without subpixel interpolation
cvFindCornerSubPix
(
img
,
&
temp
[
0
],
count
,
cvSize
(
11
,
11
),
cvSize
(
-
1
,
-
1
),
cvTermCriteria
(
CV_TERMCRIT_ITER
+
CV_TERMCRIT_EPS
,
30
,
0.01
)
);
copy
(
temp
.
begin
(),
temp
.
end
(),
pts
.
begin
()
);
}
}
cvReleaseImage
(
&
img
);
if
(
k
==
2
)
if
(
lr
)
{
{
if
(
is_found
[
0
]
==
1
&&
is_found
[
1
]
==
1
)
goodImageList
.
push_back
(
imagelist
[
i
*
2
]);
goodImageList
.
push_back
(
imagelist
[
i
*
2
+
1
]);
j
++
;
}
}
cout
<<
j
<<
" pairs have been successfully detected.
\n
"
;
nimages
=
j
;
if
(
nimages
<
2
)
{
{
assert
(
temp_points
[
0
].
size
()
==
temp_points
[
1
].
size
());
cout
<<
"Error: too little pairs to run the calibration
\n
"
;
int
current_size
=
points
[
0
].
size
();
return
;
}
points
[
0
].
resize
(
current_size
+
temp_points
[
0
].
size
(),
cvPoint2D32f
(
0.0
,
0.0
));
points
[
1
].
resize
(
current_size
+
temp_points
[
1
].
size
(),
cvPoint2D32f
(
0.0
,
0.0
));
copy
(
temp_points
[
0
].
begin
(),
temp_points
[
0
].
end
(),
points
[
0
].
begin
()
+
current_size
);
copy
(
temp_points
[
1
].
begin
(),
temp_points
[
1
].
end
(),
points
[
1
].
begin
()
+
current_size
);
nframes
++
;
imagePoints
[
0
].
resize
(
nimages
);
imagePoints
[
1
].
resize
(
nimages
);
objectPoints
.
resize
(
nimages
);
printf
(
"Pair successfully detected...
\n
"
);
for
(
i
=
0
;
i
<
nimages
;
i
++
)
{
for
(
j
=
0
;
j
<
boardSize
.
height
;
j
++
)
for
(
k
=
0
;
k
<
boardSize
.
width
;
k
++
)
objectPoints
[
i
].
push_back
(
Point3f
(
j
*
squareSize
,
k
*
squareSize
,
0
));
}
}
is_found
[
0
]
=
0
;
cout
<<
"Running stereo calibration ...
\n
"
;
is_found
[
1
]
=
0
;
}
Mat
cameraMatrix
[
2
],
distCoeffs
[
2
];
}
cameraMatrix
[
0
]
=
Mat
::
eye
(
3
,
3
,
CV_64F
);
fclose
(
f
);
cameraMatrix
[
1
]
=
Mat
::
eye
(
3
,
3
,
CV_64F
);
printf
(
"
\n
"
);
distCoeffs
[
0
]
=
Mat
::
zeros
(
8
,
1
,
CV_64F
);
// HARVEST CHESSBOARD 3D OBJECT POINT LIST:
distCoeffs
[
1
]
=
Mat
::
zeros
(
8
,
1
,
CV_64F
);
objectPoints
.
resize
(
nframes
*
n
);
Mat
R
,
T
,
E
,
F
;
for
(
i
=
0
;
i
<
ny
;
i
++
)
for
(
j
=
0
;
j
<
nx
;
j
++
)
stereoCalibrate
(
objectPoints
,
imagePoints
[
0
],
imagePoints
[
1
],
objectPoints
[
i
*
nx
+
j
]
=
cameraMatrix
[
0
],
distCoeffs
[
0
],
cvPoint3D32f
(
i
*
squareSize
,
j
*
squareSize
,
0
);
cameraMatrix
[
1
],
distCoeffs
[
1
],
for
(
i
=
1
;
i
<
nframes
;
i
++
)
imageSize
,
R
,
T
,
E
,
F
,
copy
(
objectPoints
.
begin
(),
objectPoints
.
begin
()
+
n
,
TermCriteria
(
CV_TERMCRIT_ITER
+
CV_TERMCRIT_EPS
,
100
,
1e-5
),
objectPoints
.
begin
()
+
i
*
n
);
npoints
.
resize
(
nframes
,
n
);
N
=
nframes
*
n
;
CvMat
_objectPoints
=
cvMat
(
1
,
N
,
CV_32FC3
,
&
objectPoints
[
0
]
);
CvMat
_imagePoints1
=
cvMat
(
1
,
N
,
CV_32FC2
,
&
points
[
0
][
0
]
);
CvMat
_imagePoints2
=
cvMat
(
1
,
N
,
CV_32FC2
,
&
points
[
1
][
0
]
);
CvMat
_npoints
=
cvMat
(
1
,
npoints
.
size
(),
CV_32S
,
&
npoints
[
0
]
);
cvSetIdentity
(
&
_M1
);
cvSetIdentity
(
&
_M2
);
cvZero
(
&
_D1
);
cvZero
(
&
_D2
);
// CALIBRATE THE STEREO CAMERAS
printf
(
"Running stereo calibration ..."
);
fflush
(
stdout
);
cvStereoCalibrate
(
&
_objectPoints
,
&
_imagePoints1
,
&
_imagePoints2
,
&
_npoints
,
&
_M1
,
&
_D1
,
&
_M2
,
&
_D2
,
imageSize
,
&
matR
,
&
matT
,
&
matE
,
&
matF
,
cvTermCriteria
(
CV_TERMCRIT_ITER
+
CV_TERMCRIT_EPS
,
100
,
1e-5
),
CV_CALIB_FIX_ASPECT_RATIO
+
CV_CALIB_FIX_ASPECT_RATIO
+
CV_CALIB_ZERO_TANGENT_DIST
+
CV_CALIB_ZERO_TANGENT_DIST
+
CV_CALIB_SAME_FOCAL_LENGTH
+
CV_CALIB_SAME_FOCAL_LENGTH
+
CV_CALIB_FIX_K3
);
CV_CALIB_FIX_K3
);
printf
(
" done
\n
"
)
;
cout
<<
"done
\n
"
;
// CALIBRATION QUALITY CHECK
// CALIBRATION QUALITY CHECK
// because the output fundamental matrix implicitly
// because the output fundamental matrix implicitly
// includes all the output information,
// includes all the output information,
// we can check the quality of calibration using the
// we can check the quality of calibration using the
// epipolar geometry constraint: m2^t*F*m1=0
// epipolar geometry constraint: m2^t*F*m1=0
vector
<
CvPoint3D32f
>
lines
[
2
];
double
err
=
0
;
points
[
0
].
resize
(
N
);
int
npoints
=
0
;
points
[
1
].
resize
(
N
);
vector
<
Vec3f
>
lines
[
2
];
_imagePoints1
=
cvMat
(
1
,
N
,
CV_32FC2
,
&
points
[
0
][
0
]
);
for
(
i
=
0
;
i
<
nimages
;
i
++
)
_imagePoints2
=
cvMat
(
1
,
N
,
CV_32FC2
,
&
points
[
1
][
0
]
);
{
lines
[
0
].
resize
(
N
);
int
npt
=
(
int
)
imagePoints
[
0
][
i
].
size
();
lines
[
1
].
resize
(
N
);
Mat
imgpt
[
2
];
CvMat
_L1
=
cvMat
(
1
,
N
,
CV_32FC3
,
&
lines
[
0
][
0
]);
for
(
k
=
0
;
k
<
2
;
k
++
)
CvMat
_L2
=
cvMat
(
1
,
N
,
CV_32FC3
,
&
lines
[
1
][
0
]);
//Always work in undistorted space
cvUndistortPoints
(
&
_imagePoints1
,
&
_imagePoints1
,
&
_M1
,
&
_D1
,
0
,
&
_M1
);
cvUndistortPoints
(
&
_imagePoints2
,
&
_imagePoints2
,
&
_M2
,
&
_D2
,
0
,
&
_M2
);
cvComputeCorrespondEpilines
(
&
_imagePoints1
,
1
,
&
matF
,
&
_L1
);
cvComputeCorrespondEpilines
(
&
_imagePoints2
,
2
,
&
matF
,
&
_L2
);
double
avgErr
=
0
;
for
(
i
=
0
;
i
<
N
;
i
++
)
{
{
double
err
=
fabs
(
points
[
0
][
i
].
x
*
lines
[
1
][
i
].
x
+
imgpt
[
k
]
=
Mat
(
imagePoints
[
k
][
i
]);
points
[
0
][
i
].
y
*
lines
[
1
][
i
].
y
+
lines
[
1
][
i
].
z
)
undistortPoints
(
imgpt
[
k
],
imgpt
[
k
],
cameraMatrix
[
k
],
distCoeffs
[
k
],
Mat
(),
cameraMatrix
[
k
]);
+
fabs
(
points
[
1
][
i
].
x
*
lines
[
0
][
i
].
x
+
computeCorrespondEpilines
(
imgpt
[
k
],
k
+
1
,
F
,
lines
[
k
]);
points
[
1
][
i
].
y
*
lines
[
0
][
i
].
y
+
lines
[
0
][
i
].
z
);
avgErr
+=
err
;
}
}
printf
(
"avg err = %g
\n
"
,
avgErr
/
(
nframes
*
n
)
);
for
(
j
=
0
;
j
<
npt
;
j
++
)
{
double
errij
=
fabs
(
imagePoints
[
0
][
i
][
j
].
x
*
lines
[
1
][
j
][
0
]
+
imagePoints
[
0
][
i
][
j
].
y
*
lines
[
1
][
j
][
1
]
+
lines
[
1
][
j
][
2
])
+
fabs
(
imagePoints
[
1
][
i
][
j
].
x
*
lines
[
0
][
j
][
0
]
+
imagePoints
[
1
][
i
][
j
].
y
*
lines
[
0
][
j
][
1
]
+
lines
[
0
][
j
][
2
]);
err
+=
errij
;
}
npoints
+=
npt
;
}
cout
<<
"average reprojection err = "
<<
err
/
npoints
<<
endl
;
// save intrinsic parameters
// save intrinsic parameters
CvFileStorage
*
fstorage
=
cvOpenFileStorage
(
"intrinsics.yml"
,
NULL
,
CV_STORAGE_WRITE
);
FileStorage
fs
(
"intrinsics.yml"
,
CV_STORAGE_WRITE
);
cvWrite
(
fstorage
,
"M1"
,
&
_M1
);
if
(
fs
.
isOpened
()
)
cvWrite
(
fstorage
,
"D1"
,
&
_D1
);
cvWrite
(
fstorage
,
"M2"
,
&
_M2
);
cvWrite
(
fstorage
,
"D2"
,
&
_D2
);
cvReleaseFileStorage
(
&
fstorage
);
//COMPUTE AND DISPLAY RECTIFICATION
if
(
showUndistorted
)
{
{
CvMat
*
mx1
=
cvCreateMat
(
imageSize
.
height
,
fs
<<
"M1"
<<
cameraMatrix
[
0
]
<<
"D1"
<<
distCoeffs
[
0
]
<<
imageSize
.
width
,
CV_32F
);
"M2"
<<
cameraMatrix
[
1
]
<<
"D2"
<<
distCoeffs
[
1
];
CvMat
*
my1
=
cvCreateMat
(
imageSize
.
height
,
fs
.
release
();
imageSize
.
width
,
CV_32F
);
}
CvMat
*
mx2
=
cvCreateMat
(
imageSize
.
height
,
else
imageSize
.
width
,
CV_32F
);
cout
<<
"Error: can not save the intrinsic parameters
\n
"
;
CvMat
*
my2
=
cvCreateMat
(
imageSize
.
height
,
imageSize
.
width
,
CV_32F
);
CvMat
*
img1r
=
cvCreateMat
(
imageSize
.
height
,
imageSize
.
width
,
CV_8U
);
CvMat
*
img2r
=
cvCreateMat
(
imageSize
.
height
,
imageSize
.
width
,
CV_8U
);
CvMat
*
disp
=
cvCreateMat
(
imageSize
.
height
,
imageSize
.
width
,
CV_16S
);
double
R1
[
3
][
3
],
R2
[
3
][
3
],
P1
[
3
][
4
],
P2
[
3
][
4
];
CvMat
_R1
=
cvMat
(
3
,
3
,
CV_64F
,
R1
);
CvMat
_R2
=
cvMat
(
3
,
3
,
CV_64F
,
R2
);
// IF BY CALIBRATED (BOUGUET'S METHOD)
if
(
useUncalibrated
==
0
)
{
CvMat
_P1
=
cvMat
(
3
,
4
,
CV_64F
,
P1
);
CvMat
_P2
=
cvMat
(
3
,
4
,
CV_64F
,
P2
);
cvStereoRectify
(
&
_M1
,
&
_M2
,
&
_D1
,
&
_D2
,
imageSize
,
Mat
R1
,
R2
,
P1
,
P2
,
Q
;
&
matR
,
&
matT
,
Rect
roi1
,
roi2
;
&
_R1
,
&
_R2
,
&
_P1
,
&
_P2
,
&
matQ
,
CV_CALIB_ZERO_DISPARITY
,
stereoRectify
(
cameraMatrix
[
0
],
distCoeffs
[
0
],
cameraMatrix
[
1
],
distCoeffs
[
1
],
imageSize
,
R
,
T
,
R1
,
R2
,
P1
,
P2
,
Q
,
1
,
imageSize
,
&
roi1
,
&
roi2
);
1
,
imageSize
,
&
roi1
,
&
roi2
);
CvFileStorage
*
file
=
cvOpenFileStorage
(
"extrinsics.yml"
,
NULL
,
CV_STORAGE_WRITE
);
fs
.
open
(
"extrinsics.yml"
,
CV_STORAGE_WRITE
);
cvWrite
(
file
,
"R"
,
&
matR
);
if
(
fs
.
isOpened
()
)
cvWrite
(
file
,
"T"
,
&
matT
);
{
cvWrite
(
file
,
"R1"
,
&
_R1
);
fs
<<
"R"
<<
R
<<
"T"
<<
T
<<
"R1"
<<
R1
<<
"R2"
<<
R2
<<
"P1"
<<
P1
<<
"P2"
<<
P2
<<
"Q"
<<
Q
;
cvWrite
(
file
,
"R2"
,
&
_R2
);
fs
.
release
();
cvWrite
(
file
,
"P1"
,
&
_P1
);
}
cvWrite
(
file
,
"P2"
,
&
_P2
);
cvWrite
(
file
,
"Q"
,
&
matQ
);
cvReleaseFileStorage
(
&
file
);
isVerticalStereo
=
fabs
(
P2
[
1
][
3
])
>
fabs
(
P2
[
0
][
3
]);
if
(
!
isVerticalStereo
)
roi2
.
x
+=
imageSize
.
width
;
else
else
roi2
.
y
+=
imageSize
.
height
;
cout
<<
"Error: can not save the intrinsic parameters
\n
"
;
//Precompute maps for cvRemap()
cvInitUndistortRectifyMap
(
&
_M1
,
&
_D1
,
&
_R1
,
&
_P1
,
mx1
,
my1
);
// OpenCV can handle left-right
cvInitUndistortRectifyMap
(
&
_M2
,
&
_D2
,
&
_R2
,
&
_P2
,
mx2
,
my2
);
// or up-down camera arrangements
bool
isVerticalStereo
=
fabs
(
P2
.
at
<
double
>
(
1
,
3
))
>
fabs
(
P2
.
at
<
double
>
(
0
,
3
));
//COMPUTE AND DISPLAY RECTIFICATION
if
(
!
showRectified
)
return
;
Mat
rmap
[
2
][
2
];
// IF BY CALIBRATED (BOUGUET'S METHOD)
if
(
!
useCalibrated
)
{
// we already computed everything
}
}
//OR ELSE HARTLEY'S METHOD
//OR ELSE HARTLEY'S METHOD
else
if
(
useUncalibrated
==
1
||
useUncalibrated
==
2
)
else
// use intrinsic parameters of each camera, but
// use intrinsic parameters of each camera, but
// compute the rectification transformation directly
// compute the rectification transformation directly
// from the fundamental matrix
// from the fundamental matrix
{
{
double
H1
[
3
][
3
],
H2
[
3
][
3
],
iM
[
3
][
3
];
vector
<
Point2f
>
allimgpt
[
2
];
CvMat
_H1
=
cvMat
(
3
,
3
,
CV_64F
,
H1
);
for
(
k
=
0
;
k
<
2
;
k
++
)
CvMat
_H2
=
cvMat
(
3
,
3
,
CV_64F
,
H2
);
{
CvMat
_iM
=
cvMat
(
3
,
3
,
CV_64F
,
iM
);
for
(
i
=
0
;
i
<
nimages
;
i
++
)
//Just to show you could have independently used F
std
::
copy
(
imagePoints
[
k
][
i
].
begin
(),
imagePoints
[
k
][
i
].
end
(),
back_inserter
(
allimgpt
[
k
]));
if
(
useUncalibrated
==
2
)
cvFindFundamentalMat
(
&
_imagePoints1
,
&
_imagePoints2
,
&
matF
);
cvStereoRectifyUncalibrated
(
&
_imagePoints1
,
&
_imagePoints2
,
&
matF
,
imageSize
,
&
_H1
,
&
_H2
,
3
);
cvInvert
(
&
_M1
,
&
_iM
);
cvMatMul
(
&
_H1
,
&
_M1
,
&
_R1
);
cvMatMul
(
&
_iM
,
&
_R1
,
&
_R1
);
cvInvert
(
&
_M2
,
&
_iM
);
cvMatMul
(
&
_H2
,
&
_M2
,
&
_R2
);
cvMatMul
(
&
_iM
,
&
_R2
,
&
_R2
);
//Precompute map for cvRemap()
cvInitUndistortRectifyMap
(
&
_M1
,
&
_D1
,
&
_R1
,
&
_M1
,
mx1
,
my1
);
cvInitUndistortRectifyMap
(
&
_M2
,
&
_D1
,
&
_R2
,
&
_M2
,
mx2
,
my2
);
}
}
else
F
=
findFundamentalMat
(
Mat
(
allimgpt
[
0
]),
Mat
(
allimgpt
[
1
]),
FM_8POINT
,
0
,
0
);
assert
(
0
);
Mat
H1
,
H2
;
stereoRectifyUncalibrated
(
Mat
(
allimgpt
[
0
]),
Mat
(
allimgpt
[
1
]),
F
,
imageSize
,
H1
,
H2
,
3
);
R1
=
cameraMatrix
[
0
].
inv
()
*
H1
*
cameraMatrix
[
0
];
cvReleaseMat
(
&
mx1
);
R2
=
cameraMatrix
[
1
].
inv
()
*
H2
*
cameraMatrix
[
1
];
cvReleaseMat
(
&
my1
);
cvReleaseMat
(
&
mx2
);
cvReleaseMat
(
&
my2
);
cvReleaseMat
(
&
img1r
);
cvReleaseMat
(
&
img2r
);
cvReleaseMat
(
&
disp
);
}
}
//Precompute maps for cv::remap()
initUndistortRectifyMap
(
cameraMatrix
[
0
],
distCoeffs
[
0
],
R1
,
P1
,
imageSize
,
CV_16SC2
,
rmap
[
0
][
0
],
rmap
[
0
][
1
]);
initUndistortRectifyMap
(
cameraMatrix
[
1
],
distCoeffs
[
1
],
R2
,
P2
,
imageSize
,
CV_16SC2
,
rmap
[
1
][
0
],
rmap
[
1
][
1
]);
/*for( i = 0; i < nimages; i++ )
{
Mat img =
}*/
}
}
static
bool
readStringList
(
const
string
&
filename
,
vector
<
string
>&
l
)
{
l
.
resize
(
0
);
FileStorage
fs
(
filename
,
FileStorage
::
READ
);
if
(
!
fs
.
isOpened
()
)
return
false
;
FileNode
n
=
fs
.
getFirstTopLevelNode
();
if
(
n
.
type
()
!=
FileNode
::
SEQ
)
return
false
;
FileNodeIterator
it
=
n
.
begin
(),
it_end
=
n
.
end
();
for
(
;
it
!=
it_end
;
++
it
)
l
.
push_back
((
string
)
*
it
);
return
true
;
}
int
print_help
()
{
cout
<<
"Usage:
\n
./stereo_calib -w board_width -h board_height <image list XML/YML file>
\n
"
;
return
0
;
}
int
main
(
int
argc
,
char
**
argv
)
int
main
(
int
argc
,
char
**
argv
)
{
{
if
(
argc
>
1
&&
!
strcmp
(
argv
[
1
],
"--help"
))
Size
boardSize
;
string
imagelistfn
;
for
(
int
i
=
1
;
i
<
argc
;
i
++
)
{
if
(
string
(
argv
[
i
])
==
"-w"
)
sscanf
(
argv
[
++
i
],
"%d"
,
&
boardSize
.
width
);
else
if
(
string
(
argv
[
i
])
==
"-h"
)
sscanf
(
argv
[
++
i
],
"%d"
,
&
boardSize
.
height
);
else
if
(
string
(
argv
[
i
])
==
"--help"
)
return
print_help
();
else
if
(
argv
[
i
][
0
]
==
'-'
)
{
{
printf
(
"Usage:
\n
./stereo_calib <path to images> <file wtih image list>
\n
"
)
;
cout
<<
"invalid option "
<<
argv
[
i
]
<<
endl
;
return
0
;
return
0
;
}
}
else
imagelistfn
=
argv
[
i
];
}
if
(
imagelistfn
==
""
)
{
imagelistfn
=
"stereo_calib.xml"
;
boardSize
=
Size
(
9
,
6
);
}
vector
<
string
>
imagelist
;
bool
ok
=
readStringList
(
imagelistfn
,
imagelist
);
if
(
!
ok
||
imagelist
.
empty
()
||
boardSize
.
width
<=
0
||
boardSize
.
height
<=
0
)
return
print_help
();
StereoCalib
(
argc
>
1
?
argv
[
1
]
:
"."
,
argc
>
2
?
argv
[
2
]
:
"stereo_calib.txt"
,
0
);
StereoCalib
(
imagelist
,
boardSize
);
return
0
;
return
0
;
}
}
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