bitmap转emgucv image
时间: 2023-11-16 16:04:15 浏览: 155
常用容器类型转换-odbc api 开发教程
在EmguCV中,可以使用`Bitmap`和`Image<Bgr, byte>`相互转换。下面是一个示例函数,将`Bitmap`转换为`Image<Bgr, byte>`:
```csharp
public static Image<Bgr, byte> BitmapToImage(Bitmap bitmap)
{
Image<Bgr, byte> image = new Image<Bgr, byte>(bitmap.Width, bitmap.Height);
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadOnly, bitmap.PixelFormat);
int stride = bitmapData.Stride;
int width = bitmap.Width;
int height = bitmap.Height;
unsafe
{
byte* sourcePtr = (byte*)bitmapData.Scan0.ToPointer();
byte* destPtr = (byte*)image.DataPointer.ToPointer();
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
byte b = sourcePtr[x * 3];
byte g = sourcePtr[x * 3 + 1];
byte r = sourcePtr[x * 3 + 2];
destPtr[x * 3] = b;
destPtr[x * 3 + 1] = g;
destPtr[x * 3 + 2] = r;
}
sourcePtr += stride;
destPtr += image.Stride;
}
}
bitmap.UnlockBits(bitmapData);
return image;
}
```
使用方式如下:
```csharp
Bitmap bitmap = new Bitmap("test.bmp");
Image<Bgr, byte> image = BitmapToImage(bitmap);
```
阅读全文