Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 76 additions & 83 deletions slic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ void Slic::clear_data() {
* Initialize the cluster centers and initial values of the pixel-wise cluster
* assignment and distance values.
*
* Input : The image (IplImage*).
* Input : The image (cv::Mat).
* Output: -
*/
void Slic::init_data(IplImage *image) {
void Slic::init_data(const cv::Mat &image) {
/* Initialize the cluster and distance matrices. */
for (int i = 0; i < image->width; i++) {
for (int i = 0; i < image.cols; i++) {
vector<int> cr;
vector<double> dr;
for (int j = 0; j < image->height; j++) {
for (int j = 0; j < image.rows; j++) {
cr.push_back(-1);
dr.push_back(FLT_MAX);
}
Expand All @@ -48,12 +48,12 @@ void Slic::init_data(IplImage *image) {
}

/* Initialize the centers and counters. */
for (int i = step; i < image->width - step/2; i += step) {
for (int j = step; j < image->height - step/2; j += step) {
for (int i = step; i < image.cols - step/2; i += step) {
for (int j = step; j < image.rows - step/2; j += step) {
vector<double> center;
/* Find the local minimum (gradient-wise). */
CvPoint nc = find_local_minimum(image, cvPoint(i,j));
CvScalar colour = cvGet2D(image, nc.y, nc.x);
cv::Point nc = find_local_minimum(image, cv::Point(i,j));
cv::Vec3b colour = image.at<cv::Vec3b>(nc.y, nc.x);

/* Generate the center vector. */
center.push_back(colour.val[0]);
Expand All @@ -72,17 +72,16 @@ void Slic::init_data(IplImage *image) {
/*
* Compute the distance between a cluster center and an individual pixel.
*
* Input : The cluster index (int), the pixel (CvPoint), and the Lab values of
* the pixel (CvScalar).
* Input : The cluster index (int), the pixel (cv::Point), and the Lab values of
* the pixel (cv::Scalar).
* Output: The distance (double).
*/
double Slic::compute_dist(int ci, CvPoint pixel, CvScalar colour) {
double dc = sqrt(pow(centers[ci][0] - colour.val[0], 2) + pow(centers[ci][1]
- colour.val[1], 2) + pow(centers[ci][2] - colour.val[2], 2));
double Slic::compute_dist(int ci, cv::Point pixel, cv::Vec3b colour) {
double dc = sqrt(pow(centers[ci][0] - colour[0], 2) + pow(centers[ci][1]
- colour[1], 2) + pow(centers[ci][2] - colour[2], 2));
double ds = sqrt(pow(centers[ci][3] - pixel.x, 2) + pow(centers[ci][4] - pixel.y, 2));

return sqrt(pow(dc / nc, 2) + pow(ds / ns, 2));

//double w = 1.0 / (pow(ns / nc, 2));
//return sqrt(dc) + sqrt(ds * w);
}
Expand All @@ -91,22 +90,22 @@ double Slic::compute_dist(int ci, CvPoint pixel, CvScalar colour) {
* Find a local gradient minimum of a pixel in a 3x3 neighbourhood. This
* method is called upon initialization of the cluster centers.
*
* Input : The image (IplImage*) and the pixel center (CvPoint).
* Output: The local gradient minimum (CvPoint).
* Input : The image (cv::Mat &) and the pixel center (cv::Point).
* Output: The local gradient minimum (cv::Point).
*/
CvPoint Slic::find_local_minimum(IplImage *image, CvPoint center) {
double min_grad = FLT_MAX;
CvPoint loc_min = cvPoint(center.x, center.y);
cv::Point Slic::find_local_minimum(const cv::Mat_<cv::Vec3b> &image, cv::Point center) {
double min_grad = DBL_MAX;
cv::Point loc_min(center.x, center.y);

for (int i = center.x-1; i < center.x+2; i++) {
for (int j = center.y-1; j < center.y+2; j++) {
CvScalar c1 = cvGet2D(image, j+1, i);
CvScalar c2 = cvGet2D(image, j, i+1);
CvScalar c3 = cvGet2D(image, j, i);
cv::Vec3b c1 = image(j+1, i);
cv::Vec3b c2 = image(j, i+1);
cv::Vec3b c3 = image(j, i);
/* Convert colour values to grayscale values. */
double i1 = c1.val[0];
double i2 = c2.val[0];
double i3 = c3.val[0];
double i1 = c1[0];
double i2 = c2[0];
double i3 = c3[0];
/*double i1 = c1.val[0] * 0.11 + c1.val[1] * 0.59 + c1.val[2] * 0.3;
double i2 = c2.val[0] * 0.11 + c2.val[1] * 0.59 + c2.val[2] * 0.3;
double i3 = c3.val[0] * 0.11 + c3.val[1] * 0.59 + c3.val[2] * 0.3;*/
Expand All @@ -131,32 +130,35 @@ CvPoint Slic::find_local_minimum(IplImage *image, CvPoint center) {
* Input : The Lab image (IplImage*), the stepsize (int), and the weight (int).
* Output: -
*/
void Slic::generate_superpixels(IplImage *image, int step, int nc) {
void Slic::generate_superpixels(const cv::Mat &img, int step, int nc) {
this->step = step;
this->nc = nc;
this->ns = step;

/* make a new Mat header, that allows us to iterate the image more efficiently. */
cv::Mat_<cv::Vec3b> image(img);

/* Clear previous data (if any), and re-initialize it. */
clear_data();
init_data(image);

/* Run EM for 10 iterations (as prescribed by the algorithm). */
for (int i = 0; i < NR_ITERATIONS; i++) {
/* Reset distance values. */
for (int j = 0; j < image->width; j++) {
for (int k = 0;k < image->height; k++) {
for (int j = 0; j < image.cols; j++) {
for (int k = 0;k < image.rows; k++) {
distances[j][k] = FLT_MAX;
}
}

for (int j = 0; j < (int) centers.size(); j++) {
/* Only compare to pixels in a 2 x step by 2 x step region. */
for (int k = centers[j][3] - step; k < centers[j][3] + step; k++) {
for (int l = centers[j][4] - step; l < centers[j][4] + step; l++) {
for (int k = int(centers[j][3]) - step; k < int(centers[j][3]) + step; k++) {
for (int l = int(centers[j][4]) - step; l < int(centers[j][4]) + step; l++) {

if (k >= 0 && k < image->width && l >= 0 && l < image->height) {
CvScalar colour = cvGet2D(image, l, k);
double d = compute_dist(j, cvPoint(k,l), colour);
if (k >= 0 && k < image.cols && l >= 0 && l < image.rows) {
cv::Vec3b colour = image(l, k);
double d = compute_dist(j, cv::Point(k,l), colour);

/* Update cluster allocation if the cluster minimizes the
distance. */
Expand All @@ -176,20 +178,18 @@ void Slic::generate_superpixels(IplImage *image, int step, int nc) {
}

/* Compute the new cluster centers. */
for (int j = 0; j < image->width; j++) {
for (int k = 0; k < image->height; k++) {
for (int j = 0; j < image.cols; j++) {
for (int k = 0; k < image.rows; k++) {
int c_id = clusters[j][k];

if (c_id != -1) {
CvScalar colour = cvGet2D(image, k, j);
cv::Vec3b colour = image(k, j);

centers[c_id][0] += colour.val[0];
centers[c_id][1] += colour.val[1];
centers[c_id][2] += colour.val[2];
centers[c_id][0] += colour[0];
centers[c_id][1] += colour[1];
centers[c_id][2] += colour[2];
centers[c_id][3] += j;
centers[c_id][4] += k;

center_counts[c_id] += 1;
}
}
}
Expand All @@ -210,37 +210,37 @@ void Slic::generate_superpixels(IplImage *image, int step, int nc) {
* in the paper, but forms an active part of the implementation of the authors
* of the paper.
*
* Input : The image (IplImage*).
* Input : The image (cv::Mat).
* Output: -
*/
void Slic::create_connectivity(IplImage *image) {
void Slic::create_connectivity(const cv::Mat &image) {
int label = 0, adjlabel = 0;
const int lims = (image->width * image->height) / ((int)centers.size());
const int lims = (image.cols * image.rows) / ((int)centers.size());

const int dx4[4] = {-1, 0, 1, 0};
const int dy4[4] = { 0, -1, 0, 1};

/* Initialize the new cluster matrix. */
vec2di new_clusters;
for (int i = 0; i < image->width; i++) {
for (int i = 0; i < image.cols; i++) {
vector<int> nc;
for (int j = 0; j < image->height; j++) {
for (int j = 0; j < image.rows; j++) {
nc.push_back(-1);
}
new_clusters.push_back(nc);
}

for (int i = 0; i < image->width; i++) {
for (int j = 0; j < image->height; j++) {
for (int i = 0; i < image.cols; i++) {
for (int j = 0; j < image.rows; j++) {
if (new_clusters[i][j] == -1) {
vector<CvPoint> elements;
elements.push_back(cvPoint(i, j));
vector<cv::Point> elements;
elements.push_back(cv::Point(i, j));

/* Find an adjacent label, for possible use later. */
for (int k = 0; k < 4; k++) {
int x = elements[0].x + dx4[k], y = elements[0].y + dy4[k];

if (x >= 0 && x < image->width && y >= 0 && y < image->height) {
if (x >= 0 && x < image.cols && y >= 0 && y < image.rows) {
if (new_clusters[x][y] >= 0) {
adjlabel = new_clusters[x][y];
}
Expand All @@ -252,9 +252,9 @@ void Slic::create_connectivity(IplImage *image) {
for (int k = 0; k < 4; k++) {
int x = elements[c].x + dx4[k], y = elements[c].y + dy4[k];

if (x >= 0 && x < image->width && y >= 0 && y < image->height) {
if (x >= 0 && x < image.cols && y >= 0 && y < image.rows) {
if (new_clusters[x][y] == -1 && clusters[i][j] == clusters[x][y]) {
elements.push_back(cvPoint(x, y));
elements.push_back(cv::Point(x, y));
new_clusters[x][y] = label;
count += 1;
}
Expand All @@ -279,47 +279,47 @@ void Slic::create_connectivity(IplImage *image) {
/*
* Display the cluster centers.
*
* Input : The image to display upon (IplImage*) and the colour (CvScalar).
* Input : The image to display upon (cv::Mat) and the colour (cv::Vec3b).
* Output: -
*/
void Slic::display_center_grid(IplImage *image, CvScalar colour) {
void Slic::display_center_grid(cv::Mat &image, cv::Scalar colour) {
for (int i = 0; i < (int) centers.size(); i++) {
cvCircle(image, cvPoint(centers[i][3], centers[i][4]), 2, colour, 2);
cv::circle(image, cv::Point2d(centers[i][3], centers[i][4]), 2, colour, 2);
}
}

/*
* Display a single pixel wide contour around the clusters.
*
* Input : The target image (IplImage*) and contour colour (CvScalar).
* Input : The target image (cv::Mat) and contour colour (cv::Vec3b).
* Output: -
*/
void Slic::display_contours(IplImage *image, CvScalar colour) {
void Slic::display_contours(cv::Mat &image, cv::Vec3b colour) {
const int dx8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dy8[8] = { 0, -1, -1, -1, 0, 1, 1, 1};

/* Initialize the contour vector and the matrix detailing whether a pixel
* is already taken to be a contour. */
vector<CvPoint> contours;
vector<cv::Point> contours;
vec2db istaken;
for (int i = 0; i < image->width; i++) {
for (int i = 0; i < image.cols; i++) {
vector<bool> nb;
for (int j = 0; j < image->height; j++) {
for (int j = 0; j < image.rows; j++) {
nb.push_back(false);
}
istaken.push_back(nb);
}

/* Go through all the pixels. */
for (int i = 0; i < image->width; i++) {
for (int j = 0; j < image->height; j++) {
for (int i = 0; i < image.cols; i++) {
for (int j = 0; j < image.rows; j++) {
int nr_p = 0;

/* Compare the pixel to its 8 neighbours. */
for (int k = 0; k < 8; k++) {
int x = i + dx8[k], y = j + dy8[k];

if (x >= 0 && x < image->width && y >= 0 && y < image->height) {
if (x >= 0 && x < image.cols && y >= 0 && y < image.rows) {
if (istaken[x][y] == false && clusters[i][j] != clusters[x][y]) {
nr_p += 1;
}
Expand All @@ -328,52 +328,45 @@ void Slic::display_contours(IplImage *image, CvScalar colour) {

/* Add the pixel to the contour list if desired. */
if (nr_p >= 2) {
contours.push_back(cvPoint(i,j));
contours.push_back(cv::Point(i,j));
istaken[i][j] = true;
}
}
}

/* Draw the contour pixels. */
for (int i = 0; i < (int)contours.size(); i++) {
cvSet2D(image, contours[i].y, contours[i].x, colour);
image.at<cv::Vec3b>(contours[i].y, contours[i].x) = colour;
}
}

/*
* Give the pixels of each cluster the same colour values. The specified colour
* is the mean RGB colour per cluster.
*
* Input : The target image (IplImage*).
* Input : The target image (cv::Mat).
* Output: -
*/
void Slic::colour_with_cluster_means(IplImage *image) {
vector<CvScalar> colours(centers.size());
void Slic::colour_with_cluster_means(cv::Mat &image) {
vector<cv::Vec3b> colours(centers.size());

/* Gather the colour values per cluster. */
for (int i = 0; i < image->width; i++) {
for (int j = 0; j < image->height; j++) {
for (int i = 0; i < image.cols; i++) {
for (int j = 0; j < image.rows; j++) {
int index = clusters[i][j];
CvScalar colour = cvGet2D(image, j, i);

colours[index].val[0] += colour.val[0];
colours[index].val[1] += colour.val[1];
colours[index].val[2] += colour.val[2];
colours[index] += image.at<cv::Vec3b>(j, i);
}
}

/* Divide by the number of pixels per cluster to get the mean colour. */
for (int i = 0; i < (int)colours.size(); i++) {
colours[i].val[0] /= center_counts[i];
colours[i].val[1] /= center_counts[i];
colours[i].val[2] /= center_counts[i];
colours[i] /= center_counts[i];
}

/* Fill in. */
for (int i = 0; i < image->width; i++) {
for (int j = 0; j < image->height; j++) {
CvScalar ncolour = colours[clusters[i][j]];
cvSet2D(image, j, i, ncolour);
for (int i = 0; i < image.cols; i++) {
for (int j = 0; j < image.rows; j++) {
image.at<cv::Vec3b>(j, i) = colours[clusters[i][j]];;
}
}
}
Loading