views:

32

answers:

1

Hello,

I try to implement video stabilization project in C++/cli.First of all I have bmp image sequences,and I found motion vectors that show how much specific pixel region move between each image frame . For example I have 256*256 image, I selected 200*200 region in first image frame and secong image frame.And I found how much pixel move between first region and second region.When algorithm went to the last image sequnce,program finished the work.Eventually,I obtained motion vectors.I did this operation using mean absolute method. It worked, but too slowly.My example code block is here,I found only one motion vector first index(x direction and y direction):

//M:image height =256

//N.image width =256

//BS:block size=218


//selecting and reading first and second image frame

frame=1;

s1= "C:\\bike\\" + frame+ ".bmp";
image= gcnew System::Drawing::Bitmap(s1,true );

s2= "C:\\bike\\" + (frame+1)+ ".bmp";
image2= gcnew System::Drawing::Bitmap(s2,true );




for(b=0;b<M;b++){
  for(a=0;a<N;a++)
  {System::Drawing::Color BitmapColor = image->GetPixel(a,b);
I1[b][a]=(double)((BitmapColor.R * 0.3) + (BitmapColor.G * 0.59) + (BitmapColor.B * 0.11));
  }}


for(b=0;b<M;b++){
  for(a=0;a<N;a++)
  {System::Drawing::Color BitmapColor = image2->GetPixel(a,b);
I2[b][a]=(double)((BitmapColor.R * 0.3) + (BitmapColor.G * 0.59) + (BitmapColor.B * 0.11));
  }}



//finding blocks

a=0;
for(i=19;i<237;i++){
 b=0;
 for(j=19;j<237;j++){

  Blocks[a][b]=I2[i][j];
 b++;}
 a++;
}


//finding motion vectors according to the mean absolute differences


//MAD  method

for(m=0;m<(M-BS);m++){
    for(n=0;n<(N-BS);n++){

  toplam=0;
  for(i=0;i<BS;i++){

   for(j=0;j<BS;j++){

    toplam += fabs(I1[m+i][n+j]-Blocks[i][j]);



            }
        }


// finding vectors

if( difference < mindifference){
    mindifference   = difference;
                    MV_x  = m;
      MV_y  = n;}





                            }
                             }

This code example worked.But this is very slowly.I need to implement code optimization. How can I do this without using for cycles,such as I do indexing in C++/cli like MATLAB codes(ex. I1(1:20)=100).

Could you help me please?

Best Regards...

A: 

A couple things you should note:

First, loops in C++ are not slow compared to built-in functions. In MatLab, the fewer operations the better, so it's best to call built-in functions that are a single operation done with optimized code. In C++, YOUR code gets optimized equally with the built-in functions.

Next, GetPixel is extremely slow. Try Bitmap.LockBits instead. Ironically, this seems to contradict my previous statement, but actually it isn't because looping inside LockBits is faster than you doing the loop, but because GetPixel uses a different method which is much much slower.

Once you switch to LockBits, you can probably double or triple your speed again by unrolling the loop somewhat, if the compiler isn't already doing so.

Finally, make sure you're making good use of cache locality. Try both looping orders (e.g. for (a...) for (b...) and for (b...) for (a...)) and measure the time of each to find out which is faster.

Ben Voigt