35 #include <opencv2/opencv.hpp> 36 #include "superpixels.h" 54 int lambda,
int iterations,
int type,
float sigma,
bool color, cv::Mat &labels) {
57 cv::cvtColor(mat, image_gray, CV_BGR2GRAY);
62 cv::Sobel(image_gray, grad_x, -1, 1, 0, 3, 1, 0, cv::BORDER_DEFAULT);
63 cv::convertScaleAbs(grad_x, grad_x);
65 cv::Sobel(image_gray, grad_y, -1, 0, 1, 3, 1, 0, cv::BORDER_DEFAULT);
66 cv::convertScaleAbs(grad_y, grad_y);
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);
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++) {
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];
85 imRef(I, j, i) = color;
90 int width = I_gray->width();
91 int height = I_gray->height();
92 int num_pixels = width*height;
96 variance = computeImageVarianceColor(I, width, height);
99 variance = computeImageVariance(I_gray, width, height);
103 std::vector<int> Seeds(num_pixels);
105 PlaceSeeds(I_gray, width, height, num_pixels, Seeds, &numSeeds, region_size);
106 MoveSeedsFromEdges(I_gray, width, height, num_pixels, Seeds, numSeeds, region_size);
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);
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);
122 loadEdges(weights_horizontal, num_pixels, width, height, lambda, I_x);
123 loadEdges(weights_vertical, num_pixels, width, height, lambda, I_y);
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);
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);
146 vector<int> labeling(num_pixels);
147 initializeLabeling(labeling, width, height, Seeds, numSeeds, region_size);
149 int oldEnergy, newEnergy;
151 std::vector<int> changeMask(num_pixels, 1);
152 std::vector<int> changeMaskNew(num_pixels, 0);
154 std::vector<int> order(numSeeds);
155 for (
int i = 0; i < numSeeds; i++) {
164 newEnergy = computeEnergyColor(labeling, width, height,
165 num_pixels, weights_horizontal, weights_vertical,
166 weights_diagonal_1, weights_diagonal_2, Seeds, I, type);
169 newEnergy = computeEnergy(labeling, width, height, num_pixels,
170 weights_horizontal, weights_vertical, weights_diagonal_1,
171 weights_diagonal_2, Seeds, I_gray, type);
175 oldEnergy = newEnergy + 1;
178 if (newEnergy == oldEnergy || j >= iterations) {
182 oldEnergy = newEnergy;
184 for (
int i = 0; i < numSeeds; i++) {
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);
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);
200 for (
int i = 0; i < num_pixels; i++) {
201 changeMask[i] = changeMaskNew[i];
202 changeMaskNew[i] = 0;
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];
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