graphsegmentation_demo.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. By downloading, copying, installing or using the software you agree to this
  3. license. If you do not agree to this license, do not download, install,
  4. copy or use the software.
  5. License Agreement
  6. For Open Source Computer Vision Library
  7. (3-clause BSD License)
  8. Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  9. Third party copyrights are property of their respective owners.
  10. Redistribution and use in source and binary forms, with or without modification,
  11. are permitted provided that the following conditions are met:
  12. * Redistributions of source code must retain the above copyright notice,
  13. this list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright notice,
  15. this list of conditions and the following disclaimer in the documentation
  16. and/or other materials provided with the distribution.
  17. * Neither the names of the copyright holders nor the names of the contributors
  18. may be used to endorse or promote products derived from this software
  19. without specific prior written permission.
  20. This software is provided by the copyright holders and contributors "as is" and
  21. any express or implied warranties, including, but not limited to, the implied
  22. warranties of merchantability and fitness for a particular purpose are
  23. disclaimed. In no event shall copyright holders or contributors be liable for
  24. any direct, indirect, incidental, special, exemplary, or consequential damages
  25. (including, but not limited to, procurement of substitute goods or services;
  26. loss of use, data, or profits; or business interruption) however caused
  27. and on any theory of liability, whether in contract, strict liability,
  28. or tort (including negligence or otherwise) arising in any way out of
  29. the use of this software, even if advised of the possibility of such damage.
  30. */
  31. #include "opencv2/ximgproc/segmentation.hpp"
  32. #include "opencv2/highgui.hpp"
  33. #include "opencv2/core.hpp"
  34. #include "opencv2/imgproc.hpp"
  35. #include <iostream>
  36. using namespace cv;
  37. using namespace cv::ximgproc::segmentation;
  38. Scalar hsv_to_rgb(Scalar);
  39. Scalar color_mapping(int);
  40. static void help() {
  41. std::cout << std::endl <<
  42. "A program demonstrating the use and capabilities of a particular graph based image" << std::endl <<
  43. "segmentation algorithm described in P. Felzenszwalb, D. Huttenlocher," << std::endl <<
  44. " \"Efficient Graph-Based Image Segmentation\"" << std::endl <<
  45. "International Journal of Computer Vision, Vol. 59, No. 2, September 2004" << std::endl << std::endl <<
  46. "Usage:" << std::endl <<
  47. "./graphsegmentation_demo input_image output_image [simga=0.5] [k=300] [min_size=100]" << std::endl;
  48. }
  49. Scalar hsv_to_rgb(Scalar c) {
  50. Mat in(1, 1, CV_32FC3);
  51. Mat out(1, 1, CV_32FC3);
  52. float * p = in.ptr<float>(0);
  53. p[0] = (float)c[0] * 360.0f;
  54. p[1] = (float)c[1];
  55. p[2] = (float)c[2];
  56. cvtColor(in, out, COLOR_HSV2RGB);
  57. Scalar t;
  58. Vec3f p2 = out.at<Vec3f>(0, 0);
  59. t[0] = (int)(p2[0] * 255);
  60. t[1] = (int)(p2[1] * 255);
  61. t[2] = (int)(p2[2] * 255);
  62. return t;
  63. }
  64. Scalar color_mapping(int segment_id) {
  65. double base = (double)(segment_id) * 0.618033988749895 + 0.24443434;
  66. return hsv_to_rgb(Scalar(fmod(base, 1.2), 0.95, 0.80));
  67. }
  68. int main(int argc, char** argv) {
  69. if (argc < 2 || argc > 6) {
  70. help();
  71. return -1;
  72. }
  73. Ptr<GraphSegmentation> gs = createGraphSegmentation();
  74. if (argc > 3)
  75. gs->setSigma(atof(argv[3]));
  76. if (argc > 4)
  77. gs->setK((float)atoi(argv[4]));
  78. if (argc > 5)
  79. gs->setMinSize(atoi(argv[5]));
  80. if (!gs) {
  81. std::cerr << "Failed to create GraphSegmentation Algorithm." << std::endl;
  82. return -2;
  83. }
  84. Mat input, output, output_image;
  85. input = imread(argv[1]);
  86. if (!input.data) {
  87. std::cerr << "Failed to load input image" << std::endl;
  88. return -3;
  89. }
  90. gs->processImage(input, output);
  91. double min, max;
  92. minMaxLoc(output, &min, &max);
  93. int nb_segs = (int)max + 1;
  94. std::cout << nb_segs << " segments" << std::endl;
  95. output_image = Mat::zeros(output.rows, output.cols, CV_8UC3);
  96. uint* p;
  97. uchar* p2;
  98. for (int i = 0; i < output.rows; i++) {
  99. p = output.ptr<uint>(i);
  100. p2 = output_image.ptr<uchar>(i);
  101. for (int j = 0; j < output.cols; j++) {
  102. Scalar color = color_mapping(p[j]);
  103. p2[j*3] = (uchar)color[0];
  104. p2[j*3 + 1] = (uchar)color[1];
  105. p2[j*3 + 2] = (uchar)color[2];
  106. }
  107. }
  108. imwrite(argv[2], output_image);
  109. std::cout << "Image written to " << argv[2] << std::endl;
  110. return 0;
  111. }