|
| 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 | + |
| 32 | + |
| 33 | +#include "opencv2/ximgproc/segmentation.hpp" |
| 34 | +#include "opencv2/highgui.hpp" |
| 35 | +#include <opencv2/core/utility.hpp> |
| 36 | +#include <opencv2/opencv.hpp> |
| 37 | +#include <iostream> |
| 38 | + |
| 39 | +using namespace cv; |
| 40 | +using namespace cv::ximgproc::segmentation; |
| 41 | + |
| 42 | +static void help() { |
| 43 | + std::cout << std::endl << |
| 44 | + "A program demonstrating the use and capabilities of a particular graph based image" << std::endl << |
| 45 | + "segmentation algorithm described in P. Felzenszwalb, D. Huttenlocher," << std::endl << |
| 46 | + " \"Efficient Graph-Based Image Segmentation\"" << std::endl << |
| 47 | + "International Journal of Computer Vision, Vol. 59, No. 2, September 2004" << std::endl << std::endl << |
| 48 | + "Usage:" << std::endl << |
| 49 | + "./graphsegmentation_demo input_image output_image [simga=0.5] [k=300] [min_size=100]" << std::endl; |
| 50 | +} |
| 51 | + |
| 52 | +Scalar hsv_to_rgb(Scalar c) { |
| 53 | + Mat in(1, 1, CV_32FC3); |
| 54 | + Mat out(1, 1, CV_32FC3); |
| 55 | + |
| 56 | + float * p = in.ptr<float>(0); |
| 57 | + |
| 58 | + p[0] = c[0] * 360; |
| 59 | + p[1] = c[1]; |
| 60 | + p[2] = c[2]; |
| 61 | + |
| 62 | + cvtColor(in, out, COLOR_HSV2RGB); |
| 63 | + |
| 64 | + Scalar t; |
| 65 | + |
| 66 | + Vec3f p2 = out.at<Vec3f>(0, 0); |
| 67 | + |
| 68 | + t[0] = (int)(p2[0] * 255); |
| 69 | + t[1] = (int)(p2[1] * 255); |
| 70 | + t[2] = (int)(p2[2] * 255); |
| 71 | + |
| 72 | + return t; |
| 73 | + |
| 74 | +} |
| 75 | + |
| 76 | +Scalar color_mapping(int segment_id) { |
| 77 | + |
| 78 | + double base = (double)(segment_id) * 0.618033988749895 + 0.24443434; |
| 79 | + |
| 80 | + return hsv_to_rgb(Scalar(fmod(base, 1.2), 0.95, 0.80)); |
| 81 | + |
| 82 | +} |
| 83 | + |
| 84 | +int main(int argc, char** argv) { |
| 85 | + |
| 86 | + if (argc < 2 || argc > 6) { |
| 87 | + help(); |
| 88 | + return -1; |
| 89 | + } |
| 90 | + |
| 91 | + setUseOptimized(true); |
| 92 | + setNumThreads(8); |
| 93 | + |
| 94 | + Ptr<GraphSegmentation> gs = createGraphSegmentation(); |
| 95 | + |
| 96 | + if (argc > 3) |
| 97 | + gs->setSigma(atof(argv[3])); |
| 98 | + |
| 99 | + if (argc > 4) |
| 100 | + gs->setK(atoi(argv[4])); |
| 101 | + |
| 102 | + if (argc > 5) |
| 103 | + gs->setMinSize(atoi(argv[5])); |
| 104 | + |
| 105 | + if (!gs) { |
| 106 | + std::cerr << "Failed to create GraphSegmentation Algorithm." << std::endl; |
| 107 | + return -2; |
| 108 | + } |
| 109 | + |
| 110 | + Mat input, output, output_image; |
| 111 | + |
| 112 | + input = imread(argv[1]); |
| 113 | + |
| 114 | + if (!input.data) { |
| 115 | + std::cerr << "Failed to load input image" << std::endl; |
| 116 | + return -3; |
| 117 | + } |
| 118 | + |
| 119 | + gs->processImage(input, output); |
| 120 | + |
| 121 | + double min, max; |
| 122 | + minMaxLoc(output, &min, &max); |
| 123 | + |
| 124 | + int nb_segs = (int)max + 1; |
| 125 | + |
| 126 | + std::cout << nb_segs << " segments" << std::endl; |
| 127 | + |
| 128 | + output_image = Mat::zeros(output.rows, output.cols, CV_8UC3); |
| 129 | + |
| 130 | + uint* p; |
| 131 | + uchar* p2; |
| 132 | + |
| 133 | + for (int i = 0; i < output.rows; i++) { |
| 134 | + |
| 135 | + p = output.ptr<uint>(i); |
| 136 | + p2 = output_image.ptr<uchar>(i); |
| 137 | + |
| 138 | + for (int j = 0; j < output.cols; j++) { |
| 139 | + Scalar color = color_mapping(p[j]); |
| 140 | + p2[j*3] = color[0]; |
| 141 | + p2[j*3 + 1] = color[1]; |
| 142 | + p2[j*3 + 2] = color[2]; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + imwrite(argv[2], output_image); |
| 147 | + |
| 148 | + std::cout << "Image written to " << argv[2] << std::endl; |
| 149 | + |
| 150 | + return 0; |
| 151 | +} |
0 commit comments