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