test_wrap_cuda_debayer.C

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 // Allowance for one particular pixel
00026 #define ROUNDOFF_MARGIN 0.00005//0.00005
00027 // Allowance for image wide bias
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   // Copy the input image to the CUDA device
00066   CudaImage<PixRGB<float> > cudainput = CudaImage<PixRGB<float> >(input,GLOBAL_DEVICE_MEMORY,cudaDeviceNum);
00067   CudaImage<float> red, green, blue;
00068   // Get the components
00069   cudaGetComponents(cudainput,red,green,blue);
00070   // Get the luminance
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)//(mi != ma || ma != 0.0F)
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   //printImage(diff);
00137 }
00138 void testdiff_bayer(const Image<PixRGB<float> > img1,const Image<PixRGB<float> > img2)
00139 {
00140         //Compare the 2 images pixel by pixel
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 Image<PixRGB<float> > makeR(const Image<float>& img)
00160 {
00161         Image<PixRGB<float> > result(img.getDims(),NO_INIT);
00162 
00163         Image<PixRGB<float> >::iterator start = result.beginw();
00164         Image<PixRGB<float> >::iterator end = result.endw();
00165         int i=0;
00166         int j=0;
00167         while(start!=end)
00168         {
00169                           start->setRed(img.getVal(j,i));
00170 
00171                           start->setGreen(0);
00172 
00173                           start->setBlue(0);
00174                         start++;
00175                         j++;
00176 
00177                         if(j==result.getWidth())
00178                         {
00179                                 i++;
00180                                 j=0;
00181                         }
00182                         if(i==result.getWidth())
00183                         {
00184 
00185                                 i=i-1;
00186 
00187         }
00188 
00189 
00190         //Iterate over  and set values R,0,0
00191         for(int i=0;i<result.getWidth();i++)
00192         {
00193                 for(int j=0;j<result.getHeight();j++)
00194                 {      // printf("i:%d  j:%d",i,j);
00195                         //start[i+j*result.getWidth()][0] = img.getVal(i,j);
00196                 }
00197         }
00198         return result;
00199 
00200 }
00201 Image<PixRGB<float> > makeG(const Image<float>& img)
00202 {
00203         Image<PixRGB<float> > result(img.getDims(),NO_INIT);
00204 
00205         Image<PixRGB<float> >::iterator optr = result.beginw();
00206 
00207         //Iterate over  and set values R,0,0
00208 
00209 for(int i=0;i<result.getWidth();i++)
00210         {
00211                 for(int j=0;j<result.getHeight();j++)
00212                 {      // printf("i:%d  j:%d",i,j);
00213 
00214                           optr->setGreen(img.getVal(i,j));
00215 
00216                           optr->setRed(0);
00217 
00218                           optr->setBlue(0);
00219                         ++optr;
00220                 }
00221         }
00222         return result;
00223 
00224         }
00225 
00226 Image<PixRGB<float> > makeB(const Image<float>& img)
00227 {
00228         Image<PixRGB<float> > result(img.getDims(),NO_INIT);
00229 
00230         Image<PixRGB<float> >::iterator optr = result.beginw();
00231 
00232         //Iterate over  and set values R,0,0
00233         for(int i=0;i<result.getWidth();i++)
00234         {
00235                 for(int j=0;j<result.getHeight();j++)
00236                 {      // printf("i:%d  j:%d",i,j);
00237 
00238                           optr->setBlue(img.getVal(i,j));
00239 
00240                           optr->setGreen(0);
00241 
00242                           optr->setRed(0);
00243 
00244                         optr=optr + 3 ;
00245                 }
00246         }
00247         return result;
00248 
00249         if(Row%2==0 && j%2==0)
00250         {
00251           //Definiton of terms which will be used in kernel computation
00252                 if(j<2) j_b = j_b + 2;
00253                 if(Row<2) Row_b = Row_b + 2;
00254               //The code seems to work without taking care of the boundary condition ... is it safe to avoid it..will save some on computation..!!
00255                 //if(j>(w-2)) j_m = j_m -2;
00256               //if(Row>(h-2)) Row_m = Row_m -2;
00257 
00258                 A =         getVal[ j_b-1 + ((Row_b-1) * Width) ]+
00259                          getVal[ j+1 + ((Row_b-1) * Width) ]+
00260                          getVal[ j_b-1 + ((Row+1) * Width) ]+
00261                                getVal[ j+1 + ((Row+1) * Width) ];
00262 
00263                 B =     getVal[ j + ((Row+1) * Width) ] +
00264                         getVal[ j + ((Row_b-1) * Width) ];
00265 
00266                 C=      getVal[ j + ((Row+2) * Width) ]+
00267                         getVal[ j + ((Row_b-2) * Width) ];
00268 
00269                 D=      getVal[ j_b-1 + ((Row) * Width) ]+
00270                         getVal[ j+1 + ((Row) * Width) ];
00271 
00272                 E=      getVal[ j_b-2 + ((Row) * Width) ]+
00273                                getVal[ j+2 + ((Row) * Width) ];
00274 
00275                  F=      getVal[ j + ((Row) * Width) ];
00276                 dptr[Row*Width+j].p[0]=  ((5*F) -(1 * (A + C)) + (4 * B) + ((1/2) * E))/8 ;
00277                 dptr[Row*Width+j].p[1]=   F;
00278                 dptr[Row*Width+j].p[2]=  ((5*F) -(1 * (A + E)) + (4 * D) + ((1/2) * C))/8 ;
00279         }
00280         if(Row%2==0 && j%2==1)
00281         {
00282           //Definiton of terms which will be used in kernel computation
00283                 if(j<1) j_b = j_b + 2;
00284                 if(Row<1) Row_b = Row_b + 2;
00285                 A =         getVal[ j_b-1 + ((Row_b-1) * Width) ]+
00286                          getVal[ j+1 + ((Row_b-1) * Width) ]+
00287                          getVal[ j_b-1 + ((Row+1) * Width) ]+
00288                                getVal[ j+1 + ((Row+1) * Width) ];
00289 
00290                 B =     getVal[ j + ((Row+1) * Width) ] +
00291                         getVal[ j + ((Row_b-1) * Width) ];
00292 
00293                 C=      getVal[ j + ((Row+2) * Width) ]+
00294                         getVal[ j + ((Row_b-2) * Width) ];
00295 
00296                 D=      getVal[ j_b-1 + ((Row) * Width) ]+
00297                         getVal[ j+1 + ((Row) * Width) ];
00298 
00299                 E=      getVal[ j_b-2 + ((Row) * Width) ]+
00300                                getVal[ j+2 + ((Row) * Width) ];
00301 
00302                  F=      getVal[ j + ((Row) * Width) ];
00303                 dptr[Row*Width+j].p[0]=  ((6*F) -((3/2) * (E + C)) + (2 * A)) /8 ;
00304                 dptr[Row*Width+j].p[1]=  ((4*F) -(1 * (C + E)) + (2 * (D+B)))/8;
00305                 dptr[Row*Width+j].p[2]=  F ;
00306         }
00307         if(Row%2==1 && j%2==0)
00308         {
00309            //Definiton of terms which will be used in kernel computation
00310                 if(j<1) j_b = j_b + 2;
00311                 if(Row<1) Row_b = Row_b + 2;
00312                 A =         getVal[ j_b-1 + ((Row_b-1) * Width) ]+
00313                          getVal[ j+1 + ((Row_b-1) * Width) ]+
00314                          getVal[ j_b-1 + ((Row+1) * Width) ]+
00315                                getVal[ j+1 + ((Row+1) * Width) ];
00316 
00317                 B =     getVal[ j + ((Row+1) * Width) ] +
00318                         getVal[ j + ((Row_b-1) * Width) ];
00319 
00320                 C=      getVal[ j + ((Row+2) * Width) ]+
00321                         getVal[ j + ((Row_b-2) * Width) ];
00322 
00323                 D=      getVal[ j_b-1 + ((Row) * Width) ]+
00324                         getVal[ j+1 + ((Row) * Width) ];
00325 
00326                 E=      getVal[ j_b-2 + ((Row) * Width) ]+
00327                                getVal[ j+2 + ((Row) * Width) ];
00328 
00329                  F=      getVal[ j + ((Row) * Width) ];
00330                 dptr[Row*Width+j].p[0]=  F ;
00331                 dptr[Row*Width+j].p[1]=  ((4*F) -(1 * (C + E)) + (2 * (D+B)))/8;
00332                 dptr[Row*Width+j].p[2]=  ((6*F) -((3/2) * (E + C)) + (2 * A)) /8 ;
00333 
00334         }
00335         if(Row%2==1 && j%2==1)
00336         {
00337           //Definiton of terms which will be used in kernel computation
00338                 if(j<1) j_b = j_b + 2;
00339                 if(Row<1) Row_b = Row_b + 2;
00340                 A =         getVal[ j_b-1 + ((Row_b-1) * Width) ]+
00341                          getVal[ j+1 + ((Row_b-1) * Width) ]+
00342                          getVal[ j_b-1 + ((Row+1) * Width) ]+
00343                                getVal[ j+1 + ((Row+1) * Width) ];
00344 
00345                 B =     getVal[ j + ((Row+1) * Width) ] +
00346                         getVal[ j + ((Row_b-1) * Width) ];
00347 
00348                 C=      getVal[ j + ((Row+2) * Width) ]+
00349                         getVal[ j + ((Row_b-2) * Width) ];
00350 
00351                 D=      getVal[ j_b-1 + ((Row) * Width) ]+
00352                         getVal[ j+1 + ((Row) * Width) ];
00353 
00354                 E=      getVal[ j_b-2 + ((Row) * Width) ]+
00355                                getVal[ j+2 + ((Row) * Width) ];
00356 
00357                  F=      getVal[ j + ((Row) * Width) ];
00358                 dptr[Row*Width+j].p[0]=  ((5*F) -(1 * (A + E)) + (4 * D) + ((1/2) * C))/8 ;
00359                 dptr[Row*Width+j].p[1]=   F;
00360                 dptr[Row*Width+j].p[2]=  ((5*F) -(1 * (A + C)) + (4 * B) + ((1/2) * E))/8;
00361         }
00362 
00363 }*/
00364 Image<PixRGB<float> > cpu_debayer_MHC(const Image<float>& img)
00365 {
00366         //Initialize the image
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         // printf("Width:%d Height:%d",result.getWidth(),result.getHeight());
00372         //Assign value to each pixel in the result image
00373         //Perhaps this is not a great idea to use this iterator
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 //                        if(i%2==0 &&  j%2==0)
00382         i_m = i;
00383         i_b = i;
00384         j_m = j;
00385         j_b = j;
00386 
00387 //                        {
00388 //                          optr->setRed(img.getVal(j,i+1));
00389 if(i%2==0 && j%2==0)
00390         {
00391           //Definiton of terms which will be used in kernel computation
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               //The code seems to work without taking care of the boundary condition ... is it safe to avoid it..will save some on computation..!!
00397                 //if(j>(w-2)) j_m = j_m -2;
00398               //if(i>(h-2)) i_m = i_m -2;
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           //Definiton of terms which will be used in kernel computation
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            //Definiton of terms which will be used in kernel computation
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           //Definiton of terms which will be used in kernel computation
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         }//if loop ends here
00553         }// 1 st else
00554         }// 2 nd else
00555             }// 3 rd else
00556 
00557 
00558 
00559         }//while loop ends here
00560 
00561 
00562 return result;
00563 }//function defination ends here
00564 
00565 
00566 Image<PixRGB<float> > cpu_debayer(const Image<float>& img)
00567 {
00568         //Initialize the image
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         // printf("Width:%d Height:%d",result.getWidth(),result.getHeight());
00574         //Assign value to each pixel in the result image
00575         //Perhaps this is not a great idea to use this iterator
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                         }//if ends
00648 
00649         }
00650         //Return the result
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  // int counter;
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 //  for(counter=0;counter<10;counter++)
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   //Get color components
00689   cudaGetComponents(res_cuda,res_cuda_r_only,res_cuda_g_only,res_cuda_b_only);
00690 
00691   //Make R,0,0 from r only
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  // Image<PixRGB<float> > res_copy_cuda_r = res_cuda_r.exportToImage();
00703 
00704   Raster::WriteRGB(res_cuda_r,"test_gpu_r.ppm");
00705 
00706 // Image<PixRGB<float> > res_copy_cuda_g = res_cuda_g.exportToImage();
00707 
00708   Raster::WriteRGB(res_cuda_g,"test_gpu_g.ppm");
00709  // Image<PixRGB<float> > res_copy_cuda_b = res_cuda_b.exportToImage();
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   struct timeval cstart,cend;
00718   long mtime,seconds,useconds;
00719  // int counter2;
00720   gettimeofday(&cstart,NULL);
00721   Image<PixRGB<float> > res_cpu;
00722 //for(counter2=0;counter2<10;counter2++)
00723   res_cpu = cpu_debayer_MHC(i);
00724 
00725   gettimeofday(&cend,NULL);
00726   seconds = cend.tv_sec - cstart.tv_sec;
00727   useconds= cend.tv_usec - cstart.tv_usec;
00728   mtime = ((seconds)*1000 + useconds/1000);
00729   printf("Elapsed Time CPU in ms for 1 iteration : %ld\n",mtime);
00730 
00731 
00732   Raster::WriteRGB(res_cpu,"test_cpu.ppm");
00733 
00734   Image<float> res_cpu_r_only;
00735   Image<float> res_cpu_g_only;
00736   Image<float> res_cpu_b_only;
00737 
00738   Image<PixRGB<float> > res_cpu_r;
00739   Image<PixRGB<float> > res_cpu_g;
00740   Image<PixRGB<float> > res_cpu_b;
00741 
00742   //Make R,0,0 from r only
00743   Image<float> zero1(res_cpu.getDims(),NO_INIT);
00744 
00745   getComponents(res_cpu,res_cpu_r_only,res_cpu_g_only,res_cpu_b_only);
00746   res_cpu_r = makeRGB(res_cpu_r_only,zero,zero);
00747   res_cpu_g = makeRGB(zero,res_cpu_g_only,zero);
00748   res_cpu_b = makeRGB(zero,zero,res_cpu_b_only);
00749 
00750   Raster::WriteRGB(res_cpu_r,"test_cpu_r.ppm");
00751   Raster::WriteRGB(res_cpu_g,"test_cpu_g.ppm");
00752   Raster::WriteRGB(res_cpu_b,"test_cpu_b.ppm");
00753 
00754 
00755 
00756 
00757   testDiff(res_cpu_r_only,res_cuda_r_only.exportToImage());
00758   testDiff(res_cpu_g_only,res_cuda_g_only.exportToImage());
00759   testDiff(res_cpu_b_only,res_cuda_b_only.exportToImage());
00760   */
00761 }
00762 
00763 
00764 /*
00765 void unit_test(int argc, char **argv)
00766 {
00767 
00768   if (argc != 2) LFATAL("USAGE: %s <input.pgm>", argv[0]);
00769 
00770   setFpuRoundingMode(FPU_ROUND_NEAR);
00771   int cudaDeviceNum = 0;
00772   CudaDevices::displayProperties(cudaDeviceNum);
00773 
00774   //CUT_DEVICE_INIT(cudaDeviceNum);
00775 
00776   LINFO("Reading: %s", argv[1]);
00777   Image<PixRGB<float> > img = Raster::ReadRGB(argv[1]);
00778 
00779 
00780 
00781   // Compare normal implementation versus CUDA implementation
00782   // Original Implementation
00783   Image<float> normalLum = luminanceNTSC(img);
00784   // CUDA Implementation
00785   // Copy image to CUDA
00786   CudaImage<PixRGB<float> > cimg = CudaImage<PixRGB<float> >(img,GLOBAL_DEVICE_MEMORY,cudaDeviceNum);
00787   // Run CUDA Implementation and shove it back onto the host
00788   CudaImage<float> cLum = cudaLuminanceNTSC(cimg);
00789   Image<float> hcLum = cLum.exportToImage();
00790   testDiff(normalLum,hcLum);
00791   // Compare results
00792   // Test low pass 5 cuda filter against standard
00793   testDiff(cudaLowPass5Dec(cLum,true,true).exportToImage(), lowPass5yDecY(lowPass5xDecX(normalLum)));
00794   // Test low pass 9 filter against standard
00795   testDiff(cudaLowPass9(cLum,true,true).exportToImage(), lowPass9(normalLum,true,true));
00796   // Test the Gaussian Pyramid building
00797   CudaImageSet<float> cis = cudaBuildPyrGaussian(cLum,0,9,5);
00798   ImageSet<float> is = buildPyrGaussian(normalLum,0,9,5);
00799   for(uint i=0;i<is.size();i++)
00800   {
00801     Image<float> ctmp= cis[i].exportToImage();
00802     testDiff(ctmp,is[i]);
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   //Image<float> cres = cudaLowPass5yDec(img).exportToImage();
00834   //Image<float> normres = lowPass5yDecY(img);
00835   //testDiff(normres,cres);
00836   //printImages(normres,cres);
00837 }
00838 
00839 
00840 int main(int argc, char **argv)
00841 {
00842  // unit_test(argc,argv);
00843   bayer_test(argv);
00844   //toytest();
00845 }
Generated on Sun May 8 08:04:44 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3