MultiChannelImage3DT.tcc 22 KB

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