MultiChannelImage3DT.tcc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. #include <iostream>
  2. #include <assert.h>
  3. #include <stdio.h>
  4. #include <vector>
  5. namespace NICE {
  6. template<class P>
  7. MultiChannelImage3DT<P>::MultiChannelImage3DT( int _xsize, int _ysize, int _zsize, uint _numChannels)
  8. {
  9. // data = NULL;
  10. numChannels = 0;
  11. xsize = 0;
  12. ysize = 0;
  13. zsize = 0;
  14. reInit( _xsize, _ysize, _zsize, _numChannels);
  15. }
  16. template<class P>
  17. MultiChannelImage3DT<P>::MultiChannelImage3DT()
  18. {
  19. xsize = 0;
  20. ysize = 0;
  21. zsize = 0;
  22. numChannels = 0;
  23. // data = NULL;
  24. }
  25. template<class P>
  26. P & MultiChannelImage3DT<P>::operator() (int x, int y, int z, uint channel)
  27. {
  28. assert( channel < numChannels );
  29. assert(( x < xsize ) && ( x >= 0 ) );
  30. assert(( y < ysize ) && ( y >= 0 ) );
  31. assert(( z < zsize ) && ( z >= 0 ) );
  32. assert( data[channel] != NULL );
  33. return data[channel][x + y*xsize + z*xsize*ysize];
  34. }
  35. template<class P>
  36. MultiChannelImageT<P> MultiChannelImage3DT<P>::operator[] (uint z)
  37. {
  38. MultiChannelImageT<P> img;
  39. for( int c = 0; c < numChannels; c++ )
  40. {
  41. P * datatmp = data[c];
  42. ImageT<P> tmp ( &datatmp[z*(xsize*ysize)], xsize, ysize, xsize*sizeof(P), GrayColorImageCommonImplementation::shallowCopy );
  43. img.addChannel(tmp);
  44. }
  45. return img;
  46. }
  47. template<class P>
  48. MultiChannelImage3DT<P>& MultiChannelImage3DT<P>::operator=(const MultiChannelImage3DT<P>& orig)
  49. {
  50. if( xsize == orig.xsize
  51. && ysize == orig.ysize
  52. && zsize == orig.zsize
  53. && numChannels == orig.numChannels)
  54. {
  55. int iMemSize = xsize*ysize*zsize;
  56. for(int c=0; c < numChannels; ++c)
  57. {
  58. std::copy(orig.data[c], orig.data[c]+iMemSize, data[c]);
  59. }
  60. }
  61. else
  62. {
  63. freeData();
  64. xsize = orig.xsize;
  65. ysize = orig.ysize;
  66. zsize = orig.zsize;
  67. numChannels = orig.numChannels;
  68. int iMemSize = xsize*ysize*zsize;
  69. for(int c = 0; c < numChannels; ++c)
  70. {
  71. P *t_newData = new P [iMemSize];
  72. std::copy(orig.data[c], orig.data[c]+iMemSize, t_newData);
  73. data.push_back( t_newData );
  74. }
  75. }
  76. return *this;
  77. }
  78. template<class P>
  79. MultiChannelImage3DT<P>::MultiChannelImage3DT( const MultiChannelImage3DT<P>& p )
  80. {
  81. xsize = p.xsize;
  82. ysize = p.ysize;
  83. zsize = p.zsize;
  84. numChannels = p.numChannels;
  85. int iMemSize = xsize*ysize*zsize;
  86. for(int c=0; c < numChannels; ++c)
  87. {
  88. P *t_newData = new P [iMemSize];
  89. std::copy(p.data[c], p.data[c]+iMemSize, t_newData);
  90. data.push_back( t_newData );
  91. }
  92. /* if(p.data != NULL)
  93. data = new P *[numChannels];
  94. else
  95. data = NULL;
  96. for ( int c = 0; c < ( int )numChannels; c++ )
  97. {
  98. if ( p.data[c] == NULL )
  99. {
  100. data[c] = NULL;
  101. }
  102. else
  103. {
  104. data[c] = new P [xsize*ysize*zsize];
  105. for ( int x = 0; x < xsize*ysize*zsize; x++ )
  106. {
  107. data[c][x] = p.data[c][x];
  108. }
  109. }
  110. }
  111. */
  112. }
  113. template<class P>
  114. void MultiChannelImage3DT<P>::addChannel( int newChans )
  115. {
  116. assert(xsize > 0);
  117. assert(ysize > 0);
  118. assert(zsize > 0);
  119. for (int i = 0; i < newChans; i++ )
  120. {
  121. this->data.push_back( new P [xsize*ysize*zsize] );
  122. }
  123. numChannels = this->data.size();
  124. /* old and ugly:
  125. P **tmpData = new P *[numChannels+newChans];
  126. bool allocMem = false;
  127. int i = 0;
  128. for ( ; i < (int)numChannels; i++ )
  129. {
  130. tmpData[i] = data[i];
  131. if ( data[i] != NULL )
  132. allocMem = true;
  133. }
  134. if ( allocMem )
  135. {
  136. for ( ; i < newChans + (int)numChannels; i++ )
  137. {
  138. tmpData[i] = new P [xsize*ysize*zsize];
  139. }
  140. }
  141. numChannels += newChans;
  142. delete [] data;
  143. data = new P *[numChannels];
  144. for ( i = 0; i < (int)numChannels; i++ )
  145. {
  146. data[i] = tmpData[i];
  147. }
  148. delete [] tmpData;
  149. */
  150. }
  151. template<class P>
  152. template<class SrcP>
  153. void MultiChannelImage3DT<P>::addChannel(const NICE::MultiChannelImageT<SrcP> &newMCImg)
  154. {
  155. int oldchan = numChannels;
  156. if(this->xsize > 0)
  157. {
  158. assert(newMCImg.width() == this->width() && newMCImg.height() == this->height());
  159. assert(newMCImg.channels() == this->zsize);
  160. addChannel(1);
  161. }
  162. else
  163. {
  164. reInit( newMCImg.width(), newMCImg.height(), newMCImg.channels(), 1 );
  165. }
  166. for(int z = 0; z < this->zsize; z++)
  167. {
  168. //NICE::ImageT<SrcP> newImg = newMCImg[z];
  169. for(int y = 0; y < this->ysize; y++)
  170. {
  171. for(int x = 0; x < this->xsize; x++)
  172. {
  173. //data[oldchan][x + y*xsize + z*xsize*ysize] = (P)newImg(x,y);
  174. data[oldchan][x + y*xsize + z*xsize*ysize] = (P)newMCImg.get(x,y,(unsigned int)z);
  175. }
  176. }
  177. }
  178. }
  179. template<class P>
  180. template<class SrcP>
  181. void MultiChannelImage3DT<P>::addChannel(const NICE::MultiChannelImage3DT<SrcP> &newImg)
  182. {
  183. int oldchan = numChannels;
  184. if(numChannels > 0)
  185. {
  186. assert(newImg.width() == this->width() && newImg.height() == this->height() && newImg.depth() == this->depth());
  187. addChannel(newImg.channels());
  188. }
  189. else
  190. {
  191. reInit( newImg.width(), newImg.height(), newImg.depth(), newImg.channels() );
  192. }
  193. int chanNI = 0;
  194. for(int c = oldchan; c < (int)numChannels; c++, chanNI++)
  195. {
  196. int val = 0;
  197. for(int z = 0; z < this->zsize; z++)
  198. {
  199. for(int y = 0; y < this->ysize; y++)
  200. {
  201. for(int x = 0; x < this->xsize; x++, val++)
  202. {
  203. data[c][val] = newImg.get(x,y,z,chanNI);
  204. }
  205. }
  206. }
  207. }
  208. }
  209. template<class P>
  210. template<class SrcP>
  211. void MultiChannelImage3DT<P>::addChannelReferences(const NICE::MultiChannelImage3DT<SrcP> &newImg)
  212. {
  213. if(numChannels == 0)
  214. {
  215. xsize = newImg.width();
  216. ysize = newImg.height();
  217. zsize = newImg.depth();
  218. }
  219. else
  220. {
  221. if( !( newImg.width() == this->width()
  222. && newImg.height() == this->height()
  223. && newImg.depth() == this->depth() ) )
  224. {
  225. throw( " channelwise dimensions don't fit! Abort this crazy mixing of channels" );
  226. return;
  227. }
  228. }
  229. // add channel deep data references
  230. // -> wrap this MultiChannelImage3D around the other data
  231. const std::vector< P* > vecDataChannelPtrs = newImg.getDataPointer();
  232. for( int c=0; c < vecDataChannelPtrs.size() ; ++c)
  233. data.push_back( vecDataChannelPtrs[c] );
  234. numChannels = data.size();
  235. }
  236. template<class P>
  237. MultiChannelImage3DT<P>::~MultiChannelImage3DT()
  238. {
  239. freeData();
  240. }
  241. template<class P>
  242. void MultiChannelImage3DT<P>::freeData()
  243. {
  244. if ( !data.empty())
  245. {
  246. for ( int i = 0 ; i < (int)data.size() ; i++ )
  247. if ( data[i] != NULL )
  248. delete [] data[i];
  249. data.clear();
  250. }
  251. }
  252. template<class P>
  253. void MultiChannelImage3DT<P>::freeShallowData()
  254. {
  255. this->data.clear();
  256. }
  257. template<class P>
  258. void MultiChannelImage3DT<P>::reInit( int _xsize, int _ysize, int _zsize, int _numChannels )
  259. {
  260. freeData();
  261. xsize = _xsize;
  262. ysize = _ysize;
  263. zsize = _zsize;
  264. numChannels = _numChannels;
  265. this->addChannel(numChannels);
  266. }
  267. template<class P>
  268. template<class SrcP>
  269. void MultiChannelImage3DT<P>::reInitFrom( const MultiChannelImage3DT<SrcP> & src )
  270. {
  271. this->reInit(src.width(),src.height(), src.depth(), src.channels() );
  272. }
  273. template<class P>
  274. P MultiChannelImage3DT<P>::get( int x, int y, int z, uint channel ) const
  275. {
  276. assert( channel < numChannels );
  277. assert(( x < xsize ) && ( x >= 0 ) );
  278. assert(( y < ysize ) && ( y >= 0 ) );
  279. assert(( z < zsize ) && ( z >= 0 ) );
  280. assert( data[channel] != NULL );
  281. return data[channel][x + y*xsize + z*xsize*ysize];
  282. }
  283. template<class P>
  284. std::vector<P*> MultiChannelImage3DT<P>::getDataPointer() const
  285. {
  286. return data;
  287. }
  288. template<class P>
  289. void MultiChannelImage3DT<P>::set( int x, int y, int z, P val, uint channel )
  290. {
  291. assert( channel < numChannels );
  292. assert(( x < xsize ) && ( x >= 0 ) );
  293. assert(( y < ysize ) && ( y >= 0 ) );
  294. assert(( z < zsize ) && ( z >= 0 ) );
  295. assert( data[channel] != NULL );
  296. data[channel][x + y*xsize + z*xsize*ysize] = val;
  297. }
  298. template<class P>
  299. void MultiChannelImage3DT<P>::set( P val, uint channel )
  300. {
  301. assert( channel < numChannels );
  302. assert( data[channel] != NULL );
  303. for ( int k = 0 ; k < xsize*ysize*zsize ; k++ )
  304. data[channel][k] = val;
  305. }
  306. template<class P>
  307. void MultiChannelImage3DT<P>::setAll( P val )
  308. {
  309. for ( uint channel = 0 ; channel < numChannels ; channel++ )
  310. if ( data[channel] != NULL )
  311. set( val, channel );
  312. }
  313. template<class P>
  314. void MultiChannelImage3DT<P>::statistics( P & min, P & max, uint channel ) const
  315. {
  316. assert( channel < numChannels );
  317. P val = 0;
  318. for ( long k = 0 ; k < xsize*ysize*zsize ; k++ )
  319. {
  320. val = data [channel][k];
  321. if (( k == 0 ) || ( val > max ) ) max = val;
  322. if (( k == 0 ) || ( val < min ) ) min = val;
  323. }
  324. assert(finite(max));
  325. assert(finite(min));
  326. }
  327. template<class P>
  328. void MultiChannelImage3DT<P>::correctShading( uint channel ) const
  329. {
  330. assert( channel < numChannels );
  331. // some sort of correction trick hardly understandable :-)
  332. std::vector<double> meanVals;
  333. for( int z = 0; z < zsize; z++ )
  334. {
  335. double sumVal = 0;
  336. for( int y = 0; y < ysize; y++ )
  337. {
  338. for( int x = 0; x < xsize; x++ )
  339. {
  340. sumVal += data [channel][x + y*xsize + z*xsize*ysize];
  341. }
  342. }
  343. sumVal /= (xsize*ysize);
  344. meanVals.push_back( sumVal );
  345. }
  346. P newMax = std::numeric_limits<P>::min();
  347. const short int maxVal = 255;
  348. for ( int z = 0; z < zsize; z++ )
  349. {
  350. for ( int y = 0; y < ysize; y++ )
  351. {
  352. for ( int x = 0; x < xsize; x++ )
  353. {
  354. P tmp = data [channel][x + y*xsize + z*xsize*ysize];
  355. double newVal = maxVal * ( (double) tmp / meanVals[z] );
  356. if ( ( P ) newVal > newMax )
  357. newMax = ( P ) newVal;
  358. data [channel][x + y*xsize + z*xsize*ysize] = newVal;
  359. }
  360. }
  361. }
  362. for ( long k = 0 ; k < xsize*ysize*zsize ; k++ )
  363. {
  364. data [channel][k] = data [channel][k] / newMax * maxVal;
  365. }
  366. }
  367. template<class P>
  368. void MultiChannelImage3DT<P>::equalizeHistogram( uint channel ) const
  369. {
  370. assert(channel < numChannels );
  371. for( int z = 0; z < zsize; z++ )
  372. {
  373. NICE::Image img = getChannel(z, channel );
  374. NICE::Histogram hist(img,0,255,256);
  375. NICE::IntVector *histVec = NULL;
  376. histVec = hist.cumulative();
  377. for ( int i = 0; i < (int)histVec->size(); i++)
  378. {
  379. histVec->set(i, histVec->get(i) * 255 / (double)histVec->get(histVec->size()-1));
  380. }
  381. for ( int y = 0; y < ysize; y++ )
  382. {
  383. for ( int x = 0; x < xsize; x++ )
  384. {
  385. data [channel][x + y*xsize + z*xsize*ysize] = histVec->get( img.getPixel(x,y) );
  386. }
  387. }
  388. delete histVec;
  389. }
  390. }
  391. template<class P>
  392. Image MultiChannelImage3DT<P>::getChannel( int z, uint channel ) const
  393. {
  394. assert( channel < numChannels );
  395. NICE::Image img(xsize, ysize);
  396. convertToGrey( img, z, channel, true );
  397. return img;
  398. }
  399. template<class P>
  400. ImageT<P> MultiChannelImage3DT<P>::getChannelT( int z, uint channel ) const
  401. {
  402. assert( channel < numChannels );
  403. // P min, max;
  404. // statistics ( min, max, channel );
  405. // fprintf (stderr, "MultiChannelImage3DT<>::showChannel: max %f min %f\n", (double)max, (double)min );
  406. NICE::ImageT<P> img(xsize,ysize);
  407. long k = 0;
  408. for ( int y = 0; y < ysize; y++ )
  409. for( int x = 0; x < xsize; x++, k++ )
  410. {
  411. img.setPixel( x, y, data[channel][z*xsize*ysize + k] );
  412. }
  413. return img;
  414. }
  415. template<class P>
  416. ImageT<P> MultiChannelImage3DT<P>::getXSlice ( int x, uint channel ) const
  417. {
  418. assert( channel < numChannels );
  419. NICE::ImageT<P> img(zsize, ysize);
  420. for ( int y = 0; y < ysize; y++ )
  421. for ( int z = 0; z < zsize; z++ )
  422. img.setPixel( z, y, data[channel][z*xsize*ysize + y*xsize + x]);
  423. return img;
  424. }
  425. /** convert to ice image */
  426. template<class P>
  427. void MultiChannelImage3DT<P>::convertToGrey( NICE::Image & img, int z, uint channel, bool normalize ) const
  428. {
  429. assert( channel < numChannels );
  430. P min, max;
  431. if ( normalize )
  432. statistics( min, max, channel );
  433. bool skip_assignment = false;
  434. img.resize( xsize, ysize );
  435. if ( normalize )
  436. if ( max - min < std::numeric_limits<double>::min() )
  437. {
  438. fprintf( stderr, "MultiChannelImage3DT<>::showChannel: max %f min %f\n", ( double )max, ( double )min );
  439. img.set( max );
  440. skip_assignment = true;
  441. fprintf( stderr, "MultiChannelImage3DT<>::showChannel: image is uniform! (%f)\n", ( double )max );
  442. }
  443. if ( ! skip_assignment )
  444. {
  445. long k = 0;
  446. for ( int y = 0 ; y < ysize; y++ )
  447. {
  448. for ( int x = 0 ; x < xsize ; x++, k++ )
  449. {
  450. if ( normalize )
  451. {
  452. img.setPixel( x, y, ( int )(( data[channel][z*xsize*ysize + k] - min ) * 255 / ( max - min ) ) );
  453. }
  454. else
  455. {
  456. img.setPixel( x, y, ( int )( data[channel][z*xsize*ysize + k] ) );
  457. }
  458. }
  459. }
  460. }
  461. }
  462. template<class P>
  463. void MultiChannelImage3DT<P>::convertToColor( NICE::ColorImage & img, int z, const int chan1, const int chan2, const int chan3) const
  464. {
  465. assert( chan1 < numChannels && chan2 < numChannels && chan3 < numChannels);
  466. img.resize( xsize, ysize );
  467. long k = 0;
  468. for ( int y = 0 ; y < ysize; y++ )
  469. {
  470. for ( int x = 0 ; x < xsize ; x++, k++ )
  471. {
  472. img.setPixel( x, y, 0, ( int )( data[chan1][z*xsize*ysize + k] ) );
  473. img.setPixel( x, y, 1, ( int )( data[chan2][z*xsize*ysize + k] ) );
  474. img.setPixel( x, y, 2, ( int )( data[chan3][z*xsize*ysize + k] ) );
  475. }
  476. }
  477. }
  478. template<class P>
  479. ColorImage MultiChannelImage3DT<P>::getColor(int z) const
  480. {
  481. assert( z < zsize );
  482. assert( numChannels >= 3 );
  483. NICE::ColorImage img( xsize, ysize );
  484. long k = 0;
  485. for ( int y = 0 ; y < ysize; y++ )
  486. {
  487. for ( int x = 0 ; x < xsize ; x++, k++ )
  488. {
  489. img.setPixel( x, y, 0, ( int )( data[0][z*xsize*ysize + k] ) );
  490. img.setPixel( x, y, 1, ( int )( data[1][z*xsize*ysize + k] ) );
  491. img.setPixel( x, y, 2, ( int )( data[2][z*xsize*ysize + k] ) );
  492. }
  493. }
  494. //showImage(img);
  495. //getchar();
  496. return img;
  497. }
  498. template<class P>
  499. ColorImage MultiChannelImage3DT<P>::getColorImageFromChannels(int z, int channel0, int channel1, int channel2) const
  500. {
  501. assert( z < zsize );
  502. assert( numChannels >= std::max( std::max(channel0,channel1),channel2 ) );
  503. NICE::ColorImage img( xsize, ysize );
  504. long k = 0;
  505. for ( int y = 0 ; y < ysize; y++ )
  506. {
  507. for ( int x = 0 ; x < xsize ; x++, k++ )
  508. {
  509. img.setPixel( x, y, 0, ( int )( data[channel0][z*xsize*ysize + k] ) );
  510. img.setPixel( x, y, 1, ( int )( data[channel1][z*xsize*ysize + k] ) );
  511. img.setPixel( x, y, 2, ( int )( data[channel2][z*xsize*ysize + k] ) );
  512. }
  513. }
  514. //showImage(img);
  515. //getchar();
  516. return img;
  517. }
  518. template<class P>
  519. void MultiChannelImage3DT<P>::calcIntegral( uint channel )
  520. {
  521. assert( channel < numChannels );
  522. assert( data[channel] != NULL );
  523. P *integralImage = data[channel];
  524. /** first column **/
  525. int k = xsize;
  526. for ( int y = 1 ; y < ysize; y++, k += xsize )
  527. integralImage[k] += integralImage[k-xsize];
  528. /** first row **/
  529. k = 1;
  530. for ( int x = 1 ; x < xsize; x++, k++ )
  531. integralImage[k] += integralImage[k-1];
  532. /** first stack (depth) **/
  533. k = xsize * ysize;
  534. for ( int z = 1 ; z < zsize; z++, k += (xsize*ysize) )
  535. integralImage[k] += integralImage[k-(xsize*ysize)];
  536. /** x-y plane **/
  537. k = xsize + 1;
  538. for ( int y = 1 ; y < ysize ; y++, k++ )
  539. for ( int x = 1 ; x < xsize ; x++, k++ )
  540. {
  541. integralImage[k] += integralImage[k-1];
  542. integralImage[k] += integralImage[k - xsize];
  543. integralImage[k] -= integralImage[k - xsize - 1];
  544. }
  545. /** y-z plane **/
  546. k = xsize*ysize + xsize;
  547. for ( int z = 1 ; z < zsize ; z++, k+=xsize )
  548. for ( int y = 1 ; y < ysize ; y++, k+=xsize )
  549. {
  550. integralImage[k] += integralImage[k-(xsize*ysize)];
  551. integralImage[k] += integralImage[k - xsize];
  552. integralImage[k] -= integralImage[k - xsize - (xsize*ysize)];
  553. }
  554. /** x-z plane **/
  555. k = xsize*ysize + 1;
  556. for ( int z = 1 ; z < zsize ; z++, k+=((xsize*ysize)-(xsize-1)) )
  557. for ( int x = 1 ; x < xsize ; x++, k++ )
  558. {
  559. integralImage[k] += integralImage[k-1];
  560. integralImage[k] += integralImage[k - (xsize*ysize)];
  561. integralImage[k] -= integralImage[k - (xsize*ysize) - 1];
  562. }
  563. /** all other pixels **/
  564. k = xsize*ysize + xsize + 1;
  565. for ( int z = 1 ; z < zsize ; z++, k+= xsize )
  566. {
  567. for ( int y = 1 ; y < ysize ; y++, k++ )
  568. {
  569. for ( int x = 1 ; x < xsize ; x++, k++ )
  570. {
  571. integralImage[k] += integralImage[k - (xsize*ysize)];
  572. integralImage[k] += integralImage[k - xsize];
  573. integralImage[k] += integralImage[k - 1];
  574. integralImage[k] += integralImage[k - (xsize*ysize) - xsize - 1];
  575. integralImage[k] -= integralImage[k - (xsize*ysize) - xsize];
  576. integralImage[k] -= integralImage[k - (xsize*ysize) - 1];
  577. integralImage[k] -= integralImage[k - xsize - 1];
  578. }
  579. }
  580. }
  581. }
  582. template<class P>
  583. void MultiChannelImage3DT<P>::calcVariance( uint srcchan, uint tarchan )
  584. {
  585. assert( srcchan < tarchan );
  586. assert( tarchan < numChannels );
  587. assert( data[srcchan] != NULL );
  588. assert( data[tarchan] != NULL );
  589. uint windowsize = 3;
  590. int win = (windowsize-1)/2;
  591. for ( int z = 0; z < zsize; z++ )
  592. {
  593. for ( int y = 0; y < ysize; y++ )
  594. {
  595. for ( int x = 0; x < xsize; x++ )
  596. {
  597. int meansum = 0;
  598. for ( int u = -win; u <= win; u++ )
  599. {
  600. for ( int v = -win; v <= win; v++ )
  601. {
  602. for ( int w = -win; w <= win; w++)
  603. {
  604. int u_tmp = u;
  605. int v_tmp = v;
  606. int w_tmp = w;
  607. if ( (x+u<0) || (x+u>=xsize) )
  608. u_tmp = -u_tmp;
  609. if ( (y+v<0) || (y+v>=ysize) )
  610. v_tmp = -v_tmp;
  611. if ( (z+w<0) || (z+w>=zsize) )
  612. w_tmp = -w_tmp;
  613. meansum += get( x+u_tmp, y+v_tmp, z+w_tmp, srcchan );
  614. }
  615. }
  616. }
  617. meansum /= (windowsize*windowsize*windowsize);
  618. unsigned long varsum = 0;
  619. for ( int u = -win; u <= win; u++ )
  620. {
  621. for ( int v = -win; v <= win; v++ )
  622. {
  623. for ( int w = -win; w <= win; w++)
  624. {
  625. int u_tmp = u;
  626. int v_tmp = v;
  627. int w_tmp = w;
  628. if ( (x+u<0) || (x+u>=xsize) )
  629. u_tmp = -u_tmp;
  630. if ( (y+v<0) || (y+v>=ysize) )
  631. v_tmp = -v_tmp;
  632. if ( (z+w<0) || (z+w>=zsize) )
  633. w_tmp = -w_tmp;
  634. long sdev = (get( x+u_tmp, y+v_tmp, z+w_tmp, srcchan ) - meansum );
  635. varsum += (sdev*sdev);
  636. }
  637. }
  638. }
  639. varsum /= (windowsize*windowsize+windowsize)-1;
  640. set( x, y, z, varsum, tarchan );
  641. }
  642. }
  643. }
  644. }
  645. template<class P>
  646. P MultiChannelImage3DT<P>::getIntegralValue(int ulfx, int ulfy, int ulfz, int lrbx, int lrby, int lrbz, int channel) const
  647. {
  648. ulfx = std::max(ulfx-1, -1);
  649. ulfx = std::min(ulfx, xsize-1);
  650. ulfy = std::max(ulfy-1, -1);
  651. ulfy = std::min(ulfy, ysize-1);
  652. ulfz = std::max(ulfz-1, -1);
  653. ulfz = std::min(ulfz, zsize-1);
  654. lrbx = std::max(lrbx, 0);
  655. lrbx = std::min(lrbx, xsize-1);
  656. lrby = std::max(lrby, 0);
  657. lrby = std::min(lrby, ysize-1);
  658. lrbz = std::max(lrbz, 0);
  659. lrbz = std::min(lrbz, zsize-1);
  660. double val1, val2, val3, val4, val5, val6, val7, val8;
  661. val1 = get(lrbx, lrby, lrbz, channel);
  662. if( ulfz > -1 )
  663. val2 = get(lrbx, lrby, ulfz, channel);
  664. else
  665. val2 = 0;
  666. if( ulfx > -1 )
  667. val3 = get(ulfx, lrby, lrbz, channel);
  668. else
  669. val3 = 0;
  670. if( ulfx > -1 && ulfz > -1 )
  671. val4 = get(ulfx, lrby, ulfz, channel);
  672. else
  673. val4 = 0;
  674. if( ulfy > -1 )
  675. val5 = get(lrbx, ulfy, lrbz, channel);
  676. else
  677. val5 = 0;
  678. if( ulfy > -1 && ulfz > -1 )
  679. val6 = get(lrbx, ulfy, ulfz, channel);
  680. else
  681. val6 = 0;
  682. if( ulfx > -1 && ulfy > -1 )
  683. val7 = get(ulfx, ulfy, lrbz, channel);
  684. else
  685. val7 = 0;
  686. if(ulfx > -1 && ulfy > -1 && ulfz > -1)
  687. val8 = get(ulfx, ulfy, ulfz, channel);
  688. else
  689. val8 = 0;
  690. P volume = abs((lrbx-ulfx)*(lrby-ulfy)*(lrbz-ulfz));
  691. P val = val1 - val2 - val3 + val4 - ( val5 - val6 - val7 + val8 );
  692. if (volume != 0)
  693. return val/volume;
  694. else
  695. return 0.0;
  696. }
  697. template<class P>
  698. void MultiChannelImage3DT<P>::store( std::string filename ) const
  699. {
  700. // simple raw format
  701. FILE *f = fopen( filename.c_str(), "w" );
  702. if ( f == NULL ) {
  703. fprintf( stderr, "MultiChannelImage3DT::store: error writing to %s\n", filename.c_str() );
  704. exit( -1 );
  705. }
  706. fwrite( &xsize, sizeof( int ), 1, f );
  707. fwrite( &ysize, sizeof( int ), 1, f );
  708. fwrite( &zsize, sizeof( int ), 1, f );
  709. fwrite( &numChannels, sizeof( uint ), 1, f );
  710. for ( uint channel = 0 ; channel < numChannels ; channel++ )
  711. {
  712. assert( data[channel] != NULL );
  713. fwrite( data[channel], sizeof( P ), xsize*ysize*zsize, f );
  714. }
  715. fclose( f );
  716. }
  717. template<class P>
  718. void MultiChannelImage3DT<P>::restore( std::string filename )
  719. {
  720. // simple raw format
  721. FILE *f = fopen( filename.c_str(), "r" );
  722. if ( f == NULL ) {
  723. fprintf( stderr, "MultiChannelImage3DT::store: error reading from %s\n", filename.c_str() );
  724. exit( -1 );
  725. }
  726. fread( &xsize, sizeof( int ), 1, f );
  727. fread( &ysize, sizeof( int ), 1, f );
  728. fread( &zsize, sizeof( int ), 1, f );
  729. fread( &numChannels, sizeof( uint ), 1, f );
  730. if ( numChannels > 0 ) {
  731. reInit( xsize, ysize, zsize, numChannels );
  732. for ( uint channel = 0 ; channel < numChannels ; channel++ )
  733. {
  734. assert( data[channel] != NULL );
  735. fread( data[channel], sizeof( P ), xsize*ysize*zsize, f );
  736. }
  737. } else {
  738. freeData();
  739. //data = NULL;
  740. }
  741. fclose( f );
  742. }
  743. template<class P>
  744. int MultiChannelImage3DT<P>::width() const
  745. {
  746. return xsize;
  747. }
  748. template<class P>
  749. int MultiChannelImage3DT<P>::height() const
  750. {
  751. return ysize;
  752. }
  753. template<class P>
  754. int MultiChannelImage3DT<P>::depth() const
  755. {
  756. return zsize;
  757. }
  758. template<class P>
  759. int MultiChannelImage3DT<P>::channels() const
  760. {
  761. return ( int )numChannels;
  762. }
  763. template<class P>
  764. int MultiChannelImage3DT<P>::getPixelInt( int x, int y, int z, int channel ) const
  765. {
  766. throw( "this type is not implemented\n" );
  767. return -1;
  768. }
  769. template<class P>
  770. double MultiChannelImage3DT<P>::getPixelFloat( int x, int y, int z, int channel ) const
  771. {
  772. throw( "this type is not implemented\n" );
  773. return -1.0;
  774. }
  775. template<class P>
  776. void MultiChannelImage3DT<P>::setPixelInt( int x, int y, int z, int channel, int pixel )
  777. {
  778. throw( "this type is not implemented\n" );
  779. }
  780. template<class P>
  781. void MultiChannelImage3DT<P>::setPixelFloat( int x, int y, int z, int channel, double pixel )
  782. {
  783. throw( "this type is not implemented\n" );
  784. }
  785. #define SET_FUNCS_PROTO_MACRO3D(MYTYPE) \
  786. template<>\
  787. int MultiChannelImage3DT<MYTYPE>::getPixelInt(int x, int y, int z, int channel) const;\
  788. template<>\
  789. double MultiChannelImage3DT<MYTYPE>::getPixelFloat(int x, int y, int z, int channel) const;\
  790. template<>\
  791. void MultiChannelImage3DT<MYTYPE>::setPixelInt(int x, int y, int z, int channel, int pixel);\
  792. template<>\
  793. void MultiChannelImage3DT<MYTYPE>::setPixelFloat(int x, int y, int z, int channel, double pixel);
  794. SET_FUNCS_PROTO_MACRO3D( double )
  795. SET_FUNCS_PROTO_MACRO3D( int )
  796. SET_FUNCS_PROTO_MACRO3D( long int )
  797. SET_FUNCS_PROTO_MACRO3D( float )
  798. SET_FUNCS_PROTO_MACRO3D( unsigned int )
  799. } // namespace