slic.h
Go to the documentation of this file.
1 #ifndef SLIC_H
2 #define SLIC_H
3 
4 /* slic.h.
5  *
6  * Written by: Pascal Mettes.
7  *
8  * This file contains the class elements of the class Slic. This class is an
9  * implementation of the SLIC Superpixel algorithm by Achanta et al. [PAMI'12,
10  * vol. 34, num. 11, pp. 2274-2282].
11  *
12  * This implementation is created for the specific purpose of creating
13  * over-segmentations in an OpenCV-based environment.
14  */
15 
16 #include <stdio.h>
17 #include <math.h>
18 #include <vector>
19 #include <float.h>
20 using namespace std;
21 
22 #include <opencv2/opencv.hpp>
23 //using namespace cv;
24 
25 /* The number of iterations run by the clustering algorithm. */
26 #define NR_ITERATIONS 10
27 
28 typedef cv::Vec<double, 5> Vec5d;
29 
30 
31 /*
32  * class Slic.
33  *
34  * In this class, an over-segmentation is created of an image, provided by the
35  * step-size (distance between initial cluster locations) and the colour
36  * distance parameter.
37  */
38 class Slic {
39  private:
40 
41  /* The step size per cluster, and the colour (nc) and distance (ns)
42  * parameters. */
43  int step, nc, ns;
44 
45  /* Compute the distance between a center and an individual pixel. */
46  double compute_dist(int ci, cv::Point pixel, cv::Vec3b colour);
47  /* Find the pixel with the lowest gradient in a 3x3 surrounding. */
48  cv::Point find_local_minimum(const cv::Mat_<cv::Vec3b> &image, cv::Point center);
49 
50  /* Remove and initialize the 2d vectors. */
51  void clear_data();
52  void init_data(const cv::Mat &image);
53 
54  public:
55  /* Class constructors and deconstructors. */
56  Slic();
57  ~Slic();
58 
59  /* Generate an over-segmentation for an image. */
60  void generate_superpixels(const cv::Mat &image, int step, int nc);
61  /* Enforce connectivity for an image. */
62  void create_connectivity(const cv::Mat &image);
63 
64  /* Draw functions. Resp. displayal of the centers and the contours. */
65  void display_center_grid(cv::Mat &image, cv::Scalar colour);
66  void display_contours(cv::Mat &image, cv::Vec3b colour);
67  void colour_with_cluster_means(cv::Mat &image);
68  /* The cluster assignments and distance values for each pixel. */
69  cv::Mat_<int> clusters;
70  cv::Mat_<double> distances;
71 
72  /* The LAB and xy values of the centers. */
73  cv::Mat_<Vec5d> centers;
74  /* The number of occurences of each center. */
76 };
77 
78 #endif
cv::Mat_< Vec5d > centers
Definition: slic.h:73
vector< int > center_counts
Definition: slic.h:75
cv::Vec< double, 5 > Vec5d
Definition: slic.h:28
string ns
int step
Definition: slic.h:43
cv::Mat_< int > clusters
Definition: slic.h:69
Definition: slic.h:38
cv::Mat_< double > distances
Definition: slic.h:70


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Mon May 3 2021 03:03:27