}
//=========================================================================== /// GetKValues_LABXYZ ///
/// The k seed values are taken as uniform spatial pixel samples.
//=========================================================================== void SLIC::GetKValues_LABXYZ( {
const bool hexgrid = false;
int xerr = m_width - STEP*xstrips;if(xerr < 0){xstrips--;xerr = m_width - STEP*xstrips;} int yerr = m_height - STEP*ystrips;if(yerr < 0){ystrips--;yerr = m_height- STEP*ystrips;} int zerr = m_depth - STEP*zstrips;if(zerr < 0){zstrips--;zerr = m_depth - STEP*zstrips;}
int xoff = STEP/2; int yoff = STEP/2; int zoff = STEP/2;
double xerrperstrip = double(xerr)/double(xstrips); double yerrperstrip = double(yerr)/double(ystrips); double zerrperstrip = double(zerr)/double(zstrips); int xstrips = (0.5+double(m_width)/double(STEP)); int ystrips = (0.5+double(m_height)/double(STEP)); int zstrips = (0.5+double(m_depth)/double(STEP)); int numseeds(0); int n(0);
vector
kseedsl, kseedsa, kseedsb, kseedsx, kseedsy, kseedsz, STEP)
if(perturbseeds) { }
PerturbSeeds(kseedsl, kseedsa, kseedsb, kseedsx, kseedsy, edgemag);
const int&
}
//-------------------------
numseeds = xstrips*ystrips*zstrips; //------------------------- kseedsl.resize(numseeds); kseedsa.resize(numseeds); kseedsb.resize(numseeds); kseedsx.resize(numseeds); kseedsy.resize(numseeds); kseedsz.resize(numseeds);
for( int z = 0; z < zstrips; z++ ) { }
int ze = z*zerrperstrip; int d = (z*STEP+zoff+ze);
for( int y = 0; y < ystrips; y++ ) { }
int ye = y*yerrperstrip;
for( int x = 0; x < xstrips; x++ ) { }
int xe = x*xerrperstrip;
int i = (y*STEP+yoff+ye)*m_width + (x*STEP+xoff+xe);
kseedsl[n] = m_lvecvec[d][i]; kseedsa[n] = m_avecvec[d][i]; kseedsb[n] = m_bvecvec[d][i]; kseedsx[n] = (x*STEP+xoff+xe); kseedsy[n] = (y*STEP+yoff+ye); kseedsz[n] = d; n++;
//=========================================================================== /// PerformSuperpixelSLIC ///
/// Performs k mean segmentation. It is fast because it looks locally, not /// over the entire image.
//=========================================================================== void SLIC::PerformSuperpixelSLIC(
vector
M)
kseedsl, kseedsa, kseedsb, kseedsx, kseedsy, STEP,
int*& klabels,
const int& {
const double&
const vector
int sz = m_width*m_height; const int numk = kseedsl.size(); //---------------- int offset = STEP; //----------------
vector
vector
for( int itr = 0; itr < 10; itr++ ) {
distvec.assign(sz, DBL_MAX); for( int n = 0; n < numk; n++ ) {
kseedsy[n]-offset);
kseedsy[n]+offset); kseedsx[n]-offset);
//if(STEP < 8) offset = STEP*1.5;//to prevent a crash due to a very small step size
y1 = max(0.0, x1 = max(0.0,
y2 = min((double)m_height,
x2 = min((double)m_width,
sigmal.assign(numk, 0); sigmaa.assign(numk, 0); }
}
}
if( dist < distvec[i] ) { }
distvec[i] = dist; klabels[i] = n;
distxy =
dist =
l = m_lvec[i]; a = m_avec[i]; b = m_bvec[i];
for( int y = y1; y < y2; y++ ) {
for( int x = x1; x < x2; x++ ) {
int i = y*m_width + x;
kseedsx[n]+offset);
(l - kseedsl[n])*(l - kseedsl[n]) + (a - kseedsa[n])*(a - kseedsa[n]) + (b - kseedsb[n])*(b - kseedsb[n]);
(x - kseedsx[n])*(x - kseedsx[n]) +
(y - kseedsy[n])*(y - kseedsy[n]);
//------------------------------------------------------------------------
dist += distxy*invwt;//dist = sqrt(dist) + sqrt(distxy*invwt);//this is
more exact
//------------------------------------------------------------------------
//----------------------------------------------------------------- // Recalculate the centroid and store in the seed values
//----------------------------------------------------------------- //instead of reassigning memory on each iteration, just reset.
sigmab.assign(numk, 0); sigmax.assign(numk, 0); sigmay.assign(numk, 0); clustersize.assign(numk, 0);
//------------------------------------ //edgesum.assign(numk, 0);
//------------------------------------ {int ind(0);
for( int r = 0; r < m_height; r++ ) { }}
{for( int k = 0; k < numk; k++ ) { }}
{for( int k = 0; k < numk; k++ ) {
kseedsl[k] = sigmal[k]*inv[k]; kseedsa[k] = sigmaa[k]*inv[k]; kseedsb[k] = sigmab[k]*inv[k]; kseedsx[k] = sigmax[k]*inv[k]; kseedsy[k] = sigmay[k]*inv[k];
//------------------------------------ //edgesum[k] *= inv[k];
if( clustersize[k] <= 0 ) clustersize[k] = 1;
inv[k] = 1.0/clustersize[k];//computing inverse now to multiply, than divide later for( int c = 0; c < m_width; c++ ) { }
sigmal[klabels[ind]] += m_lvec[ind]; sigmaa[klabels[ind]] += m_avec[ind]; sigmab[klabels[ind]] += m_bvec[ind]; sigmax[klabels[ind]] += c; sigmay[klabels[ind]] += r;
//------------------------------------ //edgesum[klabels[ind]] += edgemag[ind]; //------------------------------------ clustersize[klabels[ind]] += 1.0; ind++;
搜索“diyifanwen.net”或“第一范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,第一范文网,提供最新IT计算机SLIC超像素分割算法和目前超像素算法的比较代码实现 - 图文 (6)全文阅读和word下载服务。
相关推荐: