From e8e7b94443252ede423f069626d2701f3822ddfd Mon Sep 17 00:00:00 2001 From: "marina.kolpakova" Date: Tue, 7 Aug 2012 14:25:27 +0400 Subject: [PATCH 1/3] test fix --- modules/gpu/test/test_labeling.cpp | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/modules/gpu/test/test_labeling.cpp b/modules/gpu/test/test_labeling.cpp index 6ba0ef548..b8c654ec2 100644 --- a/modules/gpu/test/test_labeling.cpp +++ b/modules/gpu/test/test_labeling.cpp @@ -72,22 +72,15 @@ TEST_P(Labeling, ConnectedComponents) cv::gpu::connectivityMask(cv::gpu::GpuMat(image), mask, cv::Scalar::all(0), cv::Scalar::all(2)); - cv::gpu::labelComponents(mask, components); + ASSERT_NO_THROW(cv::gpu::labelComponents(mask, components)); - // std::cout << cv::Mat(components) << std::endl; + // for debug // cv::imshow("test", image); // cv::waitKey(0); - - // for(int i = 0; i + 32 < image.rows; i += 32) - // for(int j = 0; j + 32 < image.cols; j += 32) - // cv::rectangle(image, cv::Rect(j, i, 32, 32) , CV_RGB(255, 255, 255)); - - cv::imshow("test", image); - cv::waitKey(0); - cv::imshow("test", cv::Mat(mask) * 10); - cv::waitKey(0); - cv::imshow("test", cv::Mat(components) * 2); - cv::waitKey(0); + // cv::imshow("test", cv::Mat(mask) * 10); + // cv::waitKey(0); + // cv::imshow("test", cv::Mat(components) * 2); + // cv::waitKey(0); } INSTANTIATE_TEST_CASE_P(ConnectedComponents, Labeling, ALL_DEVICES); \ No newline at end of file From 8e274c886c9c913effb0602c8215b09a8416dbd1 Mon Sep 17 00:00:00 2001 From: "marina.kolpakova" Date: Tue, 7 Aug 2012 17:25:29 +0400 Subject: [PATCH 2/3] Greedy Labeling implementation in correctness test --- modules/gpu/test/test_labeling.cpp | 117 ++++++++++++++++++++++++++++- 1 file changed, 116 insertions(+), 1 deletion(-) diff --git a/modules/gpu/test/test_labeling.cpp b/modules/gpu/test/test_labeling.cpp index b8c654ec2..69c076827 100644 --- a/modules/gpu/test/test_labeling.cpp +++ b/modules/gpu/test/test_labeling.cpp @@ -43,6 +43,106 @@ #include #include +namespace { + + struct GreedyLabeling + { + struct dot + { + int x; + int y; + + static dot make(int i, int j) + { + dot d; d.x = i; d.y = j; + return d; + } + }; + + struct InInterval + { + InInterval(const int& _lo, const int& _hi) : lo(-_lo), hi(_hi) {}; + const int lo, hi; + + bool operator() (const unsigned char a, const unsigned char b) const + { + int d = a - b; + return lo <= d && d <= hi; + } + }; + + GreedyLabeling(cv::Mat img) + : image(img), _labels(image.cols, image.rows, CV_32SC1, cv::Scalar::all(-1)) {} + + void operator() (cv::Mat labels) const + { + InInterval inInt(0, 2); + dot* stack = new dot[image.cols * image.rows]; + + int cc = -1; + + int* dist_labels = (int*)labels.data; + int pitch = labels.step1(); + + unsigned char* source = (unsigned char*)image.data; + int width = image.cols; + int height = image.rows; + + for (int j = 0; j < image.rows; ++j) + for(int i = 0; i < image.cols; ++i) + { + if (dist_labels[j * pitch + i] != -1) continue; + + dot* top = stack; + dot p = dot::make(i, j); + cc++; + + dist_labels[j * pitch + i] = cc; + + while (top >= stack) + { + int* dl = &dist_labels[p.y * pitch + p.x]; + unsigned char* sp = &source[p.y * image.step1() + p.x]; + + //right + if( p.x < (width - 1) && dl[ +1] == -1 && inInt(sp[0], sp[+1])) + { + dl[+1] = cc; + *top++ = dot::make(p.x + 1, p.y); + } + + //left + if( p.x > 0 && dl[-1] == -1 && inInt(sp[0], sp[-1])) + { + dl[-1] = cc; + *top++ = dot::make(p.x - 1, p.y); + } + + //bottom + if( p.y < (height - 1) && dl[+pitch] == -1 && inInt(sp[0], sp[+pitch])) + { + dl[+pitch] = cc; + *top++ = dot::make(p.x, p.y + 1); + } + + //top + if( p.y > 0 && dl[-pitch] == -1 && inInt(sp[0], sp[-pitch])) + { + dl[-pitch] = cc; + *top++ = dot::make(p.x, p.y - 1); + } + + p = *--top; + } + } + delete[] stack; + } + + cv::Mat image; + cv::Mat _labels; + }; +} + struct Labeling : testing::TestWithParam { cv::gpu::DeviceInfo devInfo; @@ -64,6 +164,11 @@ TEST_P(Labeling, ConnectedComponents) cv::Mat image; cvtColor(loat_image(), image, CV_BGR2GRAY); + ASSERT_TRUE(image.type() == CV_8UC1); + + GreedyLabeling host(image); + host(host._labels); + cv::gpu::GpuMat mask; mask.create(image.rows, image.cols, CV_8UC1); @@ -74,11 +179,21 @@ TEST_P(Labeling, ConnectedComponents) ASSERT_NO_THROW(cv::gpu::labelComponents(mask, components)); + // for (int j = 0; j + 32 < components.rows; j += 32) + // for (int i = 0; i + 32 < components.cols; i += 32) + // { + // std::cout << "Tile: " << i << " " << j << std::endl; + // std::cout << cv::Mat(host._labels, cv::Rect(i,j,32,32)) << std::endl; + // std::cout << cv::Mat(cv::Mat(components), cv::Rect(i,j,32,32)) << std::endl; + // } + // for debug // cv::imshow("test", image); // cv::waitKey(0); - // cv::imshow("test", cv::Mat(mask) * 10); + // cv::imshow("test", host._labels * 50); // cv::waitKey(0); + // // cv::imshow("test", cv::Mat(mask) * 10); + // // cv::waitKey(0); // cv::imshow("test", cv::Mat(components) * 2); // cv::waitKey(0); } From fc8c65ff157ce754e208c9bcb9873b0f910f198a Mon Sep 17 00:00:00 2001 From: "marina.kolpakova" Date: Tue, 7 Aug 2012 17:27:02 +0400 Subject: [PATCH 3/3] HAVE_CUDA macro --- modules/gpu/test/test_labeling.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/modules/gpu/test/test_labeling.cpp b/modules/gpu/test/test_labeling.cpp index 69c076827..bba26ad14 100644 --- a/modules/gpu/test/test_labeling.cpp +++ b/modules/gpu/test/test_labeling.cpp @@ -43,6 +43,8 @@ #include #include +#ifdef HAVE_CUDA + namespace { struct GreedyLabeling @@ -198,4 +200,6 @@ TEST_P(Labeling, ConnectedComponents) // cv::waitKey(0); } -INSTANTIATE_TEST_CASE_P(ConnectedComponents, Labeling, ALL_DEVICES); \ No newline at end of file +INSTANTIATE_TEST_CASE_P(ConnectedComponents, Labeling, ALL_DEVICES); + +#endif \ No newline at end of file