Commit b754372c authored by Maximilien Cuony's avatar Maximilien Cuony Committed by Maximilien Cuony

Rewrote GraphSegmentation in ximgproc

parent 2b64abdb
......@@ -56,6 +56,17 @@
organization={IEEE}
}
@incollection{PFF2004,
title={Efficient graph-based image segmentation},
author={Felzenszwalb, Pedro F and Huttenlocher, Daniel P},
journal={International Journal of Computer Vision},
volume={59},
number={2},
pages={167--181},
year={2004},
publisher={Springer}
}
@article{Min2014,
title={Fast global image smoothing based on weighted least squares},
author={Min, Dongbo and Choi, Sunghwan and Lu, Jiangbo and Ham, Bumsub and Sohn, Kwanghoon and Do, Minh N},
......
......@@ -42,6 +42,7 @@
#include "ximgproc/sparse_match_interpolator.hpp"
#include "ximgproc/structured_edge_detection.hpp"
#include "ximgproc/seeds.hpp"
#include "ximgproc/segmentation.hpp"
#include "ximgproc/fast_hough_transform.hpp"
/** @defgroup ximgproc Extended Image Processing
......@@ -54,6 +55,8 @@ which somehow takes into account pixel affinities in natural images.
@defgroup ximgproc_filters Filters
@defgroup ximgproc_superpixel Superpixels
@defgroup ximgproc_segmentation Image segmentation
@}
*/
......
/*
By downloading, copying, installing or using the software you agree to this
license. If you do not agree to this license, do not download, install,
copy or use the software.
License Agreement
For Open Source Computer Vision Library
(3-clause BSD License)
Copyright (C) 2013, OpenCV Foundation, all rights reserved.
Third party copyrights are property of their respective owners.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the names of the copyright holders nor the names of the contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are
disclaimed. In no event shall copyright holders or contributors be liable for
any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused
and on any theory of liability, whether in contract, strict liability,
or tort (including negligence or otherwise) arising in any way out of
the use of this software, even if advised of the possibility of such damage.
*/
#ifndef __OPENCV_XIMGPROC_SEGMENTATION_HPP__
#define __OPENCV_XIMGPROC_SEGMENTATION_HPP__
#include <opencv2/core.hpp>
namespace cv {
namespace ximgproc {
namespace segmentation {
//! @addtogroup ximgproc_segmentation
//! @{
/** @brief Graph Based Segmentation Algorithm.
The class implements the algorithm described in @cite PFF2004 .
*/
class CV_EXPORTS_W GraphSegmentation : public Algorithm {
public:
/** @brief Segment an image and store output in dst
@param src The input image. Any number of channel (1 (Eg: Gray), 3 (Eg: RGB), 4 (Eg: RGB-D)) can be provided
@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.
*/
CV_WRAP virtual void processImage(InputArray src, OutputArray dst) = 0;
CV_WRAP virtual void setSigma(double sigma) = 0;
CV_WRAP virtual double getSigma() = 0;
CV_WRAP virtual void setK(float k) = 0;
CV_WRAP virtual float getK() = 0;
CV_WRAP virtual void setMinSize(int min_size) = 0;
CV_WRAP virtual int getMinSize() = 0;
};
/** @brief Creates a graph based segmentor
@param simga The sigma parameter, used to smooth image
@param k The k parameter of the algorythm
@param min_size The minimum size of segments
*/
CV_EXPORTS_W Ptr<GraphSegmentation> createGraphSegmentation(double sigma=0.5, float k=300, int min_size=100);
//! @}
// Represent an edge between two pixels
class Edge {
public:
int from;
int to;
float weight;
bool operator <(const Edge& e) const {
return weight < e.weight;
}
};
// A point in the sets of points
class PointSetElement {
public:
int p;
int size;
PointSetElement() { }
PointSetElement(int p_) {
p = p_;
size = 1;
}
};
// An object to manage set of points, who can be fusionned
class PointSet {
public:
PointSet(int nb_elements_);
~PointSet();
int nb_elements;
// Return the main point of the point's set
int getBasePoint(int p);
// Join two sets of points, based on their main point
void joinPoints(int p_a, int p_b);
// Return the set size of a set (based on the main point)
int size(unsigned int p) { return mapping[p].size; }
private:
PointSetElement* mapping;
};
}
}
}
#endif
/*
By downloading, copying, installing or using the software you agree to this
license. If you do not agree to this license, do not download, install,
copy or use the software.
License Agreement
For Open Source Computer Vision Library
(3-clause BSD License)
Copyright (C) 2013, OpenCV Foundation, all rights reserved.
Third party copyrights are property of their respective owners.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the names of the copyright holders nor the names of the contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are
disclaimed. In no event shall copyright holders or contributors be liable for
any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused
and on any theory of liability, whether in contract, strict liability,
or tort (including negligence or otherwise) arising in any way out of
the use of this software, even if advised of the possibility of such damage.
*/
#include "opencv2/ximgproc/segmentation.hpp"
#include "opencv2/highgui.hpp"
#include <opencv2/core/utility.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace cv::ximgproc::segmentation;
static void help() {
std::cout << std::endl <<
"A program demonstrating the use and capabilities of a particular graph based image" << std::endl <<
"segmentation algorithm described in P. Felzenszwalb, D. Huttenlocher," << std::endl <<
" \"Efficient Graph-Based Image Segmentation\"" << std::endl <<
"International Journal of Computer Vision, Vol. 59, No. 2, September 2004" << std::endl << std::endl <<
"Usage:" << std::endl <<
"./graphsegmentation_demo input_image output_image [simga=0.5] [k=300] [min_size=100]" << std::endl;
}
Scalar hsv_to_rgb(Scalar c) {
Mat in(1, 1, CV_32FC3);
Mat out(1, 1, CV_32FC3);
float * p = in.ptr<float>(0);
p[0] = c[0] * 360;
p[1] = c[1];
p[2] = c[2];
cvtColor(in, out, COLOR_HSV2RGB);
Scalar t;
Vec3f p2 = out.at<Vec3f>(0, 0);
t[0] = (int)(p2[0] * 255);
t[1] = (int)(p2[1] * 255);
t[2] = (int)(p2[2] * 255);
return t;
}
Scalar color_mapping(int segment_id) {
double base = (double)(segment_id) * 0.618033988749895 + 0.24443434;
return hsv_to_rgb(Scalar(fmod(base, 1.2), 0.95, 0.80));
}
int main(int argc, char** argv) {
if (argc < 2 || argc > 6) {
help();
return -1;
}
setUseOptimized(true);
setNumThreads(8);
Ptr<GraphSegmentation> gs = createGraphSegmentation();
if (argc > 3)
gs->setSigma(atof(argv[3]));
if (argc > 4)
gs->setK(atoi(argv[4]));
if (argc > 5)
gs->setMinSize(atoi(argv[5]));
if (!gs) {
std::cerr << "Failed to create GraphSegmentation Algorithm." << std::endl;
return -2;
}
Mat input, output, output_image;
input = imread(argv[1]);
if (!input.data) {
std::cerr << "Failed to load input image" << std::endl;
return -3;
}
gs->processImage(input, output);
double min, max;
minMaxLoc(output, &min, &max);
int nb_segs = (int)max + 1;
std::cout << nb_segs << " segments" << std::endl;
output_image = Mat::zeros(output.rows, output.cols, CV_8UC3);
uint* p;
uchar* p2;
for (int i = 0; i < output.rows; i++) {
p = output.ptr<uint>(i);
p2 = output_image.ptr<uchar>(i);
for (int j = 0; j < output.cols; j++) {
Scalar color = color_mapping(p[j]);
p2[j*3] = color[0];
p2[j*3 + 1] = color[1];
p2[j*3 + 2] = color[2];
}
}
imwrite(argv[2], output_image);
std::cout << "Image written to " << argv[2] << std::endl;
return 0;
}
This diff is collapsed.
......@@ -34,8 +34,8 @@
* the use of this software, even if advised of the possibility of such damage.
*/
#ifndef _OPENCV_EDGEFILTER_PRECOMP_HPP_
#define _OPENCV_EDGEFILTER_PRECOMP_HPP_
#ifndef _OPENCV_XIMGPROC_PRECOMP_HPP_
#define _OPENCV_XIMGPROC_PRECOMP_HPP_
#include <opencv2/core.hpp>
#include <opencv2/core/ocl.hpp>
......@@ -48,4 +48,7 @@
#include <opencv2/ximgproc.hpp>
#endif
\ No newline at end of file
#include <algorithm>
#include <map>
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment