IC Imaging Control 捕捉的BMP转换到Emgu的Image(1)

来源:互联网 发布:中小学生安全网络答题 编辑:程序博客网 时间:2024/06/09 22:24

映美精提供的IC Imaging Control .net控件,其捕捉的Bitmap,直接使用语句


Image<Gbr,Byte> img = new Image<Gbr,Byte>(bitmap)      //语句1


编译可以通过,但是执行会报 Opencv未名错误。

但是VS2008的Picturebox可以直接使用IC控件的Bitmap, 微软够强。

使用LockBits锁定Bitmap的内存

BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, 1280, 960), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);


看到IC控件捕捉的Bitmpa其Stride是负值。

若将其存为图像文件,再次载入,使用【语句1】,便会成功。


暂时先使用

Image<Gbr,Byte> img = new Image<Bgr, byte>(1280,960,bmpData.Stride,bmpData.Scan0);

可以成功。

现在测时为7毫秒左右。 先前,测试要120多毫秒。还不知道原因。

若使用先存一下文件,使用【语句1】,则耗时42毫秒。


使用Unsafe代码,就是用指针遍历Bitmap的像素,赋值给img.Dat,也耗时40多毫秒。



           unsafe           {               Byte* ptr = (byte*)bmpData.Scan0;               for (y = 0; y < 400; y++)               {                   for (x = 0; x < 1280; x++)                   {                       img .Data[x,y,0] = *ptr;                                img .Data[x, y, 1] = *(ptr + 1);                       img .Data[x, y, 2] = *(ptr + 2);                              }                        ptr += bmpData.Stride - bmpData.Width * 3;                            }                      }//unsafe





原创粉丝点击