test_watershed.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #if 0
  44. namespace opencv_test { namespace {
  45. class CV_WatershedTest : public cvtest::BaseTest
  46. {
  47. public:
  48. CV_WatershedTest();
  49. ~CV_WatershedTest();
  50. protected:
  51. void run(int);
  52. };
  53. CV_WatershedTest::CV_WatershedTest() {}
  54. CV_WatershedTest::~CV_WatershedTest() {}
  55. void CV_WatershedTest::run( int /* start_from */)
  56. {
  57. string exp_path = string(ts->get_data_path()) + "watershed/wshed_exp.png";
  58. Mat exp = imread(exp_path, 0);
  59. Mat orig = imread(string(ts->get_data_path()) + "inpaint/orig.png");
  60. FileStorage fs(string(ts->get_data_path()) + "watershed/comp.xml", FileStorage::READ);
  61. if (orig.empty() || !fs.isOpened())
  62. {
  63. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
  64. return;
  65. }
  66. CvSeq* cnts = (CvSeq*)fs["contours"].readObj();
  67. Mat markers(orig.size(), CV_32SC1);
  68. markers = Scalar(0);
  69. IplImage iplmrks = cvIplImage(markers);
  70. vector<unsigned char> colors(1);
  71. for(int i = 0; cnts != 0; cnts = cnts->h_next, ++i )
  72. {
  73. cvDrawContours( &iplmrks, cnts, cvScalar(Scalar::all(i + 1)), cvScalar(Scalar::all(i + 1)), -1, CV_FILLED);
  74. Point* p = (Point*)cvGetSeqElem(cnts, 0);
  75. //expected image was added with 1 in order to save to png
  76. //so now we subtract 1 to get real color
  77. if(!exp.empty())
  78. colors.push_back(exp.ptr(p->y)[p->x] - 1);
  79. }
  80. fs.release();
  81. const int compNum = (int)(colors.size() - 1);
  82. watershed(orig, markers);
  83. for(int j = 0; j < markers.rows; ++j)
  84. {
  85. int* line = markers.ptr<int>(j);
  86. for(int i = 0; i < markers.cols; ++i)
  87. {
  88. int& pixel = line[i];
  89. if (pixel == -1) // border
  90. continue;
  91. if (pixel <= 0 || pixel > compNum)
  92. continue; // bad result, doing nothing and going to get error latter;
  93. // repaint in saved color to compare with expected;
  94. if(!exp.empty())
  95. pixel = colors[pixel];
  96. }
  97. }
  98. Mat markers8U;
  99. markers.convertTo(markers8U, CV_8U, 1, 1);
  100. if( exp.empty() || orig.size() != exp.size() )
  101. {
  102. imwrite(exp_path, markers8U);
  103. exp = markers8U;
  104. }
  105. ASSERT_EQ(0, cvtest::norm(markers8U, exp, NORM_INF));
  106. }
  107. TEST(Imgproc_Watershed, regression) { CV_WatershedTest test; test.safe_run(); }
  108. }} // namespace
  109. #endif