MultiChannelImage3DT.tcc 21 KB

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