Superpixel Benchmark
Superpixel benchmark, tools and algorithms.
crs_opencv.h
Go to the documentation of this file.
1 
32 #ifndef CRS_OPENCV_H
33 #define CRS_OPENCV_H
34 
35 #include <opencv2/opencv.hpp>
36 #include "FeatureType.h"
37 #include "ContourRelaxation.h"
38 #include "InitializationFunctions.h"
39 
43 class CRS_OpenCV {
44 public:
55  static void computeSuperpixels(const cv::Mat &image, int region_height,
56  int region_width, double clique_cost, double compactness,
57  int iterations, int color_space, cv::Mat &labels) {
58 
59  double diagonal_cost = clique_cost/std::sqrt(2);
60 
61  bool color_image = false;
62  if (image.channels() == 3) {
63  color_image = true;
64  }
65 
66  std::vector<FeatureType> features;
67  if (color_image) {
68  features.push_back(Color);
69  }
70  else {
71  features.push_back(Grayvalue);
72  }
73 
74  features.push_back(Compactness);
75 
76  ContourRelaxation<boost::uint16_t> contour_relaxation(features);
77  contour_relaxation.setCompactnessData(compactness);
78 
79  if (color_image) {
80 
81  cv::Mat image_YCrCb;
82  std::vector<cv::Mat> image_channels;
83 
84  switch (color_space) {
85  default:
86  case 0: // YCrCb
87  cv::cvtColor(image, image_YCrCb, CV_BGR2YCrCb);
88  cv::split(image_YCrCb, image_channels);
89  break;
90  case 1: // RGB
91  cv::split(image, image_channels);
92  break;
93  }
94 
95  contour_relaxation.setColorData(image_channels[0], image_channels[1],
96  image_channels[2]);
97  }
98  else {
99 // cv::Mat imageGray = image.clone();
100 // cv::cvtColor(imageGray, image, CV_GRAY2BGR);
101 
102  contour_relaxation.setGrayvalueData(image);
103  }
104 
105  cv::Mat label_image = createBlockInitialization<boost::uint16_t>(image.size(),
106  region_width, region_height);
107  cv::Mat relaxed_label_image;
108  cv::Mat mean_image;
109 
110  contour_relaxation.relax(label_image, clique_cost, diagonal_cost,
111  iterations, relaxed_label_image, mean_image);
112 
113  labels.create(image.rows, image.cols, CV_32SC1);
114  for (int i = 0; i < image.rows; i++) {
115  for (int j = 0; j < image.cols; j++) {
116  labels.at<int>(i, j) = relaxed_label_image.at<boost::uint16_t>(i, j);
117  }
118  }
119  }
120 };
121 
122 #endif /* CRS_OPENCV_H */
123 
Wrapper for running CRS on OpenCV images.
Definition: crs_opencv.h:43
static void computeSuperpixels(const cv::Mat &image, int region_height, int region_width, double clique_cost, double compactness, int iterations, int color_space, cv::Mat &labels)
Compute superpixels using CRS.
Definition: crs_opencv.h:55