00001 #include "Image/Image.H"
00002 #include "Image/ImageSet.H"
00003 #include "Image/PyramidOps.H"
00004 #include "Image/LowPass.H"
00005 #include "Image/Pixels.H"
00006 #include "Image/ColorOps.H"
00007 #include "Image/MathOps.H"
00008 #include "Raster/Raster.H"
00009 #include "CUDA/CudaImage.H"
00010
00011 #include "wrap_c_cuda.h"
00012 #include "CUDA/cutil.h"
00013 #include "CUDA/CudaColorOps.H"
00014 #include "CUDA/CudaLowPass.H"
00015 #include "CUDA/CudaDevices.H"
00016 #include "CUDA/CudaImageSet.H"
00017 #include "CUDA/CudaImageSetOps.H"
00018 #include "CUDA/CudaPyramidOps.H"
00019 #include "CUDA/CUDAdebayer.H"
00020 #include "Util/fpu.H"
00021 #include <cmath>
00022 #include <fstream>
00023 #include <sys/time.h>
00024
00025
00026 #define ROUNDOFF_MARGIN 0.00005//0.00005
00027
00028 #define BIAS_MARGIN 0.000015//0.000001
00029
00030
00031 void compareregions(const Image<float> &c, const Image<float> &g, const uint rowStart, const uint rowStop, const uint colStart, const uint colStop)
00032 {
00033 uint w,h;
00034 w = c.getWidth();
00035 h = c.getHeight();
00036 if(w != (uint) g.getWidth() || h != (uint) g.getHeight())
00037 {
00038 LINFO("Images are not the same size");
00039 return;
00040 }
00041 if(rowStart > rowStop || colStart > colStop || rowStop > h || colStop > w)
00042 {
00043 LINFO("Invalid regions to compare");
00044 return;
00045 }
00046 for(uint j=rowStart;j<rowStop;j++)
00047 {
00048 printf("\nC[%d]: ",j);
00049 for(uint i=colStart;i<colStop;i++)
00050 {
00051 printf("%.3f ",c.getVal(i,j));
00052 }
00053 printf("\nG[%d]: ",j);
00054 for(uint i=colStart;i<colStop;i++)
00055 {
00056 printf("%.3f ",g.getVal(i,j));
00057 }
00058 }
00059 printf("\n");
00060
00061 }
00062
00063 void calculateCudaSaliency(const CudaImage<PixRGB<float> > &input, const int cudaDeviceNum)
00064 {
00065
00066 CudaImage<PixRGB<float> > cudainput = CudaImage<PixRGB<float> >(input,GLOBAL_DEVICE_MEMORY,cudaDeviceNum);
00067 CudaImage<float> red, green, blue;
00068
00069 cudaGetComponents(cudainput,red,green,blue);
00070
00071 CudaImage<float> lumin = cudaLuminance(cudainput);
00072 }
00073
00074
00075 void printImage(const Image<float> &in)
00076 {
00077 int cnt=0;
00078 for(int i=0;i<in.getWidth();i++)
00079 {
00080 for(int j=0;j<in.getHeight();j++)
00081 {
00082 printf("%g ",in.getVal(i,j));
00083 cnt++;
00084 if(cnt==30)
00085 {
00086 cnt=0;
00087 }
00088 }
00089 printf("\n");
00090 }
00091 }
00092
00093 void printImages(const Image<float> &in1, const Image<float> &in2)
00094 {
00095 int w,h;
00096 w = in1.getWidth(); h = in2.getHeight();
00097 if(in2.getWidth() != w || in2.getHeight() != h)
00098 LFATAL("Cannot compare two different image sizes im1[%dx%d] im2[%dx%d]",
00099 in1.getWidth(),in1.getHeight(),in2.getWidth(),in2.getHeight());
00100 for(int i=0;i<in1.getWidth();i++)
00101 {
00102 for(int j=0;j<in1.getHeight();j++)
00103 {
00104 printf("At [%d,%d] Im1 = %f Im2 = %f\n",i,j,in1.getVal(i,j),in2.getVal(i,j));
00105 }
00106 }
00107 }
00108
00109
00110 void testDiff(const Image<float> in1, const Image<float> in2)
00111 {
00112 Image<float> diff = in1-in2;
00113 Point2D<int> p;
00114 float mi, ma, av;
00115 int w,h;
00116 w = in1.getWidth(); h = in2.getHeight();
00117 if(in2.getWidth() != w || in2.getHeight() != h)
00118 LFATAL("Cannot compare two different image sizes im1[%dx%d] im2[%dx%d]",
00119 in1.getWidth(),in1.getHeight(),in2.getWidth(),in2.getHeight());
00120 getMinMaxAvg(diff,mi,ma,av);
00121 bool acceptable = mi> -ROUNDOFF_MARGIN && ma< ROUNDOFF_MARGIN && std::abs(av) < BIAS_MARGIN;
00122 LINFO("%s: %ux%u image, #1 - #2: avg=%f, diff = [%f .. %f]",
00123 mi == ma && ma == 0.0F ? "MATCH" : (acceptable ? "ACCEPT" : "FAIL"), w, h, av, mi, ma);
00124 if(!acceptable)
00125 {
00126 getMinMaxAvg(in1,mi,ma,av);
00127 LINFO("Image 1 [%dx%d]: avg=%f, diff = [%f .. %f]",
00128 w, h, av, mi, ma);
00129 getMinMaxAvg(in2,mi,ma,av);
00130 LINFO("Image 2 [%dx%d]: avg=%f, diff = [%f .. %f]",
00131 w, h, av, mi, ma);
00132 findMax(diff,p,ma);
00133 LINFO("Maximum difference %f is located at %dx%d",ma,p.i,p.j);
00134 compareregions(in1,in2,std::max(0,p.j-5),std::min(h,p.j+5),std::max(0,p.i-5),std::min(w,p.i+5));
00135 }
00136
00137 }
00138 void testdiff_bayer(const Image<PixRGB<float> > img1,const Image<PixRGB<float> > img2)
00139 {
00140
00141 Image<PixRGB<float> >::const_iterator img1_ptr = img1.begin();
00142 Image<PixRGB<float> >::const_iterator img2_ptr = img2.begin();
00143 Image<PixRGB<float> >::const_iterator stop = img2.end();
00144
00145 int i,counter=0;
00146 while(img2_ptr!=stop)
00147 {
00148 counter++;
00149 for(i=0;i<3;i++)
00150 printf("Counter:%d Image1:%f || Image2:%f \n",counter, img1_ptr->p[i], img2_ptr->p[i]);
00151
00152 getchar();
00153
00154 ++img1_ptr;
00155
00156 ++img2_ptr;
00157 }
00158 }
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364 Image<PixRGB<float> > cpu_debayer_MHC(const Image<float>& img)
00365 {
00366
00367 Image<PixRGB<float> > result(img.getDims(),NO_INIT);
00368 Image<PixRGB<float> >::iterator optr = result.beginw();
00369 Image<PixRGB<float> >::iterator end = result.endw();
00370
00371
00372
00373
00374 int A,B,C,D,E,F;
00375 int i,i_m,i_b;
00376 int j,j_m,j_b;
00377 i=0;
00378 j=0;
00379 while(optr!=end)
00380 {
00381
00382 i_m = i;
00383 i_b = i;
00384 j_m = j;
00385 j_b = j;
00386
00387
00388
00389 if(i%2==0 && j%2==0)
00390 {
00391
00392 if(i>=(result.getHeight()-2)) i_m= result.getHeight();
00393 if(j>=(result.getWidth()-2)) j_m= result.getWidth();
00394 if(j<2) j_b = 0;
00395 if(i<2) i_b = 0;
00396
00397
00398
00399
00400 A = img.getVal( j_b-1, i_b-1 )+
00401 img.getVal( j_m+1 , i_b-1)+
00402 img.getVal( j_b-1 , i_m+1)+
00403 img.getVal( j_m+1 ,i_m+1 );
00404
00405 B = img.getVal( j, i_m+1 ) +
00406 img.getVal( j, i_b-1) ;
00407
00408 C= img.getVal( j,i_m+2 )+
00409 img.getVal( j, i_b-2 );
00410
00411 D= img.getVal( j_b-1,i )+
00412 img.getVal( j_m+1 ,i );
00413
00414 E= img.getVal( j_b-2 ,i )+
00415 img.getVal( j_m+2,i );
00416
00417 F= img.getVal( j ,i );
00418 optr->setRed( ((5*F) -(1 * (A + C)) + (4 * B) + ((1/2) * E))/8);
00419 optr->setGreen(F);
00420 optr->setBlue(((5*F) -(1 * (A + E)) + (4 * D) + ((1/2) * C))/8) ;
00421 ++optr;
00422
00423 j++;
00424
00425 if(j==result.getWidth())
00426 {
00427 i++;
00428 j=0;
00429 }
00430 }
00431 else
00432 {
00433 if(i%2==0 && j%2==1)
00434 {
00435
00436 if(j<2) j_b = 0;
00437 if(i<2) i_b = 0;
00438 if(i>=(result.getHeight()-2)) i_m=result.getWidth();
00439 if(j>=(result.getWidth()-2)) j_m= result.getWidth();
00440
00441 A = img.getVal( j_b-1,i_b-1 )+
00442 img.getVal( j_m+1,i_b-1 )+
00443 img.getVal( j_b-1,i_m+1 )+
00444 img.getVal( j_m+1,i_m+1 );
00445
00446 B = img.getVal( j,i_m+1 ) +
00447 img.getVal( j,i_b-1 );
00448
00449 C= img.getVal( j,i_m+2 )+
00450 img.getVal( j,i_b-2);
00451
00452 D= img.getVal( j_b-1,i )+
00453 img.getVal( j_m+1 ,i );
00454
00455 E= img.getVal( j_b-2,i )+
00456 img.getVal( j_m+2,i );
00457
00458 F= img.getVal( j,i );
00459 optr->setRed(((6*F) -((3/2) * (E + C)) + (2 * A)) /8) ;
00460 optr->setGreen(((4*F) -(1 * (C + E)) + (2 * (D+B)))/8);
00461 optr->setBlue(F) ;
00462 ++optr;
00463
00464 j++;
00465
00466 if(j==result.getWidth())
00467 {
00468 i++;
00469 j=0;
00470 }
00471 }
00472 else
00473 {
00474 if(i%2==1 && j%2==0)
00475 {
00476
00477 if(j<2) j_b = 0;
00478 if(i<2) i_b = 0;
00479 if(i>=(result.getHeight()-2)) i_m= result.getHeight();
00480 if(j>=(result.getWidth()-2)) j_m= result.getWidth();
00481 A = img.getVal( j_b-1,i_b-1)+
00482 img.getVal( j_m+1,i_b-1 )+
00483 img.getVal( j_b-1,i_m+1 )+
00484 img.getVal( j_m+1,i_m+1 );
00485
00486 B = img.getVal( j,i_m+1 ) +
00487 img.getVal(j,i_b-1 );
00488
00489 C= img.getVal( j,i_m+2 )+
00490 img.getVal( j,i_b-2 );
00491
00492 D= img.getVal( j_b-1,i )+
00493 img.getVal( j_m+1,i );
00494
00495 E= img.getVal( j_b-2,i )+
00496 img.getVal( j_m+2,i );
00497
00498 F= img.getVal( j,i );
00499 optr->setRed(F) ;
00500 optr->setGreen(((4*F) -(1 * (C + E)) + (2 * (D+B)))/8);
00501 optr->setBlue(((6*F) -((3/2) * (E + C)) + (2 * A)) /8) ;
00502 ++optr;
00503
00504 j++;
00505
00506 if(j==result.getWidth())
00507 {
00508 i++;
00509 j=0;
00510 }
00511
00512 }
00513 else
00514 {
00515 if(i%2==1 && j%2==1)
00516 {
00517
00518 if(j<2) j_b = 0;
00519 if(i<2) i_b = 0;
00520 if(i>=(result.getHeight()-2)) i_m= result.getHeight();
00521 if(j>=(result.getWidth()-2)) j_m= result.getWidth();
00522 A = img.getVal( j_b-1,i_b-1 )+
00523 img.getVal( j_m+1,i_b-1 )+
00524 img.getVal( j_b-1,i_m+1 )+
00525 img.getVal( j_m+1,i_m+1 );
00526
00527 B = img.getVal( j,i_m+1 ) +
00528 img.getVal( j,i_b-1 );
00529
00530 C= img.getVal( j,i_m+2 )+
00531 img.getVal( j,i_b-2 );
00532
00533 D= img.getVal( j_b-1,i )+
00534 img.getVal( j_m+1,i );
00535
00536 E= img.getVal( j_b-2,i )+
00537 img.getVal( j_m+2,i );
00538
00539 F= img.getVal( j,i );
00540 optr->setRed(((5*F) -(1 * (A + E)) + (4 * D) + ((1/2) * C))/8) ;
00541 optr->setGreen(F);
00542 optr->setBlue(((5*F) -(1 * (A + C)) + (4 * B) + ((1/2) * E))/8);
00543 ++optr;
00544
00545 j++;
00546
00547 if(j==result.getWidth())
00548 {
00549 i++;
00550 j=0;
00551 }
00552 }
00553 }
00554 }
00555 }
00556
00557
00558
00559 }
00560
00561
00562 return result;
00563 }
00564
00565
00566 Image<PixRGB<float> > cpu_debayer(const Image<float>& img)
00567 {
00568
00569 Image<PixRGB<float> > result(img.getDims(),NO_INIT);
00570 Image<PixRGB<float> >::iterator optr = result.beginw();
00571 Image<PixRGB<float> >::iterator end = result.endw();
00572
00573
00574
00575
00576 int i=0;
00577 int j=0;
00578 while(optr!=end)
00579 {
00580 if(i%2==0 && j%2==0)
00581 {
00582 optr->setRed(img.getVal(j,i+1));
00583
00584 optr->setGreen(img.getVal(j,i));
00585
00586 optr->setBlue(img.getVal(j+1,i));
00587 ++optr;
00588
00589 j++;
00590
00591 if(j==result.getWidth())
00592 {
00593 i++;
00594 j=0;
00595 }
00596
00597 }
00598 if(i%2==1 && j%2==0)
00599 {
00600 optr->setRed(img.getVal(j,i));
00601
00602 optr->setGreen(img.getVal(j,i-1));
00603
00604 optr->setBlue(img.getVal(j+1,i-1));
00605 ++optr;
00606 j++;
00607
00608
00609 if(j==result.getWidth())
00610 {
00611 i++;
00612 j=0;
00613 }
00614
00615 }
00616 if(i%2==0 && j%2==1)
00617 {
00618 optr->setRed(img.getVal(j-1,i));
00619
00620 optr->setGreen(img.getVal(j,i+1));
00621
00622 optr->setBlue(img.getVal(j,i));
00623 ++optr; j++;
00624
00625 if(j==result.getWidth())
00626 {
00627 i++;
00628 j=0;
00629 }
00630
00631 }
00632 if(i%2==1 && j%2==1)
00633 {
00634 optr->setRed(img.getVal(j-1,i));
00635
00636 optr->setGreen(img.getVal(j,i));
00637
00638 optr->setBlue(img.getVal(j,i-1));
00639 ++optr; j++;
00640
00641 if(j==result.getWidth())
00642 {
00643 i++;
00644 j=0;
00645 }
00646
00647 }
00648
00649 }
00650
00651 return result;
00652 }
00653
00654 void bayer_test(char **argv)
00655 {
00656
00657 Image<float> i = Raster::ReadFloat(argv[1]);
00658
00659 int deviceNum = 1 ;
00660 CudaDevices::displayProperties(deviceNum);
00661
00662 CudaImage<PixRGB<float> > res_cuda;
00663 CudaImage<float> f;
00664 CudaImage<float> res_cuda_r_only;
00665 CudaImage<float> res_cuda_g_only;
00666 CudaImage<float> res_cuda_b_only;
00667 Image<PixRGB<float> > res_cuda_r;
00668 Image<PixRGB<float> > res_cuda_g;
00669 Image<PixRGB<float> > res_cuda_b;
00670 cudaEvent_t start,stop;
00671 cudaEventCreate(&start);
00672 cudaEventCreate(&stop);
00673 cudaEventRecord(start,0);
00674
00675 {
00676 f = CudaImage<float>(i,GLOBAL_DEVICE_MEMORY,deviceNum);
00677
00678 res_cuda = cuda_1_debayer(f);
00679 }
00680 cudaEventRecord(stop,0);
00681 cudaEventSynchronize(stop);
00682 float elapsedTime;
00683 cudaEventElapsedTime(&elapsedTime,start,stop);
00684 printf("Elasped Time GPU in ms for 1 iteration : %f\n",elapsedTime);
00685 cudaEventDestroy(start);
00686 cudaEventDestroy(stop);
00687
00688
00689 cudaGetComponents(res_cuda,res_cuda_r_only,res_cuda_g_only,res_cuda_b_only);
00690
00691
00692 Image<float> zero(res_cuda.getDims(),NO_INIT);
00693
00694 res_cuda_r = makeRGB(res_cuda_r_only.exportToImage(),zero,zero);
00695 res_cuda_g = makeRGB(zero,res_cuda_g_only.exportToImage(),zero);
00696 res_cuda_b = makeRGB(zero,zero,res_cuda_b_only.exportToImage());
00697
00698
00699
00700
00701
00702
00703
00704 Raster::WriteRGB(res_cuda_r,"test_gpu_r.ppm");
00705
00706
00707
00708 Raster::WriteRGB(res_cuda_g,"test_gpu_g.ppm");
00709
00710
00711 Raster::WriteRGB(res_cuda_b,"test_gpu_b.ppm");
00712
00713 Image<PixRGB<float> > res_copy_cuda = res_cuda.exportToImage();
00714
00715 Raster::WriteRGB(res_copy_cuda,"test_gpu.ppm");
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761 }
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806 void toytest()
00807 {
00808 int dev = 0;
00809 CudaDevices::displayProperties(dev);
00810 Image<float> img = Image<float>(10,10,NO_INIT);
00811 img.setVal(0,0,10.0F); img.setVal(0,1,20.0F); img.setVal(0,2,30.0F); img.setVal(0,3,40.0F); img.setVal(0,4,50.0F);
00812 img.setVal(0,5,20.0F); img.setVal(0,6,30.0F); img.setVal(0,7,40.0F); img.setVal(0,8,50.0F); img.setVal(0,9,60.0F);
00813 img.setVal(1,0,30.0F); img.setVal(1,1,40.0F); img.setVal(1,2,50.0F); img.setVal(1,3,60.0F); img.setVal(1,4,70.0F);
00814 img.setVal(0,5,40.0F); img.setVal(1,6,50.0F); img.setVal(1,7,60.0F); img.setVal(1,8,70.0F); img.setVal(1,9,80.0F);
00815 img.setVal(2,0,50.0F); img.setVal(2,1,60.0F); img.setVal(2,2,70.0F); img.setVal(2,3,80.0F); img.setVal(2,4,90.0F);
00816 img.setVal(2,5,60.0F); img.setVal(2,6,70.0F); img.setVal(2,7,80.0F); img.setVal(2,8,90.0F); img.setVal(2,9,100.F);
00817 img.setVal(3,0,70.0F); img.setVal(3,1,80.0F); img.setVal(3,2,90.0F); img.setVal(3,3,100.F); img.setVal(3,4,110.F);
00818 img.setVal(3,5,80.0F); img.setVal(3,6,90.0F); img.setVal(3,7,100.F); img.setVal(3,8,110.F); img.setVal(3,9,120.F);
00819 img.setVal(4,0,90.0F); img.setVal(4,1,100.F); img.setVal(4,2,110.F); img.setVal(4,3,120.F); img.setVal(4,4,130.F);
00820 img.setVal(4,5,80.0F); img.setVal(4,6,90.0F); img.setVal(4,7,100.F); img.setVal(4,8,110.F); img.setVal(4,9,120.F);
00821 img.setVal(5,0,70.0F); img.setVal(5,1,80.0F); img.setVal(5,2,90.0F); img.setVal(5,3,100.F); img.setVal(5,4,110.F);
00822 img.setVal(5,5,60.0F); img.setVal(5,6,70.0F); img.setVal(5,7,80.0F); img.setVal(5,8,90.0F); img.setVal(5,9,100.F);
00823 img.setVal(6,0,50.0F); img.setVal(6,1,60.0F); img.setVal(6,2,70.0F); img.setVal(6,3,80.0F); img.setVal(6,4,90.0F);
00824 img.setVal(6,5,40.0F); img.setVal(6,6,50.0F); img.setVal(6,7,60.0F); img.setVal(6,8,70.0F); img.setVal(6,9,80.0F);
00825 img.setVal(7,0,30.0F); img.setVal(7,1,40.0F); img.setVal(7,2,50.0F); img.setVal(7,3,60.0F); img.setVal(7,4,70.0F);
00826 img.setVal(7,5,20.0F); img.setVal(7,6,30.0F); img.setVal(7,7,40.0F); img.setVal(7,8,50.0F); img.setVal(7,9,60.0F);
00827 img.setVal(8,0,10.0F); img.setVal(8,1,20.0F); img.setVal(8,2,30.0F); img.setVal(8,3,40.0F); img.setVal(8,4,50.0F);
00828 img.setVal(8,5,00.0F); img.setVal(8,6,10.0F); img.setVal(8,7,20.0F); img.setVal(8,8,30.0F); img.setVal(8,9,40.0F);
00829 img.setVal(9,0,00.0F); img.setVal(9,1,00.0F); img.setVal(9,2,10.0F); img.setVal(9,3,20.0F); img.setVal(9,4,30.0F);
00830 img.setVal(9,5,00.0F); img.setVal(9,6,00.0F); img.setVal(9,7,00.0F); img.setVal(9,8,10.0F); img.setVal(9,9,20.0F);
00831
00832
00833
00834
00835
00836
00837 }
00838
00839
00840 int main(int argc, char **argv)
00841 {
00842
00843 bayer_test(argv);
00844
00845 }