Superpixel Benchmark
Superpixel benchmark, tools and algorithms.
vlslic_opencv.h
Go to the documentation of this file.
1 
32 #ifndef VLSLIC_OPENCV_H
33 #define VLSLIC_OPENCV_H
34 
35 #include <opencv2/opencv.hpp>
36 #include "slic.h"
37 
42 public:
50  static void computeSuperpixels(const cv::Mat &mat, int region_size,
51  double regularization, int min_region_size, int iterations, cv::Mat &labels)
52  {
53  // Convert image to one-dimensional array.
54  float* image = new float[mat.rows*mat.cols*mat.channels()];
55  for (int i = 0; i < mat.rows; ++i) {
56  for (int j = 0; j < mat.cols; ++j) {
57  if (mat.channels() == 1) {
58  image[j + mat.cols*i] = mat.at<unsigned char>(i, j);
59  }
60  else if (mat.channels() == 3) {
61  image[j + mat.cols*i + mat.cols*mat.rows*0] = mat.at<cv::Vec3b>(i, j)[0];
62  image[j + mat.cols*i + mat.cols*mat.rows*1] = mat.at<cv::Vec3b>(i, j)[1];
63  image[j + mat.cols*i + mat.cols*mat.rows*2] = mat.at<cv::Vec3b>(i, j)[2];
64  }
65  }
66  }
67 
68  vl_uint32* segmentation = new vl_uint32[mat.rows*mat.cols];
69  vl_size height = mat.rows;
70  vl_size width = mat.cols;
71  vl_size channels = mat.channels();
72 
73  vl_slic_segment_t(segmentation, image, width, height, channels, region_size,
74  regularization, min_region_size, iterations);
75 
76  // Convert segmentation.
77  labels.create(mat.rows, mat.cols, CV_32SC1);
78  for (int i = 0; i < mat.rows; ++i) {
79  for (int j = 0; j < mat.cols; ++j) {
80  labels.at<int>(i, j) = (int) segmentation[j + mat.cols*i];
81  }
82  }
83  }
84 };
85 
86 #endif /* VLSLIC_OPENCV_H */
87 
static void computeSuperpixels(const cv::Mat &mat, int region_size, double regularization, int min_region_size, int iterations, cv::Mat &labels)
Computing superpixels using vlSLIC.
Definition: vlslic_opencv.h:50
Wrapper for running vlSLIC on OpenCV images.
Definition: vlslic_opencv.h:41