[置顶] 将C++ IplImage 图像用C#读取

如何将C++ IplImage 图像用C#读取  ?

将opencv 的C++程序做成 dll 动态链接库 用C#调用 

当然这里需要安装emgucv  ,也可以自己实现这个类。

我把这个类贴在后面了。

-----------------------------下面开始------------------------------------------

下面我把实现贴出来给大家参考:

1.制作dll    

#include "stdafx.h"
#define DLL_API extern "C" _declspec(dllexport)   
#include <Windows.h>  
#include <stdio.h>  
#include <opencv2\opencv.hpp>
#include <opencv\cxcore.h>
#include <opencv2/legacy/compat.hpp>
using namespace std;
using namespace cv;

DLL_API IplImage * _stdcall run1()
{

	IplImage *src;
	src = cvLoadImage("d:/1.jpg");
	
	return src;
}

 2.C#中读取dll

 需要开启 unsafe 模式

[DllImport("dll_test_0410.dll")]
        unsafe
        public static extern MIplImage* run1();


调用函数并显示成图片:

需要将生成的dll 放入c#工程的bin里面对应的debug或者release


 unsafe
        MIplImage* a;
        unsafe
        private void button5_Click(object sender, EventArgs e)
        {
            IntPtr aa= new IntPtr();
            a= run1();
            int m= a->width;
            aa = a->imageData;
            int uu =a->height;
            int step = a->widthStep;
            Image<Bgr, byte> src = new Image<Bgr, byte>(m, uu, step, aa);//没有安装emgucv的话这个方法不能用,用intPtr转换
            pictureBox1.Image =  src.ToBitmap();
////////////////方法二,但是MIplImage还需要定义速度也慢,下面为单通道图像,多通道类似写一下就行//////
            byte []uuu = new byte[width*height];
            Marshal.Copy(aa,uuu,0,width*height);
            Bitmap dst = new Bitmap(width, height);
            Color color= new Color();
            for(int j=0;j<height;j++)
            
            {for(int i=0;i<width;i++)
            {
                byte m = uuu[j*width+i];
                color = Color.FromArgb(m, m, m);
                dst.SetPixel(i, j, color);
            }
            }
            
             pictureBox1.Image = dst;

        }




显示效果:



转载请注明:http://blog.csdn.net/yeyang911/article/details/23375933


可以自己定义这个类 长度为112和opencv里面IplImage 长度一样

下面是我按照C++的格式对应写的结构体  
        [StructLayout(LayoutKind.Sequential)]  
        public struct MIplImage  
        {  
  
            //  
            // 摘要:  
            //     sizeof(IplImage)  
            public int nSize;  
            //  
            // 摘要:  
            //     version (=0)  
            public int ID;  
            //  
            // 摘要:  
            //     Most of OpenCV functions support 1,2,3 or 4 channels  
            public int nChannels;  
            //  
            // 摘要:  
            //     ignored by OpenCV  
            public int alphaChannel;  
            //  
            // 摘要:  
            //     pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16U, IPL_DEPTH_16S,  
            //     IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F are supported  
            public IPL_DEPTH depth;  
            //  
            // 摘要:  
            //     ignored by OpenCV  
            public byte colorModel0;  
            //  
            // 摘要:  
            //     ignored by OpenCV  
            public byte colorModel1;  
            //  
            // 摘要:  
            //     ignored by OpenCV  
            public byte colorModel2;  
            //  
            // 摘要:  
            //     ignored by OpenCV  
            public byte colorModel3;  
            //  
            // 摘要:  
            //     ignored by OpenCV  
            public byte channelSeq0;  
            //  
            // 摘要:  
            //     ignored by OpenCV  
            public byte channelSeq1;  
            //  
            // 摘要:  
            //     ignored by OpenCV  
            public byte channelSeq2;  
            //  
            // 摘要:  
            //     ignored by OpenCV  
            public byte channelSeq3;  
            //  
            // 摘要:  
            //     0 - interleaved color channels, 1 - separate color channels.  cvCreateImage  
            //     can only create interleaved images  
            public int dataOrder;  
            //  
            // 摘要:  
            //     0 - top-left origin, 1 - bottom-left origin (Windows bitmaps style)  
            public int origin;  
             
            // 摘要:  
            //     Alignment of image rows (4 or 8).  OpenCV ignores it and uses widthStep instead  
            public int align;  
            //  
            // 摘要:  
            //     image width in pixels  
            public int width;  
            //  
            // 摘要:  
            //     image height in pixels  
            public int height;  
            //  
            // 摘要:  
            //     image ROI. when it is not NULL, this specifies image region to process  
            public IntPtr roi;  
            //  
            // 摘要:  
            //     must be NULL in OpenCV  
            public IntPtr maskROI;  
            //  
            // 摘要:  
            //     ditto  
            public IntPtr imageId;  
            //  
            // 摘要:  
            //     ditto  
            public IntPtr tileInfo;  
            //  
            // 摘要:  
            //     image data size in bytes (=image->height*image->widthStep in case of interleaved  
            //     data)  
            public int imageSize;  
            //  
            // 摘要:  
            //     pointer to aligned image data  
            public IntPtr imageData;  
            //  
            // 摘要:  
            //     size of aligned image row in bytes  
            public int widthStep;  
            //  
            // 摘要:  
            //     border completion mode, ignored by OpenCV  
            public int BorderMode0;  
            //  
            // 摘要:  
            //     border completion mode, ignored by OpenCV  
            public int BorderMode1;  
            //  
            // 摘要:  
            //     border completion mode, ignored by OpenCV  
            public int BorderMode2;  
            //  
            // 摘要:  
            //     border completion mode, ignored by OpenCV  
            public int BorderMode3;  
            //  
            // 摘要:  
            //     border const, ignored by OpenCV  
            public int BorderConst0;  
            //  
            // 摘要:  
            //     border const, ignored by OpenCV  
            public int BorderConst1;  
            //  
            // 摘要:  
            //     border const, ignored by OpenCV  
            public int BorderConst2;  
            //  
            // 摘要:  
            //     border const, ignored by OpenCV  
            public int BorderConst3;        
            //  
            // 摘要:  
            //     pointer to a very origin of image data (not necessarily aligned) - it is  
            //     needed for correct image deallocation  
            public IntPtr imageDataOrigin;        
        }  

还需要下面这个结构体

public enum IPL_DEPTH  
   {  
       // 摘要:  
       //     1bit unsigned  
       IPL_DEPTH_1U = 1,  
       //  
       // 摘要:  
       //     8bit unsigned (Byte)  
       IPL_DEPTH_8U = 8,  
       //  
       // 摘要:  
       //     16bit unsigned  
       IPL_DEPTH_16U = 16,  
       //  
       // 摘要:  
       //     32bit float (Single)  
       IPL_DEPTH_32F = 32,  
       //  
       // 摘要:  
       //     double  
       IPL_DEPTH_64F = 64,  
       //  
       // 摘要:  
       //     indicates if the value is signed  
       IPL_DEPTH_SIGN = 0x80000000,  
       //  
       // 摘要:  
       //     8bit signed  
       IPL_DEPTH_8S = IPL_DEPTH_SIGN| 8,  
       //  
       // 摘要:  
       //     16bit signed  
       IPL_DEPTH_16S = IPL_DEPTH_SIGN|16,  
       //  
       // 摘要:  
       //     32bit signed  
       IPL_DEPTH_32S = IPL_DEPTH_SIGN|32,  
   }  


你可能感兴趣的:(IplImage,IplImage,IplImage,将C++,C#读取,图像用C#读,图像用C#读取)