C#Bitmap
?/p>
C++ opencv Mat
相互转换
C#
调用
C++
编译成的
dll
,这?/p>
dll
中包?/p>
Opencv
个的
Mat
?/p>
c#
?/p>
Bitmap
转换,具体代码如下:
C++
部分?/p>
首先创建
win32
应用程序,选择类库模板
DLL_API
uchar
*
_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
);
Mat
ss = 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#
中调用这?/p>
dll
[
DllImport
(
@"E:\Files\BarcodeProject\Code\testCode\OpenCvAssemblies\Debug\OpenC
vAssemblies.dll"
)]
public
static
extern
IntPtr
run1(
string
a,
out
int
width,
out
int
height,
out
int
step);
至此完成
C++ Mat
?/p>
Bitmap
的转?/p>
下面?/p>
Bitmap
?/p>
Mat
的转?/p>
C#
部分
public
static
BitmapInfo
GetImagePixel(
Bitmap
Source)
{
byte
[] result;
int
step;
int
iWidth = Source.Width;
int
iHeight = Source.Height;
Rectangle
rect =
new
Rectangle
(0, 0, iWidth, iHeight);
System.Drawing.Imaging.
BitmapData
bmpData = Source.LockBits(rect,
System.Drawing.Imaging.
ImageLockMode
.ReadWrite, Source.PixelFormat);
IntPtr
iPtr = bmpData.Scan0;
int
iBytes = iWidth * iHeight * 3;
//
根据通道数进行设?/p>