forked from opencv/opencv_contrib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_mser.cpp
178 lines (156 loc) · 6.63 KB
/
test_mser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include "test_precomp.hpp"
namespace opencv_test { namespace {
// we use such nested structure to combine test values
typedef std::tuple< std::tuple<bool /* useBboxes */, bool /* useContourData */>,
int /* numNeighbors */, std::string /*file path*/> MSERTestParams;
class MSERTest : public ::testing::TestWithParam<MSERTestParams> {};
// compare results to OpenCV's MSER detector
// by comparing resulting contours
TEST_P(MSERTest, accuracy)
{
auto p = GetParam();
bool useBboxes = std::get<0>(std::get<0>(p));
bool useContourData = std::get<1>(std::get<0>(p));
int numNeighbors = std::get<1>(p); // 4 or 8
std::string imgPath = std::get<2>(p);
cv::Mat src = imread(cvtest::findDataFile(imgPath), cv::IMREAD_GRAYSCALE);
uint32_t delta = 2;
uint32_t minArea = 256;
uint32_t maxArea = (int)src.total()/4;
float maxVariation = 0.15f;
float minDiversity = 0.2f;
std::vector<std::vector<Point>> contours;
std::vector<cv::Rect> bboxes;
std::vector<cv::fastcv::FCVMSER::ContourData> contourData;
cv::Ptr<cv::fastcv::FCVMSER> mser;
mser = cv::fastcv::FCVMSER::create(src.size(), numNeighbors, delta, minArea, maxArea,
maxVariation, minDiversity);
if (useBboxes)
{
if (useContourData)
{
mser->detect(src, contours, bboxes, contourData);
}
else
{
mser->detect(src, contours, bboxes);
}
}
else
{
mser->detect(src, contours);
}
Rect imgRect(0, 0, src.cols, src.rows);
if (useBboxes)
{
ASSERT_EQ(contours.size(), bboxes.size());
for (size_t i = 0; i < contours.size(); i++)
{
ASSERT_TRUE(imgRect.contains(bboxes[i].tl()));
ASSERT_TRUE(imgRect.contains(bboxes[i].br()));
for (size_t j = 0; j < contours[i].size(); j++)
{
ASSERT_TRUE(bboxes[i].contains(contours[i][j]));
}
}
}
if (useContourData)
{
ASSERT_EQ(contours.size(), contourData.size());
for (size_t i = 0; i < contours.size(); i++)
{
int polarity = contourData[i].polarity;
EXPECT_TRUE(polarity == -1 || polarity == 1);
}
}
// compare each pair of contours using dist transform of their points
// find pair of contours by similar moments
typedef cv::Matx<double, 10, 1> MomentVec;
auto calcEstimate = [](const std::vector<std::vector<Point>>& contours, Size srcSize) -> std::vector<std::pair<Mat, MomentVec>>
{
std::vector<std::pair<Mat, MomentVec>> res;
for (size_t i = 0; i < contours.size(); i++)
{
const std::vector<Point>& contour = contours[i];
Mat ptsMap(srcSize, CV_8U, Scalar(255));
for(size_t j = 0; j < contour.size(); ++j)
{
ptsMap.at<uchar>(contour[j].y, contour[j].x) = 0;
}
Mat distTrans(srcSize, CV_8U);
cv::distanceTransform(ptsMap, distTrans, DIST_L2, DIST_MASK_PRECISE);
cv::Moments m = cv::moments(contour);
double invRows = 1.0 / srcSize.height, invCols = 1.0 / srcSize.width;
double invRows2 = invRows / srcSize.height, invCols2 = invCols / srcSize.width;
double invRows3 = invRows2 / srcSize.height, invCols3 = invCols2 / srcSize.width;
MomentVec mx = { m.m00, m.m10 * invCols, m.m01 * invRows,
m.m20 * invCols2, m.m11 * invCols * invRows, m.m02 * invRows2,
m.m30 * invCols3,
m.m21 * invCols2 * invRows,
m.m12 * invCols * invRows2,
m.m03 * invRows3};
res.push_back({distTrans, mx});
}
return res;
};
std::vector<std::pair<Mat, MomentVec>> contourEstimate = calcEstimate(contours, src.size());
std::vector<std::vector<Point>> ocvContours;
std::vector<cv::Rect> ocvBboxes;
cv::Ptr<MSER> ocvMser = cv::MSER::create(delta, minArea, maxArea, maxVariation, minDiversity);
ocvMser->detectRegions(src, ocvContours, ocvBboxes);
std::vector<std::pair<Mat, MomentVec>> ocvContourEstimate = calcEstimate(ocvContours, src.size());
// brute force match by moments comparison
double overallL2Sqr = 0;
int nInliers = 0;
for (size_t i = 0; i < contourEstimate.size(); i++)
{
double minDist = std::numeric_limits<double>::max();
size_t minIdx = -1;
for (size_t j = 0; j < ocvContourEstimate.size(); j++)
{
double d = cv::norm(contourEstimate[i].second - ocvContourEstimate[j].second);
if (d < minDist)
{
minDist = d; minIdx = j;
}
}
// compare dist transforms of contours
Mat ref = ocvContourEstimate[minIdx].first;
Mat fcv = contourEstimate[i].first;
double normL2Sqr = cvtest::norm(ref, fcv, cv::NORM_L2SQR);
double normInf = cvtest::norm(ref, fcv, cv::NORM_INF);
normL2Sqr = normL2Sqr / src.size().area();
if (cvtest::debugLevel > 0)
{
Mat draw(src.rows, src.cols*2, CV_8U);
ref.copyTo(draw(Range::all(), Range(0, src.cols)));
fcv.copyTo(draw(Range::all(), Range(src.cols, src.cols*2)));
cv::putText(draw, cv::format("dM: %f L2^2: %f Inf: %f",minDist, normL2Sqr, normInf), Point(0, src.rows),
cv::FONT_HERSHEY_COMPLEX, 1, Scalar::all(128));
cv::imwrite(cv::format("dist_n%d_c%03d_r%03d.png", numNeighbors, (int)i, (int)minIdx), draw);
}
if (normInf < 50.0)
{
overallL2Sqr += normL2Sqr;
nInliers++;
}
}
double overallL2 = std::sqrt(overallL2Sqr);
EXPECT_LT(std::sqrt(overallL2), 11.45);
double ratioInliers = double(nInliers) / contourEstimate.size();
EXPECT_GT(ratioInliers, 0.363);
}
INSTANTIATE_TEST_CASE_P(FastCV_Extension, MSERTest,
::testing::Combine(::testing::Values( // useBboxes useContourData
std::tuple<bool, bool> { true, false},
std::tuple<bool, bool> {false, false},
std::tuple<bool, bool> { true, true}),
::testing::Values(4, 8), // numNeighbors
::testing::Values("cv/shared/baboon.png", "cv/mser/puzzle.png")
)
);
}} // namespaces opencv_test, ::