简体   繁体   English

C ++ / CLI中视频稳定项目的平均绝对差方法

[英]mean absolute difference method for video stabilisation project in C++/CLI

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 . 我尝试在C ++ / cli中实现视频稳定项目。首先,我拥有bmp图像序列,并且找到了运动矢量,这些运动矢量显示了每个图像帧之间移动了多少特定像素区域。 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. 例如我有256 * 256的图像,我在第一个图像帧和第二个图像帧中选择了200 * 200个区域,然后发现第一区域和第二个区域之间移动了多少像素。当算法转到最后一个图像序列时,程序完成了最终,我获得了运动矢量。我使用均值绝对方法进行了此操作。 It worked, but too slowly.My example code block is here,I found only one motion vector first index(x direction and y direction): 它起作用了,但是太慢了。我的示例代码块在这里,我发现只有一个运动矢量的第一个索引(x方向和y方向):

//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). 如何不使用循环就可以做到这一点,例如我在C ++ / cli中像MATLAB代码一样进行索引(例如I1(1:20)= 100)。

Could you help me please? 请问你能帮帮我吗?

Best Regards... 最好的祝福...

A couple things you should note: 您应注意以下几点:

First, loops in C++ are not slow compared to built-in functions. 首先,与内置函数相比,C ++中的循环并不慢。 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. 在MatLab中,操作越少越好,因此最好调用内置函数,这些函数是使用优化代码完成的单个操作。 In C++, YOUR code gets optimized equally with the built-in functions. 在C ++中,您的代码通过内置函数得到了同样的优化。

Next, GetPixel is extremely slow. 接下来, GetPixel非常慢。 Try Bitmap.LockBits instead. 请尝试使用Bitmap.LockBits 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. 具有讽刺意味的是,这似乎与我之前的声明相矛盾,但这并不是因为LockBits内部的循环比您执行循环要快,而是因为GetPixel使用了另一种慢得多的方法。

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. 切换到LockBits ,如果编译器尚未这样做,则可以通过稍微展开循环来再次将速度提高一倍或两倍。

Finally, make sure you're making good use of cache locality. 最后,请确保您充分利用了缓存的局部性。 Try both looping orders (eg for (a...) for (b...) and for (b...) for (a...) ) and measure the time of each to find out which is faster. 尝试两个循环顺序(例如for (a...) for (b...)for (b...) for (a...) ),并测量每个循环的时间以找出哪个更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM