Superpixel Benchmark
Superpixel benchmark, tools and algorithms.
cis_opencv.h
Go to the documentation of this file.
1 
32 #ifndef CIS_OPENCV_H
33 #define CIS_OPENCV_H
34 
35 #include <opencv2/opencv.hpp>
36 #include "superpixels.h"
37 
41 class CIS_OpenCV {
42 public:
53  static void computeSuperpixels(const cv::Mat &mat, int region_size,
54  int lambda, int iterations, int type, float sigma, bool color, cv::Mat &labels) {
55 
56  cv::Mat image_gray;
57  cv::cvtColor(mat, image_gray, CV_BGR2GRAY);
58 
59  cv::Mat grad_x;
60  cv::Mat grad_y;
61 
62  cv::Sobel(image_gray, grad_x, -1, 1, 0, 3, 1, 0, cv::BORDER_DEFAULT);
63  cv::convertScaleAbs(grad_x, grad_x);
64 
65  cv::Sobel(image_gray, grad_y, -1, 0, 1, 3, 1, 0, cv::BORDER_DEFAULT);
66  cv::convertScaleAbs(grad_y, grad_y);
67 
68  image<unsigned char> *I_gray = new image<unsigned char>(mat.cols, mat.rows);
69  for (int i = 0; i < mat.rows; i++){
70  for (int j = 0; j < mat.cols; j++) {
71  imRef(I_gray, j, i) = image_gray.at<unsigned char>(i, j);
72  }
73  }
74 
75  image<rgb> *I;
76  if (color) {
77  I = new image<rgb>(mat.cols, mat.rows);
78  for (int i = 0; i < mat.rows; i++){
79  for (int j = 0; j < mat.cols; j++) {
80  rgb color;
81  color.r = mat.at<cv::Vec3b>(i, j)[2];
82  color.g = mat.at<cv::Vec3b>(i, j)[1];
83  color.b = mat.at<cv::Vec3b>(i, j)[0];
84 
85  imRef(I, j, i) = color;
86  }
87  }
88  }
89 
90  int width = I_gray->width();
91  int height = I_gray->height();
92  int num_pixels = width*height;
93 
94  float variance;
95  if (color) {
96  variance = computeImageVarianceColor(I, width, height);
97  }
98  else {
99  variance = computeImageVariance(I_gray, width, height);
100  }
101 
102  // Initialize and place seeds
103  std::vector<int> Seeds(num_pixels);
104  int numSeeds = 0;
105  PlaceSeeds(I_gray, width, height, num_pixels, Seeds, &numSeeds, region_size);
106  MoveSeedsFromEdges(I_gray, width, height, num_pixels, Seeds, numSeeds, region_size);
107 
108  std::vector<int> weights_horizontal(num_pixels, lambda);
109  std::vector<int> weights_vertical(num_pixels, lambda);
110  std::vector<int> weights_diagonal_1(num_pixels, lambda);
111  std::vector<int> weights_diagonal_2(num_pixels, lambda);
112 
113  image<unsigned char> *I_x = new image<unsigned char>(mat.cols, mat.rows);
114  image<unsigned char> *I_y = new image<unsigned char>(mat.cols, mat.rows);
115  for (int i = 0; i < mat.rows; i++){
116  for (int j = 0; j < mat.cols; j++) {
117  imRef(I_x, j, i) = grad_x.at<unsigned char>(i, j);
118  imRef(I_y, j, i) = grad_y.at<unsigned char>(i, j);
119  }
120  }
121 
122  loadEdges(weights_horizontal, num_pixels, width, height, lambda, I_x);
123  loadEdges(weights_vertical, num_pixels, width, height, lambda, I_y);
124 
125  if (color) {
126  computeWeightsColor(weights_horizontal, num_pixels, width,height,
127  lambda, variance, -1, 0, I, type, sigma);
128  computeWeightsColor(weights_vertical, num_pixels, width,height,
129  lambda, variance, 0, -1, I, type, sigma);
130  computeWeightsColor(weights_diagonal_1, num_pixels, width,height,
131  lambda, variance, -1, -1, I, type, sigma);
132  computeWeightsColor(weights_diagonal_2, num_pixels, width,height,
133  lambda, variance, 1, -1, I, type, sigma);
134  }
135  else {
136  computeWeights(weights_horizontal, num_pixels, width,height,
137  lambda, variance, -1, 0, I_gray, type, sigma);
138  computeWeights(weights_vertical, num_pixels, width,height,
139  lambda, variance, 0, -1, I_gray, type, sigma);
140  computeWeights(weights_diagonal_1, num_pixels, width,height,
141  lambda, variance, -1, -1, I_gray, type, sigma);
142  computeWeights(weights_diagonal_2, num_pixels, width,height,
143  lambda, variance, 1, -1, I_gray, type, sigma);
144  }
145 
146  vector<int> labeling(num_pixels);
147  initializeLabeling(labeling, width, height, Seeds, numSeeds, region_size);
148 
149  int oldEnergy, newEnergy;
150 
151  std::vector<int> changeMask(num_pixels, 1);
152  std::vector<int> changeMaskNew(num_pixels, 0);
153 
154  std::vector<int> order(numSeeds);
155  for (int i = 0; i < numSeeds; i++) {
156  order[i] = i;
157  }
158 
159  int j = 0;
160  //purturbSeeds(order,numSeeds);
161 
162  while (true) {
163  if (color) {
164  newEnergy = computeEnergyColor(labeling, width, height,
165  num_pixels, weights_horizontal, weights_vertical,
166  weights_diagonal_1, weights_diagonal_2, Seeds, I, type);
167  }
168  else {
169  newEnergy = computeEnergy(labeling, width, height, num_pixels,
170  weights_horizontal, weights_vertical, weights_diagonal_1,
171  weights_diagonal_2, Seeds, I_gray, type);
172 
173  }
174  if (j == 0) {
175  oldEnergy = newEnergy + 1;
176  }
177 
178  if (newEnergy == oldEnergy || j >= iterations) {
179  break;
180  }
181 
182  oldEnergy = newEnergy;
183 
184  for (int i = 0; i < numSeeds; i++) {
185  if (color) {
186  expandOnLabelColor(order[i], width, height, num_pixels,
187  Seeds, numSeeds, labeling, weights_horizontal,
188  weights_vertical, lambda, weights_diagonal_1,
189  weights_diagonal_2, region_size, changeMask,
190  changeMaskNew, I, type, variance);
191  }
192  else {
193  expandOnLabel(order[i], width, height, num_pixels, Seeds,
194  numSeeds, labeling, weights_horizontal, weights_vertical,
195  lambda, weights_diagonal_1, weights_diagonal_2, region_size,
196  changeMask, changeMaskNew, I_gray, type, variance);
197  }
198  }
199 
200  for (int i = 0; i < num_pixels; i++) {
201  changeMask[i] = changeMaskNew[i];
202  changeMaskNew[i] = 0;
203  }
204 
205  //purturbSeeds(order, numSeeds);
206  j++;
207  }
208 
209  labels.create(mat.rows, mat.cols, CV_32SC1);
210  for (int i = 0; i < height; ++i) {
211  for (int j = 0; j < width; ++j) {
212  labels.at<int>(i, j) = labeling[j + i*width];
213  }
214  }
215 
216  delete I_gray;
217  if (color) {
218  delete I;
219  }
220  delete I_x;
221  delete I_y;
222  }
223 
224 };
225 
226 #endif /* CIS_OPENCV_H */
227 
static void computeSuperpixels(const cv::Mat &mat, int region_size, int lambda, int iterations, int type, float sigma, bool color, cv::Mat &labels)
Computing superpixels using CIS; for details also see README.md.
Definition: cis_opencv.h:53
Wrapper for running CIS with OpenCV images.
Definition: cis_opencv.h:41