Skip to content

Commit a96ffa3

Browse files
committed
テーブル作成計画を全探索するように変更
1 parent 1b4d408 commit a96ffa3

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

modules/ximgproc/src/sparse_table_morphology.cpp

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <iostream>
99
#include <stack>
1010
#include <algorithm>
11-
#include <queue>
1211

1312
namespace cv {
1413
namespace ximgproc {
@@ -134,40 +133,46 @@ std::vector<StStep> planSparseTableConstruction(std::vector<std::vector<bool>> s
134133
* https://link.springer.com/article/10.1007/BF01758762
135134
*
136135
*/
137-
auto comparePos = [](Point lp, Point rp) {
138-
int diffx = lp.x - rp.x;
139-
int diffy = lp.y - rp.y;
140-
int diff = diffx + diffy;
141-
if (diff != 0) return diff < 0;
142-
if (diffx != 0) return diffx < 0;
143-
return diffy < 0;
144-
};
145-
std::priority_queue<Point, std::vector<Point>, decltype(comparePos)> points{ comparePos };
136+
std::vector<Point> pos;
146137
sparseMatMap[0][0] = true;
147138
for (int r = 0; r < sparseMatMap.size(); r++)
148139
for (int c = 0; c < sparseMatMap[r].size(); c++)
149-
if (sparseMatMap[r][c]) points.push(Point(c, r));
150-
140+
if (sparseMatMap[r][c]) pos.emplace_back(c, r);
151141
std::vector<StStep> plan;
152-
while (points.size() >= 2)
142+
while(pos.size() > 1)
153143
{
154-
Point p1 = points.top();
155-
points.pop();
156-
Point p2 = points.top();
157-
points.pop();
158-
int newX = min(p1.x, p2.x);
159-
int newY = min(p1.y, p2.y);
160-
if (!sparseMatMap[newY][newX])
144+
int maxCost = -1;
145+
int maxI = 0;
146+
int maxJ = 0;
147+
int maxX = 0;
148+
int maxY = 0;
149+
for (int i = 0; i < pos.size(); i++)
161150
{
162-
sparseMatMap[newY][newX] = true;
163-
points.push(Point(newX, newY));
151+
for (int j = i + 1; j < pos.size(); j++)
152+
{
153+
int _x = min(pos[i].x, pos[j].x);
154+
int _y = min(pos[i].y, pos[j].y);
155+
int cost = _x + _y;
156+
if (maxCost < cost)
157+
{
158+
maxCost = cost;
159+
maxI = i;
160+
maxJ = j;
161+
maxX = _x;
162+
maxY = _y;
163+
}
164+
}
164165
}
165-
166-
for (int col = p1.x - 1; col >= newX; col--) plan.emplace_back(p1.y, col, Dim::Col);
167-
for (int row = p1.y - 1; row >= newY; row--) plan.emplace_back(row, p1.x, Dim::Row);
168-
for (int col = p2.x - 1; col >= newX; col--) plan.emplace_back(p2.y, col, Dim::Col);
169-
for (int row = p2.y - 1; row >= newY; row--) plan.emplace_back(row, p2.x, Dim::Row);
166+
for (int col = pos[maxI].x - 1; col >= maxX; col--) plan.emplace_back(pos[maxI].y, col, Dim::Col);
167+
for (int row = pos[maxI].y - 1; row >= maxY; row--) plan.emplace_back(row, maxX, Dim::Row);
168+
for (int col = pos[maxJ].x - 1; col >= maxX; col--) plan.emplace_back(pos[maxJ].y, col, Dim::Col);
169+
for (int row = pos[maxJ].y - 1; row >= maxY; row--) plan.emplace_back(row, maxX, Dim::Row);
170+
171+
pos[maxI] = Point(maxX, maxY);
172+
std::swap(pos[maxJ], pos[pos.size() - 1]);
173+
pos.pop_back();
170174
}
175+
171176
std::reverse(plan.begin(), plan.end());
172177
return plan;
173178
}

modules/ximgproc/test/test_sparse_table_morphology.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace stMorph {
1212
TEST(ximgproc_SparseTableMorph, compare_with_original_erode)
1313
{
1414
// preparation
15-
int kRadius = 10;
15+
int kRadius = 15;
1616
//Size sz(200, 150);
1717
Size sz = szVGA;
1818
int type = CV_8UC3;
@@ -23,7 +23,7 @@ TEST(ximgproc_SparseTableMorph, compare_with_original_erode)
2323
Mat expected(sz, type);
2424
Mat actual(sz, type);
2525
Size kernelSize(kSize, kSize);
26-
Mat kernel = getStructuringElement(cv::MorphShapes::MORPH_ELLIPSE, kernelSize, Point(kRadius, kRadius));
26+
Mat kernel = getStructuringElement(cv::MorphShapes::MORPH_RECT, kernelSize, Point(kRadius, kRadius));
2727

2828
src.setTo(240);
2929
putText(src, "A", Point(sz.height / 5 * 1, sz.height / 20 * 15), HersheyFonts::FONT_HERSHEY_TRIPLEX, 10, Scalar(255, 40, 40), 30, LineTypes::FILLED);
@@ -71,9 +71,6 @@ TEST(ximgproc_SparseTableMorph, compare_with_original_erode)
7171

7272
TEST(develop, POW2RECT_COVERING)
7373
{
74-
int kSize = 11;
75-
Size kernelSize(kSize, kSize);
76-
//Mat kernel = getStructuringElement(MorphShapes::MORPH_ELLIPSE, kernelSize);
7774
uchar ary[] {
7875
0, 1, 0, 1, 0, 1, 0, 1,
7976
1, 1, 0, 1, 1, 1, 1, 1,
@@ -87,7 +84,7 @@ TEST(develop, POW2RECT_COVERING)
8784
Mat kernel(8, 8, CV_8UC1, ary);
8885
std::vector<Rect> rects = ximgproc::stMorph::genPow2RectsToCoverKernel(kernel);
8986

90-
int rate = 40;
87+
int rate = 20;
9188
resize(kernel * 255, kernel, Size(), rate, rate, InterpolationFlags::INTER_NEAREST);
9289
cvtColor(kernel, kernel, cv::COLOR_GRAY2BGR);
9390
Scalar color[20]{
@@ -121,9 +118,9 @@ TEST(develop, PLANNING)
121118
std::vector<bool>{0,0,0,0,0,0,1,0},
122119
std::vector<bool>{0,0,0,0,0,1,0,0},
123120
std::vector<bool>{0,0,0,0,1,0,0,0},
124-
std::vector<bool>{0,0,0,0,0,1,0,0},
121+
std::vector<bool>{0,0,0,1,0,0,0,0},
125122
std::vector<bool>{0,0,1,0,0,0,0,0},
126-
std::vector<bool>{0,1,0,0,1,0,0,0},
123+
std::vector<bool>{0,1,0,0,0,0,0,0},
127124
std::vector<bool>{1,0,0,0,0,0,0,0},
128125
};
129126
auto res = ximgproc::stMorph::planSparseTableConstruction(map);

0 commit comments

Comments
 (0)