参考以下博客进行了修改,修改了灰度阈值以及最后标注红线的部分。https://blog.csdn.net/dangkie/article/details/78996761#comments
void
StegerLine
(
)
{
Mat img0
=
imread
(
"D:\\结构光实验数据\\2019-11-20\\1_nl.bmp"
,
1
)
;
Mat img
;
cvtColor
(
img0
,
img0
,
CV_BGR2GRAY
)
;
img
=
img0
.
clone
(
)
;
img
.
convertTo
(
img
,
CV_32FC1
)
;
GaussianBlur
(
img
,
img
,
Size
(
0
,
0
)
,
6
,
6
)
;
Mat m1
,
m2
;
m1
=
(
Mat_
<
float
>
(
1
,
2
)
<<
1
,
-
1
)
;
m2
=
(
Mat_
<
float
>
(
2
,
1
)
<<
1
,
-
1
)
;
Mat dx
,
dy
;
filter2D
(
img
,
dx
,
CV_32FC1
,
m1
)
;
filter2D
(
img
,
dy
,
CV_32FC1
,
m2
)
;
Mat m3
,
m4
,
m5
;
m3
=
(
Mat_
<
float
>
(
1
,
3
)
<<
1
,
-
2
,
1
)
;
m4
=
(
Mat_
<
float
>
(
3
,
1
)
<<
1
,
-
2
,
1
)
;
m5
=
(
Mat_
<
float
>
(
2
,
2
)
<<
1
,
-
1
,
-
1
,
1
)
;
Mat dxx
,
dyy
,
dxy
;
filter2D
(
img
,
dxx
,
CV_32FC1
,
m3
)
;
filter2D
(
img
,
dyy
,
CV_32FC1
,
m4
)
;
filter2D
(
img
,
dxy
,
CV_32FC1
,
m5
)
;
double
maxD
=
-
1
;
int
imgcol
=
img
.
cols
;
int
imgrow
=
img
.
rows
;
vector
<
double
>
Pt
;
for
(
int
i
=
0
;
i
<
imgcol
;
i
++
)
{
for
(
int
j
=
0
;
j
<
imgrow
;
j
++
)
{
if
(
img0
.
at
<
uchar
>
(
j
,
i
)
>
50
)
{
Mat
hessian
(
2
,
2
,
CV_32FC1
)
;
hessian
.
at
<
float
>
(
0
,
0
)
=
dxx
.
at
<
float
>
(
j
,
i
)
;
hessian
.
at
<
float
>
(
0
,
1
)
=
dxy
.
at
<
float
>
(
j
,
i
)
;
hessian
.
at
<
float
>
(
1
,
0
)
=
dxy
.
at
<
float
>
(
j
,
i
)
;
hessian
.
at
<
float
>
(
1
,
1
)
=
dyy
.
at
<
float
>
(
j
,
i
)
;
Mat eValue
;
Mat eVectors
;
eigen
(
hessian
,
eValue
,
eVectors
)
;
double
nx
,
ny
;
double
fmaxD
=
0
;
if
(
fabs
(
eValue
.
at
<
float
>
(
0
,
0
)
)
>=
fabs
(
eValue
.
at
<
float
>
(
1
,
0
)
)
)
{
nx
=
eVectors
.
at
<
float
>
(
0
,
0
)
;
ny
=
eVectors
.
at
<
float
>
(
0
,
1
)
;
fmaxD
=
eValue
.
at
<
float
>
(
0
,
0
)
;
}
else
{
nx
=
eVectors
.
at
<
float
>
(
1
,
0
)
;
ny
=
eVectors
.
at
<
float
>
(
1
,
1
)
;
fmaxD
=
eValue
.
at
<
float
>
(
1
,
0
)
;
}
double
t
=
-
(
nx
*
dx
.
at
<
float
>
(
j
,
i
)
+
ny
*
dy
.
at
<
float
>
(
j
,
i
)
)
/
(
nx
*
nx
*
dxx
.
at
<
float
>
(
j
,
i
)
+
2
*
nx
*
ny
*
dxy
.
at
<
float
>
(
j
,
i
)
+
ny
*
ny
*
dyy
.
at
<
float
>
(
j
,
i
)
)
;
if
(
fabs
(
t
*
nx
)
<=
0.5
&&
fabs
(
t
*
ny
)
<=
0.5
)
{
Pt
.
push_back
(
i
)
;
Pt
.
push_back
(
j
)
;
}
}
}
}
cvtColor
(
img0
,
img0
,
CV_GRAY2BGR
)
;
for
(
int
k
=
0
;
k
<
Pt
.
size
(
)
/
2
;
k
++
)
{
Point rpt
;
rpt
.
x
=
Pt
[
2
*
k
+
0
]
;
rpt
.
y
=
Pt
[
2
*
k
+
1
]
;
circle
(
img0
,
rpt
,
1
,
Scalar
(
0
,
0
,
255
)
)
;
}
namedWindow
(
"result"
,
CV_WINDOW_NORMAL
)
;
imshow
(
"result"
,
img0
)
;
imwrite
(
"output.bmp"
,
img0
)
;
waitKey
(
0
)
;
}
修改后的接口
vector
<
Point
>
StegerLine
(
cv
::
Mat
&
InputImg
)
{
Mat img0
=
InputImg
;
Mat img
;
img0
.
copyTo
(
img
)
;
img
.
convertTo
(
img
,
CV_32FC1
)
;
GaussianBlur
(
img
,
img
,
Size
(
0
,
0
)
,
6
,
6
)
;
Mat m1
,
m2
;
m1
=
(
Mat_
<
float
>
(
1
,
2
)
<<
1
,
-
1
)
;
m2
=
(
Mat_
<
float
>
(
2
,
1
)
<<
1
,
-
1
)
;
Mat dx
,
dy
;
filter2D
(
img
,
dx
,
CV_32FC1
,
m1
)
;
filter2D
(
img
,
dy
,
CV_32FC1
,
m2
)
;
Mat m3
,
m4
,
m5
;
m3
=
(
Mat_
<
float
>
(
1
,
3
)
<<
1
,
-
2
,
1
)
;
m4
=
(
Mat_
<
float
>
(
3
,
1
)
<<
1
,
-
2
,
1
)
;
m5
=
(
Mat_
<
float
>
(
2
,
2
)
<<
1
,
-
1
,
-
1
,
1
)
;
Mat dxx
,
dyy
,
dxy
;
filter2D
(
img
,
dxx
,
CV_32FC1
,
m3
)
;
filter2D
(
img
,
dyy
,
CV_32FC1
,
m4
)
;
filter2D
(
img
,
dxy
,
CV_32FC1
,
m5
)
;
double
maxD
=
-
1
;
int
imgcol
=
img
.
cols
;
int
imgrow
=
img
.
rows
;
vector
<
double
>
Pt
;
for
(
int
i
=
0
;
i
<
imgcol
;
i
++
)
{
for
(
int
j
=
0
;
j
<
imgrow
;
j
++
)
{
if
(
img0
.
at
<
uchar
>
(
j
,
i
)
>
50
)
{
Mat
hessian
(
2
,
2
,
CV_32FC1
)
;
hessian
.
at
<
float
>
(
0
,
0
)
=
dxx
.
at
<
float
>
(
j
,
i
)
;
hessian
.
at
<
float
>
(
0
,
1
)
=
dxy
.
at
<
float
>
(
j
,
i
)
;
hessian
.
at
<
float
>
(
1
,
0
)
=
dxy
.
at
<
float
>
(
j
,
i
)
;
hessian
.
at
<
float
>
(
1
,
1
)
=
dyy
.
at
<
float
>
(
j
,
i
)
;
Mat eValue
;
Mat eVectors
;
eigen
(
hessian
,
eValue
,
eVectors
)
;
double
nx
,
ny
;
double
fmaxD
=
0
;
if
(
fabs
(
eValue
.
at
<
float
>
(
0
,
0
)
)
>=
fabs
(
eValue
.
at
<
float
>
(
1
,
0
)
)
)
{
nx
=
eVectors
.
at
<
float
>
(
0
,
0
)
;
ny
=
eVectors
.
at
<
float
>
(
0
,
1
)
;
fmaxD
=
eValue
.
at
<
float
>
(
0
,
0
)
;
}
else
{
nx
=
eVectors
.
at
<
float
>
(
1
,
0
)
;
ny
=
eVectors
.
at
<
float
>
(
1
,
1
)
;
fmaxD
=
eValue
.
at
<
float
>
(
1
,
0
)
;
}
double
t
=
-
(
nx
*
dx
.
at
<
float
>
(
j
,
i
)
+
ny
*
dy
.
at
<
float
>
(
j
,
i
)
)
/
(
nx
*
nx
*
dxx
.
at
<
float
>
(
j
,
i
)
+
2
*
nx
*
ny
*
dxy
.
at
<
float
>
(
j
,
i
)
+
ny
*
ny
*
dyy
.
at
<
float
>
(
j
,
i
)
)
;
if
(
fabs
(
t
*
nx
)
<=
0.5
&&
fabs
(
t
*
ny
)
<=
0.5
)
{
Pt
.
push_back
(
i
)
;
Pt
.
push_back
(
j
)
;
}
}
}
}
cvtColor
(
img0
,
img0
,
CV_GRAY2BGR
)
;
vector
<
Point
>
LaserLinePtSet
;
for
(
int
k
=
0
;
k
<
Pt
.
size
(
)
/
2
;
k
++
)
{
Point rpt
;
rpt
.
x
=
Pt
[
2
*
k
+
0
]
;
rpt
.
y
=
Pt
[
2
*
k
+
1
]
;
LaserLinePtSet
.
push_back
(
rpt
)
;
circle
(
img0
,
rpt
,
1
,
Scalar
(
0
,
0
,
255
)
)
;
}
namedWindow
(
"result"
,
CV_WINDOW_NORMAL
)
;
imshow
(
"result"
,
img0
)
;
imwrite
(
"output.bmp"
,
img0
)
;
return
LaserLinePtSet
;
}