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 using namespace std; 46 using namespace testing; 47 using namespace perf; 48 49 DEF_PARAM_TEST_1(Image, string); 50 51 struct GreedyLabeling 52 { 53 struct dot 54 { 55 int x; 56 int y; 57 58 static dot make(int i, int j) 59 { 60 dot d; d.x = i; d.y = j; 61 return d; 62 } 63 }; 64 65 struct InInterval 66 { 67 InInterval(const int& _lo, const int& _hi) : lo(-_lo), hi(_hi) {} 68 const int lo, hi; 69 70 bool operator() (const unsigned char a, const unsigned char b) const 71 { 72 int d = a - b; 73 return lo <= d && d <= hi; 74 } 75 76 private: 77 InInterval& operator=(const InInterval&); 78 79 80 }; 81 82 GreedyLabeling(cv::Mat img) 83 : image(img), _labels(image.size(), CV_32SC1, cv::Scalar::all(-1)) {stack = new dot[image.cols * image.rows];} 84 85 ~GreedyLabeling(){delete[] stack;} 86 87 void operator() (cv::Mat labels) const 88 { 89 labels.setTo(cv::Scalar::all(-1)); 90 InInterval inInt(0, 2); 91 int cc = -1; 92 93 int* dist_labels = (int*)labels.data; 94 int pitch = static_cast<int>(labels.step1()); 95 96 unsigned char* source = (unsigned char*)image.data; 97 int width = image.cols; 98 int height = image.rows; 99 100 for (int j = 0; j < image.rows; ++j) 101 for (int i = 0; i < image.cols; ++i) 102 { 103 if (dist_labels[j * pitch + i] != -1) continue; 104 105 dot* top = stack; 106 dot p = dot::make(i, j); 107 cc++; 108 109 dist_labels[j * pitch + i] = cc; 110 111 while (top >= stack) 112 { 113 int* dl = &dist_labels[p.y * pitch + p.x]; 114 unsigned char* sp = &source[p.y * image.step1() + p.x]; 115 116 dl[0] = cc; 117 118 //right 119 if( p.x < (width - 1) && dl[ +1] == -1 && inInt(sp[0], sp[+1])) 120 *top++ = dot::make(p.x + 1, p.y); 121 122 //left 123 if( p.x > 0 && dl[-1] == -1 && inInt(sp[0], sp[-1])) 124 *top++ = dot::make(p.x - 1, p.y); 125 126 //bottom 127 if( p.y < (height - 1) && dl[+pitch] == -1 && inInt(sp[0], sp[+image.step1()])) 128 *top++ = dot::make(p.x, p.y + 1); 129 130 //top 131 if( p.y > 0 && dl[-pitch] == -1 && inInt(sp[0], sp[-static_cast<int>(image.step1())])) 132 *top++ = dot::make(p.x, p.y - 1); 133 134 p = *--top; 135 } 136 } 137 } 138 139 cv::Mat image; 140 cv::Mat _labels; 141 dot* stack; 142 }; 143 144 PERF_TEST_P(Image, DISABLED_Labeling_ConnectivityMask, 145 Values<string>("gpu/labeling/aloe-disp.png")) 146 { 147 declare.time(1.0); 148 149 const cv::Mat image = readImage(GetParam(), cv::IMREAD_GRAYSCALE); 150 ASSERT_FALSE(image.empty()); 151 152 if (PERF_RUN_CUDA()) 153 { 154 cv::cuda::GpuMat d_image(image); 155 cv::cuda::GpuMat mask; 156 157 TEST_CYCLE() cv::cuda::connectivityMask(d_image, mask, cv::Scalar::all(0), cv::Scalar::all(2)); 158 159 CUDA_SANITY_CHECK(mask); 160 } 161 else 162 { 163 FAIL_NO_CPU(); 164 } 165 } 166 167 PERF_TEST_P(Image, DISABLED_Labeling_ConnectedComponents, 168 Values<string>("gpu/labeling/aloe-disp.png")) 169 { 170 declare.time(1.0); 171 172 const cv::Mat image = readImage(GetParam(), cv::IMREAD_GRAYSCALE); 173 ASSERT_FALSE(image.empty()); 174 175 if (PERF_RUN_CUDA()) 176 { 177 cv::cuda::GpuMat d_mask; 178 cv::cuda::connectivityMask(cv::cuda::GpuMat(image), d_mask, cv::Scalar::all(0), cv::Scalar::all(2)); 179 180 cv::cuda::GpuMat components; 181 182 TEST_CYCLE() cv::cuda::labelComponents(d_mask, components); 183 184 CUDA_SANITY_CHECK(components); 185 } 186 else 187 { 188 GreedyLabeling host(image); 189 190 TEST_CYCLE() host(host._labels); 191 192 cv::Mat components = host._labels; 193 CPU_SANITY_CHECK(components); 194 } 195 } 196