迭代与emgu简历的图像的像素 [英] Iterate over pixels of an image with emgu cv

查看:233
本文介绍了迭代与emgu简历的图像的像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要遍历图像的所有像素,并用搜索模式进行比较。凭借最佳的性能和C#。我发现emgu简历,对于英特尔的OpenCV的包装。但我不知道如何正确使用emgu。有谁知道我能做到这一点?什么是图像的数据属性?这是为imageData?如果是的话,它的价值是什么?谢谢

I want to iterate over all pixel of an image and compare with a search pattern. With best performance possible and in C#. I found emgu cv, a wrapper for Intels opencv. But I don't know how to use emgu properly. Does anybody know how I can do this? What is the data Property of Image? Is this the imagedata? If yes, which value is what? Thanks

我编写我在C#中的功能和它的工作很好,但这样太慢了!我已经在C的algorhytm我翻译成C#。 C#是3到4倍比C慢! (我的功能在图像上的几乎每一个像素进行迭代,以寻求在图像中的形状 - >休变换)

I coded my function in C# and it worked well, but way too slow! I had already an algorhytm in c which I translated into C#. C# is 3 to 4 TIMES slower than c! (My function iterates over almost every pixel of an image to seek a shape in an image. -> Hugh Transformation)

唉,我听说不安全的代码可能会更快,因为它不检查数组边界值和东西。真的吗?直接运行不安全代码身体机器上?

Well, I heard that unsafe code could be faster because it does not check array boundries and stuff. Is that true? Runs unsafe code directly on the physically machine?

无论如何,我试图把不安全的代码到我的功能。但是,我没能得到一个指向我的3D阵列或用指针来访问三维阵列。我怎样才能从上面不安全的代码重写代码?并将此带来额外的性能提升,甚至跑步速度最快的C代码?

Anyway, I tried to put unsafe code into my function. But I was not able to get a pointer to my 3D-array or to access the 3D-array with the pointer. How can I rewrite this code from above with unsafe code? And would this bring an additional performance boost or even run as fast as c-code?

推荐答案

由于emgu网站状态有primarly两种策略:

As emgu website state there are primarly two strategies:

的安全(慢)的方式

假设你正在工作在一个图片< BGR,字节>
你可以通过调用

Suppose you are working on an Image<Bgr, Byte>. You can obtain the pixel on the y-th row and x-th column by calling

Bgr color = img[y, x];



设定第y行和第x列在象素也简单

Setting the pixel on the y-th row and x-th column is also simple

img[y,x] = color;



快速的方式

的图像的像素值被存储在数据属性,一个三维阵列。
好了,所以这是真实的,但并没有告诉怎么做,在真实的情景。因此,让我们看到了一些工作的代码,然后再讨论的性能和优化:

The Image pixels values are stored in the Data property, a 3D array. Ok so this is true but does not tell how to do it in a real scenario. So let's see some working code and then discuss performance and optimization:

Image<Bgr, Byte> original = newImage<Bgr, byte>(1024, 768);
    Stopwatch evaluator = newStopwatch(); 
    int repetitions = 20;
    Bgr color = newBgr(100, 40, 243);

    evaluator.Start();
    for (int run = 0; run < repetitions; run++)
    {
    for (int j = 0; j < original.Cols; j++)
    {
    for (int i = 0; i < original.Rows; i++)
    {
    original[i, j] = color;
    }
    }
    }
    evaluator.Stop();
    Console.WriteLine("Average execution time for {0} iteration \n using column per row access: {1}ms\n", repetitions, evaluator.ElapsedMilliseconds / repetitions);



因此​​,这是一个使用设置图像的安全缓慢的方式平均运行时间,你有20个运行后像素
在我的机器需要的 1021ms ...

所以1021毫秒为单位的平均时间,循环和设置的像素数等于1024 * 768。我们可以做一个更好一点由一行行的循环

So 1021 milliseconds as average time for looping and setting a number of pixels equal to 1024*768. We could have done a little bit better by looping on row by row

让我们重构一点我们的代码,让我们用直接使用的image.data属性更快的方法:

So let's refactor a little bit our code and let's use the faster way using directly Image.Data property:

evaluator.Reset();

evaluator.Start();
for (int run = 0; run < repetitions; run++)
{
for (int i = 0; i < original.Rows; i++)
{
for (int j = 0; j < original.Cols; j++)
{
original.Data[i, j, 0] = 100;

original.Data[i, j, 1] = 40;
original.Data[i, j, 2] = 243;
}
}
}
evaluator.Stop();
Console.WriteLine("Average execution time for {0} iterations \n using Data property: {1}ms\n", repetitions, evaluator.ElapsedMilliseconds / repetitions);

在我的机器需要的 519ms
所以我们获得了50%的性能提升。执行时间已经下降了两个因素。

On my machine it takes 519ms. So we have gained a performance boost of 50%. The execution time has been decreased by a factor of two.

于是在脑海中的代码保存我们使用C#仔细寻找,我们可以做一个小的变革,再次大幅提升我们的形象像素设置,性能... 我们不应该使用C#属性在循环中!

So looking carefully at the code keeping in mind that we are using C#, we can do a minor change that will drastically boost again our image pixel setting performance... we should not use c# property inside a loop!!!

evaluator.Reset();

evaluator.Start();
byte[,,] data = original.Data;

for (int run = repetitions - 1; run >= 0; run--)
{
for (int i = original.Rows - 1; i >= 0; i--)
{
for (int j = original.Cols - 1; j >= 0; j--)
{
data[i, j, 0] = 100;

data[i, j, 1] = 40;
data[i, j, 2] = 243;
}
}
}
evaluator.Stop();

通过这个最新的一段代码,你将有一个巨大的性能提升(73ms)由于正确使用C#语言。

With this latest piece of code you will have a huge performance boost (73ms) due to correct use of C# language.

这篇关于迭代与emgu简历的图像的像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆