EMGUCV最快设置像素 [英] EMGUCV fastest set pixel

查看:527
本文介绍了EMGUCV最快设置像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要最快的方法来迭代EmguCV位图矩阵并设置像素。我在Google之后找到了此文件,但700x500的图片大约需要3秒钟:

 


for(int i = 0; i< img.Rows; i ++)

for(int j = 0; j< img.Cols; j ++){

img.Data [x,y,0] = 255;

img.Data [x,y,1] = 255;

img.Data [x,y,2] = 255;

}


预先感谢。

解决方案

Emgu.CV.Image<> 的吸气剂s的 Data 属性返回对该类内部使用的三维数组的引用。当您分配给该数组的元素时(就像在上面的代码中所做的那样),您没有在调用 Data 属性的setter,而是在操纵数组本身。



我怀疑您遇到的速度下降与调用非托管代码有关,因为EmguCV代码基本上是非托管的。



尝试一下,看看速度是否有变化:

  Byte [,,] data = img.Data; 
int行= img.Rows;
int cols = img.Cols;

表示(int y = 0; y<行; ++ i)
{
表示(int x = 0; x {
data [x,y,0] = 255;
data [x,y,1] = 255;
data [x,y,2] = 255;
}
}

图片类具有方法 SetValue ,该方法将每个像素设置为特定值。如果要实现的目的是将图像清除为白色,请尝试调用 img.SetValue(new Rgb(255,255,255))(或您使用的任何一种颜色类型)图片),而不是手动进行。可能会更快。


I Need fastest way to iterate an EmguCV bitmap matrix and set pixel. i found this after google but it takes about 3 sec for 700x500 image:

Documents says that access (get) data matrix is just o(1) but its not clearly declare about set data.



    for(int i = 0; i < img.Rows ; i++)

        for(int j = 0; j < img.Cols; j++){

            img.Data[x,y,0] = 255;

            img.Data[x,y,1] = 255;

            img.Data[x,y,2] = 255;

        }


Thanks in advance.

解决方案

The getter for Emgu.CV.Image<>'s Data property returns a reference to the three-dimension array that is used internally by the class. When you assign to an element of that array (as you are doing in the code above) you are not calling the setter for the Data property, you are manipulating the array itself.

I suspect that the slowdown you are experiencing is related to calling unmanaged code, since the EmguCV code is largely unmanaged.

Try this and see if there is any change in speed:

Byte[,,] data = img.Data;
int rows = img.Rows;
int cols = img.Cols;

for (int y = 0; y < rows; ++i)
{
    for (int x = 0; x < cols; ++x)
    {
        data[x, y, 0] = 255;
        data[x, y, 1] = 255;
        data[x, y, 2] = 255;
    }
}

Also the Image class has a method SetValue that sets every pixel to a specific value. If what you are trying to achieve is to clear the image to white, try calling img.SetValue(new Rgb(255,255,255)) (or whichever color type you're using for the image) instead of doing it manually. May be quicker.

这篇关于EMGUCV最快设置像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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