Skip to content

Commit

Permalink
Working example.
Browse files Browse the repository at this point in the history
  • Loading branch information
David authored and David committed Jul 17, 2014
1 parent 867c25a commit e08018c
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 2.8)

project(vlfeat_slic_example)

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

add_library(vlfeat_slic lib_vlfeat/vl/host.c
lib_vlfeat/vl/random.c
lib_vlfeat/vl/generic.c
lib_vlfeat/vl/slic.c)
add_subdirectory(vlfeat_slic_cli)
6 changes: 6 additions & 0 deletions vlfeat_slic_cli/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
include_directories(../lib_vlfeat/)

find_package(OpenCV REQUIRED)

add_executable(vlfeat_slic_example main.cpp)
target_link_libraries(vlfeat_slic_example ${OpenCV_LIBS} vlfeat_slic)
93 changes: 93 additions & 0 deletions vlfeat_slic_cli/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// OpenCV can be used to read images.
#include <opencv2/opencv.hpp>

// The VLFeat header files need to be declared external.
extern "C" {
#include "vl/generic.h"
#include "vl/slic.h"
}

int main() {
// Read the Lenna image. The matrix 'mat' will have 3 8 bit channels
// corresponding to BGR color space.
cv::Mat mat = cv::imread("Lenna.png", CV_LOAD_IMAGE_COLOR);

// Convert image to one-dimensional array.
float* image = new float[mat.rows*mat.cols*mat.channels()];
for (int i = 0; i < mat.rows; ++i) {
for (int j = 0; j < mat.cols; ++j) {
// Assuming three channels ...
image[j + mat.cols*i + mat.cols*mat.rows*0] = mat.at<cv::Vec3b>(i, j)[0];
image[j + mat.cols*i + mat.cols*mat.rows*1] = mat.at<cv::Vec3b>(i, j)[1];
image[j + mat.cols*i + mat.cols*mat.rows*2] = mat.at<cv::Vec3b>(i, j)[2];
}
}

// The algorithm will store the final segmentation in a one-dimensional array.
vl_uint32* segmentation = new vl_uint32[mat.rows*mat.cols];
vl_size height = mat.rows;
vl_size width = mat.cols;
vl_size channels = mat.channels();

// The region size defines the number of superpixels obtained.
// Regularization describes a trade-off between the color term and the
// spatial term.
vl_size region = 30;
float regularization = 1000.;
vl_size minRegion = 10;

vl_slic_segment(segmentation, image, width, height, channels, region, regularization, minRegion);

// Convert segmentation.
int** labels = new int*[mat.rows];
for (int i = 0; i < mat.rows; ++i) {
labels[i] = new int[mat.cols];

for (int j = 0; j < mat.cols; ++j) {
labels[i][j] = (int) segmentation[j + mat.cols*i];
}
}

int label = 0;
int labelTop = -1;
int labelBottom = -1;
int labelLeft = -1;
int labelRight = -1;

for (int i = 0; i < mat.rows; i++) {
for (int j = 0; j < mat.cols; j++) {

label = labels[i][j];

labelTop = label;
if (i > 0) {
labelTop = labels[i - 1][j];
}

labelBottom = label;
if (i < mat.rows - 1) {
labelBottom = labels[i + 1][j];
}

labelLeft = label;
if (j > 0) {
labelLeft = labels[i][j - 1];
}

labelRight = label;
if (j < mat.cols - 1) {
labelRight = labels[i][j + 1];
}

if (label != labelTop || label != labelBottom || label!= labelLeft || label != labelRight) {
mat.at<cv::Vec3b>(i, j)[0] = 0;
mat.at<cv::Vec3b>(i, j)[1] = 0;
mat.at<cv::Vec3b>(i, j)[2] = 255;
}
}
}

cv::imwrite("Lenna_contours.png", mat);

return 0;
}

0 comments on commit e08018c

Please sign in to comment.