Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduced 30% time by changing 2d vectors to cv::Mat #5

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
cleanup
  • Loading branch information
berak authored and k-okada committed Aug 30, 2016
commit ba5a0ac7f8b20ece704165f2a82e6f383ae106ad
10 changes: 5 additions & 5 deletions slic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void Slic::init_data(const cv::Mat &image) {
vector<double> center;
/* Find the local minimum (gradient-wise). */
cv::Point nc = find_local_minimum(image, cv::Point(i,j));
cv::Vec3b colour = image.at<Vec3b>(nc.y, nc.x);
cv::Vec3b colour = image.at<cv::Vec3b>(nc.y, nc.x);

/* Generate the center vector. */
center.push_back(colour.val[0]);
Expand Down Expand Up @@ -136,7 +136,7 @@ void Slic::generate_superpixels(const cv::Mat &img, int step, int nc) {
this->ns = step;

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

/* Clear previous data (if any), and re-initialize it. */
clear_data();
Expand Down Expand Up @@ -336,7 +336,7 @@ void Slic::display_contours(cv::Mat &image, cv::Vec3b colour) {

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

Expand All @@ -354,7 +354,7 @@ void Slic::colour_with_cluster_means(cv::Mat &image) {
for (int i = 0; i < image.cols; i++) {
for (int j = 0; j < image.rows; j++) {
int index = clusters[i][j];
colours[index] += image.at<Vec3b>(j, i);
colours[index] += image.at<cv::Vec3b>(j, i);
}
}

Expand All @@ -366,7 +366,7 @@ void Slic::colour_with_cluster_means(cv::Mat &image) {
/* Fill in. */
for (int i = 0; i < image.cols; i++) {
for (int j = 0; j < image.rows; j++) {
image.at<Vec3b>(j, i) = colours[clusters[i][j]];;
image.at<cv::Vec3b>(j, i) = colours[clusters[i][j]];;
}
}
}
4 changes: 2 additions & 2 deletions slic.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
using namespace std;

#include <opencv2/opencv.hpp>
using namespace cv;
//using namespace cv;

/* 2d matrices are handled by 2d vectors. */
#define vec2dd vector<vector<double> >
Expand Down Expand Up @@ -54,7 +54,7 @@ class Slic {
/* Compute the distance between a center and an individual pixel. */
double compute_dist(int ci, cv::Point pixel, cv::Vec3b colour);
/* Find the pixel with the lowest gradient in a 3x3 surrounding. */
cv::Point find_local_minimum(const cv::Mat_<Vec3b> &image, cv::Point center);
cv::Point find_local_minimum(const cv::Mat_<cv::Vec3b> &image, cv::Point center);

/* Remove and initialize the 2d vectors. */
void clear_data();
Expand Down