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.hpp"
36 #include "opencv2/imgproc.hpp"
37 #include <iostream>
38 
39 using namespace cv;
40 using namespace cv::ximgproc::segmentation;
41 
42 Scalar hsv_to_rgb(Scalar);
43 Scalar color_mapping(int);
44 
help()45 static void help() {
46     std::cout << std::endl <<
47     "A program demonstrating the use and capabilities of a particular graph based image" << std::endl <<
48     "segmentation algorithm described in P. Felzenszwalb, D. Huttenlocher," << std::endl <<
49     "             \"Efficient Graph-Based Image Segmentation\"" << std::endl <<
50     "International Journal of Computer Vision, Vol. 59, No. 2, September 2004" << std::endl << std::endl <<
51     "Usage:" << std::endl <<
52     "./graphsegmentation_demo input_image output_image [simga=0.5] [k=300] [min_size=100]" << std::endl;
53 }
54 
hsv_to_rgb(Scalar c)55 Scalar hsv_to_rgb(Scalar c) {
56     Mat in(1, 1, CV_32FC3);
57     Mat out(1, 1, CV_32FC3);
58 
59     float * p = in.ptr<float>(0);
60 
61     p[0] = (float)c[0] * 360.0f;
62     p[1] = (float)c[1];
63     p[2] = (float)c[2];
64 
65     cvtColor(in, out, COLOR_HSV2RGB);
66 
67     Scalar t;
68 
69     Vec3f p2 = out.at<Vec3f>(0, 0);
70 
71     t[0] = (int)(p2[0] * 255);
72     t[1] = (int)(p2[1] * 255);
73     t[2] = (int)(p2[2] * 255);
74 
75     return t;
76 
77 }
78 
color_mapping(int segment_id)79 Scalar color_mapping(int segment_id) {
80 
81     double base = (double)(segment_id) * 0.618033988749895 + 0.24443434;
82 
83     return hsv_to_rgb(Scalar(fmod(base, 1.2), 0.95, 0.80));
84 
85 }
86 
main(int argc,char ** argv)87 int main(int argc, char** argv) {
88 
89     if (argc < 2 || argc > 6) {
90         help();
91         return -1;
92     }
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((float)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] = (uchar)color[0];
141             p2[j*3 + 1] = (uchar)color[1];
142             p2[j*3 + 2] = (uchar)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 }
152