Superpixel Benchmark
Superpixel benchmark, tools and algorithms.
ergc_opencv.h
Go to the documentation of this file.
1 
32 #ifndef ERGC_OPENCV_H
33 #define ERGC_OPENCV_H
34 
35 #include <CImg.h>
36 #include <opencv2/opencv.hpp>
37 #include "ergc.h"
38 
42 class ERGC_OpenCV {
43 public:
53  static void computeSuperpixels(const cv::Mat &image, int region_height, int region_width,
54  bool lab, bool perturb_seeds, int m, cv::Mat &labels) {
55 
56  int dx = region_width; // Seeds sampling wrt axis x (for custom grids)
57  int dy = region_height; // Seeds sampling wrt axis y (for custom grids)
58  // int m = 0; // Compacity value
59 
62  CImg<> im(image.cols, image.rows, 1, 3);
63  for (int i = 0; i < image.rows; i++) {
64  for (int j = 0; j < image.cols; j++) {
65  im(j, i, 0, 0) = image.at<cv::Vec3b>(i, j)[0];
66  im(j, i, 0, 1) = image.at<cv::Vec3b>(i, j)[1];
67  im(j, i, 0, 2) = image.at<cv::Vec3b>(i, j)[2];
68  }
69  }
70 
71  // useful variables
72  CImg<> distances;
73  CImg<int> states, seeds;
74  vector<SP*> SPs;
75 
76  // convert to Lab if needed (better superpixels with color images)
77  if (lab) {
78  im.RGBtoLab();
79  }
80 
81  CImg<> gradient;
82  if (perturb_seeds) {
83  gradient = compute_gradient(im);
84  }
85 
86  placeSeedsOnCustomGrid2d(im.width(), im.height(), dx, dy, seeds);
87 
88  if (perturb_seeds) {
89  CImg<int> perturbedSeeds;
90  if (im.depth() == 1) {
91  perturbSeeds2d(seeds, gradient, perturbedSeeds);
92  }
93  else {
94  perturbSeeds3d(seeds, gradient, perturbedSeeds);
95  }
96 
97  seeds = perturbedSeeds;
98  }
99 
100  initialize_images(seeds, distances, states);
101  SPs = initialize_superpixels(im, seeds);
102 
103  fmm3d(distances, seeds, states, im, SPs, m);
104 
105  labels.create(image.rows, image.cols, CV_32SC1);
106  for (int i = 0; i < image.rows; i++) {
107  for (int j = 0; j < image.cols; j++) {
108  labels.at<int>(i, j) = seeds(j, i, 0, 0);
109  }
110  }
111  }
112 };
113 
114 #endif /* ERGC_OPENCV_H */
115 
static void computeSuperpixels(const cv::Mat &image, int region_height, int region_width, bool lab, bool perturb_seeds, int m, cv::Mat &labels)
Computer superpixels using ERGC.
Definition: ergc_opencv.h:53
Wrapper for running ERGC on OpenCV images.
Definition: ergc_opencv.h:42