123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332 |
- #include <cfloat>
- #include <cmath>
- #include <iostream>
- #include <fstream>
- #include "SLIC.h"
- 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;
- }
- }
- 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;
- }
- void SLIC::RGB2LAB(const int& sR, const int& sG, const int& sB, double& lval, double& aval, double& bval)
- {
-
-
-
- double X, Y, Z;
- RGB2XYZ(sR, sG, sB, X, Y, Z);
-
-
-
- double epsilon = 0.008856;
- double kappa = 903.3;
- double Xr = 0.950456;
- double Yr = 1.0;
- double Zr = 1.088754;
- 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);
- }
- 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 j = 0; j < sz; j++ )
- {
- int r = (ubuff[j] >> 16) & 0xFF;
- int g = (ubuff[j] >> 8) & 0xFF;
- int b = (ubuff[j] ) & 0xFF;
- RGB2LAB( r, g, b, lvec[j], avec[j], bvec[j] );
- }
- }
- 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] );
- }
- }
- }
- 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);
- 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 ( labels[mainindex] != labels[index] ) np++;
- }
- }
- }
- if ( np > 1 )
- {
- contourx[cind] = k;
- contoury[cind] = j;
- istaken[mainindex] = true;
-
- cind++;
- }
- mainindex++;
- }
- }
- int numboundpix = cind;
- 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;
- }
- }
- }
- }
- 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] = dx*dx + dy*dy;
- }
- }
- }
- void SLIC::PerturbSeeds(
- vector<double>& kseedsl,
- vector<double>& kseedsa,
- vector<double>& kseedsb,
- vector<double>& kseedsx,
- vector<double>& kseedsy,
- const vector<double>& edges)
- {
- 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 numseeds = kseedsl.size();
- for ( int n = 0; n < numseeds; n++ )
- {
- int ox = kseedsx[n];
- int oy = kseedsy[n];
- int oind = oy*m_width + ox;
- int storeind = oind;
- for ( int i = 0; i < 8; i++ )
- {
- int nx = ox+dx8[i];
- int ny = oy+dy8[i];
- if ( nx >= 0 && nx < m_width && ny >= 0 && ny < m_height)
- {
- int nind = ny*m_width + nx;
- if ( edges[nind] < edges[storeind])
- {
- storeind = nind;
- }
- }
- }
- if (storeind != oind)
- {
- kseedsx[n] = storeind%m_width;
- kseedsy[n] = storeind/m_width;
- kseedsl[n] = m_lvec[storeind];
- kseedsa[n] = m_avec[storeind];
- kseedsb[n] = m_bvec[storeind];
- }
- }
- }
- void SLIC::GetLABXYSeeds_ForGivenStepSize(
- vector<double>& kseedsl,
- vector<double>& kseedsa,
- vector<double>& kseedsb,
- vector<double>& kseedsx,
- vector<double>& kseedsy,
- const int& STEP,
- const bool& perturbseeds,
- const vector<double>& edgemag)
- {
- const bool hexgrid = false;
- int numseeds(0);
- int n(0);
-
-
- int xstrips = (0.5+double(m_width)/double(STEP));
- int ystrips = (0.5+double(m_height)/double(STEP));
- 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;
- }
- double xerrperstrip = double(xerr)/double(xstrips);
- double yerrperstrip = double(yerr)/double(ystrips);
- int xoff = STEP/2;
- int yoff = STEP/2;
-
- numseeds = xstrips*ystrips;
-
- kseedsl.resize(numseeds);
- kseedsa.resize(numseeds);
- kseedsb.resize(numseeds);
- kseedsx.resize(numseeds);
- kseedsy.resize(numseeds);
- for ( int y = 0; y < ystrips; y++ )
- {
- int ye = y*yerrperstrip;
- for ( int x = 0; x < xstrips; x++ )
- {
- int xe = x*xerrperstrip;
- int seedx = (x*STEP+xoff+xe);
- if (hexgrid) {
- seedx = x*STEP+(xoff<<(y&0x1))+xe;
- seedx = min(m_width-1,seedx);
- }
- int seedy = (y*STEP+yoff+ye);
- int i = seedy*m_width + seedx;
- kseedsl[n] = m_lvec[i];
- kseedsa[n] = m_avec[i];
- kseedsb[n] = m_bvec[i];
- kseedsx[n] = seedx;
- kseedsy[n] = seedy;
- n++;
- }
- }
- if (perturbseeds)
- {
- PerturbSeeds(kseedsl, kseedsa, kseedsb, kseedsx, kseedsy, edgemag);
- }
- }
- void SLIC::GetKValues_LABXYZ(
- vector<double>& kseedsl,
- vector<double>& kseedsa,
- vector<double>& kseedsb,
- vector<double>& kseedsx,
- vector<double>& kseedsy,
- vector<double>& kseedsz,
- const int& STEP)
- {
- const bool hexgrid = false;
- int numseeds(0);
- int n(0);
- 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 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;
- }
- double xerrperstrip = double(xerr)/double(xstrips);
- double yerrperstrip = double(yerr)/double(ystrips);
- double zerrperstrip = double(zerr)/double(zstrips);
- int xoff = STEP/2;
- int yoff = STEP/2;
- int zoff = STEP/2;
-
- 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++;
- }
- }
- }
- }
- void SLIC::PerformSuperpixelSLIC(
- vector<double>& kseedsl,
- vector<double>& kseedsa,
- vector<double>& kseedsb,
- vector<double>& kseedsx,
- vector<double>& kseedsy,
- int*& klabels,
- const int& STEP,
- const vector<double>& edgemag,
- const double& M)
- {
- int sz = m_width*m_height;
- const int numk = kseedsl.size();
-
- int offset = STEP;
-
-
- vector<double> clustersize(numk, 0);
- vector<double> inv(numk, 0);
- vector<double> sigmal(numk, 0);
- vector<double> sigmaa(numk, 0);
- vector<double> sigmab(numk, 0);
- vector<double> sigmax(numk, 0);
- vector<double> sigmay(numk, 0);
- vector<double> distvec(sz, DBL_MAX);
- double invwt = 1.0/((STEP/M)*(STEP/M));
- int x1, y1, x2, y2;
- double l, a, b;
- double dist;
- double distxy;
- for ( int itr = 0; itr < 10; itr++ )
- {
- distvec.assign(sz, DBL_MAX);
- for ( int n = 0; n < numk; n++ )
- {
- y1 = max(0.0, kseedsy[n]-offset);
- y2 = min((double)m_height, kseedsy[n]+offset);
- x1 = max(0.0, kseedsx[n]-offset);
- x2 = min((double)m_width, kseedsx[n]+offset);
- for ( int y = y1; y < y2; y++ )
- {
- for ( int x = x1; x < x2; x++ )
- {
- int i = y*m_width + x;
- l = m_lvec[i];
- a = m_avec[i];
- b = m_bvec[i];
- dist = (l - kseedsl[n])*(l - kseedsl[n]) +
- (a - kseedsa[n])*(a - kseedsa[n]) +
- (b - kseedsb[n])*(b - kseedsb[n]);
- distxy = (x - kseedsx[n])*(x - kseedsx[n]) +
- (y - kseedsy[n])*(y - kseedsy[n]);
-
- dist += distxy*invwt;
-
- if ( dist < distvec[i] )
- {
- distvec[i] = dist;
- klabels[i] = n;
- }
- }
- }
- }
-
-
-
-
- sigmal.assign(numk, 0);
- sigmaa.assign(numk, 0);
- sigmab.assign(numk, 0);
- sigmax.assign(numk, 0);
- sigmay.assign(numk, 0);
- clustersize.assign(numk, 0);
-
-
-
- {
- int ind(0);
- for ( int r = 0; r < m_height; r++ )
- {
- 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;
-
-
-
- clustersize[klabels[ind]] += 1.0;
- ind++;
- }
- }
- }
- {
- for ( int k = 0; k < numk; k++ )
- {
- if ( clustersize[k] <= 0 ) clustersize[k] = 1;
- inv[k] = 1.0/clustersize[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];
-
-
-
- }
- }
- }
- }
- void SLIC::PerformSupervoxelSLIC(
- vector<double>& kseedsl,
- vector<double>& kseedsa,
- vector<double>& kseedsb,
- vector<double>& kseedsx,
- vector<double>& kseedsy,
- vector<double>& kseedsz,
- int**& klabels,
- const int& STEP,
- const double& compactness)
- {
- int sz = m_width*m_height;
- const int numk = kseedsl.size();
-
-
- int offset = STEP;
-
-
- vector<double> clustersize(numk, 0);
- vector<double> inv(numk, 0);
- vector<double> sigmal(numk, 0);
- vector<double> sigmaa(numk, 0);
- vector<double> sigmab(numk, 0);
- vector<double> sigmax(numk, 0);
- vector<double> sigmay(numk, 0);
- vector<double> sigmaz(numk, 0);
- vector< double > initdouble(sz, DBL_MAX);
- vector< vector<double> > distvec(m_depth, initdouble);
-
- double invwt = 1.0/((STEP/compactness)*(STEP/compactness));
- int x1, y1, x2, y2, z1, z2;
- double l, a, b;
- double dist;
- double distxyz;
- for ( int itr = 0; itr < 5; itr++ )
- {
- distvec.assign(m_depth, initdouble);
- for ( int n = 0; n < numk; n++ )
- {
- y1 = max(0.0, kseedsy[n]-offset);
- y2 = min((double)m_height, kseedsy[n]+offset);
- x1 = max(0.0, kseedsx[n]-offset);
- x2 = min((double)m_width, kseedsx[n]+offset);
- z1 = max(0.0, kseedsz[n]-offset);
- z2 = min((double)m_depth, kseedsz[n]+offset);
- for ( int z = z1; z < z2; z++ )
- {
- for ( int y = y1; y < y2; y++ )
- {
- for ( int x = x1; x < x2; x++ )
- {
- int i = y*m_width + x;
- l = m_lvecvec[z][i];
- a = m_avecvec[z][i];
- b = m_bvecvec[z][i];
- dist = (l - kseedsl[n])*(l - kseedsl[n]) +
- (a - kseedsa[n])*(a - kseedsa[n]) +
- (b - kseedsb[n])*(b - kseedsb[n]);
- distxyz = (x - kseedsx[n])*(x - kseedsx[n]) +
- (y - kseedsy[n])*(y - kseedsy[n]) +
- (z - kseedsz[n])*(z - kseedsz[n]);
-
- dist += distxyz*invwt;
-
- if ( dist < distvec[z][i] )
- {
- distvec[z][i] = dist;
- klabels[z][i] = n;
- }
- }
- }
- }
- }
-
-
-
-
- sigmal.assign(numk, 0);
- sigmaa.assign(numk, 0);
- sigmab.assign(numk, 0);
- sigmax.assign(numk, 0);
- sigmay.assign(numk, 0);
- sigmaz.assign(numk, 0);
- clustersize.assign(numk, 0);
- for ( int d = 0; d < m_depth; d++ )
- {
- int ind(0);
- for ( int r = 0; r < m_height; r++ )
- {
- for ( int c = 0; c < m_width; c++ )
- {
- sigmal[klabels[d][ind]] += m_lvecvec[d][ind];
- sigmaa[klabels[d][ind]] += m_avecvec[d][ind];
- sigmab[klabels[d][ind]] += m_bvecvec[d][ind];
- sigmax[klabels[d][ind]] += c;
- sigmay[klabels[d][ind]] += r;
- sigmaz[klabels[d][ind]] += d;
- clustersize[klabels[d][ind]] += 1.0;
- ind++;
- }
- }
- }
- {
- for ( int k = 0; k < numk; k++ )
- {
- if ( clustersize[k] <= 0 ) clustersize[k] = 1;
- inv[k] = 1.0/clustersize[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];
- kseedsz[k] = sigmaz[k]*inv[k];
- }
- }
- }
- }
- void SLIC::SaveSuperpixelLabels(
- const int*& labels,
- const int& width,
- const int& height,
- const string& filename,
- const string& path)
- {
- #ifdef WINDOWS
- char fname[256];
- char extn[256];
- _splitpath(filename.c_str(), NULL, NULL, fname, extn);
- string temp = fname;
- string finalpath = path + temp + string(".dat");
- #else
- string nameandextension = filename;
- size_t pos = filename.find_last_of("/");
- if (pos != string::npos)
- {
- nameandextension = filename.substr(pos+1);
- }
- string newname = nameandextension.replace(nameandextension.rfind(".")+1, 3, "dat");
- string finalpath = path+newname;
- #endif
- int sz = width*height;
- ofstream outfile;
- outfile.open(finalpath.c_str(), ios::binary);
- for ( int i = 0; i < sz; i++ )
- {
- outfile.write((const char*)&labels[i], sizeof(int));
- }
- outfile.close();
- }
- void SLIC::SaveSupervoxelLabels(
- const int**& labels,
- const int& width,
- const int& height,
- const int& depth,
- const string& filename,
- const string& path)
- {
- #ifdef WINDOWS
- char fname[256];
- char extn[256];
- _splitpath(filename.c_str(), NULL, NULL, fname, extn);
- string temp = fname;
- string finalpath = path + temp + string(".dat");
- #else
- string nameandextension = filename;
- size_t pos = filename.find_last_of("/");
- if (pos != string::npos)
- {
- nameandextension = filename.substr(pos+1);
- }
- string newname = nameandextension.replace(nameandextension.rfind(".")+1, 3, "dat");
- string finalpath = path+newname;
- #endif
- int sz = width*height;
- ofstream outfile;
- outfile.open(finalpath.c_str(), ios::binary);
- for ( int d = 0; d < depth; d++ )
- {
- for ( int i = 0; i < sz; i++ )
- {
- outfile.write((const char*)&labels[d][i], sizeof(int));
- }
- }
- outfile.close();
- }
- void SLIC::EnforceLabelConnectivity(
- const int* labels,
- const int width,
- const int height,
- int*& nlabels,
- int& numlabels,
- const int& K)
- {
- const int dx4[4] = {-1, 0, 1, 0};
- const int dy4[4] = { 0, -1, 0, 1};
- const int sz = width*height;
- const int SUPSZ = sz/K;
-
- for ( int i = 0; i < sz; i++ ) nlabels[i] = -1;
- int label(0);
- int* xvec = new int[sz];
- int* yvec = new int[sz];
- int oindex(0);
- int adjlabel(0);
- for ( int j = 0; j < height; j++ )
- {
- for ( int k = 0; k < width; k++ )
- {
- if ( 0 > nlabels[oindex] )
- {
- nlabels[oindex] = label;
-
-
-
- xvec[0] = k;
- yvec[0] = j;
-
-
-
- {
- for ( int n = 0; n < 4; n++ )
- {
- int x = xvec[0] + dx4[n];
- int y = yvec[0] + dy4[n];
- if ( (x >= 0 && x < width) && (y >= 0 && y < height) )
- {
- int nindex = y*width + x;
- if (nlabels[nindex] >= 0) adjlabel = nlabels[nindex];
- }
- }
- }
- int count(1);
- for ( int c = 0; c < count; c++ )
- {
- for ( int n = 0; n < 4; n++ )
- {
- int x = xvec[c] + dx4[n];
- int y = yvec[c] + dy4[n];
- if ( (x >= 0 && x < width) && (y >= 0 && y < height) )
- {
- int nindex = y*width + x;
- if ( 0 > nlabels[nindex] && labels[oindex] == labels[nindex] )
- {
- xvec[count] = x;
- yvec[count] = y;
- nlabels[nindex] = label;
- count++;
- }
- }
- }
- }
-
-
-
-
- if (count <= SUPSZ >> 2)
- {
- for ( int c = 0; c < count; c++ )
- {
- int ind = yvec[c]*width+xvec[c];
- nlabels[ind] = adjlabel;
- }
- label--;
- }
- label++;
- }
- oindex++;
- }
- }
- numlabels = label;
- if (xvec) delete [] xvec;
- if (yvec) delete [] yvec;
- }
- void SLIC::EnforceSupervoxelLabelConnectivity(
- int**& labels,
- const int& width,
- const int& height,
- const int& depth,
- int& numlabels,
- const int& STEP)
- {
- const int dx10[10] = {-1, 0, 1, 0, -1, 1, 1, -1, 0, 0};
- const int dy10[10] = { 0, -1, 0, 1, -1, -1, 1, 1, 0, 0};
- const int dz10[10] = { 0, 0, 0, 0, 0, 0, 0, 0, -1, 1};
- int sz = width*height;
- const int SUPSZ = STEP*STEP*STEP;
- int adjlabel(0);
- int* xvec = new int[SUPSZ*10];
- int* yvec = new int[SUPSZ*10];
- int* zvec = new int[SUPSZ*10];
-
-
-
- int** nlabels = new int*[depth];
- {
- for ( int d = 0; d < depth; d++ )
- {
- nlabels[d] = new int[sz];
- for ( int i = 0; i < sz; i++ ) nlabels[d][i] = -1;
- }
- }
-
-
-
- int lab(0);
- {
- for ( int d = 0; d < depth; d++ )
- {
- int i(0);
- for ( int h = 0; h < height; h++ )
- {
- for ( int w = 0; w < width; w++ )
- {
- if (nlabels[d][i] < 0)
- {
- nlabels[d][i] = lab;
-
-
-
- {
- for ( int n = 0; n < 10; n++ )
- {
- int x = w + dx10[n];
- int y = h + dy10[n];
- int z = d + dz10[n];
- if ( (x >= 0 && x < width) && (y >= 0 && y < height) && (z >= 0 && z < depth) )
- {
- int nindex = y*width + x;
- if (nlabels[z][nindex] >= 0)
- {
- adjlabel = nlabels[z][nindex];
- }
- }
- }
- }
- xvec[0] = w;
- yvec[0] = h;
- zvec[0] = d;
- int count(1);
- for ( int c = 0; c < count; c++ )
- {
- for ( int n = 0; n < 10; n++ )
- {
- int x = xvec[c] + dx10[n];
- int y = yvec[c] + dy10[n];
- int z = zvec[c] + dz10[n];
- if ( (x >= 0 && x < width) && (y >= 0 && y < height) && (z >= 0 && z < depth))
- {
- int nindex = y*width + x;
- if ( 0 > nlabels[z][nindex] && labels[d][i] == labels[z][nindex] )
- {
- xvec[count] = x;
- yvec[count] = y;
- zvec[count] = z;
- nlabels[z][nindex] = lab;
- count++;
- }
- }
- }
- }
-
-
-
-
- if (count <= (SUPSZ >> 2))
- {
- for ( int c = 0; c < count; c++ )
- {
- int ind = yvec[c]*width+xvec[c];
- nlabels[zvec[c]][ind] = adjlabel;
- }
- lab--;
- }
-
- lab++;
- }
- i++;
- }
- }
- }
- }
-
-
-
- {
- for ( int d = 0; d < depth; d++ )
- {
- for ( int i = 0; i < sz; i++ ) labels[d][i] = nlabels[d][i];
- }
- }
- {
- for ( int d = 0; d < depth; d++ )
- {
- delete [] nlabels[d];
- }
- }
- delete [] nlabels;
-
- if (xvec) delete [] xvec;
- if (yvec) delete [] yvec;
- if (zvec) delete [] zvec;
-
- numlabels = lab;
-
- }
- void SLIC::DoSuperpixelSegmentation_ForGivenSuperpixelSize(
- const unsigned int* ubuff,
- const int width,
- const int height,
- int*& klabels,
- int& numlabels,
- const int& superpixelsize,
- const double& compactness, bool lab)
- {
-
- const int STEP = sqrt(double(superpixelsize))+0.5;
-
- vector<double> kseedsl(0);
- vector<double> kseedsa(0);
- vector<double> kseedsb(0);
- vector<double> kseedsx(0);
- vector<double> kseedsy(0);
-
- m_width = width;
- m_height = height;
- int sz = m_width*m_height;
-
-
- klabels = new int[sz];
- for ( int s = 0; s < sz; s++ ) klabels[s] = -1;
-
- if (lab)
- {
- DoRGBtoLABConversion(ubuff, m_lvec, m_avec, m_bvec);
- }
- else
- {
- m_lvec = new double[sz];
- m_avec = new double[sz];
- m_bvec = new double[sz];
- for ( int i = 0; i < sz; i++ )
- {
- m_lvec[i] = ubuff[i] >> 16 & 0xff;
- m_avec[i] = ubuff[i] >> 8 & 0xff;
- m_bvec[i] = ubuff[i] & 0xff;
- }
- }
-
- bool perturbseeds(false);
- vector<double> edgemag(0);
- if (perturbseeds) DetectLabEdges(m_lvec, m_avec, m_bvec, m_width, m_height, edgemag);
- GetLABXYSeeds_ForGivenStepSize(kseedsl, kseedsa, kseedsb, kseedsx, kseedsy, STEP, perturbseeds, edgemag);
- PerformSuperpixelSLIC(kseedsl, kseedsa, kseedsb, kseedsx, kseedsy, klabels, STEP, edgemag,compactness);
- numlabels = kseedsl.size();
- int* nlabels = new int[sz];
- EnforceLabelConnectivity(klabels, m_width, m_height, nlabels, numlabels, double(sz)/double(STEP*STEP));
- {
- for (int i = 0; i < sz; i++ ) klabels[i] = nlabels[i];
- }
- if (nlabels) delete [] nlabels;
- }
- void SLIC::DoSuperpixelSegmentation_ForGivenNumberOfSuperpixels(
- const unsigned int* ubuff,
- const int width,
- const int height,
- int*& klabels,
- int& numlabels,
- const int& K,
- const double& compactness,
- bool lab)
- {
- const int superpixelsize = 0.5+double(width*height)/double(K);
- DoSuperpixelSegmentation_ForGivenSuperpixelSize(ubuff,width,height,klabels,numlabels,superpixelsize,compactness, lab);
- }
- void SLIC::DoSupervoxelSegmentation(
- unsigned int**& ubuffvec,
- const int& width,
- const int& height,
- const int& depth,
- int**& klabels,
- int& numlabels,
- const int& supervoxelsize,
- const double& compactness)
- {
-
- const int STEP = 0.5 + pow(double(supervoxelsize),1.0/3.0);
-
- vector<double> kseedsl(0);
- vector<double> kseedsa(0);
- vector<double> kseedsb(0);
- vector<double> kseedsx(0);
- vector<double> kseedsy(0);
- vector<double> kseedsz(0);
-
- m_width = width;
- m_height = height;
- m_depth = depth;
- int sz = m_width*m_height;
-
-
- m_lvecvec = new double*[depth];
- m_avecvec = new double*[depth];
- m_bvecvec = new double*[depth];
- for ( int d = 0; d < depth; d++ )
- {
-
- m_lvecvec[d] = new double[sz];
- m_avecvec[d] = new double[sz];
- m_bvecvec[d] = new double[sz];
- for ( int s = 0; s < sz; s++ )
- {
- klabels[d][s] = -1;
- }
- }
- DoRGBtoLABConversion(ubuffvec, m_lvecvec, m_avecvec, m_bvecvec);
- GetKValues_LABXYZ(kseedsl, kseedsa, kseedsb, kseedsx, kseedsy, kseedsz, STEP);
- PerformSupervoxelSLIC(kseedsl, kseedsa, kseedsb, kseedsx, kseedsy, kseedsz, klabels, STEP, compactness);
- EnforceSupervoxelLabelConnectivity(klabels, width, height, depth, numlabels, STEP);
- }
|