1 /*M/////////////////////////////////////////////////////////////////////////////////////// 2 // 3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. 4 // 5 // By downloading, copying, installing or using the software you agree to this license. 6 // If you do not agree to this license, do not download, install, 7 // copy or use the software. 8 // 9 // 10 // License Agreement 11 // For Open Source Computer Vision Library 12 // 13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved. 14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved. 15 // Third party copyrights are property of their respective owners. 16 // 17 // Redistribution and use in source and binary forms, with or without modification, 18 // are permitted provided that the following conditions are met: 19 // 20 // * Redistribution's of source code must retain the above copyright notice, 21 // this list of conditions and the following disclaimer. 22 // 23 // * Redistribution's in binary form must reproduce the above copyright notice, 24 // this list of conditions and the following disclaimer in the documentation 25 // and/or other materials provided with the distribution. 26 // 27 // * The name of the copyright holders may not be used to endorse or promote products 28 // derived from this software without specific prior written permission. 29 // 30 // This software is provided by the copyright holders and contributors "as is" and 31 // any express or implied warranties, including, but not limited to, the implied 32 // warranties of merchantability and fitness for a particular purpose are disclaimed. 33 // In no event shall the Intel Corporation or contributors be liable for any direct, 34 // indirect, incidental, special, exemplary, or consequential damages 35 // (including, but not limited to, procurement of substitute goods or services; 36 // loss of use, data, or profits; or business interruption) however caused 37 // and on any theory of liability, whether in contract, strict liability, 38 // or tort (including negligence or otherwise) arising in any way out of 39 // the use of this software, even if advised of the possibility of such damage. 40 // 41 //M*/ 42 43 #include "perf_precomp.hpp" 44 45 namespace opencv_test { namespace { 46 47 DEF_PARAM_TEST_1(Image, string); 48 49 struct GreedyLabeling 50 { 51 struct dot 52 { 53 int x; 54 int y; 55 makeopencv_test::__anon7316d0e30111::GreedyLabeling::dot56 static dot make(int i, int j) 57 { 58 dot d; d.x = i; d.y = j; 59 return d; 60 } 61 }; 62 63 struct InInterval 64 { InIntervalopencv_test::__anon7316d0e30111::GreedyLabeling::InInterval65 InInterval(const int& _lo, const int& _hi) : lo(-_lo), hi(_hi) {} 66 const int lo, hi; 67 operator ()opencv_test::__anon7316d0e30111::GreedyLabeling::InInterval68 bool operator() (const unsigned char a, const unsigned char b) const 69 { 70 int d = a - b; 71 return lo <= d && d <= hi; 72 } 73 74 private: 75 InInterval& operator=(const InInterval&); 76 77 78 }; 79 GreedyLabelingopencv_test::__anon7316d0e30111::GreedyLabeling80 GreedyLabeling(cv::Mat img) 81 : image(img), _labels(image.size(), CV_32SC1, cv::Scalar::all(-1)) {stack = new dot[image.cols * image.rows];} 82 ~GreedyLabelingopencv_test::__anon7316d0e30111::GreedyLabeling83 ~GreedyLabeling(){delete[] stack;} 84 operator ()opencv_test::__anon7316d0e30111::GreedyLabeling85 void operator() (cv::Mat labels) const 86 { 87 labels.setTo(cv::Scalar::all(-1)); 88 InInterval inInt(0, 2); 89 int cc = -1; 90 91 int* dist_labels = (int*)labels.data; 92 int pitch = static_cast<int>(labels.step1()); 93 94 unsigned char* source = (unsigned char*)image.data; 95 int width = image.cols; 96 int height = image.rows; 97 98 for (int j = 0; j < image.rows; ++j) 99 for (int i = 0; i < image.cols; ++i) 100 { 101 if (dist_labels[j * pitch + i] != -1) continue; 102 103 dot* top = stack; 104 dot p = dot::make(i, j); 105 cc++; 106 107 dist_labels[j * pitch + i] = cc; 108 109 while (top >= stack) 110 { 111 int* dl = &dist_labels[p.y * pitch + p.x]; 112 unsigned char* sp = &source[p.y * image.step1() + p.x]; 113 114 dl[0] = cc; 115 116 //right 117 if( p.x < (width - 1) && dl[ +1] == -1 && inInt(sp[0], sp[+1])) 118 *top++ = dot::make(p.x + 1, p.y); 119 120 //left 121 if( p.x > 0 && dl[-1] == -1 && inInt(sp[0], sp[-1])) 122 *top++ = dot::make(p.x - 1, p.y); 123 124 //bottom 125 if( p.y < (height - 1) && dl[+pitch] == -1 && inInt(sp[0], sp[+image.step1()])) 126 *top++ = dot::make(p.x, p.y + 1); 127 128 //top 129 if( p.y > 0 && dl[-pitch] == -1 && inInt(sp[0], sp[-static_cast<int>(image.step1())])) 130 *top++ = dot::make(p.x, p.y - 1); 131 132 p = *--top; 133 } 134 } 135 } 136 137 cv::Mat image; 138 cv::Mat _labels; 139 dot* stack; 140 }; 141 142 PERF_TEST_P(Image, DISABLED_Labeling_ConnectivityMask, 143 Values<string>("gpu/labeling/aloe-disp.png")) 144 { 145 declare.time(1.0); 146 147 const cv::Mat image = readImage(GetParam(), cv::IMREAD_GRAYSCALE); 148 ASSERT_FALSE(image.empty()); 149 150 if (PERF_RUN_CUDA()) 151 { 152 cv::cuda::GpuMat d_image(image); 153 cv::cuda::GpuMat mask; 154 155 TEST_CYCLE() cv::cuda::connectivityMask(d_image, mask, cv::Scalar::all(0), cv::Scalar::all(2)); 156 157 CUDA_SANITY_CHECK(mask); 158 } 159 else 160 { 161 FAIL_NO_CPU(); 162 } 163 } 164 165 PERF_TEST_P(Image, DISABLED_Labeling_ConnectedComponents, 166 Values<string>("gpu/labeling/aloe-disp.png")) 167 { 168 declare.time(1.0); 169 170 const cv::Mat image = readImage(GetParam(), cv::IMREAD_GRAYSCALE); 171 ASSERT_FALSE(image.empty()); 172 173 if (PERF_RUN_CUDA()) 174 { 175 cv::cuda::GpuMat d_mask; 176 cv::cuda::connectivityMask(cv::cuda::GpuMat(image), d_mask, cv::Scalar::all(0), cv::Scalar::all(2)); 177 178 cv::cuda::GpuMat components; 179 180 TEST_CYCLE() cv::cuda::labelComponents(d_mask, components); 181 182 CUDA_SANITY_CHECK(components); 183 } 184 else 185 { 186 GreedyLabeling host(image); 187 188 TEST_CYCLE() host(host._labels); 189 190 cv::Mat components = host._labels; 191 CPU_SANITY_CHECK(components); 192 } 193 } 194 195 }} // namespace 196