OpenCv,EmguCv及.net之间的图像类型转换

news/2024/7/8 15:35:22 标签: .net, byte, image, string, 2010

OpenCv,EmguCv及.net之间的互动(The Interaction of OpenCv, EmguCv AND .net)

作者:王先荣 原文链接:http://www.cnblogs.com/xrwang/archive/2010/01/26/TheInteractionOfOpenCv-EmguCvANDDotNet.html

前言

    在.net中使用OpenCv和EmguCv时,必须在三者支持的图像格式之间进行转换。.net中用Bitmap类来承载图像,OpenCv中用IplImage指针来承载图像,EmguCv中用Image<TColor,TDepth>来承载图像。本文主要讲述如何在IplImage、Image<TColor,TDepth>和Bitmap之间转换。

IplImage  <=>  MIplImage

    MIplImage是IplImage中的托管实现,它是.net与OpenCv之间沟通的桥梁。IplImage指针和MIplImage之间的转换主要用到了Marshal类中的PtrToStructure、StructureToPtr、AllocHGlobal和FreeHGlobal这几个静态方法。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/// <summary>
/// 将MIplImage结构转换到IplImage指针;
/// 注意:指针在使用完之后必须用Marshal.FreeHGlobal方法释放。
/// </summary>
/// <param name="mi">MIplImage对象</param>
/// <returns>返回IplImage指针</returns>
public static IntPtr MIplImageToIplImagePointer(MIplImage mi)
{
     IntPtr ptr = Marshal.AllocHGlobal(mi.nSize);
     Marshal.StructureToPtr(mi, ptr, false );
     return ptr;
}
 
/// <summary>
/// 将IplImage指针转换成MIplImage结构
/// </summary>
/// <param name="ptr">IplImage指针</param>
/// <returns>返回MIplImage结构</returns>
public static MIplImage IplImagePointerToMIplImage(IntPtr ptr)
{
     return (MIplImage)Marshal.PtrToStructure(ptr, typeof (MIplImage));
}

    需要注意的是,不能使用 MIplImage * pmi=(MIplImage *)ptr.ToPointer();  和  IntPtr ptr=&mi; 之类的写法。

IplImage  <=>  Image<TColor,TDepth>

    用了MIplImage的辅助,我们可以很容易实现IplImage指针和Image<TColor,TDepth>之间的转换。

Image<TColor,TDepth>  <=>  Bitmap

    EmguCv中已经实现了这二者之间的转换,分别是Image<TColor,TDepth>类的下列成员:

(1)public Bitmap Bitmap { get; set; }

该属性可以获取或者设置位图;对于Image<Gray, Byte>, Image<Bgr, Byte> 和 Image<Bgra, Byte>这三种情况效率很高,因为Image<TColor,TDepth>和Bitmap共享数据内存。

(2)public Bitmap ToBitmap(int width,int height)及public Bitmap ToBitmap()方法

(3)public Image(Bitmap bmp)
(4)public Image(int width,int height,int stride,IntPtr scan0)
这个构造函数几乎是万能的,只要您清楚图像的内存分布,以及想要的目的。
 
IplImage  <=>  Bitmap
    IplImage指针和Bitmap间的转换有两种方式,第一种是利用Image<TColor,TDepth>作媒介;第二种是自己写转换的方法,例如我写的下列代码:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/// < summary >
/// 将IplImage指针转换成位图对象;
/// 对于不支持的像素格式,可以先使用cvCvtColor函数转换成支持的图像指针
/// </ summary >
/// < param name = string" style="margin:0px!important; padding:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',Consolas,'Bitstream Vera Sans Mono',Courier,monospace!important; min-height:inherit!important; white-space:pre-wrap; color:blue!important">"ptr" >IplImage指针</ param >
/// < returns >返回位图对象</ returns >
public static Bitmap IplImagePointerToBitmap(IntPtr ptr)
{
     MIplImage mi = IplImagePointerToMIplImage(ptr);
     PixelFormat pixelFormat;    //像素格式
     string unsupportedDepth = "不支持的像素位深度IPL_DEPTH";
     string unsupportedChannels = "不支持的通道数(仅支持1,2,4通道)";
     switch(mi.nChannels)
     {
         case 1:
             switch (mi.depth)
             {
                 case IPL_DEPTH.IPL_DEPTH_8U:
                     pixelFormat = PixelFormat.Format8bppIndexed;
                     break;
                 case IPL_DEPTH.IPL_DEPTH_16U:
                     pixelFormat = PixelFormat.Format16bppGrayScale;
                     break;
                 default:
                     throw new NotImplementedException(unsupportedDepth);
             }
             break;
         case 3:
             switch (mi.depth)
             {
                 case IPL_DEPTH.IPL_DEPTH_8U:
                     pixelFormat = PixelFormat.Format24bppRgb;
                     break;
                 case IPL_DEPTH.IPL_DEPTH_16U:
                     pixelFormat = PixelFormat.Format48bppRgb;
                     break;
                 default:
                     throw new NotImplementedException(unsupportedDepth);
             }
             break;
         case 4:
             switch (mi.depth)
             {
                 case IPL_DEPTH.IPL_DEPTH_8U:
                     pixelFormat = PixelFormat.Format32bppArgb;
                     break;
                 case IPL_DEPTH.IPL_DEPTH_16U:
                     pixelFormat = PixelFormat.Format64bppArgb;
                     break;
                 default:
                     throw new NotImplementedException(unsupportedDepth);
             }
             break;
         default:
             throw new NotImplementedException(unsupportedChannels);
 
     }
     Bitmap bitmap = new Bitmap(mi.width, mi.height, mi.widthStep, pixelFormat, mi.imageData);
     //对于灰度图像,还要修改调色板
     if (pixelFormat == PixelFormat.Format8bppIndexed)
         SetColorPaletteOfGrayscaleBitmap(bitmap);
     return bitmap;
}
 
/// < summary >
/// 将位图转换成IplImage指针
/// </ summary >
/// < param name = string" style="margin:0px!important; padding:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',Consolas,'Bitstream Vera Sans Mono',Courier,monospace!important; min-height:inherit!important; white-space:pre-wrap; color:blue!important">"bitmap" >位图对象</ param >
/// < returns >返回IplImage指针</ returns >
public static IntPtr BitmapToIplImagePointer(Bitmap bitmap)
{
     IImage iimage = null;
     switch (bitmap.PixelFormat)
     {
         case PixelFormat.Format8bppIndexed:
             iimage = new Image< Gray , Byte>(bitmap);
             break;
         case PixelFormat.Format16bppGrayScale:
             iimage = new Image< Gray , UInt16>(bitmap);
             break;
         case PixelFormat.Format24bppRgb:
             iimage = new Image< Bgr , Byte>(bitmap);
             break;
         case PixelFormat.Format32bppArgb:
             iimage = new Image< Bgra , Byte>(bitmap);
             break;
         case PixelFormat.Format48bppRgb:
             iimage = new Image< Bgr , UInt16>(bitmap);
             break;
         case PixelFormat.Format64bppArgb:
             iimage = new Image< Bgra , UInt16>(bitmap);
             break;
         default:
             Image< Bgra , Byte> tmp1 = new Image< Bgra , Byte>(bitmap.Size);
             Byte[, ,] data = tmp1.Data;
             for (int i = 0; i < bitmap.Width ; i++)
             {
                 for (int j = string" style="margin:0px!important; padding:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',Consolas,'Bitstream Vera Sans Mono',Courier,monospace!important; min-height:inherit!important; white-space:pre-wrap; color:blue!important">0 ; j < bitmap.Height; j++)
                 {
                     Color color = string" style="margin:0px!important; padding:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',Consolas,'Bitstream Vera Sans Mono',Courier,monospace!important; min-height:inherit!important; white-space:pre-wrap; color:blue!important">bitmap .GetPixel(i, j);
                     data[j, i, 0] = color.B;
                     data[j, i, 1] = color.G;
                     data[j, i, 2] = color.R;
                     data[j, i, 3] = color.A;
                 }
             }
             iimage = string" style="margin:0px!important; padding:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',Consolas,'Bitstream Vera Sans Mono',Courier,monospace!important; min-height:inherit!important; white-space:pre-wrap; color:blue!important">tmp1 ;
             break;
     }
     return iimage.Ptr;
}
 
/// <summary>
/// 设置256级灰度位图的调色板
/// </ summary >
/// < param name = string" style="margin:0px!important; padding:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',Consolas,'Bitstream Vera Sans Mono',Courier,monospace!important; min-height:inherit!important; white-space:pre-wrap; color:blue!important">"bitmap" ></ param >
public static void SetColorPaletteOfGrayscaleBitmap(Bitmap bitmap)
{
     PixelFormat pixelFormat = bitmap.PixelFormat;
     if (pixelFormat == PixelFormat.Format8bppIndexed)
     {
         ColorPalette palette = bitmap.Palette;
         for (int i = 0; i < palette.Entries.Length; i++)
             palette.Entries[i] = Color.FromArgb(255, i, i, i);
         bitmap.Palette = palette;
     }
}

总算,写完了,打字真累啊。


http://www.niftyadmin.cn/n/790770.html

相关文章

慢Sql排查

https://blog.csdn.net/qq_21040559/article/details/107381444 使用第三方工具进行检测&#xff0c;比如阿里的druid数据库连接池&#xff0c;加入相关配置之后可以从面板更直观的监控sql的性能&#xff0c;更方便我们对问题的排查。 记录MySQL慢查询 MySQL慢查询就是在日志中…

DLL介绍及编写

1. 动态链接之含义 在链接应用程序时常使用所谓“静态链接”的方法&#xff0c;即将各个目标文件(.obj)、运行时函数库(.lib)以及已编译的资源文件(.res)链接到一起&#xff0c;形成一个可执行文件(.exe)。使用静态链接时&#xff0c;可执行文件需要使用的各种函数和资源都已包…

jvm内存模型以及各个分区

1. JVM 内存模型 Java虚拟机内存的各个区域包括&#xff1a; 程序计数器Java虚拟机栈本地方法栈Java堆方法区2. 程序计数器 什么是程序计数器呢&#xff1f; 程序计数器是一块较小的内存空间,它可以是当前程序所执行的字节码的行号指示器。 注意&#xff1a;如果线程正在执…

Redission分布式锁原理

https://blog.csdn.net/a772304419/article/details/105647110

LIB和DLL的区别与使用

LIB和DLL的区别与使用 共有两种库&#xff1a; 一种是LIB包含了函数所在的DLL文件和文件中函数位置的信息&#xff08;入口&#xff09;&#xff0c;代码由运行时加载在进程空间中的DLL提供&#xff0c;称为动态链接库dynamic link library。 一种是LIB包含函数代码本身&#x…

消息队列应用

二、消息队列应用场景 以下介绍消息队列在实际应用中常用的使用场景。异步处理&#xff0c;应用解耦&#xff0c;流量削锋和消息通讯四个场景 2.1异步处理 场景说明&#xff1a;用户注册后&#xff0c;需要发注册邮件和注册短信。传统的做法有两种 1.串行的方式&#xff1b;…

DLL初学者指南(非MFC)

DLL初学者指南(非MFC) 作者&#xff1a;Notsosuperhero 译者&#xff1a;小刀人 环境&#xff1a; Visual C 2003, Windows 源代码下载&#xff1a;DLL_Project.rar -DLL Project的源代码及测试项目 原文出处&#xff1a; codeguru 我正在学习DLLs&#xff0c;谈不上对其有…

数据库压力大解决思路

https://blog.csdn.net/chrsh44e/article/details/108884060