TestConverter.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * NICE-Core - efficient algebra and computer vision methods
  3. * - libimage - An image/template for new NICE libraries
  4. * See file License for license information.
  5. */
  6. #ifdef NICE_USELIB_CPPUNIT
  7. #include "TestConverter.h"
  8. #include <string>
  9. #include <exception>
  10. #include <sstream>
  11. #include <iomanip>
  12. #include <iostream>
  13. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  14. #include <iocompression/md5.h>
  15. #endif
  16. using namespace NICE;
  17. using namespace std;
  18. CPPUNIT_TEST_SUITE_REGISTRATION( TestConverter );
  19. void TestConverter::setUp() {
  20. }
  21. void TestConverter::tearDown() {
  22. }
  23. void TestConverter::testConstructor() {
  24. }
  25. void TestConverter::testOperators() {
  26. }
  27. void TestConverter::testGrayFilter() {
  28. int width=16, height=16;
  29. ImageT<short> gimg(width,height);
  30. ImageT<short> gbuffer(width,height);
  31. gbuffer = 0;
  32. gimg=100;
  33. for(int w=0;w<width;w++) {
  34. gimg(w,height/2) = abs(w*height/2-width);
  35. }
  36. for(int h=0;h<height;h++) {
  37. gimg(width/3,h) = abs(width/3-h);
  38. }
  39. gimg(0,0) = 0;
  40. gimg(width-1,height-1) = 0;
  41. {
  42. float karray[]={1.,0.,-1.};
  43. VectorT<float> kernel(karray,3);
  44. {
  45. std::stringstream inout;
  46. gbuffer=0;
  47. DeprecatedConverter::filterX(gimg,kernel,1,&gbuffer);
  48. inout << fixed << setw(3) << setprecision(0) << gbuffer << endl;
  49. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  50. CPPUNIT_ASSERT_MD5_STRING("c6ea3197098b21bb8dab8fc849ce4e6c",inout.str());
  51. #endif
  52. }
  53. {
  54. std::stringstream inout;
  55. gbuffer=0;
  56. DeprecatedConverter::filterX(gimg,kernel,2,&gbuffer);
  57. inout << fixed << setw(3) << setprecision(0) << gbuffer << endl;
  58. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  59. CPPUNIT_ASSERT_MD5_STRING("34c67c6d452ae52e5d083b5bdd65a15f",inout.str());
  60. #endif
  61. }
  62. {
  63. std::stringstream inout;
  64. gbuffer=0;
  65. DeprecatedConverter::filterX(gimg,kernel,0,&gbuffer);
  66. inout << fixed << setw(3) << setprecision(0) << gbuffer << endl;
  67. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  68. CPPUNIT_ASSERT_MD5_STRING("b7dbf6cca3b44b476e71ac5c54b89882",inout.str());
  69. #endif
  70. }
  71. {
  72. /*
  73. std::stringstream inout;
  74. gbuffer=0;
  75. DeprecatedConverter::filterY(gimg,kernel,1,&gbuffer);
  76. inout << fixed << setw(3) << setprecision(0) << gbuffer << endl;
  77. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  78. CPPUNIT_ASSERT_MD5_STRING("9ab894bd2c60eb9ab5270b68f5ff59b5",inout.str());
  79. #endif
  80. */
  81. }
  82. {
  83. /*
  84. std::stringstream inout;
  85. gbuffer=0;
  86. DeprecatedConverter::filterY(gimg,kernel,2,&gbuffer);
  87. inout << fixed << setw(3) << setprecision(0) << gbuffer << endl;
  88. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  89. CPPUNIT_ASSERT_MD5_STRING("b575fdc2dc1d28aaf4ec84cf9d559000",inout.str());
  90. #endif
  91. */
  92. }
  93. {
  94. /*
  95. std::stringstream inout;
  96. gbuffer=0;
  97. DeprecatedConverter::filterY(gimg,kernel,0,&gbuffer);
  98. inout << fixed << setw(3) << setprecision(0) << gbuffer << endl;
  99. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  100. CPPUNIT_ASSERT_MD5_STRING("5ed12d69541ec45a637547561f4bad18",inout.str());
  101. #endif
  102. */
  103. }
  104. FloatImage gkern(3,3,GrayColorImageCommonImplementation::noAlignment);
  105. gkern(1,1)=gkern(1,0)=gkern(1,2)=0;
  106. gkern(0,1)=2.0;
  107. gkern(2,1)=-2.0;
  108. gkern(0,0)=gkern(0,2)=1.0;
  109. gkern(2,0)=gkern(2,2)=-1.0;
  110. {
  111. std::stringstream inout;
  112. gbuffer=0;
  113. DeprecatedConverter::filter(gimg,gkern,1,1,&gbuffer);
  114. inout << fixed << setw(3) << setprecision(0) << gbuffer << endl;
  115. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  116. CPPUNIT_ASSERT_MD5_STRING("796dd2b8c03ffaed06456c825ad89d6a",inout.str());
  117. #endif
  118. }
  119. {
  120. std::stringstream inout;
  121. gbuffer=0;
  122. DeprecatedConverter::filter(gimg,gkern,0,0,&gbuffer);
  123. inout << fixed << setw(3) << setprecision(0) << gbuffer << endl;
  124. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  125. CPPUNIT_ASSERT_MD5_STRING("81478bfeb4654208d61d9191bed7bde7",inout.str());
  126. #endif
  127. }
  128. {
  129. std::stringstream inout;
  130. gbuffer=0;
  131. DeprecatedConverter::filter(gimg,gkern,2,2,&gbuffer);
  132. inout << fixed << setw(3) << setprecision(0) << gbuffer << endl;
  133. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  134. CPPUNIT_ASSERT_MD5_STRING("5e3d5bdf3ea094f6cd7e8c5dca7aae4c",inout.str());
  135. #endif
  136. }
  137. }
  138. }
  139. void TestConverter::testConvolution() {
  140. int width=16, height=16;
  141. FloatImage fimg(width,height);
  142. FloatImage fbuffer(width-2,height-2);
  143. fbuffer = 0;
  144. fimg=100;
  145. for(int w=0;w<width;w++) {
  146. fimg(w,height/2) = abs(w*height/2-width);
  147. }
  148. for(int h=0;h<height;h++) {
  149. fimg(width/3,h) = abs(width/3-h);
  150. }
  151. fimg(0,0) = 0;
  152. fimg(width-1,height-1) = 0;
  153. FloatImage fkern(3,3,GrayColorImageCommonImplementation::noAlignment);
  154. fkern(1,1)=fkern(1,0)=fkern(1,2)=0;
  155. fkern(0,1)=2.0;
  156. fkern(2,1)=-2.0;
  157. fkern(0,0)=fkern(0,2)=1.0;
  158. fkern(2,0)=fkern(2,2)=-1.0;
  159. {
  160. std::stringstream inout;
  161. fbuffer=0;
  162. DeprecatedConverter::convolution(fimg,fkern,&fbuffer);
  163. inout << fixed << setw(3) << setprecision(0) << fbuffer << endl;
  164. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  165. CPPUNIT_ASSERT_MD5_STRING("ca4f8fde956b49957f4f8ca013bd16d9",inout.str());
  166. #endif
  167. }
  168. }
  169. void TestConverter::testFloatFilter() {
  170. int width=16, height=16;
  171. FloatImage fimg(width,height);
  172. FloatImage fbuffer(width,height);
  173. fbuffer = 0;
  174. fimg=100;
  175. for(int w=0;w<width;w++) {
  176. fimg(w,height/2) = abs(w*height/2-width);
  177. }
  178. for(int h=0;h<height;h++) {
  179. fimg(width/3,h) = abs(width/3-h);
  180. }
  181. fimg(0,0) = 0;
  182. fimg(width-1,height-1) = 0;
  183. {
  184. VectorT<float> kernel(3);
  185. for(int i=0;i<(int)kernel.size();i++) {
  186. kernel[i] = (1-i);
  187. }
  188. {
  189. std::stringstream inout;
  190. fbuffer=0;
  191. DeprecatedConverter::filterX(fimg,kernel,1,&fbuffer);
  192. inout << fixed << setw(3) << setprecision(0) << fbuffer << endl;
  193. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  194. CPPUNIT_ASSERT_MD5_STRING("c6ea3197098b21bb8dab8fc849ce4e6c",inout.str());
  195. #endif
  196. }
  197. {
  198. std::stringstream inout;
  199. fbuffer=0;
  200. DeprecatedConverter::filterX(fimg,kernel,2,&fbuffer);
  201. inout << fixed << setw(3) << setprecision(0) << fbuffer << endl;
  202. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  203. CPPUNIT_ASSERT_MD5_STRING("34c67c6d452ae52e5d083b5bdd65a15f",inout.str());
  204. #endif
  205. }
  206. {
  207. std::stringstream inout;
  208. fbuffer=0;
  209. DeprecatedConverter::filterX(fimg,kernel,0,&fbuffer);
  210. inout << fixed << setw(3) << setprecision(0) << fbuffer << endl;
  211. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  212. CPPUNIT_ASSERT_MD5_STRING("b7dbf6cca3b44b476e71ac5c54b89882",inout.str());
  213. #endif
  214. }
  215. {
  216. /*
  217. std::stringstream inout;
  218. fbuffer=0;
  219. DeprecatedConverter::filterY(fimg,kernel,1,&fbuffer);
  220. inout << fixed << setw(3) << setprecision(0) << fbuffer << endl;
  221. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  222. CPPUNIT_ASSERT_MD5_STRING("9ab894bd2c60eb9ab5270b68f5ff59b5",inout.str());
  223. #endif
  224. */
  225. }
  226. {
  227. /*
  228. std::stringstream inout;
  229. fbuffer=0;
  230. DeprecatedConverter::filterY(fimg,kernel,2,&fbuffer);
  231. inout << fixed << setw(3) << setprecision(0) << fbuffer << endl;
  232. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  233. CPPUNIT_ASSERT_MD5_STRING("b575fdc2dc1d28aaf4ec84cf9d559000",inout.str());
  234. #endif
  235. */
  236. }
  237. {
  238. /*
  239. std::stringstream inout;
  240. fbuffer=0;
  241. DeprecatedConverter::filterY(fimg,kernel,0,&fbuffer);
  242. inout << fixed << setw(3) << setprecision(0) << fbuffer << endl;
  243. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  244. CPPUNIT_ASSERT_MD5_STRING("5ed12d69541ec45a637547561f4bad18",inout.str());
  245. #endif
  246. */
  247. }
  248. FloatImage fkern(3,3,GrayColorImageCommonImplementation::noAlignment);
  249. fkern(1,1)=fkern(1,0)=fkern(1,2)=0;
  250. fkern(0,1)=2.0;
  251. fkern(2,1)=-2.0;
  252. fkern(0,0)=fkern(0,2)=1.0;
  253. fkern(2,0)=fkern(2,2)=-1.0;
  254. fkern.mirror(ippAxsBoth);
  255. {
  256. std::stringstream inout;
  257. fbuffer=0;
  258. DeprecatedConverter::filter(fimg,fkern,1,1,&fbuffer);
  259. inout << fixed << setw(3) << setprecision(0) << fbuffer << endl;
  260. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  261. CPPUNIT_ASSERT_MD5_STRING("796dd2b8c03ffaed06456c825ad89d6a",inout.str());
  262. #endif
  263. }
  264. {
  265. std::stringstream inout;
  266. fbuffer=0;
  267. DeprecatedConverter::filter(fimg,fkern,0,0,&fbuffer);
  268. inout << fixed << setw(3) << setprecision(0) << fbuffer << endl;
  269. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  270. CPPUNIT_ASSERT_MD5_STRING("81478bfeb4654208d61d9191bed7bde7",inout.str());
  271. #endif
  272. }
  273. {
  274. std::stringstream inout;
  275. fbuffer=0;
  276. DeprecatedConverter::filter(fimg,fkern,2,2,&fbuffer);
  277. inout << fixed << setw(3) << setprecision(0) << fbuffer << endl;
  278. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  279. CPPUNIT_ASSERT_MD5_STRING("5e3d5bdf3ea094f6cd7e8c5dca7aae4c",inout.str());
  280. #endif
  281. }
  282. }
  283. }
  284. void TestConverter::testHistogram() {
  285. int width=16, height=16;
  286. Image gimg(width,height,GrayColorImageCommonImplementation::noAlignment);
  287. int p=0;
  288. for(int w=0;w<width;w++)
  289. for(int h=0;h<height;h++,p++) {
  290. gimg.setPixel(w,h, p);
  291. }
  292. VectorT<int> *histo = DeprecatedConverter::histogram(gimg);
  293. VectorT<int> *cumuhisto = DeprecatedConverter::histogramCumulative(gimg);
  294. VectorT<float> *normhisto = DeprecatedConverter::histogramNormalized(gimg);
  295. {
  296. std::stringstream inout;
  297. for(uint i=0;i<histo->size();i++) {
  298. inout << (*histo)[i] << " ";
  299. }
  300. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  301. CPPUNIT_ASSERT_MD5_STRING("e1424600e005af0c45dd405d3e71e6b5",inout.str());
  302. #endif
  303. }
  304. {
  305. std::stringstream inout;
  306. for(uint i=0;i<cumuhisto->size();i++) {
  307. inout << (*cumuhisto)[i] << " ";
  308. }
  309. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  310. CPPUNIT_ASSERT_MD5_STRING("9261e264e6848d33f223c726d54cbdd6",inout.str());
  311. #endif
  312. }
  313. {
  314. std::stringstream inout;
  315. for(uint i=0;i<normhisto->size();i++) {
  316. inout << (*normhisto)[i] << " ";
  317. }
  318. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  319. CPPUNIT_ASSERT_MD5_STRING("73e2f2a300d678a44eda8cc08a9ce502",inout.str());
  320. #endif
  321. }
  322. ColorImage cimg(width,height,GrayColorImageCommonImplementation::noAlignment);
  323. p=0;
  324. for(int w=0;w<width;w++)
  325. for(int h=0;h<height;h++,p++) {
  326. cimg.setPixel(w,h, p, p, p);
  327. }
  328. VectorT<int> *color_histo = DeprecatedConverter::histogram(cimg);
  329. VectorT<int> *color_cumuhisto = DeprecatedConverter::histogramCumulative(cimg);
  330. VectorT<float> *color_normhisto = DeprecatedConverter::histogramNormalized(cimg);
  331. {
  332. std::stringstream inout;
  333. for(uint i=0;i<color_histo->size();i++) {
  334. inout << (*color_histo)[i] << " ";
  335. }
  336. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  337. CPPUNIT_ASSERT_MD5_STRING("da9470d8c8079238380a20695973d03b",inout.str());
  338. #endif
  339. }
  340. {
  341. std::stringstream inout;
  342. for(uint i=0;i<color_cumuhisto->size();i++) {
  343. inout << (*color_cumuhisto)[i] << " ";
  344. }
  345. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  346. CPPUNIT_ASSERT_MD5_STRING("4eb56a726ee138c35cd8a69179e34ccc",inout.str());
  347. #endif
  348. }
  349. {
  350. std::stringstream inout;
  351. for(uint i=0;i<color_normhisto->size();i++) {
  352. inout << (*color_normhisto)[i] << " ";
  353. }
  354. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  355. CPPUNIT_ASSERT_MD5_STRING("f0187401062bec17c699456ea4907bdb",inout.str());
  356. #endif
  357. }
  358. }
  359. void TestConverter::testSomething() {
  360. int width=8, height=8;
  361. ColorImage cimg(width,height,GrayColorImageCommonImplementation::noAlignment);
  362. for(int w=0;w<width;w++)
  363. for(int h=0;h<height;h++) {
  364. cimg.setPixel(w,h, (w*h)*7,(w*h)*12,(w*h)*9);
  365. }
  366. cimg.setPixel(0,0,255,255,255);
  367. Image gimg(width,height, GrayColorImageCommonImplementation::noAlignment);
  368. CPPUNIT_ASSERT_NO_THROW(DeprecatedConverter::rgbToGray(cimg, &gimg));
  369. std::stringstream inout;
  370. for(int w=0;w<width;w++) {
  371. for(int h=0;h<height;h++) {
  372. inout << (int)gimg.getPixel(w,h) << " ";
  373. }
  374. inout << endl;
  375. }
  376. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  377. // allow both conversions
  378. try {
  379. // correct conversion
  380. CPPUNIT_ASSERT_MD5_STRING("aa9e7d699518e0e33c97ae673decbeeb", inout.str());
  381. } catch(exception e) {
  382. // conversion with ipp bug (not rounded) 255,255,255 -> 254
  383. CPPUNIT_ASSERT_MD5_STRING("3c693fccf4abc0095a0a9fe991d8c3ee", inout.str());
  384. }
  385. #endif
  386. }
  387. #endif
  388. void TestConverter::testGaussFilter() {
  389. int width=16, height=16;
  390. Image gimg(width,height);
  391. Image gbuffer(width,height);
  392. gbuffer = 0;
  393. gimg=100;
  394. for(int w=0;w<width;w++) {
  395. gimg(w,height/2) = abs(w*height/2-width);
  396. }
  397. for(int h=0;h<height;h++) {
  398. gimg(width/3,h) = abs(width/3-h);
  399. }
  400. gimg(0,0) = 0;
  401. gimg(width-1,height-1) = 0;
  402. {
  403. std::stringstream inout;
  404. gbuffer=0;
  405. std::string fileName("testGaussfilter-orig.png");
  406. std::string fileNameTest("testGaussfilter-gauss-1.png");
  407. gimg.write(ImageFile(fileName));
  408. DeprecatedConverter::filterGauss(gimg,0.85,&gbuffer);
  409. gbuffer.write(ImageFile(fileNameTest));
  410. inout << fixed << setw(3) << setprecision(0) << gbuffer << endl;
  411. remove(fileName.c_str());
  412. remove(fileNameTest.c_str());
  413. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  414. // CPPUNIT_ASSERT_MD5_STRING("b7dbf6cca3b44b476e71ac5c54b89882",inout.str());
  415. #endif
  416. }
  417. }