Skip to content

Commit 6f39016

Browse files
committed
スパーステーブル作成計画の近似手法実装
1 parent 32f11f4 commit 6f39016

File tree

2 files changed

+45
-37
lines changed

2 files changed

+45
-37
lines changed

modules/ximgproc/src/sparse_table_morphology.cpp

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <iostream>
4040
#include <stack>
4141
#include <algorithm>
42+
#include <queue>
4243

4344
namespace cv {
4445
namespace ximgproc {
@@ -195,36 +196,43 @@ std::vector<Rect> genPow2RectsToCoverKernel(InputArray _kernel)
195196
*/
196197
static std::vector<StStep> makePlan(std::vector<std::vector<bool>> sparseMatMap)
197198
{
198-
// todo: implement the reference paper 2-approximation algorithm
199-
200-
std::vector<StStep> ans;
201-
std::vector<std::vector<bool>> visitedMap(sparseMatMap.size(), std::vector<bool>(sparseMatMap[0].size(), false));
202-
visitedMap[0][0] = true;
203-
for (int row = 0; row < sparseMatMap.size(); row++)
199+
auto comparePos = [](Point lp, Point rp) {
200+
int diffx = lp.x - rp.x;
201+
int diffy = lp.y - rp.y;
202+
int diff = diffx + diffy;
203+
if (diff != 0) return diff < 0;
204+
if (diffx != 0) return diffx < 0;
205+
return diffy < 0;
206+
};
207+
std::priority_queue<Point, std::vector<Point>, decltype(comparePos)> points{ comparePos };
208+
for (int r = 0; r < sparseMatMap.size(); r++)
204209
{
205-
for (int col = 0; col < sparseMatMap[row].size(); col++)
210+
for (int c = 0; c < sparseMatMap[r].size(); c++)
206211
{
207-
if (sparseMatMap[row][col])
208-
{
209-
for (int c = 0; c <= col; c++)
210-
{
211-
if (!visitedMap[0][c])
212-
{
213-
visitedMap[0][c] = true;
214-
ans.emplace_back(0, c - 1, Dim::Col);
215-
}
216-
}
217-
for (int r = 0; r <= row; r++)
218-
{
219-
if (!visitedMap[r][col])
220-
{
221-
visitedMap[r][col] = true;
222-
ans.emplace_back(r - 1, col, Dim::Row);
223-
}
224-
}
225-
}
212+
if (sparseMatMap[r][c]) points.push(Point(c, r));
226213
}
227214
}
215+
216+
std::vector<StStep> ans;
217+
while (points.size() >= 2)
218+
{
219+
Point p1 = points.top();
220+
points.pop();
221+
Point p2 = points.top();
222+
points.pop();
223+
int newX = min(p1.x, p2.x);
224+
int newY = min(p1.y, p2.y);
225+
points.push(Point(newX, newY));
226+
227+
for (int col = p1.x - 1; col >= newX; col--) ans.emplace_back(p1.y, col, Dim::Col);
228+
for (int row = p1.y - 1; row >= newY; row--) ans.emplace_back(row, p1.x, Dim::Row);
229+
for (int col = p2.x - 1; col >= newX; col--) ans.emplace_back(p2.y, col, Dim::Col);
230+
for (int row = p2.y - 1; row >= newY; row--) ans.emplace_back(row, p2.x, Dim::Row);
231+
}
232+
Point p1 = points.top();
233+
for (int col = p1.x - 1; col >= 0; col--) ans.emplace_back(p1.y, col, Dim::Col);
234+
for (int row = p1.y - 1; row >= 0; row--) ans.emplace_back(row, 0, Dim::Row);
235+
std::reverse(ans.begin(), ans.end());
228236
return ans;
229237
}
230238

modules/ximgproc/test/test_sparse_table_morphology.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ TEST(ximgproc_SparseTableMorph, compare_with_original_erode)
2525
Mat kernel = getStructuringElement(cv::MorphShapes::MORPH_ELLIPSE, kernelSize, Point(kRadius, kRadius));
2626

2727
src.setTo(240);
28-
putText(src, "A", Point(sz.height / 5 * 1, sz.height / 20 * 15), HersheyFonts::FONT_HERSHEY_TRIPLEX, 10, Scalar(250, 40, 40), 30, LineTypes::FILLED);
29-
putText(src, "B", Point(sz.height / 5 * 2, sz.height / 20 * 16), HersheyFonts::FONT_HERSHEY_TRIPLEX, 10, Scalar(20, 230, 0), 30, LineTypes::FILLED);
28+
putText(src, "A", Point(sz.height / 5 * 1, sz.height / 20 * 15), HersheyFonts::FONT_HERSHEY_TRIPLEX, 10, Scalar(255, 40, 40), 30, LineTypes::FILLED);
29+
putText(src, "B", Point(sz.height / 5 * 2, sz.height / 20 * 16), HersheyFonts::FONT_HERSHEY_TRIPLEX, 10, Scalar(20, 255, 0), 30, LineTypes::FILLED);
3030
putText(src, "C", Point(sz.height / 5 * 3, sz.height / 20 * 17), HersheyFonts::FONT_HERSHEY_TRIPLEX, 10, Scalar(10, 10, 255), 30, LineTypes::FILLED);
3131

3232
cv::TickMeter timer;
@@ -196,7 +196,7 @@ TEST(develop, POW2RECT_COVERING)
196196

197197
// visualize sparse table
198198
std::vector<std::vector<Mat>> st = std::get<0>(ret);
199-
int cellSize = 10;
199+
int cellSize = 16;
200200
Mat concatSt;
201201
std::vector<Mat> hconMat(st.size(), Mat());
202202
for (int row = 0; row < st.size(); row++) for (int col = 0; col < st[row].size(); col++)
@@ -230,14 +230,14 @@ TEST(develop, POW2RECT_COVERING)
230230
for (int i = 0; i < rects.size(); i++)
231231
{
232232
Rect rect = rects[i];
233-
Point lt((rect.x ) * rate + i, (rect.y ) * rate + i);
234-
Point lb((rect.x ) * rate + i, (rect.y + (1 << rect.height)) * rate - 15 + i);
235-
Point rb((rect.x + (1 << rect.width)) * rate - 15 + i, (rect.y + (1 << rect.height)) * rate - 15 + i);
236-
Point rt((rect.x + (1 << rect.width)) * rate - 15 + i, (rect.y ) * rate + i);
237-
cv::line(kernel, lt, lb, color[i], 2);
238-
cv::line(kernel, lb, rb, color[i], 2);
239-
cv::line(kernel, rb, rt, color[i], 2);
240-
cv::line(kernel, rt, lt, color[i], 2);
233+
Point lt((rect.x ) * rate + i % 11, (rect.y ) * rate + i % 11);
234+
Point lb((rect.x ) * rate + i % 11, (rect.y + (1 << rect.height)) * rate - 11 + i % 11);
235+
Point rb((rect.x + (1 << rect.width)) * rate - 11 + i % 11, (rect.y + (1 << rect.height)) * rate - 11 + i % 11);
236+
Point rt((rect.x + (1 << rect.width)) * rate - 11 + i % 11, (rect.y ) * rate + i % 11);
237+
cv::line(kernel, lt, lb, color[i % 20], 2);
238+
cv::line(kernel, lb, rb, color[i % 20], 2);
239+
cv::line(kernel, rb, rt, color[i % 20], 2);
240+
cv::line(kernel, rt, lt, color[i % 20], 2);
241241
}
242242
imshow("kernel", kernel);
243243

0 commit comments

Comments
 (0)