资源简介
slic超像素分割方法源代码。c++实现,简洁明了
代码片段和文件信息
#include “slic.h“
/*
* Constructor. Nothing is done here.
*/
Slic::Slic() {
}
/*
* Destructor. Clear any present data.
*/
Slic::~Slic() {
clear_data();
}
/*
* Clear the data as saved by the algorithm.
*
* Input : -
* Output: -
*/
void Slic::clear_data() {
clusters.clear();
distances.clear();
centers.clear();
center_counts.clear();
}
/*
* Initialize the cluster centers and initial values of the pixel-wise cluster
* assignment and distance values.
*
* Input : The image (IplImage*).
* Output: -
*/
void Slic::init_data(IplImage *image) {
/* Initialize the cluster and distance matrices. */
for (int i = 0; i < image->width; i++) {
vector cr;
vector dr;
for (int j = 0; j < image->height; j++) {
cr.push_back(-1);
dr.push_back(FLT_MAX);
}
clusters.push_back(cr);
distances.push_back(dr);
}
/* 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) {
vector center;
/* Find the local minimum (gradient-wise). */
CvPoint nc = find_local_minimum(image cvPoint(ij));
CvScalar colour = cvGet2D(image nc.y nc.x);
/* Generate the center vector. */
center.push_back(colour.val[0]);
center.push_back(colour.val[1]);
center.push_back(colour.val[2]);
center.push_back(nc.x);
center.push_back(nc.y);
/* Append to vector of centers. */
centers.push_back(center);
center_counts.push_back(0);
}
}
}
/*
* 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).
* 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 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);
}
/*
* 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).
*/
CvPoint Slic::find_local_minimum(IplImage *image CvPoint center) {
double min_grad = FLT_MAX;
CvPoint loc_min = cvPoint(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
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2013-05-12 20:32 SLIC-Superpixels-master\
文件 611 2013-05-12 20:32 SLIC-Superpixels-master\README.md
文件 164271 2013-05-12 20:32 SLIC-Superpixels-master\dog.png
文件 57263 2013-05-12 20:32 SLIC-Superpixels-master\dog_segmentation.png
文件 12759 2013-05-12 20:32 SLIC-Superpixels-master\slic.cpp
文件 2515 2013-05-12 20:32 SLIC-Superpixels-master\slic.h
文件 1217 2013-05-12 20:32 SLIC-Superpixels-master\test_slic.cpp
评论
共有 条评论