c#Bitmap与c++ Mat 相互转换 下载本文

内容发布更新时间 : 2024/4/27 11:44:31星期一 下面是文章的全部内容请认真阅读。

C#Bitmap和C++ opencv Mat相互转换

C#调用C++编译成的dll,这个dll中包含Opencv个的Mat到c#的Bitmap转换,具体代码如下:

C++部分:

首先创建win32应用程序,选择类库模板

DLL_APIuchar * _stdcall run1(char* filename, int&width, int&height, int&step) {

IplImage* uu = cvLoadImage(filename);

IplImage* dst1 = cvCreateImage(cvSize(uu->width, uu->height), 8, 1); cvCvtColor(uu, dst1, CV_RGB2GRAY); Matss = cvarrToMat(uu);

uchar * data = new uchar[uu->width*uu->height * 3]; data = ss.data;

width = ss.size().width; height = ss.size().height; step = ss.step; return data; }

C#中调用这个dll

[DllImport(@\vAssemblies.dll\)]

publicstaticexternIntPtr run1(string a, outint width, outint height, outint step);

至此完成C++ Mat到Bitmap的转换

下面是Bitmap 到 Mat的转换 C# 部分

publicstaticBitmapInfoGetImagePixel(Bitmap Source) { byte[] result; int step;

intiWidth = Source.Width; intiHeight = Source.Height;

Rectanglerect = newRectangle(0, 0, iWidth, iHeight);

System.Drawing.Imaging.BitmapDatabmpData = Source.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, Source.PixelFormat); IntPtriPtr = bmpData.Scan0;

intiBytes = iWidth * iHeight * 3; //根据通道数进行设置 byte[] PixelValues = newbyte[iBytes];

//int time = Environment.TickCount;

System.Runtime.InteropServices.Marshal.Copy(iPtr, PixelValues, 0, iBytes); //time = Environment.TickCount - time; //Console.WriteLine(time.ToString() + \ Source.UnlockBits(bmpData); step = bmpData.Stride; result = PixelValues; // return result; // step = 0;

BitmapInfo bi = newBitmapInfo{ Result=result, Step=step }; return bi; } }

///

/// Bitmap信息类 ///

publicclassBitmapInfo {

publicbyte[] Result { get; set; } publicint Step { get; set; }

}

Step是扫描的步长,这个很重要,如果这个步长不是原来的值,就会造成图像偏移,从而造成失真。

C++部分

DLL_APIvoid_stdcallshow(uchar* data,intwidth,intheight,intstep) {

Matimage(height,width, CV_8UC3,data,step); //image.data = data; imshow(\,image);

//Mat(int rows, int cols, int type, void* data, size_t step = AUTO_STEP); }

C# 中调用方式

BitmapInfo bi = GetImagePixel(bitmap);

show(bi.Result,bitmap.Width,bitmap.Height,bi.Step);

至此完成他们的相互转换