Superpixel Benchmark
Superpixel benchmark, tools and algorithms.
vc_opencv.h
Go to the documentation of this file.
1 
32 #ifndef VC_OPENCV__H
33 #define VC_OPENCV__H
34 
35 #include "VCells.h"
36 #include <opencv2/opencv.hpp>
37 
41 class VC_OpenCV {
42 public:
53  static void computeSuperpixels(const cv::Mat &image, int superpixels,
54  double weight_length, int radius, int num_nei_cluster,
55  int num_direct_nei, int threshold, cv::Mat &labels) {
56 
57  // i: height, j: width, k: channels
58  // (pBmpBuf + i * lineByte + j * 3 + k)
59  VCells vc(superpixels, weight_length);
60 // vc.pBmpBuf = new unsigned char[image.rows*image.cols*image.channels()];
61  vc.bmpWidth = image.cols;
62  vc.bmpHeight = image.rows;
63  vc.lineByte = vc.bmpWidth;
64  // lineByte = (bmpWidth * biBitCount / 8 + 3) / 4 * 4;
65 
66  struct pixel* pixelArray = new pixel[vc.bmpHeight*vc.bmpWidth];
67  for (int i = 0; i < vc.bmpHeight; i++) {
68  for (int j = 0; j < vc.bmpWidth; j++) {
69  int index = vc.getIndexFromRC(i, j);
70  pixelArray[index].color[0] = image.at<cv::Vec3b>(i, j)[0];
71  pixelArray[index].color[1] = image.at<cv::Vec3b>(i, j)[1];
72  pixelArray[index].color[2] = image.at<cv::Vec3b>(i, j)[2];
73  }
74  }
75 
76  /************************************************************************************/
77  /************ initialization ************/
78  /************************************************************************************/
79  struct centroid* generators = new centroid[superpixels];
80  vc.initializePixel(pixelArray);
81  vc.initializeGenerators(generators, pixelArray);
82  vc.classicCVT(pixelArray, generators);
83 
84  /************************************************************************************/
85  /************ VCells by EWCVT ************/
86  /************************************************************************************/
87  vc.EWCVT(generators, pixelArray);
88 
89  labels.create(image.rows, image.cols, CV_32SC1);
90  for (int i = 0; i < vc.bmpHeight; i++) {
91  for (int j = 0; j < vc.bmpWidth; j++) {
92  int index = vc.getIndexFromRC(i, j);
93  labels.at<int>(i, j) = pixelArray[index].indexCluster;
94  }
95  }
96 
97  delete[] pixelArray;
98  delete[] generators;
99  }
100 };
101 
102 #endif /* VC_OPENCV__H */
103 
static void computeSuperpixels(const cv::Mat &image, int superpixels, double weight_length, int radius, int num_nei_cluster, int num_direct_nei, int threshold, cv::Mat &labels)
Compute superpixels using VC.
Definition: vc_opencv.h:53
Wrapper for running VC on OpenCV images.
Definition: vc_opencv.h:41