C#+Helcon的HObject图像,OpenCV的Mat图像(代替IplImage类型。。。
全⽹图像转化源码C#版独⼀,为了弄清不同类型储存结构,我太难了!
⼀,认识HObject类型,HImage类型,Mat类型,Bitmap类型;(。。。没整理呢)
1. HObject类型
2. HImage类型
3. ,,,
data:Mat对象中的⼀个指针,指向内存中存放矩阵数据的⼀块内存 (uchar* data)
dims:Mat所代表的矩阵的维度,如 3 * 4 的矩阵为 2 维, 3 * 4 * 5 的为3维
channels:通道,矩阵中的每⼀个矩阵元素拥有的值的个数,⽐如说 3 * 4 矩阵中⼀共 12 个元素,如果每个元素有三个值,那么就说这个矩阵是 3 通道的,即 channels = 3。常见的是⼀张彩⾊图⽚有红、绿、蓝三个通道。
depth:深度,即每⼀个像素的位数(bits),在opencv的Mat.depth()中得到的是⼀个 0 – 6 的数字,分别
代表不同的位数:enum { CV_8U=0, CV_8S=1, CV_16U=2,
CV_16S=3, CV_32S=4, CV_32F=5, CV_64F=6 }; 可见 0和1都代表8位, 2和3都代表16位,4和5代表32位,6代表64位;
step:是⼀个数组,定义了矩阵的布局,具体见下⾯图⽚分析,另外注意 step1 (step / elemSize1),M.step[m-1] 总是等于 elemSize,M.step1(m-1)总是等于 channels;
elemSize : 矩阵中每⼀个元素的数据⼤⼩,如果Mat中的数据的数据类型是 CV_8U 那么 elemSize = 1,CV_8UC3 那么 elemSize = 3,CV_16UC2 那么 elemSize = 4;记住另外有个 elemSize1 表⽰的是矩阵中数据类型的⼤⼩,即 elemSize / channels 的⼤⼩
4. Bitmap类型(System.Drawing)
5.Image类型(System.Drawing)
6.ImageSource类型(System.Windows.Media)
⼆,不⼀定要转化(直接读⽂件的情况)很简单,有点乱,整理出来再传
三,转化⽅法(避免 “存” “读”⽂件)
0.Bitmap=>ImageSource
///// <summary>
///// ⽂本转BitmapImage图 -Wpf⼯程中
//public System.Windows.Media.ImageSource BitmapToBitmapImage(System.Drawing.Bitmap bitmap)
//{
// MemoryStream stream = new MemoryStream();
// bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
// ImageBrush imageBrush = new ImageBrush();
// ImageSourceConverter imageSourceConverter = new ImageSourceConverter();
// return (ImageSource)imageSourceConverter.ConvertFrom(stream);
//}
object to1.HObject<=>Bitmap(还未验证,会验证的)
(1)把Halcon的“HObject图像”转换到Bitmap图像
1 /// <summary>
2 /// 把halcon图像转换到bitmap图像
3 /// </summary>
4 /// <param name="image">HObject对象</param>
5 /// <param name="res">Bitmap对象</param>
6 private Bitmap HObjectToBitmap(HObject hImage)
7 {
8 HTuple hpoint, type, width, height;
9 Bitmap res;
10 const int Alpha = 255;
11 HOperatorSet.GetImagePointer1(hImage, out hpoint, out type, out width, out height);
12
13
14 res = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
15 ColorPalette pal = res.Palette;
16 for (int i = 0; i <= 255; i++)
17 {
18 string insds = Color.FromArgb(Alpha, i, i, i).ToString();
19 }
20 res.Palette = pal;
21 Rectangle rect = new Rectangle(0, 0, width, height);
22 BitmapData bitmapData = res.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
23 int PixelSize = Bitmap.GetPixelFormatSize(bitmapData.PixelFormat) / 8;
24
25 if (width % 4 == 0)//4的倍数的时候,直接复制
26 CopyMemory(bitmapData.Scan0, hpoint, width * height * PixelSize);
27 else//根据⾼循环
28 {
29 for (int i = 0; i < height - 1; i++)
30 {
31 CopyMemory(bitmapData.Scan0, hpoint, width * PixelSize);
32 hpoint.I += width;
33 bitmapData.Scan0 += bitmapData.Stride;
34 }
35 }
36 res.UnlockBits(bitmapData);
37 return res;
38 }
View Code
(2)把Bitmap图像转换到Halcon的“HObject图像”
1 /// <summary>
2 /// 把bitmap图像转换到halcon图像
3 /// </summary>
4 /// <param name="bmp"></param>
5 /// <returns></returns>
6 public HObject BitmapToHObject(Bitmap bmp)
7 {
8 try
9 {
10
11 Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
12
13 System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, PixelFormat.Fo rmat8bppIndexed);
14 IntPtr pointer = bmpData.Scan0;
15
16 byte[] dataBlue = new byte[bmp.Width * bmp.Height];
17 unsafe
18 {
19 fixed (byte* ptrdata = dataBlue)
20 {
21 for (int i = 0; i < bmp.Height; i++)
22 {
23 CopyMemory((IntPtr)(ptrdata + bmp.Width * i), pointer + bmpData.Stride * i, bmp.Width);
24 }
25 HObject ho;
26 HOperatorSet.GenImage1(out ho, "byte", bmp.Width, bmp.Height, (int)ptrdata);
27 HImage himg = new HImage("byte", bmp.Width, bmp.Height, (IntPtr)ptrdata);
28
29 //HOperatorSet.DispImage(ho, hWindowControl1.HalconWindow);
30
31 bmp.UnlockBits(bmpData);
32 return ho;
33 }
34 }
35 }
36 catch (Exception exc)
37 {
38 return null;
39 }
40
41 }
View Code
2.Mat<=>Bitmap(还未写,会写的)
(1)把OpenCV的“Mat图像”转换到Bitmap图像
(2)把Bitmap图像转换到OpenCV的“Mat图像”
3.HObject<=>Mat
(1)把Halcon的“HObject图像”转换到OpenCV的“Mat图像”
1 /// <summary>
2 /// 把Halcon图像转换到OpenCV图像
3 /// </summary>
4 /// <param name="hImage">Halcon图像_HObject</param>
5 /// <returns>OpenCV图像_Mat</returns>
6 public Mat HImageToMat(HObject hImage)
7 {
8 try
9 {
10 Mat mImage; // 返回值
11 HTuple htChannels; // 通道
12 HTuple cType = null; // 类型
13 HTuple width, height; // 宽,⾼
14 width = height = 0;
15
16 htChannels = null;
17 HOperatorSet.CountChannels(hImage, out htChannels); // 获取通道
18
19 // 通道存在值
20 if (htChannels.Length == 0)
21 {
22 return null;
23 }
24 if (htChannels[0].I == 1) // 单通道
25 {
26 HTuple ptr; // HTuple_单通道值指针
27 HOperatorSet.GetImagePointer1(hImage, out ptr, out cType, out width, out height); // 单通道取值⽅法(图⽚,输出“单通道值指针”,输出“类型”,输出“宽”,输出“⾼”) // (ptr=2157902018096 cType=byte width=830 height=822)
28 mImage = new Mat(new OpenCvSharp.Size(width, height), MatType.CV_8UC1, new
Scalar(0)); // 实例化mImage(⼤⼩,MatType.CV _8UC1,new Scalar(0))
29 int Width = width;
30
31 unsafe
32 {
33 //for (int i = 0; i < height; i++) // 循环赋值
34 //{
35 // IntPtr start = IntPtr.Add(mImage.Data, i * width); // Mat的单通道_Data地址+偏移(start=0x000001f66d4df300 Data=0x000001f66d4df300 i * 830)
36 // CopyMemory(start, new IntPtr((byte*)ptr.IP + width * i), width); // CopyMemory(要复制到的地址,复制源的地址,复制的长度)
37 //}
38 CopyMemory(mImage.Data, new IntPtr((byte*)ptr.IP), width*height);// CopyMemory(要复制到的地址,复制源的地址,复制的长度)
39 }
40 return mImage;
41 }
42 else if (htChannels[0].I == 3) // 三通道
43 {
44 HTuple ptrRed; // HTuple_R通道值指针
45 HTuple ptrGreen; // HTuple_G通道值指针
46 HTuple ptrBlue; // HTuple_B通道值指针
47
48 HOperatorSet.GetImagePointer3(hImage, out ptrRed, out ptrGreen, out ptrBlue, out cType, out width, out height); // 三通道取值⽅法(图⽚,输出“R通道值指针”,输出“G通道值指针”,输出“B通道值指针”,输出“类型”,输出“宽”,输出“⾼”)
49 Mat pImageRed = new Mat(new OpenCvSharp.Size(width, height), MatType.CV_8UC1); // Mat_R通道值指针(⼤⼩,MatType.CV_8UC1)
50 Mat pImageGreen = new Mat(new OpenCvSharp.Size(width, height), MatType.CV_8UC1); // Mat_G通道值指针(⼤⼩,MatType.CV_8UC1)
51 Mat pImageBlue = new Mat(new OpenCvSharp.Size(width, height), MatType.CV_8UC1); // Mat_B通道值指针(⼤⼩,MatType.CV_8UC1)
52 mImage = new Mat(new OpenCvSharp.Size(width, height), MatType.CV_8UC3, new Scalar(0, 0, 0)); // Mat_图⽚(⼤⼩,M atType.CV_8UC1,new Scalar(0, 0, 0))
53 unsafe
54 {
55 //for (int i = 0; i < height; i++)
56 //{
57 // //long step = mImage.Step();
58 // IntPtr startRed = IntPtr.Add(pImageRed.Data, i * width); // Mat的Red_Data地址+偏移(start=0x000001f66d4df300
Data=0x000001f66d4df300 i * 830)
59 // IntPtr startGreen = IntPtr.Add(pImageGreen.Data, i * width); // Mat的Green_Data地址+偏移(start=0x000001f66d4df300 Data=0x000001f66d4df300 i * 830)
60 // IntPtr startBlue = IntPtr.Add(pImageBlue.Data, i * width); // Mat的Blue_Data地址+偏移(start=0x000001f66d4df300 Data=0x000001f66d4df300 i * 830)
61 // CopyMemory(startRed, new IntPtr((byte*)ptrRed.IP + width * i), width); // CopyMemory(要复制到的地址,复制源的地址,复制的长度)_Red
62 // CopyMemory(startGreen, new IntPtr((byte*)ptrGreen.IP + width * i), width); // CopyMemory(要复制到的地址,复制源的地址,复制的长度)_Green
63 // CopyMemory(startBlue, new IntPtr((byte*)ptrBlue.IP + width * i), width); // CopyMemory(要复制到的地址,复制源的地址,复制的长度)_Blue
64 //}
65 CopyMemory(pImageRed.Data, new IntPtr((byte*)ptrRed.IP), width*height); // CopyMemory(要复制到的地址,复制源的地址,复制的长度)_Red
66 CopyMemory(pImageGreen.Data, new IntPtr((byte*)ptrGreen.IP), width * height); // CopyMemory(要复制到的地址,复制源的地址,复制的长度)_Green
67 CopyMemory(pImageBlue.Data, new IntPtr((byte*)ptrBlue.IP), width * height); // CopyMemory(要复制到的地址,复制源的地址,复制的长度)_Blue
68 }
69 Mat[] multi = new Mat[] { pImageBlue, pImageGreen, pImageRed }; // 存储rgb三通道
70 Cv2.Merge(multi, mImage); // rgb三通道合成⼀张图
71 pImageRed.Dispose(); // Mat_R通道值指针销毁
72 pImageGreen.Dispose(); // Mat_G通道值指针销毁
73 pImageBlue.Dispose(); // Mat_B通道值指针销毁
74 return mImage;
75 }
76 else
77 {
78 return null;
79 }
80 }
81 catch (Exception ex)
82 {
83 throw ex;
84 }
85 }
View Code
(2)把OpenCV的”Mat图像“转换到Halcon的“HObject图像”
1 /// <summary>
2 /// 把OpenCV图像转换到Halcon图像
3 /// </summary>
4 /// <param name="mImage">OpenCV图像_Mat</param>
5 /// <returns>Halcon图像_HObject</returns>
6 public HObject MatToHImage(Mat mImage)
7 {
8 try
9 {
10 HObject hImage;
11 int matChannels = 0; // 通道数
12 Type matType = null;
13 int width, height; // 宽,⾼
14 width = height = 0; // 宽,⾼初始化
15
16 // 获取通道数
17 matChannels = mImage.Channels();
18 if (matChannels == 0)
19 {
20 return null;
21 }
22 if (matChannels == 1) // 单通道
23 {
24 IntPtr ptr; // 灰度图通道
25 Mat[] mats = mImage.Split();
26
27 // 改⾃:Mat.GetImagePointer1(mImage, out ptr, out matType, out width, out height); // ptr=2157902018096 cType=byte width=8 30 height=822
28 ptr = mats[0].Data; // 取灰度图值
29 matType = mImage.GetType(); // byte
30 height = mImage.Rows; // ⾼
31 width = mImage.Cols; // 宽
32
33 // 改⾃:hImage = new HObject(new OpenCvSharp.Size(width, height), MatType.CV_8UC1, new Scalar(0));
34 byte[] dataGrayScaleImage = new byte[width * height]; //Mat dataGrayScaleImage = new Mat(new OpenCvSharp.Size(width, height) , MatType.CV_8UC1);
35
36 unsafe
37 {
38 fixed (byte* ptrdata = dataGrayScaleImage)
39 {
40 #region 按⾏复制
41 //for (int i = 0; i < height; i++)
42 //{
43 // CopyMemory((IntPtr)(ptrdata + width * i), new IntPtr((long)ptr + width * i), width);
44 //}
45 #endregion
46
47 CopyMemory((IntPtr)ptrdata, new IntPtr((long)ptr), width * height);
48 HOperatorSet.GenImage1(out hImage, "byte", width, height, (IntPtr)ptrdata);
49
50 }
51 }
52 return hImage;
53 }
54 else if (matChannels == 3) // 三通道
55 {
56 IntPtr ptrRed; // R通道图
57 IntPtr ptrGreen; // G通道图
58 IntPtr ptrBlue; // B通道图
59 Mat[] mats =mImage.Split();
60
61 ptrRed = mats[0].Data; // 取R通道值
62 ptrGreen = mats[1].Data; // 取G通道值
63 ptrBlue = mats[2].Data; // 取B通道值
64 matType = mImage.GetType(); // 类型
65 height = mImage.Rows; // ⾼
66 width = mImage.Cols; // 宽
67
68 // 改⾃:hImage = new HObject(new OpenCvSharp.Size(width, height), MatType.CV_8UC1, new Scalar(0));
69 byte[] dataRed = new byte[width * height]; //Mat dataGrayScaleImage = new Mat(new OpenCvSharp.Size(width, height), MatType.C V_8UC1);
70 byte[] dataGreen = new byte[width * height];
71 byte[] dataBlue = new byte[width * height];
72
73 unsafe
74 {
75 fixed (byte* ptrdataRed = dataRed , ptrdataGreen = dataGreen, ptrdataBlue = dataBlue)
76 {
77 #region 按⾏复制
78 //HImage himg = new HImage("byte", width, height, (IntPtr)ptrdataRed);
79 //for (int i = 0; i < height; i++)
80 //{
81 // CopyMemory((IntPtr)(ptrdataRed + width * i), new IntPtr((long)ptrRed + width * i), width);
82 // CopyMemory((IntPtr)(ptrdataGreen + width * i), new IntPtr((long)ptrGreen + width * i), width);
83 // CopyMemory((IntPtr)(ptrdataBlue + width * i), new IntPtr((long)ptrBlue + width * i), width);
84 //}
85 #endregion
86
87 CopyMemory((IntPtr)ptrdataRed , new IntPtr((long)ptrRed ), width* height); // 复制R通道
88 CopyMemory((IntPtr)ptrdataGreen , new IntPtr((long)ptrGreen ), width* height); // 复制G通道
89 CopyMemory((IntPtr)ptrdataBlue , new IntPtr((long)ptrBlue ), width* height); // 复制B通道
90 HOperatorSet.GenImage3(out hImage, "byte", width, height, (IntPtr)ptrdataRed, (IntPtr)ptrdataGreen, (IntPtr)ptrdataBlue); // 合成
91 }
92 }
93 return hImage;
94 }
95 else
96 {
97 return null;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论