// SLIC.cpp: implementation of the SLIC class.
//
// Copyright (C) Radhakrishna Achanta 2012
// All rights reserved
// Email: firstname.lastname@epfl.ch
//////////////////////////////////////////////////////////////////////
#include <cfloat>
#include <cmath>
#include <iostream>
#include <fstream>
#include "SLIC.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
SLIC::SLIC()
{
m_lvec = NULL;
m_avec = NULL;
m_bvec = NULL;
m_lvecvec = NULL;
m_avecvec = NULL;
m_bvecvec = NULL;
}
SLIC::~SLIC()
{
if (m_lvec) delete[] m_lvec;
if (m_avec) delete[] m_avec;
if (m_bvec) delete[] m_bvec;
if (m_lvecvec)
{
for (int d = 0; d < m_depth; d++) delete[] m_lvecvec[d];
delete[] m_lvecvec;
}
if (m_avecvec)
{
for (int d = 0; d < m_depth; d++) delete[] m_avecvec[d];
delete[] m_avecvec;
}
if (m_bvecvec)
{
for (int d = 0; d < m_depth; d++) delete[] m_bvecvec[d];
delete[] m_bvecvec;
}
}
//==============================================================================
/// RGB2XYZ
///
/// sRGB (D65 illuninant assumption) to XYZ conversion
//==============================================================================
void SLIC::RGB2XYZ(
const int& sR,
const int& sG,
const int& sB,
double& X,
double& Y,
double& Z)
{
double R = sR / 255.0;
double G = sG / 255.0;
double B = sB / 255.0;
double r, g, b;
if (R <= 0.04045) r = R / 12.92;
else r = pow((R + 0.055) / 1.055, 2.4);
if (G <= 0.04045) g = G / 12.92;
else g = pow((G + 0.055) / 1.055, 2.4);
if (B <= 0.04045) b = B / 12.92;
else b = pow((B + 0.055) / 1.055, 2.4);
X = r*0.4124564 + g*0.3575761 + b*0.1804375;
Y = r*0.2126729 + g*0.7151522 + b*0.0721750;
Z = r*0.0193339 + g*0.1191920 + b*0.9503041;
}
//===========================================================================
/// RGB2LAB
//===========================================================================
void SLIC::RGB2LAB(const int& sR, const int& sG, const int& sB, double& lval, double& aval, double& bval)
{
//------------------------
// sRGB to XYZ conversion
//------------------------
double X, Y, Z;
RGB2XYZ(sR, sG, sB, X, Y, Z);
//------------------------
// XYZ to LAB conversion
//------------------------
double epsilon = 0.008856; //actual CIE standard
double kappa = 903.3; //actual CIE standard
double Xr = 0.950456; //reference white
double Yr = 1.0; //reference white
double Zr = 1.088754; //reference white
double xr = X / Xr;
double yr = Y / Yr;
double zr = Z / Zr;
double fx, fy, fz;
if (xr > epsilon) fx = pow(xr, 1.0 / 3.0);
else fx = (kappa*xr + 16.0) / 116.0;
if (yr > epsilon) fy = pow(yr, 1.0 / 3.0);
else fy = (kappa*yr + 16.0) / 116.0;
if (zr > epsilon) fz = pow(zr, 1.0 / 3.0);
else fz = (kappa*zr + 16.0) / 116.0;
lval = 116.0*fy - 16.0;
aval = 500.0*(fx - fy);
bval = 200.0*(fy - fz);
}
//===========================================================================
/// DoRGBtoLABConversion
///
/// For whole image: overlaoded floating point version
//===========================================================================
void SLIC::DoRGBtoLABConversion(
const unsigned int*& ubuff,
double*& lvec,
double*& avec,
double*& bvec)
{
int sz = m_width*m_height;
lvec = new double[sz];
avec = new double[sz];
bvec = new double[sz];
for (int i = 0; i<m_height; i++)
{
//int r = (ubuff[j] >> 16) & 0xFF;
//int g = (ubuff[j] >> 8) & 0xFF;
//int b = (ubuff[j] ) & 0xFF;
for (int j = 0; j < m_width; j++)
{
int index = m_height*j + i;
int r = ubuff[index];
int g = ubuff[index + sz];
int b = ubuff[index + sz * 2];
//index=i*m_width+j;
RGB2LAB(r, g, b, lvec[index], avec[index], bvec[index]);
}
}
}
//===========================================================================
/// DoRGBtoLABConversion
///
/// For whole volume
//===========================================================================
void SLIC::DoRGBtoLABConversion(
unsigned int**& ubuff,
double**& lvec,
double**& avec,
double**& bvec)
{
int sz = m_width*m_height;
for (int d = 0; d < m_depth; d++)
{
for (int j = 0; j < sz; j++)
{
int r = (ubuff[d][j] >> 16) & 0xFF;
int g = (ubuff[d][j] >> 8) & 0xFF;
int b = (ubuff[d][j]) & 0xFF;
RGB2LAB(r, g, b, lvec[d][j], avec[d][j], bvec[d][j]);
}
}
}
//=================================================================================
/// DrawContoursAroundSegments
///
/// Internal contour drawing option exists. One only needs to comment the if
/// statement inside the loop that looks at neighbourhood.
//=================================================================================
void SLIC::DrawContoursAroundSegments(
unsigned int*& ubuff,
int*& labels,
const int& width,
const int& height,
const unsigned int& color)
{
const int dx8[8] = { -1, -1, 0, 1, 1, 1, 0, -1 };
const int dy8[8] = { 0, -1, -1, -1, 0, 1, 1, 1 };
/* int sz = width*height;
vector<bool> istaken(sz, false);
int mainindex(0);
for( int j = 0; j < height; j++ )
{
for( int k = 0; k < width; k++ )
{
int np(0);
for( int i = 0; i < 8; i++ )
{
int x = k + dx8[i];
int y = j + dy8[i];
if( (x >= 0 && x < width) && (y >= 0 && y < height) )
{
int index = y*width + x;
if( false == istaken[index] )//comment this to obtain internal contours
{
if( labels[mainindex] != labels[index] ) np++;
}
}
}
if( np > 1 )//change to 2 or 3 for thinner lines
{
ubuff[mainindex] = color;
istaken[mainindex] = true;
}
mainindex++;
}
}*/
int sz = width*height;
vector<bool> istaken(sz, false);
vector<int> contourx(sz); vector<int> contoury(sz);
int mainindex(0); int cind(0);
for (int j = 0; j < height; j++)
{
for (int k = 0; k < width; k++)
{
int np(0);
for (int i = 0; i < 8; i++)
{
int x = k + dx8[i];
int y = j + dy8[i];
if ((x >= 0 && x < width) && (y >= 0 && y < height))
{
int index = y*width + x;
//if( false == istaken[index] )//comment this to obtain internal contours
{
if (labels[mainindex] != labels[index]) np++;
}
}
}
if (np > 1)
{
contourx[cind] = k;
contoury[cind] = j;
istaken[mainindex] = true;
//img[mainindex] = color;
cind++;
}
mainindex++;
}
}
int numboundpix = cind;//int(contourx.size());
for (int j = 0; j < numboundpix; j++)
{
int ii = contoury[j] * width + contourx[j];
ubuff[ii] = 0xffffff;
for (int n = 0; n < 8; n++)
{
int x = contourx[j] + dx8[n];
int y = contoury[j] + dy8[n];
if ((x >= 0 && x < width) && (y >= 0 && y < height))
{
int ind = y*width + x;
if (!istaken[ind]) ubuff[ind] = 0;
}
}
}
}
//==============================================================================
/// DetectLabEdges
//==============================================================================
void SLIC::DetectLabEdges(
const double* lvec,
const double* avec,
const double* bvec,
const int& width,
const int& height,
vector<double>& edges)
{
int sz = width*height;
edges.resize(sz, 0);
for (int j = 1; j < height - 1; j++)
{
for (int k = 1; k < width - 1; k++)
{
int i = j*width + k;
double dx = (lvec[i - 1] - lvec[i + 1])*(lvec[i - 1] - lvec[i + 1]) +
(avec[i - 1] - avec[i + 1])*(avec[i - 1] - avec[i + 1]) +
(bvec[i - 1] - bvec[i + 1])*(bvec[i - 1] - bvec[i + 1]);
double dy = (lvec[i - width] - lvec[i + width])*(lvec[i - width] - lvec[i + width]) +
(avec[i - width] - avec[i + width])*(avec[i - width] - avec[i + width]) +
(bvec[i - width] - bvec[i + width])*(bvec[i - width] - bvec[i + width]);
//edges[i] = fabs(dx) + fabs(dy);
edges[i] = dx*dx + dy*dy;
}
}
}
//===========================================================================
/// PerturbSeeds
//===========================================================================
void SL
没有合适的资源?快使用搜索试试~ 我知道了~
超像素分割SLIC(简单线性迭代聚类)用opencv分割程序
共29个文件
tlog:6个
cpp:3个
obj:3个
5星 · 超过95%的资源 需积分: 35 155 下载量 177 浏览量
2017-06-22
21:45:46
上传
评论 7
收藏 12.06MB ZIP 举报
温馨提示
将SLIC作者的源码中主要算法部分的代码提取出来,并用opencv输出处理图像,方便后续的程序的处理。
资源推荐
资源详情
资源评论
收起资源包目录
用opencv做出slic分割图.zip (29个子文件)
用opencv做出slic分割图
SLIC独立代码
SLIC.cpp 36KB
SLIC独立代码.vcxproj.user 165B
SLIC独立代码.vcxproj.filters 1KB
SLIC独立代码.vcxproj 4KB
result.jpg 101KB
test.jpg 30KB
ComputeTime.cpp 746B
Debug
vc120.pdb 852KB
vc120.idb 1.32MB
SLIC独立代码.log 3KB
ComputeTime.obj 136KB
main.obj 322KB
SLIC.obj 748KB
SLIC独立代码.tlog
CL.write.1.tlog 4KB
SLIC独立代码.lastbuildstate 200B
link.command.1.tlog 5KB
CL.read.1.tlog 104KB
link.write.1.tlog 1KB
cl.command.1.tlog 6KB
link.read.1.tlog 7KB
SLIC.h 9KB
main.cpp 3KB
ComputeTime.h 354B
SLIC独立代码.v12.suo 27KB
Debug
SLIC独立代码.ilk 968KB
SLIC独立代码.pdb 2.32MB
SLIC独立代码.exe 216KB
SLIC独立代码.sdf 36.63MB
SLIC独立代码.sln 994B
共 29 条
- 1
资源评论
- 血_影2018-07-25学习下 谢谢分享
- 拾回程序猿的圈圈∞2018-03-27很好用,谢谢!竟然一点都没有报错呢~
- hzkjzdh2021-04-07非常好用,感谢分享
- 生生不息的海龟2018-12-10可以用 感谢
充数的竽
- 粉丝: 15
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功