MultiChannelImage3DT.tcc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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 / (double) 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. }
  336. template<class P>
  337. Image MultiChannelImage3DT<P>::getChannel( int z, uint channel ) const
  338. {
  339. assert( channel < numChannels );
  340. NICE::Image img;
  341. convertToGrey( img, z, channel, true );
  342. return img;
  343. }
  344. template<class P>
  345. ImageT<P> MultiChannelImage3DT<P>::getChannelT( int z, uint channel ) const
  346. {
  347. assert( channel < numChannels );
  348. NICE::ImageT<P> img;
  349. convertToGrey( img, z, channel, false );
  350. P min, max;
  351. statistics ( min, max, channel );
  352. fprintf (stderr, "MultiChannelImage3DT<>::showChannel: max %f min %f\n", (double)max, (double)min );
  353. return img;
  354. }
  355. /** convert to ice image */
  356. template<class P>
  357. void MultiChannelImage3DT<P>::convertToGrey( NICE::Image & img, int z, uint channel, bool normalize ) const
  358. {
  359. assert( channel < numChannels );
  360. P min, max;
  361. if ( normalize ) {
  362. statistics( min, max, channel );
  363. fprintf( stderr, "MultiChannelImage3DT<>::showChannel: max %f min %f\n", ( double )max, ( double )min );
  364. }
  365. bool skip_assignment = false;
  366. img.resize( xsize, ysize );
  367. if ( normalize )
  368. if ( max - min < std::numeric_limits<double>::min() )
  369. {
  370. img.set( max );
  371. skip_assignment = true;
  372. fprintf( stderr, "MultiChannelImage3DT::showChannel: image is uniform! (%f)\n", ( double )max );
  373. }
  374. if ( ! skip_assignment )
  375. {
  376. long k = 0;
  377. for ( int y = 0 ; y < ysize; y++ )
  378. {
  379. for ( int x = 0 ; x < xsize ; x++, k++ )
  380. {
  381. if ( normalize )
  382. {
  383. img.setPixel( x, y, ( int )(( data[channel][z*xsize*ysize + k] - min ) * 255 / ( max - min ) ) );
  384. }
  385. else
  386. {
  387. img.setPixel( x, y, ( int )( data[channel][z*xsize*ysize + k] ) );
  388. }
  389. }
  390. }
  391. }
  392. }
  393. /** convert to ice image template */
  394. template<class P>
  395. void MultiChannelImage3DT<P>::convertToGrey( NICE::ImageT<P> & img, int z, uint channel, bool normalize ) const
  396. {
  397. assert( channel < numChannels );
  398. P min, max;
  399. if ( normalize ) {
  400. statistics( min, max, channel );
  401. fprintf( stderr, "MultiChannelImage3DT<>::showChannel: max %f min %f\n", ( double )max, ( double )min );
  402. }
  403. bool skip_assignment = false;
  404. img.resize( xsize, ysize );
  405. if ( normalize )
  406. if ( max - min < std::numeric_limits<double>::min() )
  407. {
  408. img.set( max );
  409. skip_assignment = true;
  410. fprintf( stderr, "MultiChannelImage3DT::showChannel: image is uniform! (%f)\n", ( double )max );
  411. }
  412. if ( ! skip_assignment )
  413. {
  414. long k = 0;
  415. for ( int y = 0 ; y < ysize; y++ )
  416. {
  417. for ( int x = 0 ; x < xsize ; x++, k++ )
  418. {
  419. if ( normalize )
  420. {
  421. img.setPixel( x, y, ( int )(( data[channel][z*xsize*ysize + k] - min ) * 255 / ( max - min ) ) );
  422. }
  423. else
  424. {
  425. img.setPixel( x, y, ( int )( data[channel][z*xsize*ysize + k] ) );
  426. }
  427. }
  428. }
  429. }
  430. }
  431. template<class P>
  432. void MultiChannelImage3DT<P>::convertToColor( NICE::ColorImage & img, int z, const int chan1, const int chan2, const int chan3) const
  433. {
  434. assert( chan1 < numChannels && chan2 < numChannels && chan3 < numChannels);
  435. img.resize( xsize, ysize );
  436. long k = 0;
  437. for ( int y = 0 ; y < ysize; y++ )
  438. {
  439. for ( int x = 0 ; x < xsize ; x++, k++ )
  440. {
  441. img.setPixel( x, y, 0, ( int )( data[chan1][z*xsize*ysize + k] ) );
  442. img.setPixel( x, y, 1, ( int )( data[chan2][z*xsize*ysize + k] ) );
  443. img.setPixel( x, y, 2, ( int )( data[chan3][z*xsize*ysize + k] ) );
  444. }
  445. }
  446. }
  447. template<class P>
  448. ColorImage MultiChannelImage3DT<P>::getColor(int z) const
  449. {
  450. assert( z < zsize );
  451. assert( numChannels >= 3 );
  452. NICE::ColorImage img( xsize, ysize );
  453. long k = 0;
  454. for ( int y = 0 ; y < ysize; y++ )
  455. {
  456. for ( int x = 0 ; x < xsize ; x++, k++ )
  457. {
  458. img.setPixel( x, y, 0, ( int )( data[0][z*xsize*ysize + k] ) );
  459. img.setPixel( x, y, 1, ( int )( data[1][z*xsize*ysize + k] ) );
  460. img.setPixel( x, y, 2, ( int )( data[2][z*xsize*ysize + k] ) );
  461. }
  462. }
  463. //showImage(img);
  464. //getchar();
  465. return img;
  466. }
  467. template<class P>
  468. void MultiChannelImage3DT<P>::calcIntegral( uint channel )
  469. {
  470. assert( channel < numChannels );
  471. assert( data[channel] != NULL );
  472. P *integralImage = data[channel];
  473. /** first column **/
  474. int k = xsize;
  475. for ( int y = 1 ; y < ysize; y++, k += xsize )
  476. integralImage[k] += integralImage[k-xsize];
  477. /** first row **/
  478. k = 1;
  479. for ( int x = 1 ; x < xsize; x++, k++ )
  480. integralImage[k] += integralImage[k-1];
  481. /** first stack (depth) **/
  482. k = xsize * ysize;
  483. for ( int z = 1 ; z < zsize; z++, k += (xsize*ysize) )
  484. integralImage[k] += integralImage[k-(xsize*ysize)];
  485. /** x-y plane **/
  486. k = xsize + 1;
  487. for ( int y = 1 ; y < ysize ; y++, k++ )
  488. for ( int x = 1 ; x < xsize ; x++, k++ )
  489. {
  490. integralImage[k] += integralImage[k-1];
  491. integralImage[k] += integralImage[k - xsize];
  492. integralImage[k] -= integralImage[k - xsize - 1];
  493. }
  494. /** y-z plane **/
  495. k = xsize*ysize + xsize;
  496. for ( int z = 1 ; z < zsize ; z++, k+=xsize )
  497. for ( int y = 1 ; y < zsize ; y++, k+=xsize )
  498. {
  499. integralImage[k] += integralImage[k-(xsize*ysize)];
  500. integralImage[k] += integralImage[k - xsize];
  501. integralImage[k] -= integralImage[k - xsize - (xsize*ysize)];
  502. }
  503. /** x-z plane **/
  504. k = xsize*ysize + 1;
  505. for ( int z = 1 ; z < zsize ; z++, k+=((xsize*ysize)-(xsize-1)) )
  506. for ( int x = 1 ; x < xsize ; x++, k++ )
  507. {
  508. integralImage[k] += integralImage[k-1];
  509. integralImage[k] += integralImage[k - (xsize*ysize)];
  510. integralImage[k] -= integralImage[k - (xsize*ysize) - 1];
  511. }
  512. /** all other pixels **/
  513. k = xsize*ysize + xsize + 1;
  514. for ( int z = 1 ; z < zsize ; z++, k+= xsize )
  515. {
  516. for ( int y = 1 ; y < ysize ; y++, k++ )
  517. {
  518. for ( int x = 1 ; x < xsize ; x++, k++ )
  519. {
  520. integralImage[k] += integralImage[k - (xsize*ysize)];
  521. integralImage[k] += integralImage[k - xsize];
  522. integralImage[k] += integralImage[k - 1];
  523. integralImage[k] += integralImage[k - (xsize*ysize) - xsize - 1];
  524. integralImage[k] -= integralImage[k - (xsize*ysize) - xsize];
  525. integralImage[k] -= integralImage[k - (xsize*ysize) - 1];
  526. integralImage[k] -= integralImage[k - xsize - 1];
  527. }
  528. }
  529. }
  530. }
  531. template<class P>
  532. void MultiChannelImage3DT<P>::calcGLCM( std::vector<std::vector<double> > & mat, const std::vector<int> dis, uint channel )
  533. {
  534. assert( channel < numChannels );
  535. assert( data[channel] != NULL );
  536. assert( dis.size() > 2 );
  537. P min, max;
  538. statistics( min, max, channel );
  539. long k = 0;
  540. mat = std::vector<std::vector<double> > ( max+1, std::vector<double> ( max+1, 0.0 ) );
  541. for (int z = 0; z < zsize; z++ )
  542. {
  543. for (int y = 0; y < ysize; y++ )
  544. {
  545. for (int x = 0; x < xsize; x++)
  546. {
  547. if ( (x+dis[0]>=0) && (x+dis[0]<xsize)
  548. && (y+dis[1]>=0) && (y+dis[1]<ysize)
  549. && (z+dis[2]>=0) && (z+dis[2]<zsize) )
  550. {
  551. P val = get( x, y, z, channel );
  552. P dval = get( x+dis[0], y+dis[1], z+dis[2], channel );
  553. mat[val][dval]++;
  554. mat[dval][val]++; // symmetry
  555. k += 2;
  556. }
  557. }
  558. }
  559. }
  560. for (int i = 0; i <= max; i++)
  561. {
  562. for (int j = 0; j <= max; j++)
  563. {
  564. mat[i][j] /= k;
  565. }
  566. }
  567. }
  568. template<class P>
  569. void MultiChannelImage3DT<P>::calcVariance( uint srcchan, uint tarchan )
  570. {
  571. assert( srcchan < tarchan );
  572. assert( tarchan < numChannels );
  573. assert( data[srcchan] != NULL );
  574. assert( data[tarchan] != NULL );
  575. uint windowsize = 3;
  576. int win = (windowsize-1)/2;
  577. for ( int z = 0; z < zsize; z++ )
  578. {
  579. for ( int y = 0; y < ysize; y++ )
  580. {
  581. for ( int x = 0; x < xsize; x++ )
  582. {
  583. int meansum = 0;
  584. for ( int u = -win; u <= win; u++ )
  585. {
  586. for ( int v = -win; v <= win; v++ )
  587. {
  588. for ( int w = -win; w <= win; w++)
  589. {
  590. int u_tmp = u;
  591. int v_tmp = v;
  592. int w_tmp = w;
  593. if ( (x+u<0) || (x+u>=xsize) )
  594. u_tmp = -u_tmp;
  595. if ( (y+v<0) || (y+v>=ysize) )
  596. v_tmp = -v_tmp;
  597. if ( (z+w<0) || (z+w>=zsize) )
  598. w_tmp = -w_tmp;
  599. meansum += get( x+u_tmp, y+v_tmp, z+w_tmp, srcchan );
  600. }
  601. }
  602. }
  603. meansum /= (windowsize*windowsize*windowsize);
  604. unsigned long varsum = 0;
  605. for ( int u = -win; u <= win; u++ )
  606. {
  607. for ( int v = -win; v <= win; v++ )
  608. {
  609. for ( int w = -win; w <= win; w++)
  610. {
  611. int u_tmp = u;
  612. int v_tmp = v;
  613. int w_tmp = w;
  614. if ( (x+u<0) || (x+u>=xsize) )
  615. u_tmp = -u_tmp;
  616. if ( (y+v<0) || (y+v>=ysize) )
  617. v_tmp = -v_tmp;
  618. if ( (z+w<0) || (z+w>=zsize) )
  619. w_tmp = -w_tmp;
  620. long sdev = (get( x+u_tmp, y+v_tmp, z+w_tmp, srcchan ) - meansum );
  621. varsum += (sdev*sdev);
  622. }
  623. }
  624. }
  625. varsum /= (windowsize*windowsize+windowsize)-1;
  626. set( x, y, z, varsum, tarchan );
  627. }
  628. }
  629. }
  630. }
  631. template<class P>
  632. P MultiChannelImage3DT<P>::getIntegralValue(int ulfx, int ulfy, int ulfz, int lrbx, int lrby, int lrbz, int channel)
  633. {
  634. ulfx = std::max(ulfx-1, -1);
  635. ulfx = std::min(ulfx, xsize-1);
  636. ulfy = std::max(ulfy-1, -1);
  637. ulfy = std::min(ulfy, ysize-1);
  638. ulfz = std::max(ulfz-1, -1);
  639. ulfz = std::min(ulfz, zsize-1);
  640. lrbx = std::max(lrbx, 0);
  641. lrbx = std::min(lrbx, xsize-1);
  642. lrby = std::max(lrby, 0);
  643. lrby = std::min(lrby, ysize-1);
  644. lrbz = std::max(lrbz, 0);
  645. lrbz = std::min(lrbz, zsize-1);
  646. P val1, val2, val3, val4, val5, val6, val7, val8;
  647. val1 = get(lrbx, lrby, lrbz, channel);
  648. if( ulfz > -1 )
  649. val2 = get(lrbx, lrby, ulfz, channel);
  650. else
  651. val2 = 0;
  652. if( ulfx > -1 )
  653. val3 = get(ulfx, lrby, lrbz, channel);
  654. else
  655. val3 = 0;
  656. if( ulfx > -1 && ulfz > -1 )
  657. val4 = get(ulfx, lrby, ulfz, channel);
  658. else
  659. val4 = 0;
  660. if( ulfy > -1 )
  661. val5 = get(lrbx, ulfy, lrbz, channel);
  662. else
  663. val5 = 0;
  664. if( ulfy > -1 && ulfz > -1 )
  665. val6 = get(lrbx, ulfy, ulfz, channel);
  666. else
  667. val6 = 0;
  668. if( ulfx > -1 && ulfy > -1 )
  669. val7 = get(ulfx, ulfy, lrbz, channel);
  670. else
  671. val7 = 0;
  672. if(ulfx > -1 && ulfy > -1 && ulfz > -1)
  673. val8 = get(ulfx, ulfy, ulfz, channel);
  674. else
  675. val8 = 0;
  676. P volume = abs((P)(lrbx-ulfx)*(lrby-ulfy)*(lrbz-ulfz));
  677. P val = val1 - val2 - val3 + val4 - ( val5 - val6 - val7 + val8 );
  678. return val/volume;
  679. }
  680. template<class P>
  681. void MultiChannelImage3DT<P>::store( std::string filename ) const
  682. {
  683. // simple raw format
  684. FILE *f = fopen( filename.c_str(), "w" );
  685. if ( f == NULL ) {
  686. fprintf( stderr, "MultiChannelImage3DT::store: error writing to %s\n", filename.c_str() );
  687. exit( -1 );
  688. }
  689. fwrite( &xsize, sizeof( int ), 1, f );
  690. fwrite( &ysize, sizeof( int ), 1, f );
  691. fwrite( &zsize, sizeof( int ), 1, f );
  692. fwrite( &numChannels, sizeof( uint ), 1, f );
  693. for ( uint channel = 0 ; channel < numChannels ; channel++ )
  694. {
  695. assert( data[channel] != NULL );
  696. fwrite( data[channel], sizeof( P ), xsize*ysize*zsize, f );
  697. }
  698. fclose( f );
  699. }
  700. template<class P>
  701. void MultiChannelImage3DT<P>::restore( std::string filename )
  702. {
  703. // simple raw format
  704. FILE *f = fopen( filename.c_str(), "r" );
  705. if ( f == NULL ) {
  706. fprintf( stderr, "MultiChannelImage3DT::store: error reading from %s\n", filename.c_str() );
  707. exit( -1 );
  708. }
  709. fread( &xsize, sizeof( int ), 1, f );
  710. fread( &ysize, sizeof( int ), 1, f );
  711. fread( &zsize, sizeof( int ), 1, f );
  712. fread( &numChannels, sizeof( uint ), 1, f );
  713. if ( numChannels > 0 ) {
  714. reInit( xsize, ysize, zsize, numChannels );
  715. for ( uint channel = 0 ; channel < numChannels ; channel++ )
  716. {
  717. assert( data[channel] != NULL );
  718. fread( data[channel], sizeof( P ), xsize*ysize*zsize, f );
  719. }
  720. } else {
  721. freeData();
  722. data = NULL;
  723. }
  724. fclose( f );
  725. }
  726. template<class P>
  727. int MultiChannelImage3DT<P>::width() const
  728. {
  729. return xsize;
  730. }
  731. template<class P>
  732. int MultiChannelImage3DT<P>::height() const
  733. {
  734. return ysize;
  735. }
  736. template<class P>
  737. int MultiChannelImage3DT<P>::depth() const
  738. {
  739. return zsize;
  740. }
  741. template<class P>
  742. int MultiChannelImage3DT<P>::channels() const
  743. {
  744. return ( int )numChannels;
  745. }
  746. template<class P>
  747. int MultiChannelImage3DT<P>::getPixelInt( int x, int y, int z, int channel ) const
  748. {
  749. throw( "this type is not implemented\n" );
  750. return -1;
  751. }
  752. template<class P>
  753. double MultiChannelImage3DT<P>::getPixelFloat( int x, int y, int z, int channel ) const
  754. {
  755. throw( "this type is not implemented\n" );
  756. return -1.0;
  757. }
  758. template<class P>
  759. void MultiChannelImage3DT<P>::setPixelInt( int x, int y, int z, int channel, int pixel )
  760. {
  761. throw( "this type is not implemented\n" );
  762. }
  763. template<class P>
  764. void MultiChannelImage3DT<P>::setPixelFloat( int x, int y, int z, int channel, double pixel )
  765. {
  766. throw( "this type is not implemented\n" );
  767. }
  768. /*
  769. #define SET_FUNCS_PROTO_MACRO(MYTYPE) \
  770. template<>\
  771. int MultiChannelImage3DT<MYTYPE>::getPixelInt(int x, int y, int channel) const;\
  772. template<>\
  773. double MultiChannelImage3DT<MYTYPE>::getPixelFloat(int x, int y, int channel) const;\
  774. template<>\
  775. void MultiChannelImage3DT<MYTYPE>::setPixelInt(int x, int y, int channel, int pixel);\
  776. template<>\
  777. void MultiChannelImage3DT<MYTYPE>::setPixelFloat(int x, int y, int channel, double pixel);
  778. SET_FUNCS_PROTO_MACRO( double )
  779. SET_FUNCS_PROTO_MACRO( int )
  780. SET_FUNCS_PROTO_MACRO( long int )
  781. SET_FUNCS_PROTO_MACRO( float )
  782. SET_FUNCS_PROTO_MACRO( unsigned int )
  783. */
  784. } // namespace