Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
1
11CTag
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
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
付尧(20研)
11CTag
Commits
b2a97527
Commit
b2a97527
authored
Oct 08, 2021
by
付尧(20研)
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
main
parent
d0b43c57
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
135 additions
and
11 deletions
+135
-11
main.cpp
main.cpp
+135
-11
No files found.
main.cpp
View file @
b2a97527
...
@@ -6,15 +6,119 @@ double distance(Point2d a,Point2d b)
...
@@ -6,15 +6,119 @@ double distance(Point2d a,Point2d b)
{
{
return
sqrtf
((
powf
((
a
.
x
-
b
.
x
),
2
))
+
powf
((
a
.
y
-
b
.
y
),
2
));
return
sqrtf
((
powf
((
a
.
x
-
b
.
x
),
2
))
+
powf
((
a
.
y
-
b
.
y
),
2
));
}
}
void
KeyPointsToPoints
(
vector
<
KeyPoint
>
kpts
,
vector
<
Point2d
>
&
pts
,
int
minx
,
int
miny
)
{
for
(
int
i
=
0
;
i
<
kpts
.
size
();
i
++
)
{
kpts
[
i
].
pt
.
x
+=
minx
;
kpts
[
i
].
pt
.
y
+=
miny
;
pts
.
push_back
(
kpts
[
i
].
pt
);
}
}
bool
sortwccenter
(
Point2d
a
,
Point2d
b
)
{
}
class
EleCTag
{
public
:
vector
<
Point
>
corner
;
vector
<
Point2d
>
wccenter
;
vector
<
Point2d
>
bccenter
;
};
void
sortCornerWCenter
(
EleCTag
&
ECTag
,
int
midx
,
int
midy
)
//调整白圆和角点顺序
{
cout
<<
midx
<<
endl
;
cout
<<
midy
<<
endl
;
double
vectmp1
[
2
]
=
{
ECTag
.
corner
[
2
].
x
-
ECTag
.
corner
[
1
].
x
,
ECTag
.
corner
[
2
].
y
-
ECTag
.
corner
[
1
].
y
};
double
vectmp2
[
2
]
=
{
ECTag
.
corner
[
1
].
x
-
ECTag
.
corner
[
0
].
x
,
ECTag
.
corner
[
1
].
y
-
ECTag
.
corner
[
0
].
y
};
double
t
=
vectmp1
[
0
]
*
vectmp2
[
1
]
-
vectmp1
[
1
]
*
vectmp2
[
0
];
//叉乘
if
(
t
>
0
)
//逆时针变为顺时针
{
int
tmpcornerx
=
ECTag
.
corner
[
1
].
x
;
int
tmpcornery
=
ECTag
.
corner
[
1
].
y
;
ECTag
.
corner
[
1
].
x
=
ECTag
.
corner
[
3
].
x
;
ECTag
.
corner
[
1
].
y
=
ECTag
.
corner
[
3
].
y
;
ECTag
.
corner
[
3
].
x
=
tmpcornerx
;
ECTag
.
corner
[
3
].
y
=
tmpcornery
;
}
int
minCornerNum
=
0
;
double
minSum
=
DBL_MAX
;
double
Sum
=
0
;
for
(
int
i
=
0
;
i
<
4
;
i
++
)
//通过与圆距离找到0号角点
{
for
(
int
j
=
0
;
j
<
7
;
j
++
)
{
double
tmpdis
=
distance
(
ECTag
.
corner
[
i
],
ECTag
.
wccenter
[
j
]);
Sum
+=
tmpdis
;
}
if
(
Sum
<
minSum
)
{
minSum
=
Sum
;
minCornerNum
=
i
;
}
Sum
=
0
;
}
if
(
minCornerNum
!=
0
)
{
for
(
int
i
=
0
;
i
!=
minCornerNum
;
i
++
)
{
ECTag
.
corner
.
push_back
(
ECTag
.
corner
[
i
]);
}
ECTag
.
corner
.
erase
(
ECTag
.
corner
.
begin
(),
ECTag
.
corner
.
begin
()
+
minCornerNum
);
}
sort
(
ECTag
.
wccenter
.
begin
(),
ECTag
.
wccenter
.
end
(),
[
&
ECTag
](
auto
a
,
auto
b
)
//排序白圆ID
{
if
(
distance
(
a
,
ECTag
.
corner
[
0
])
+
distance
(
a
,
ECTag
.
corner
[
1
])
<
distance
(
b
,
ECTag
.
corner
[
0
])
+
distance
(
b
,
ECTag
.
corner
[
1
]))
return
true
;
});
sort
(
ECTag
.
wccenter
.
begin
(),
ECTag
.
wccenter
.
begin
()
+
4
,
[
&
ECTag
](
auto
a
,
auto
b
)
{
if
(
distance
(
a
,
ECTag
.
corner
[
0
])
<
distance
(
b
,
ECTag
.
corner
[
0
]))
return
true
;
});
//cout << "ECTag.wccenter:" << ECTag.wccenter << endl;
}
void
sortBCenter
(
EleCTag
&
ECTag
)
{
sort
(
ECTag
.
bccenter
.
begin
(),
ECTag
.
bccenter
.
end
(),
[
&
ECTag
](
auto
a
,
auto
b
)
//排序白圆ID
{
if
(
distance
(
a
,
ECTag
.
wccenter
[
0
])
<
distance
(
b
,
ECTag
.
wccenter
[
0
]))
return
true
;
});
sort
(
ECTag
.
bccenter
.
begin
()
+
1
,
ECTag
.
bccenter
.
begin
()
+
3
,
[
&
ECTag
](
auto
a
,
auto
b
)
//排序白圆ID
{
if
(
distance
(
a
,
ECTag
.
wccenter
[
3
])
<
distance
(
b
,
ECTag
.
wccenter
[
3
]))
return
true
;
});
cout
<<
"ECTag.bccenter:"
<<
ECTag
.
bccenter
<<
endl
;
}
double
c2ST
(
Point2d
a
,
Point2d
b
,
Point2d
c
)
//计算三角形面积的2倍
{
double
s
=
a
.
x
*
b
.
y
+
b
.
x
*
c
.
y
+
c
.
x
*
a
.
y
-
a
.
x
*
c
.
y
-
b
.
x
*
a
.
y
-
c
.
x
*
b
.
y
;
return
s
;
}
void
countCR
(
EleCTag
&
ECTag
)
//计算交比
{
for
(
int
i
=
0
;
i
<
7
;
i
++
)
{
double
st1
=
c2ST
(
ECTag
.
wccenter
[
i
],
ECTag
.
bccenter
[
0
],
ECTag
.
bccenter
[
1
]);
double
st2
=
c2ST
(
ECTag
.
wccenter
[
i
],
ECTag
.
bccenter
[
2
],
ECTag
.
bccenter
[
3
]);
double
st3
=
c2ST
(
ECTag
.
wccenter
[
i
],
ECTag
.
bccenter
[
0
],
ECTag
.
bccenter
[
2
]);
double
st4
=
c2ST
(
ECTag
.
wccenter
[
i
],
ECTag
.
bccenter
[
1
],
ECTag
.
bccenter
[
3
]);
double
cr
=
st1
*
st2
/
st3
/
st4
;
cout
<<
"cr:"
<<
cr
<<
endl
;
}
}
int
main
()
int
main
()
{
{
string
imgpath
=
"
D:/11CTag/t1.png
"
;
string
imgpath
=
"
F:/11cTag/p2.bmp
"
;
Mat
img
=
imread
(
imgpath
,
0
);
Mat
img
=
imread
(
imgpath
,
0
);
Mat
local
;
Mat
local
;
adaptiveThreshold
(
img
,
local
,
255
,
cv
::
ADAPTIVE_THRESH_MEAN_C
,
cv
::
THRESH_BINARY
,
51
,
11
);
adaptiveThreshold
(
img
,
local
,
255
,
cv
::
ADAPTIVE_THRESH_MEAN_C
,
cv
::
THRESH_BINARY
,
51
,
11
);
vector
<
vector
<
Point
>>
contours
;
vector
<
vector
<
Point
>>
contours
;
vector
<
Vec4i
>
hierarchy
;
vector
<
Vec4i
>
hierarchy
;
findContours
(
local
,
contours
,
hierarchy
,
RETR_TREE
,
CHAIN_APPROX_SIMPLE
,
Point
());
findContours
(
local
,
contours
,
hierarchy
,
RETR_TREE
,
CHAIN_APPROX_SIMPLE
,
Point
());
cout
<<
hierarchy
[
0
]
<<
endl
;
Mat
imageContours
=
Mat
::
zeros
(
local
.
size
(),
CV_8UC1
);
Mat
imageContours
=
Mat
::
zeros
(
local
.
size
(),
CV_8UC1
);
Mat
Contours
=
Mat
::
zeros
(
local
.
size
(),
CV_8UC1
);
//绘制
Mat
Contours
=
Mat
::
zeros
(
local
.
size
(),
CV_8UC1
);
//绘制
Mat
dstImg
=
Mat
::
zeros
(
local
.
size
(),
CV_8UC1
);
//绘制
Mat
dstImg
=
Mat
::
zeros
(
local
.
size
(),
CV_8UC1
);
//绘制
...
@@ -35,11 +139,11 @@ int main()
...
@@ -35,11 +139,11 @@ int main()
//矩形必须是四个点,必须是凸的,
//矩形必须是四个点,必须是凸的,
if
(
contours_poly
[
i
].
size
()
==
4
&&
fabs
(
contourArea
(
Mat
(
contours_poly
[
i
])))
>
1000
&&
isContourConvex
(
Mat
(
contours_poly
[
i
])))
if
(
contours_poly
[
i
].
size
()
==
4
&&
fabs
(
contourArea
(
Mat
(
contours_poly
[
i
])))
>
1000
&&
isContourConvex
(
Mat
(
contours_poly
[
i
])))
{
{
cout
<<
contours_poly
[
i
]
<<
endl
;
//
cout << contours_poly[i] << endl;
//potential_quad.push_back(contours_poly[i]);
//potential_quad.push_back(contours_poly[i]);
drawContours
(
dstImg
,
contours_poly
,
i
,
Scalar
(
255
),
1
,
8
);
//绘制
drawContours
(
dstImg
,
contours_poly
,
i
,
Scalar
(
255
),
1
,
8
);
//绘制
Mat
imgtmp
=
img
.
clone
();
Mat
imgtmp
=
img
.
clone
();
int
maxx
=
0
,
maxy
=
0
,
minx
=
9999
,
miny
=
9999
;
int
maxx
=
0
,
maxy
=
0
,
minx
=
INT_MAX
,
miny
=
INT_MAX
;
for
(
int
j
=
0
;
j
<
4
;
j
++
)
for
(
int
j
=
0
;
j
<
4
;
j
++
)
{
{
maxx
=
max
(
contours_poly
[
i
][
j
].
x
,
maxx
);
maxx
=
max
(
contours_poly
[
i
][
j
].
x
,
maxx
);
...
@@ -49,28 +153,48 @@ int main()
...
@@ -49,28 +153,48 @@ int main()
}
}
Rect
rect
(
minx
,
miny
,
maxx
-
minx
,
maxy
-
miny
);
Rect
rect
(
minx
,
miny
,
maxx
-
minx
,
maxy
-
miny
);
Mat
roi
=
Mat
(
imgtmp
,
rect
);
Mat
roi
=
Mat
(
imgtmp
,
rect
);
SimpleBlobDetector
::
Params
params
;
SimpleBlobDetector
::
Params
params
;
//检测白色圆
params
.
blobColor
=
255
;
params
.
blobColor
=
255
;
params
.
filterByCircularity
=
true
;
//斑点圆度的限制变量
params
.
minCircularity
=
0.8
f
;
//斑点的最小圆度
cv
::
Ptr
<
cv
::
SimpleBlobDetector
>
detector
=
SimpleBlobDetector
::
create
(
params
);
cv
::
Ptr
<
cv
::
SimpleBlobDetector
>
detector
=
SimpleBlobDetector
::
create
(
params
);
vector
<
KeyPoint
>
keyp
;
vector
<
KeyPoint
>
keyp
;
detector
->
detect
(
roi
,
keyp
);
detector
->
detect
(
roi
,
keyp
);
if
(
keyp
.
size
()
==
7
)
//7个白色的圆
if
(
keyp
.
size
()
==
7
)
//7个白色的圆
{
{
cout
<<
keyp
[
1
].
pt
.
x
+
minx
<<
endl
;
int
midx
=
(
maxx
+
minx
)
/
2
;
int
midy
=
(
maxy
+
miny
)
/
2
;
EleCTag
ECTag
;
ECTag
.
corner
=
contours_poly
[
i
];
KeyPointsToPoints
(
keyp
,
ECTag
.
wccenter
,
minx
,
miny
);
//const clock_t begin_time = clock();
sortCornerWCenter
(
ECTag
,
midx
,
midy
);
//float seconds = float(clock() - begin_time);
//cout << "ECTag.wccenter:" << ECTag.wccenter << endl;
//cout << "seconds:"<<seconds << endl;
SimpleBlobDetector
::
Params
params2
;
//检测内部黑色圆
params2
.
blobColor
=
0
;
params2
.
filterByCircularity
=
true
;
params2
.
minCircularity
=
0.8
f
;
cv
::
Ptr
<
cv
::
SimpleBlobDetector
>
detector2
=
SimpleBlobDetector
::
create
(
params2
);
vector
<
KeyPoint
>
keyp2
;
detector2
->
detect
(
roi
,
keyp2
);
KeyPointsToPoints
(
keyp2
,
ECTag
.
bccenter
,
minx
,
miny
);
sortBCenter
(
ECTag
);
cout
<<
"keyp2.size:"
<<
keyp2
.
size
()
<<
endl
;
countCR
(
ECTag
);
imshow
(
"ROI"
,
roi
);
imshow
(
"ROI"
,
roi
);
waitKey
(
0
);
waitKey
(
0
);
}
}
}
}
drawContours
(
imageContours
,
contours
,
i
,
Scalar
(
255
),
1
,
8
,
hierarchy
);
drawContours
(
imageContours
,
contours
,
i
,
Scalar
(
255
),
1
,
8
,
hierarchy
);
}
}
imshow
(
"approx"
,
dstImg
);
//
imshow("approx", dstImg);
imshow
(
"Contours Image"
,
imageContours
);
//轮廓
//
imshow("Contours Image", imageContours); //轮廓
//imshow("Point of Contours", Contours); //向量contours内保存的所有轮廓点集
//imshow("Point of Contours", Contours); //向量contours内保存的所有轮廓点集
imshow
(
"imgraw"
,
local
);
//
imshow("imgraw", local);
waitKey
(
0
);
//
waitKey(0);
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