ImageProcess.cs
7.0 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CodeTest
{
public class ImageProcess
{
public static HObject Bitmap2HObjectBpp24(Bitmap bmp )
{
HObject ho_Image = null;
try
{
HOperatorSet.GenEmptyObj(out ho_Image);
ho_Image.Dispose();
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
HOperatorSet.GenImageInterleaved(out ho_Image, srcBmpData.Scan0, "bgrx", bmp.Width, bmp.Height, 0, "byte", 0, 0, 0, 0, -1, 0);
bmp.UnlockBits(srcBmpData);
}
catch (Exception ex)
{
ho_Image = null;
}
return ho_Image;
}
public static void Bitmap2HObjectBpp8(Bitmap bmp, out HObject image)
{
try
{
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
HOperatorSet.GenImage1(out image, "byte", bmp.Width, bmp.Height, srcBmpData.Scan0);
bmp.UnlockBits(srcBmpData);
}
catch (Exception ex)
{
image = null;
}
}
public static void GenertateGrayBitmap(HObject image, out Bitmap res)
{
HTuple hpoint, type, width, height;
const int Alpha = 255;
int[] ptr = new int[2];
HOperatorSet.GetImagePointer1(image, out hpoint, out type, out width, out height);
res = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
ColorPalette pal = res.Palette;
for (int i = 0; i <= 255; i++)
{
pal.Entries[i] = Color.FromArgb(Alpha, i, i, i);
}
res.Palette = pal;
Rectangle rect = new Rectangle(0, 0, width, height);
BitmapData bitmapData = res.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
int PixelSize = Bitmap.GetPixelFormatSize(bitmapData.PixelFormat) / 8;
ptr[0] = bitmapData.Scan0.ToInt32();
ptr[1] = hpoint.I;
if (width % 4 == 0)
CopyMemory(ptr[0], ptr[1], width * height * PixelSize);
else
{
for (int i = 0; i < height - 1; i++)
{
ptr[1] += width;
CopyMemory(ptr[0], ptr[1], width * PixelSize);
ptr[0] += bitmapData.Stride;
}
}
res.UnlockBits(bitmapData);
}
private static void CopyMemory(int v1, int v2, HTuple hTuple)
{
}
public static void GenertateRGBBitmap(HObject image, out Bitmap res)
{
HTuple hred, hgreen, hblue, type, width, height;
HOperatorSet.GetImagePointer3(image, out hred, out hgreen, out hblue, out type, out width, out height);
res = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
Rectangle rect = new Rectangle(0, 0, width, height);
BitmapData bitmapData = res.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
//unsafe
//{
// byte* bptr = (byte*)bitmapData.Scan0;
// byte* r = ((byte*)hred.I);
// byte* g = ((byte*)hgreen.I);
// byte* b = ((byte*)hblue.I);
// for (int i = 0; i < width * height; i++)
// {
// bptr[i * 4] = (b)[i];
// bptr[i * 4 + 1] = (g)[i];
// bptr[i * 4 + 2] = (r)[i];
// bptr[i * 4 + 3] = 255;
// }
//}
res.UnlockBits(bitmapData);
}
/// <summary>
/// 将Halcon中8位灰度图转换为Bitmap图像
/// </summary>
/// <param name="image">Halcon中8位灰度图</param>
/// <param name="res">.net中Bitmap图像</param>
public static void ConvertHalconGrayByteImageToBitmap(HObject image, out Bitmap res)
{
HTuple hpoint, type, width, height;
const int Alpha = 255;
int[] ptr = new int[2];
HOperatorSet.GetImagePointer1(image, out hpoint, out type, out width, out height);
res = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
ColorPalette pal = res.Palette;
for (int i = 0; i <= 255; i++)
{
pal.Entries[i] = Color.FromArgb(Alpha, i, i, i);
}
res.Palette = pal;
Rectangle rect = new Rectangle(0, 0, width, height);
BitmapData bitmapData = res.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
int PixelSize = Bitmap.GetPixelFormatSize(bitmapData.PixelFormat) / 8;
ptr[0] = bitmapData.Scan0.ToInt32();
ptr[1] = hpoint.I;
if (width % 4 == 0)
CopyMemory(ptr[0], ptr[1], width * height * PixelSize);
else
{
for (int i = 0; i < height - 1; i++)
{
ptr[1] += width;
CopyMemory(ptr[0], ptr[1], width * PixelSize);
ptr[0] += bitmapData.Stride;
}
}
res.UnlockBits(bitmapData);
}
///// <summary>
///// 将Halcon中RGB图像转换为Bitmap图像
///// </summary>
///// <param name="image">Halcon中RGB图像</param>
///// <param name="res">.net中Bitmap图像</param>
//public static void GenertateRGBBitmap(HObject image, out Bitmap res)
//{
// HTuple hred, hgreen, hblue, type, width, height;
// HOperatorSet.GetImagePointer3(image, out hred, out hgreen, out hblue, out type, out width, out height);
// res = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
// Rectangle rect = new Rectangle(0, 0, width, height);
// BitmapData bitmapData = res.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
// unsafe
// {
// byte* bptr = (byte*)bitmapData.Scan0;
// byte* r = ((byte*)hred.I);
// byte* g = ((byte*)hgreen.I);
// byte* b = ((byte*)hblue.I);
// for (int i = 0; i < width * height; i++)
// {
// bptr[i * 4] = (b)[i];
// bptr[i * 4 + 1] = (g)[i];
// bptr[i * 4 + 2] = (r)[i];
// bptr[i * 4 + 3] = 255;
// }
// }
// res.UnlockBits(bitmapData);
//}
}
}