Skip to content

Commit 8d2c132

Browse files
committed
Merge pull request #361 from epfl-lts2:graphsegmentation
2 parents 81b23b7 + 8f9ac94 commit 8d2c132

File tree

7 files changed

+627
-3
lines changed

7 files changed

+627
-3
lines changed

modules/ximgproc/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ Extended Image Processing
77
4. Adaptive Manifold Filter
88
5. Joint Bilateral Filter
99
6. Superpixels
10+
7. Graph segmentation

modules/ximgproc/doc/ximgproc.bib

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ @inproceedings{Lim2013
5656
organization={IEEE}
5757
}
5858

59+
@incollection{PFF2004,
60+
title={Efficient graph-based image segmentation},
61+
author={Felzenszwalb, Pedro F and Huttenlocher, Daniel P},
62+
journal={International Journal of Computer Vision},
63+
volume={59},
64+
number={2},
65+
pages={167--181},
66+
year={2004},
67+
publisher={Springer}
68+
}
69+
5970
@article{Min2014,
6071
title={Fast global image smoothing based on weighted least squares},
6172
author={Min, Dongbo and Choi, Sunghwan and Lu, Jiangbo and Ham, Bumsub and Sohn, Kwanghoon and Do, Minh N},

modules/ximgproc/include/opencv2/ximgproc.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "ximgproc/sparse_match_interpolator.hpp"
4343
#include "ximgproc/structured_edge_detection.hpp"
4444
#include "ximgproc/seeds.hpp"
45+
#include "ximgproc/segmentation.hpp"
4546
#include "ximgproc/fast_hough_transform.hpp"
4647
#include "ximgproc/estimated_covariance.hpp"
4748

@@ -55,6 +56,8 @@ which somehow takes into account pixel affinities in natural images.
5556
@defgroup ximgproc_filters Filters
5657
5758
@defgroup ximgproc_superpixel Superpixels
59+
60+
@defgroup ximgproc_segmentation Image segmentation
5861
@}
5962
*/
6063

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
#ifndef __OPENCV_XIMGPROC_SEGMENTATION_HPP__
33+
#define __OPENCV_XIMGPROC_SEGMENTATION_HPP__
34+
35+
#include <opencv2/core.hpp>
36+
37+
namespace cv {
38+
namespace ximgproc {
39+
namespace segmentation {
40+
//! @addtogroup ximgproc_segmentation
41+
//! @{
42+
43+
/** @brief Graph Based Segmentation Algorithm.
44+
The class implements the algorithm described in @cite PFF2004 .
45+
*/
46+
class CV_EXPORTS_W GraphSegmentation : public Algorithm {
47+
public:
48+
/** @brief Segment an image and store output in dst
49+
@param src The input image. Any number of channel (1 (Eg: Gray), 3 (Eg: RGB), 4 (Eg: RGB-D)) can be provided
50+
@param dst The output segmentation. It's a CV_32SC1 Mat with the same number of cols and rows as input image, with an unique, sequential, id for each pixel.
51+
*/
52+
CV_WRAP virtual void processImage(InputArray src, OutputArray dst) = 0;
53+
54+
CV_WRAP virtual void setSigma(double sigma) = 0;
55+
CV_WRAP virtual double getSigma() = 0;
56+
57+
CV_WRAP virtual void setK(float k) = 0;
58+
CV_WRAP virtual float getK() = 0;
59+
60+
CV_WRAP virtual void setMinSize(int min_size) = 0;
61+
CV_WRAP virtual int getMinSize() = 0;
62+
};
63+
64+
/** @brief Creates a graph based segmentor
65+
@param sigma The sigma parameter, used to smooth image
66+
@param k The k parameter of the algorythm
67+
@param min_size The minimum size of segments
68+
*/
69+
CV_EXPORTS_W Ptr<GraphSegmentation> createGraphSegmentation(double sigma=0.5, float k=300, int min_size=100);
70+
//! @}
71+
72+
// Represent an edge between two pixels
73+
class Edge {
74+
public:
75+
int from;
76+
int to;
77+
float weight;
78+
79+
bool operator <(const Edge& e) const {
80+
return weight < e.weight;
81+
}
82+
};
83+
84+
// A point in the sets of points
85+
class PointSetElement {
86+
public:
87+
int p;
88+
int size;
89+
90+
PointSetElement() { }
91+
92+
PointSetElement(int p_) {
93+
p = p_;
94+
size = 1;
95+
}
96+
};
97+
98+
// An object to manage set of points, who can be fusionned
99+
class PointSet {
100+
public:
101+
PointSet(int nb_elements_);
102+
~PointSet();
103+
104+
int nb_elements;
105+
106+
// Return the main point of the point's set
107+
int getBasePoint(int p);
108+
109+
// Join two sets of points, based on their main point
110+
void joinPoints(int p_a, int p_b);
111+
112+
// Return the set size of a set (based on the main point)
113+
int size(unsigned int p) { return mapping[p].size; }
114+
115+
private:
116+
PointSetElement* mapping;
117+
118+
};
119+
120+
}
121+
}
122+
}
123+
124+
#endif
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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

Comments
 (0)