test_connectedcomponents.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  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. #include "test_precomp.hpp"
  43. namespace opencv_test {
  44. namespace {
  45. class CV_ConnectedComponentsTest : public cvtest::BaseTest
  46. {
  47. public:
  48. CV_ConnectedComponentsTest();
  49. ~CV_ConnectedComponentsTest();
  50. protected:
  51. void run(int);
  52. };
  53. CV_ConnectedComponentsTest::CV_ConnectedComponentsTest() {}
  54. CV_ConnectedComponentsTest::~CV_ConnectedComponentsTest() {}
  55. // This function force a row major order for the labels
  56. void normalizeLabels(Mat1i& imgLabels, int iNumLabels) {
  57. vector<int> vecNewLabels(iNumLabels + 1, 0);
  58. int iMaxNewLabel = 0;
  59. for (int r = 0; r < imgLabels.rows; ++r) {
  60. for (int c = 0; c < imgLabels.cols; ++c) {
  61. int iCurLabel = imgLabels(r, c);
  62. if (iCurLabel > 0) {
  63. if (vecNewLabels[iCurLabel] == 0) {
  64. vecNewLabels[iCurLabel] = ++iMaxNewLabel;
  65. }
  66. imgLabels(r, c) = vecNewLabels[iCurLabel];
  67. }
  68. }
  69. }
  70. }
  71. void CV_ConnectedComponentsTest::run(int /* start_from */)
  72. {
  73. int ccltype[] = { cv::CCL_DEFAULT, cv::CCL_WU, cv::CCL_GRANA, cv::CCL_BOLELLI, cv::CCL_SAUF, cv::CCL_BBDT, cv::CCL_SPAGHETTI };
  74. string exp_path = string(ts->get_data_path()) + "connectedcomponents/ccomp_exp.png";
  75. Mat exp = imread(exp_path, 0);
  76. Mat orig = imread(string(ts->get_data_path()) + "connectedcomponents/concentric_circles.png", 0);
  77. if (orig.empty())
  78. {
  79. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  80. return;
  81. }
  82. Mat bw = orig > 128;
  83. for (uint cclt = 0; cclt < sizeof(ccltype) / sizeof(int); ++cclt)
  84. {
  85. Mat1i labelImage;
  86. int nLabels = connectedComponents(bw, labelImage, 8, CV_32S, ccltype[cclt]);
  87. normalizeLabels(labelImage, nLabels);
  88. // Validate test results
  89. for (int r = 0; r < labelImage.rows; ++r) {
  90. for (int c = 0; c < labelImage.cols; ++c) {
  91. int l = labelImage.at<int>(r, c);
  92. bool pass = l >= 0 && l <= nLabels;
  93. if (!pass) {
  94. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
  95. return;
  96. }
  97. }
  98. }
  99. if (exp.empty() || orig.size() != exp.size())
  100. {
  101. imwrite(exp_path, labelImage);
  102. exp = labelImage;
  103. }
  104. if (0 != cvtest::norm(labelImage > 0, exp > 0, NORM_INF))
  105. {
  106. ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
  107. return;
  108. }
  109. if (nLabels != cvtest::norm(labelImage, NORM_INF) + 1)
  110. {
  111. ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
  112. return;
  113. }
  114. }
  115. ts->set_failed_test_info(cvtest::TS::OK);
  116. }
  117. TEST(Imgproc_ConnectedComponents, regression) { CV_ConnectedComponentsTest test; test.safe_run(); }
  118. TEST(Imgproc_ConnectedComponents, grana_buffer_overflow)
  119. {
  120. cv::Mat darkMask;
  121. darkMask.create(31, 87, CV_8U);
  122. darkMask = 0;
  123. cv::Mat labels;
  124. cv::Mat stats;
  125. cv::Mat centroids;
  126. int nbComponents = cv::connectedComponentsWithStats(darkMask, labels, stats, centroids, 8, CV_32S, cv::CCL_GRANA);
  127. EXPECT_EQ(1, nbComponents);
  128. }
  129. static cv::Mat createCrashMat(int numThreads) {
  130. const int h = numThreads * 4 * 2 + 8;
  131. const double nParallelStripes = std::max(1, std::min(h / 2, numThreads * 4));
  132. const int w = 4;
  133. const int nstripes = cvRound(nParallelStripes <= 0 ? h : MIN(MAX(nParallelStripes, 1.), h));
  134. const cv::Range stripeRange(0, nstripes);
  135. const cv::Range wholeRange(0, h);
  136. cv::Mat m(h, w, CV_8U);
  137. m = 0;
  138. // Look for a range that starts with odd value and ends with even value
  139. cv::Range bugRange;
  140. for (int s = stripeRange.start; s < stripeRange.end; s++) {
  141. cv::Range sr(s, s + 1);
  142. cv::Range r;
  143. r.start = (int)(wholeRange.start +
  144. ((uint64)sr.start * (wholeRange.end - wholeRange.start) + nstripes / 2) / nstripes);
  145. r.end = sr.end >= nstripes ?
  146. wholeRange.end :
  147. (int)(wholeRange.start +
  148. ((uint64)sr.end * (wholeRange.end - wholeRange.start) + nstripes / 2) / nstripes);
  149. if (r.start > 0 && r.start % 2 == 1 && r.end % 2 == 0 && r.end >= r.start + 2) {
  150. bugRange = r;
  151. break;
  152. }
  153. }
  154. if (bugRange.empty()) { // Could not create a buggy range
  155. return m;
  156. }
  157. // Fill in bug Range
  158. for (int x = 1; x < w; x++) {
  159. m.at<char>(bugRange.start - 1, x) = 1;
  160. }
  161. m.at<char>(bugRange.start + 0, 0) = 1;
  162. m.at<char>(bugRange.start + 0, 1) = 1;
  163. m.at<char>(bugRange.start + 0, 3) = 1;
  164. m.at<char>(bugRange.start + 1, 1) = 1;
  165. m.at<char>(bugRange.start + 2, 1) = 1;
  166. m.at<char>(bugRange.start + 2, 3) = 1;
  167. m.at<char>(bugRange.start + 3, 0) = 1;
  168. m.at<char>(bugRange.start + 3, 1) = 1;
  169. return m;
  170. }
  171. TEST(Imgproc_ConnectedComponents, parallel_wu_labels)
  172. {
  173. cv::Mat mat = createCrashMat(cv::getNumThreads());
  174. if (mat.empty()) {
  175. return;
  176. }
  177. const int nbPixels = cv::countNonZero(mat);
  178. cv::Mat labels;
  179. cv::Mat stats;
  180. cv::Mat centroids;
  181. int nb = 0;
  182. EXPECT_NO_THROW(nb = cv::connectedComponentsWithStats(mat, labels, stats, centroids, 8, CV_32S, cv::CCL_WU));
  183. int area = 0;
  184. for (int i = 1; i < nb; ++i) {
  185. area += stats.at<int32_t>(i, cv::CC_STAT_AREA);
  186. }
  187. EXPECT_EQ(nbPixels, area);
  188. }
  189. TEST(Imgproc_ConnectedComponents, missing_background_pixels)
  190. {
  191. cv::Mat m = Mat::ones(10, 10, CV_8U);
  192. cv::Mat labels;
  193. cv::Mat stats;
  194. cv::Mat centroids;
  195. EXPECT_NO_THROW(cv::connectedComponentsWithStats(m, labels, stats, centroids, 8, CV_32S, cv::CCL_WU));
  196. EXPECT_EQ(stats.at<int32_t>(0, cv::CC_STAT_WIDTH), 0);
  197. EXPECT_EQ(stats.at<int32_t>(0, cv::CC_STAT_HEIGHT), 0);
  198. EXPECT_EQ(stats.at<int32_t>(0, cv::CC_STAT_LEFT), -1);
  199. EXPECT_TRUE(std::isnan(centroids.at<double>(0, 0)));
  200. EXPECT_TRUE(std::isnan(centroids.at<double>(0, 1)));
  201. }
  202. TEST(Imgproc_ConnectedComponents, spaghetti_bbdt_sauf_stats)
  203. {
  204. cv::Mat1b img(16, 16);
  205. img << 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  206. 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
  207. 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0,
  208. 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
  209. 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
  210. 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0,
  211. 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0,
  212. 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0,
  213. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  214. 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
  215. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1,
  216. 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1,
  217. 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1,
  218. 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1,
  219. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
  220. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1;
  221. cv::Mat1i labels;
  222. cv::Mat1i stats;
  223. cv::Mat1d centroids;
  224. int ccltype[] = { cv::CCL_WU, cv::CCL_GRANA, cv::CCL_BOLELLI, cv::CCL_SAUF, cv::CCL_BBDT, cv::CCL_SPAGHETTI };
  225. for (uint cclt = 0; cclt < sizeof(ccltype) / sizeof(int); ++cclt) {
  226. EXPECT_NO_THROW(cv::connectedComponentsWithStats(img, labels, stats, centroids, 8, CV_32S, ccltype[cclt]));
  227. EXPECT_EQ(stats(0, cv::CC_STAT_LEFT), 0);
  228. EXPECT_EQ(stats(0, cv::CC_STAT_TOP), 0);
  229. EXPECT_EQ(stats(0, cv::CC_STAT_WIDTH), 16);
  230. EXPECT_EQ(stats(0, cv::CC_STAT_HEIGHT), 15);
  231. EXPECT_EQ(stats(0, cv::CC_STAT_AREA), 144);
  232. EXPECT_EQ(stats(1, cv::CC_STAT_LEFT), 1);
  233. EXPECT_EQ(stats(1, cv::CC_STAT_TOP), 1);
  234. EXPECT_EQ(stats(1, cv::CC_STAT_WIDTH), 3);
  235. EXPECT_EQ(stats(1, cv::CC_STAT_HEIGHT), 3);
  236. EXPECT_EQ(stats(1, cv::CC_STAT_AREA), 9);
  237. EXPECT_EQ(stats(2, cv::CC_STAT_LEFT), 1);
  238. EXPECT_EQ(stats(2, cv::CC_STAT_TOP), 1);
  239. EXPECT_EQ(stats(2, cv::CC_STAT_WIDTH), 8);
  240. EXPECT_EQ(stats(2, cv::CC_STAT_HEIGHT), 7);
  241. EXPECT_EQ(stats(2, cv::CC_STAT_AREA), 40);
  242. EXPECT_EQ(stats(3, cv::CC_STAT_LEFT), 10);
  243. EXPECT_EQ(stats(3, cv::CC_STAT_TOP), 2);
  244. EXPECT_EQ(stats(3, cv::CC_STAT_WIDTH), 5);
  245. EXPECT_EQ(stats(3, cv::CC_STAT_HEIGHT), 2);
  246. EXPECT_EQ(stats(3, cv::CC_STAT_AREA), 8);
  247. EXPECT_EQ(stats(4, cv::CC_STAT_LEFT), 11);
  248. EXPECT_EQ(stats(4, cv::CC_STAT_TOP), 5);
  249. EXPECT_EQ(stats(4, cv::CC_STAT_WIDTH), 3);
  250. EXPECT_EQ(stats(4, cv::CC_STAT_HEIGHT), 3);
  251. EXPECT_EQ(stats(4, cv::CC_STAT_AREA), 9);
  252. EXPECT_EQ(stats(5, cv::CC_STAT_LEFT), 2);
  253. EXPECT_EQ(stats(5, cv::CC_STAT_TOP), 9);
  254. EXPECT_EQ(stats(5, cv::CC_STAT_WIDTH), 1);
  255. EXPECT_EQ(stats(5, cv::CC_STAT_HEIGHT), 1);
  256. EXPECT_EQ(stats(5, cv::CC_STAT_AREA), 1);
  257. EXPECT_EQ(stats(6, cv::CC_STAT_LEFT), 12);
  258. EXPECT_EQ(stats(6, cv::CC_STAT_TOP), 9);
  259. EXPECT_EQ(stats(6, cv::CC_STAT_WIDTH), 1);
  260. EXPECT_EQ(stats(6, cv::CC_STAT_HEIGHT), 1);
  261. EXPECT_EQ(stats(6, cv::CC_STAT_AREA), 1);
  262. // Labels' order could be different!
  263. if (cclt == cv::CCL_WU || cclt == cv::CCL_SAUF) {
  264. // CCL_SAUF, CCL_WU
  265. EXPECT_EQ(stats(9, cv::CC_STAT_LEFT), 1);
  266. EXPECT_EQ(stats(9, cv::CC_STAT_TOP), 11);
  267. EXPECT_EQ(stats(9, cv::CC_STAT_WIDTH), 4);
  268. EXPECT_EQ(stats(9, cv::CC_STAT_HEIGHT), 2);
  269. EXPECT_EQ(stats(9, cv::CC_STAT_AREA), 8);
  270. EXPECT_EQ(stats(7, cv::CC_STAT_LEFT), 6);
  271. EXPECT_EQ(stats(7, cv::CC_STAT_TOP), 10);
  272. EXPECT_EQ(stats(7, cv::CC_STAT_WIDTH), 4);
  273. EXPECT_EQ(stats(7, cv::CC_STAT_HEIGHT), 2);
  274. EXPECT_EQ(stats(7, cv::CC_STAT_AREA), 8);
  275. EXPECT_EQ(stats(8, cv::CC_STAT_LEFT), 0);
  276. EXPECT_EQ(stats(8, cv::CC_STAT_TOP), 10);
  277. EXPECT_EQ(stats(8, cv::CC_STAT_WIDTH), 16);
  278. EXPECT_EQ(stats(8, cv::CC_STAT_HEIGHT), 6);
  279. EXPECT_EQ(stats(8, cv::CC_STAT_AREA), 21);
  280. }
  281. else {
  282. // CCL_BBDT, CCL_GRANA, CCL_SPAGHETTI, CCL_BOLELLI
  283. EXPECT_EQ(stats(7, cv::CC_STAT_LEFT), 1);
  284. EXPECT_EQ(stats(7, cv::CC_STAT_TOP), 11);
  285. EXPECT_EQ(stats(7, cv::CC_STAT_WIDTH), 4);
  286. EXPECT_EQ(stats(7, cv::CC_STAT_HEIGHT), 2);
  287. EXPECT_EQ(stats(7, cv::CC_STAT_AREA), 8);
  288. EXPECT_EQ(stats(8, cv::CC_STAT_LEFT), 6);
  289. EXPECT_EQ(stats(8, cv::CC_STAT_TOP), 10);
  290. EXPECT_EQ(stats(8, cv::CC_STAT_WIDTH), 4);
  291. EXPECT_EQ(stats(8, cv::CC_STAT_HEIGHT), 2);
  292. EXPECT_EQ(stats(8, cv::CC_STAT_AREA), 8);
  293. EXPECT_EQ(stats(9, cv::CC_STAT_LEFT), 0);
  294. EXPECT_EQ(stats(9, cv::CC_STAT_TOP), 10);
  295. EXPECT_EQ(stats(9, cv::CC_STAT_WIDTH), 16);
  296. EXPECT_EQ(stats(9, cv::CC_STAT_HEIGHT), 6);
  297. EXPECT_EQ(stats(9, cv::CC_STAT_AREA), 21);
  298. }
  299. EXPECT_EQ(stats(10, cv::CC_STAT_LEFT), 9);
  300. EXPECT_EQ(stats(10, cv::CC_STAT_TOP), 12);
  301. EXPECT_EQ(stats(10, cv::CC_STAT_WIDTH), 5);
  302. EXPECT_EQ(stats(10, cv::CC_STAT_HEIGHT), 2);
  303. EXPECT_EQ(stats(10, cv::CC_STAT_AREA), 7);
  304. }
  305. }
  306. TEST(Imgproc_ConnectedComponents, chessboard_even)
  307. {
  308. cv::Size size(16, 16);
  309. cv::Mat1b input(size);
  310. cv::Mat1i output_8c(size);
  311. cv::Mat1i output_4c(size);
  312. // Chessboard image with even number of rows and cols
  313. // Note that this is the maximum number of labels for 4-way connectivity
  314. {
  315. input <<
  316. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  317. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  318. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  319. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  320. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  321. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  322. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  323. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  324. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  325. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  326. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  327. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  328. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  329. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  330. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  331. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1;
  332. output_8c <<
  333. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  334. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  335. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  336. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  337. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  338. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  339. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  340. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  341. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  342. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  343. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  344. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  345. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  346. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  347. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  348. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1;
  349. output_4c <<
  350. 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0,
  351. 0, 9, 0, 10, 0, 11, 0, 12, 0, 13, 0, 14, 0, 15, 0, 16,
  352. 17, 0, 18, 0, 19, 0, 20, 0, 21, 0, 22, 0, 23, 0, 24, 0,
  353. 0, 25, 0, 26, 0, 27, 0, 28, 0, 29, 0, 30, 0, 31, 0, 32,
  354. 33, 0, 34, 0, 35, 0, 36, 0, 37, 0, 38, 0, 39, 0, 40, 0,
  355. 0, 41, 0, 42, 0, 43, 0, 44, 0, 45, 0, 46, 0, 47, 0, 48,
  356. 49, 0, 50, 0, 51, 0, 52, 0, 53, 0, 54, 0, 55, 0, 56, 0,
  357. 0, 57, 0, 58, 0, 59, 0, 60, 0, 61, 0, 62, 0, 63, 0, 64,
  358. 65, 0, 66, 0, 67, 0, 68, 0, 69, 0, 70, 0, 71, 0, 72, 0,
  359. 0, 73, 0, 74, 0, 75, 0, 76, 0, 77, 0, 78, 0, 79, 0, 80,
  360. 81, 0, 82, 0, 83, 0, 84, 0, 85, 0, 86, 0, 87, 0, 88, 0,
  361. 0, 89, 0, 90, 0, 91, 0, 92, 0, 93, 0, 94, 0, 95, 0, 96,
  362. 97, 0, 98, 0, 99, 0, 100, 0, 101, 0, 102, 0, 103, 0, 104, 0,
  363. 0, 105, 0, 106, 0, 107, 0, 108, 0, 109, 0, 110, 0, 111, 0, 112,
  364. 113, 0, 114, 0, 115, 0, 116, 0, 117, 0, 118, 0, 119, 0, 120, 0,
  365. 0, 121, 0, 122, 0, 123, 0, 124, 0, 125, 0, 126, 0, 127, 0, 128;
  366. }
  367. int ccltype[] = { cv::CCL_DEFAULT, cv::CCL_WU, cv::CCL_GRANA, cv::CCL_BOLELLI, cv::CCL_SAUF, cv::CCL_BBDT, cv::CCL_SPAGHETTI };
  368. cv::Mat1i labels;
  369. cv::Mat diff;
  370. int nLabels = 0;
  371. for (size_t cclt = 0; cclt < sizeof(ccltype) / sizeof(int); ++cclt) {
  372. EXPECT_NO_THROW(nLabels = cv::connectedComponents(input, labels, 8, CV_32S, ccltype[cclt]));
  373. normalizeLabels(labels, nLabels);
  374. diff = labels != output_8c;
  375. EXPECT_EQ(cv::countNonZero(diff), 0);
  376. EXPECT_NO_THROW(nLabels = cv::connectedComponents(input, labels, 4, CV_32S, ccltype[cclt]));
  377. normalizeLabels(labels, nLabels);
  378. diff = labels != output_4c;
  379. EXPECT_EQ(cv::countNonZero(diff), 0);
  380. }
  381. }
  382. TEST(Imgproc_ConnectedComponents, chessboard_odd)
  383. {
  384. cv::Size size(15, 15);
  385. cv::Mat1b input(size);
  386. cv::Mat1i output_8c(size);
  387. cv::Mat1i output_4c(size);
  388. // Chessboard image with odd number of rows and cols
  389. // Note that this is the maximum number of labels for 4-way connectivity
  390. {
  391. input <<
  392. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  393. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  394. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  395. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  396. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  397. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  398. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  399. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  400. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  401. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  402. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  403. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  404. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  405. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  406. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1;
  407. output_8c <<
  408. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  409. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  410. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  411. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  412. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  413. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  414. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  415. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  416. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  417. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  418. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  419. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  420. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  421. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  422. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1;
  423. output_4c <<
  424. 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8,
  425. 0, 9, 0, 10, 0, 11, 0, 12, 0, 13, 0, 14, 0, 15, 0,
  426. 16, 0, 17, 0, 18, 0, 19, 0, 20, 0, 21, 0, 22, 0, 23,
  427. 0, 24, 0, 25, 0, 26, 0, 27, 0, 28, 0, 29, 0, 30, 0,
  428. 31, 0, 32, 0, 33, 0, 34, 0, 35, 0, 36, 0, 37, 0, 38,
  429. 0, 39, 0, 40, 0, 41, 0, 42, 0, 43, 0, 44, 0, 45, 0,
  430. 46, 0, 47, 0, 48, 0, 49, 0, 50, 0, 51, 0, 52, 0, 53,
  431. 0, 54, 0, 55, 0, 56, 0, 57, 0, 58, 0, 59, 0, 60, 0,
  432. 61, 0, 62, 0, 63, 0, 64, 0, 65, 0, 66, 0, 67, 0, 68,
  433. 0, 69, 0, 70, 0, 71, 0, 72, 0, 73, 0, 74, 0, 75, 0,
  434. 76, 0, 77, 0, 78, 0, 79, 0, 80, 0, 81, 0, 82, 0, 83,
  435. 0, 84, 0, 85, 0, 86, 0, 87, 0, 88, 0, 89, 0, 90, 0,
  436. 91, 0, 92, 0, 93, 0, 94, 0, 95, 0, 96, 0, 97, 0, 98,
  437. 0, 99, 0, 100, 0, 101, 0, 102, 0, 103, 0, 104, 0, 105, 0,
  438. 106, 0, 107, 0, 108, 0, 109, 0, 110, 0, 111, 0, 112, 0, 113;
  439. }
  440. int ccltype[] = { cv::CCL_DEFAULT, cv::CCL_WU, cv::CCL_GRANA, cv::CCL_BOLELLI, cv::CCL_SAUF, cv::CCL_BBDT, cv::CCL_SPAGHETTI };
  441. cv::Mat1i labels;
  442. cv::Mat diff;
  443. int nLabels = 0;
  444. for (size_t cclt = 0; cclt < sizeof(ccltype) / sizeof(int); ++cclt) {
  445. EXPECT_NO_THROW(nLabels = cv::connectedComponents(input, labels, 8, CV_32S, ccltype[cclt]));
  446. normalizeLabels(labels, nLabels);
  447. diff = labels != output_8c;
  448. EXPECT_EQ(cv::countNonZero(diff), 0);
  449. EXPECT_NO_THROW(nLabels = cv::connectedComponents(input, labels, 4, CV_32S, ccltype[cclt]));
  450. normalizeLabels(labels, nLabels);
  451. diff = labels != output_4c;
  452. EXPECT_EQ(cv::countNonZero(diff), 0);
  453. }
  454. }
  455. TEST(Imgproc_ConnectedComponents, maxlabels_8conn_even)
  456. {
  457. cv::Size size(16, 16);
  458. cv::Mat1b input(size);
  459. cv::Mat1i output_8c(size);
  460. cv::Mat1i output_4c(size);
  461. {
  462. input <<
  463. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  464. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  465. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  466. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  467. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  468. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  469. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  470. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  471. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  472. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  473. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  474. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  475. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  476. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  477. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  478. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
  479. output_8c <<
  480. 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0,
  481. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  482. 9, 0, 10, 0, 11, 0, 12, 0, 13, 0, 14, 0, 15, 0, 16, 0,
  483. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  484. 17, 0, 18, 0, 19, 0, 20, 0, 21, 0, 22, 0, 23, 0, 24, 0,
  485. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  486. 25, 0, 26, 0, 27, 0, 28, 0, 29, 0, 30, 0, 31, 0, 32, 0,
  487. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  488. 33, 0, 34, 0, 35, 0, 36, 0, 37, 0, 38, 0, 39, 0, 40, 0,
  489. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  490. 41, 0, 42, 0, 43, 0, 44, 0, 45, 0, 46, 0, 47, 0, 48, 0,
  491. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  492. 49, 0, 50, 0, 51, 0, 52, 0, 53, 0, 54, 0, 55, 0, 56, 0,
  493. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  494. 57, 0, 58, 0, 59, 0, 60, 0, 61, 0, 62, 0, 63, 0, 64, 0,
  495. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
  496. output_4c <<
  497. 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0,
  498. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  499. 9, 0, 10, 0, 11, 0, 12, 0, 13, 0, 14, 0, 15, 0, 16, 0,
  500. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  501. 17, 0, 18, 0, 19, 0, 20, 0, 21, 0, 22, 0, 23, 0, 24, 0,
  502. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  503. 25, 0, 26, 0, 27, 0, 28, 0, 29, 0, 30, 0, 31, 0, 32, 0,
  504. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  505. 33, 0, 34, 0, 35, 0, 36, 0, 37, 0, 38, 0, 39, 0, 40, 0,
  506. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  507. 41, 0, 42, 0, 43, 0, 44, 0, 45, 0, 46, 0, 47, 0, 48, 0,
  508. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  509. 49, 0, 50, 0, 51, 0, 52, 0, 53, 0, 54, 0, 55, 0, 56, 0,
  510. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  511. 57, 0, 58, 0, 59, 0, 60, 0, 61, 0, 62, 0, 63, 0, 64, 0,
  512. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
  513. }
  514. int ccltype[] = { cv::CCL_DEFAULT, cv::CCL_WU, cv::CCL_GRANA, cv::CCL_BOLELLI, cv::CCL_SAUF, cv::CCL_BBDT, cv::CCL_SPAGHETTI };
  515. cv::Mat1i labels;
  516. cv::Mat diff;
  517. int nLabels = 0;
  518. for (size_t cclt = 0; cclt < sizeof(ccltype) / sizeof(int); ++cclt) {
  519. EXPECT_NO_THROW(nLabels = cv::connectedComponents(input, labels, 8, CV_32S, ccltype[cclt]));
  520. normalizeLabels(labels, nLabels);
  521. diff = labels != output_8c;
  522. EXPECT_EQ(cv::countNonZero(diff), 0);
  523. EXPECT_NO_THROW(nLabels = cv::connectedComponents(input, labels, 4, CV_32S, ccltype[cclt]));
  524. normalizeLabels(labels, nLabels);
  525. diff = labels != output_4c;
  526. EXPECT_EQ(cv::countNonZero(diff), 0);
  527. }
  528. }
  529. TEST(Imgproc_ConnectedComponents, maxlabels_8conn_odd)
  530. {
  531. cv::Size size(15, 15);
  532. cv::Mat1b input(size);
  533. cv::Mat1i output_8c(size);
  534. cv::Mat1i output_4c(size);
  535. {
  536. input <<
  537. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  538. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  539. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  540. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  541. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  542. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  543. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  544. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  545. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  546. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  547. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  548. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  549. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  550. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  551. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1;
  552. output_8c <<
  553. 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8,
  554. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  555. 9, 0, 10, 0, 11, 0, 12, 0, 13, 0, 14, 0, 15, 0, 16,
  556. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  557. 17, 0, 18, 0, 19, 0, 20, 0, 21, 0, 22, 0, 23, 0, 24,
  558. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  559. 25, 0, 26, 0, 27, 0, 28, 0, 29, 0, 30, 0, 31, 0, 32,
  560. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  561. 33, 0, 34, 0, 35, 0, 36, 0, 37, 0, 38, 0, 39, 0, 40,
  562. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  563. 41, 0, 42, 0, 43, 0, 44, 0, 45, 0, 46, 0, 47, 0, 48,
  564. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  565. 49, 0, 50, 0, 51, 0, 52, 0, 53, 0, 54, 0, 55, 0, 56,
  566. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  567. 57, 0, 58, 0, 59, 0, 60, 0, 61, 0, 62, 0, 63, 0, 64;
  568. output_4c <<
  569. 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8,
  570. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  571. 9, 0, 10, 0, 11, 0, 12, 0, 13, 0, 14, 0, 15, 0, 16,
  572. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  573. 17, 0, 18, 0, 19, 0, 20, 0, 21, 0, 22, 0, 23, 0, 24,
  574. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  575. 25, 0, 26, 0, 27, 0, 28, 0, 29, 0, 30, 0, 31, 0, 32,
  576. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  577. 33, 0, 34, 0, 35, 0, 36, 0, 37, 0, 38, 0, 39, 0, 40,
  578. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  579. 41, 0, 42, 0, 43, 0, 44, 0, 45, 0, 46, 0, 47, 0, 48,
  580. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  581. 49, 0, 50, 0, 51, 0, 52, 0, 53, 0, 54, 0, 55, 0, 56,
  582. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  583. 57, 0, 58, 0, 59, 0, 60, 0, 61, 0, 62, 0, 63, 0, 64;
  584. }
  585. int ccltype[] = { cv::CCL_DEFAULT, cv::CCL_WU, cv::CCL_GRANA, cv::CCL_BOLELLI, cv::CCL_SAUF, cv::CCL_BBDT, cv::CCL_SPAGHETTI };
  586. cv::Mat1i labels;
  587. cv::Mat diff;
  588. int nLabels = 0;
  589. for (size_t cclt = 0; cclt < sizeof(ccltype) / sizeof(int); ++cclt) {
  590. EXPECT_NO_THROW(nLabels = cv::connectedComponents(input, labels, 8, CV_32S, ccltype[cclt]));
  591. normalizeLabels(labels, nLabels);
  592. diff = labels != output_8c;
  593. EXPECT_EQ(cv::countNonZero(diff), 0);
  594. EXPECT_NO_THROW(nLabels = cv::connectedComponents(input, labels, 4, CV_32S, ccltype[cclt]));
  595. normalizeLabels(labels, nLabels);
  596. diff = labels != output_4c;
  597. EXPECT_EQ(cv::countNonZero(diff), 0);
  598. }
  599. }
  600. TEST(Imgproc_ConnectedComponents, single_row)
  601. {
  602. cv::Size size(1, 15);
  603. cv::Mat1b input(size);
  604. cv::Mat1i output_8c(size);
  605. cv::Mat1i output_4c(size);
  606. {
  607. input <<
  608. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1;
  609. output_8c <<
  610. 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8;
  611. output_4c <<
  612. 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8;
  613. }
  614. int ccltype[] = { cv::CCL_DEFAULT, cv::CCL_WU, cv::CCL_GRANA, cv::CCL_BOLELLI, cv::CCL_SAUF, cv::CCL_BBDT, cv::CCL_SPAGHETTI };
  615. cv::Mat1i labels;
  616. cv::Mat diff;
  617. int nLabels = 0;
  618. for (size_t cclt = 0; cclt < sizeof(ccltype) / sizeof(int); ++cclt) {
  619. EXPECT_NO_THROW(nLabels = cv::connectedComponents(input, labels, 8, CV_32S, ccltype[cclt]));
  620. normalizeLabels(labels, nLabels);
  621. diff = labels != output_8c;
  622. EXPECT_EQ(cv::countNonZero(diff), 0);
  623. EXPECT_NO_THROW(nLabels = cv::connectedComponents(input, labels, 4, CV_32S, ccltype[cclt]));
  624. normalizeLabels(labels, nLabels);
  625. diff = labels != output_4c;
  626. EXPECT_EQ(cv::countNonZero(diff), 0);
  627. }
  628. }
  629. TEST(Imgproc_ConnectedComponents, single_column)
  630. {
  631. cv::Size size(15, 1);
  632. cv::Mat1b input(size);
  633. cv::Mat1i output_8c(size);
  634. cv::Mat1i output_4c(size);
  635. {
  636. input <<
  637. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1;
  638. output_8c <<
  639. 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8;
  640. output_4c <<
  641. 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8;
  642. }
  643. int ccltype[] = { cv::CCL_DEFAULT, cv::CCL_WU, cv::CCL_GRANA, cv::CCL_BOLELLI, cv::CCL_SAUF, cv::CCL_BBDT, cv::CCL_SPAGHETTI };
  644. cv::Mat1i labels;
  645. cv::Mat diff;
  646. int nLabels = 0;
  647. for (size_t cclt = 0; cclt < sizeof(ccltype) / sizeof(int); ++cclt) {
  648. EXPECT_NO_THROW(nLabels = cv::connectedComponents(input, labels, 8, CV_32S, ccltype[cclt]));
  649. normalizeLabels(labels, nLabels);
  650. diff = labels != output_8c;
  651. EXPECT_EQ(cv::countNonZero(diff), 0);
  652. EXPECT_NO_THROW(nLabels = cv::connectedComponents(input, labels, 4, CV_32S, ccltype[cclt]));
  653. normalizeLabels(labels, nLabels);
  654. diff = labels != output_4c;
  655. EXPECT_EQ(cv::countNonZero(diff), 0);
  656. }
  657. }
  658. TEST(Imgproc_ConnectedComponents, 4conn_regression_21366)
  659. {
  660. Mat src = Mat::zeros(Size(10, 10), CV_8UC1);
  661. {
  662. Mat labels, stats, centroids;
  663. EXPECT_NO_THROW(cv::connectedComponentsWithStats(src, labels, stats, centroids, 4));
  664. }
  665. }
  666. }
  667. } // namespace