forked from shimat/opencvsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitmapConverter_Mat.cs
422 lines (395 loc) · 16.4 KB
/
BitmapConverter_Mat.cs
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using OpenCvSharp.CPlusPlus;
using OpenCvSharp.Utilities;
namespace OpenCvSharp.Extensions
{
static partial class BitmapConverter
{
#region ToMat
#if LANG_JP
/// <summary>
/// System.Drawing.BitmapからOpenCVのMatへ変換して返す.
/// </summary>
/// <param name="src">変換するSystem.Drawing.Bitmap</param>
/// <returns>変換結果のMat</returns>
#else
/// <summary>
/// Converts System.Drawing.Bitmap to Mat
/// </summary>
/// <param name="src">System.Drawing.Bitmap object to be converted</param>
/// <returns>A Mat object which is converted from System.Drawing.Bitmap</returns>
#endif
public static Mat ToMat(this Bitmap src)
{
if (src == null)
throw new ArgumentNullException("src");
int w = src.Width;
int h = src.Height;
int channels;
switch (src.PixelFormat)
{
case PixelFormat.Format24bppRgb:
case PixelFormat.Format32bppRgb:
channels = 3; break;
case PixelFormat.Format32bppArgb:
case PixelFormat.Format32bppPArgb:
channels = 4; break;
case PixelFormat.Format8bppIndexed:
case PixelFormat.Format1bppIndexed:
channels = 1; break;
default:
throw new NotImplementedException();
}
Mat dst = new Mat(h, w, MatType.CV_8UC(channels));
ToMat(src, dst);
return dst;
}
#if LANG_JP
/// <summary>
/// System.Drawing.BitmapからOpenCVのMatへ変換して返す.
/// </summary>
/// <param name="src">変換するSystem.Drawing.Bitmap</param>
/// <param name="dst">変換結果を格納するMat</param>
#else
/// <summary>
/// Converts System.Drawing.Bitmap to Mat
/// </summary>
/// <param name="src">System.Drawing.Bitmap object to be converted</param>
/// <param name="dst">A Mat object which is converted from System.Drawing.Bitmap</param>
#endif
public static unsafe void ToMat(this Bitmap src, Mat dst)
{
if (src == null)
throw new ArgumentNullException("src");
if (dst == null)
throw new ArgumentNullException("dst");
if (dst.IsDisposed)
throw new ArgumentException("The specified dst is disposed.", "dst");
if (dst.Depth() != MatType.CV_8U)
throw new NotSupportedException("Mat depth != CV_8U");
if (dst.Dims() != 2)
throw new NotSupportedException("Mat dims != 2");
if (src.Width != dst.Width || src.Height != dst.Height)
throw new ArgumentException("src.Size != dst.Size");
int w = src.Width;
int h = src.Height;
Rectangle rect = new Rectangle(0, 0, w, h);
BitmapData bd = null;
try
{
bd = src.LockBits(rect, ImageLockMode.ReadOnly, src.PixelFormat);
byte* p = (byte*)bd.Scan0.ToPointer();
int sstep = bd.Stride;
int offset = sstep - (w / 8);
uint dstep = (uint)dst.Step();
IntPtr dstData = dst.Data;
byte* dstPtr = (byte*)dstData.ToPointer();
bool submat = dst.IsSubmatrix();
bool continuous = dst.IsContinuous();
switch (src.PixelFormat)
{
case PixelFormat.Format1bppIndexed:
{
if (dst.Channels() != 1)
throw new ArgumentException("Invalid nChannels");
if (submat)
throw new NotImplementedException("submatrix not supported");
int x = 0;
int y;
int bytePos;
byte b;
int i;
for (y = 0; y < h; y++)
{
// 横は必ず4byte幅に切り上げられる。
// この行の各バイトを調べていく
for (bytePos = 0; bytePos < sstep; bytePos++)
{
if (x < w)
{
// 現在の位置のバイトからそれぞれのビット8つを取り出す
b = p[bytePos];
for (i = 0; i < 8; i++)
{
if (x >= w)
{
break;
}
// IplImageは8bit/pixel
dstPtr[dstep * y + x] = ((b & 0x80) == 0x80) ? (byte)255 : (byte)0;
b <<= 1;
x++;
}
}
}
// 次の行へ
x = 0;
p += sstep;
}
}
break;
case PixelFormat.Format8bppIndexed:
case PixelFormat.Format24bppRgb:
{
if (src.PixelFormat == PixelFormat.Format8bppIndexed)
if (dst.Channels() != 1)
throw new ArgumentException("Invalid nChannels");
if (src.PixelFormat == PixelFormat.Format24bppRgb)
if (dst.Channels() != 3)
throw new ArgumentException("Invalid nChannels");
// ステップが同じで連続なら、一気にコピー
if (dstep == sstep && !submat && continuous)
{
uint length = (uint)(dst.DataEnd.ToInt64() - dstData.ToInt64());
Util.CopyMemory(dstData, bd.Scan0, length);
}
else
{
// 各行ごとにdstの行バイト幅コピー
byte* sp = (byte*)bd.Scan0;
byte* dp = (byte*)dst.Data;
for (int y = 0; y < h; y++)
{
Util.CopyMemory(dp, sp, dstep);
sp += sstep;
dp += dstep;
}
}
}
break;
case PixelFormat.Format32bppRgb:
case PixelFormat.Format32bppArgb:
case PixelFormat.Format32bppPArgb:
{
switch (dst.Channels())
{
case 4:
if (!submat && continuous)
{
uint length = (uint)(dst.DataEnd.ToInt64() - dstData.ToInt64());
Util.CopyMemory(dstData, bd.Scan0, length);
}
else
{
byte* sp = (byte*)bd.Scan0;
byte* dp = (byte*)dst.Data;
for (int y = 0; y < h; y++)
{
Util.CopyMemory(dp, sp, dstep);
sp += sstep;
dp += dstep;
}
}
break;
case 3:
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
dstPtr[y * dstep + x * 3 + 0] = p[y * sstep + x * 4 + 0];
dstPtr[y * dstep + x * 3 + 1] = p[y * sstep + x * 4 + 1];
dstPtr[y * dstep + x * 3 + 2] = p[y * sstep + x * 4 + 2];
}
}
break;
default:
throw new ArgumentException("Invalid nChannels");
}
}
break;
}
}
finally
{
if(bd != null)
src.UnlockBits(bd);
}
}
#endregion
#region ToBitmap
#if LANG_JP
/// <summary>
/// OpenCVのMatをSystem.Drawing.Bitmapに変換する
/// </summary>
/// <param name="src">変換するMat</param>
/// <returns>System.Drawing.Bitmap</returns>
#else
/// <summary>
/// Converts Mat to System.Drawing.Bitmap
/// </summary>
/// <param name="src">Mat</param>
/// <returns></returns>
#endif
public static Bitmap ToBitmap(this Mat src)
{
if (src == null)
{
throw new ArgumentNullException("src");
}
PixelFormat pf;
switch (src.Channels())
{
case 1:
pf = PixelFormat.Format8bppIndexed; break;
case 3:
pf = PixelFormat.Format24bppRgb; break;
case 4:
pf = PixelFormat.Format32bppArgb; break;
default:
throw new ArgumentException("Number of channels must be 1, 3 or 4.", "src");
}
return ToBitmap(src, pf);
}
#if LANG_JP
/// <summary>
/// OpenCVのMatをSystem.Drawing.Bitmapに変換する
/// </summary>
/// <param name="src">変換するMat</param>
/// <param name="pf">ピクセル深度</param>
/// <returns>System.Drawing.Bitmap</returns>
#else
/// <summary>
/// Converts Mat to System.Drawing.Bitmap
/// </summary>
/// <param name="src">Mat</param>
/// <param name="pf">Pixel Depth</param>
/// <returns></returns>
#endif
public static Bitmap ToBitmap(this Mat src, PixelFormat pf)
{
if (src == null)
throw new ArgumentNullException("src");
src.ThrowIfDisposed();
Bitmap bitmap = new Bitmap(src.Width, src.Height, pf);
ToBitmap(src, bitmap);
return bitmap;
}
#if LANG_JP
/// <summary>
/// OpenCVのMatを指定した出力先にSystem.Drawing.Bitmapとして変換する
/// </summary>
/// <param name="src">変換するMat</param>
/// <param name="dst">出力先のSystem.Drawing.Bitmap</param>
/// <remarks>Author: shimat, Gummo (ROI support)</remarks>
#else
/// <summary>
/// Converts Mat to System.Drawing.Bitmap
/// </summary>
/// <param name="src">Mat</param>
/// <param name="dst">Mat</param>
/// <remarks>Author: shimat, Gummo (ROI support)</remarks>
#endif
public static unsafe void ToBitmap(this Mat src, Bitmap dst)
{
if (src == null)
throw new ArgumentNullException("src");
if (dst == null)
throw new ArgumentNullException("dst");
if (src.IsDisposed)
throw new ArgumentException("The image is disposed.", "src");
if (src.Depth() != MatType.CV_8U)
throw new ArgumentException("Depth of the image must be CV_8U");
//if (src.IsSubmatrix())
// throw new ArgumentException("Submatrix is not supported");
if (src.Width != dst.Width || src.Height != dst.Height)
throw new ArgumentException("");
PixelFormat pf = dst.PixelFormat;
// 1プレーン用の場合、グレースケールのパレット情報を生成する
if (pf == PixelFormat.Format8bppIndexed)
{
ColorPalette plt = dst.Palette;
for (int x = 0; x < 256; x++)
{
plt.Entries[x] = Color.FromArgb(x, x, x);
}
dst.Palette = plt;
}
int w = src.Width;
int h = src.Height;
Rectangle rect = new Rectangle(0, 0, w, h);
BitmapData bd = null;
bool submat = src.IsSubmatrix();
bool continuous = src.IsContinuous();
try
{
bd = dst.LockBits(rect, ImageLockMode.WriteOnly, pf);
IntPtr srcData = src.Data;
byte* pSrc = (byte*)(srcData.ToPointer());
byte* pDst = (byte*)(bd.Scan0.ToPointer());
int ch = src.Channels();
int sstep = (int)src.Step();
int dstep = ((src.Width * ch) + 3) / 4 * 4; // 4の倍数に揃える
int stride = bd.Stride;
switch (pf)
{
case PixelFormat.Format1bppIndexed:
{
if (submat)
throw new NotImplementedException("submatrix not supported");
// BitmapDataは4byte幅だが、IplImageは1byte幅
// 手作業で移し替える
//int offset = stride - (w / 8);
int x = 0;
int y;
int bytePos;
byte mask;
byte b = 0;
int i;
for (y = 0; y < h; y++)
{
for (bytePos = 0; bytePos < stride; bytePos++)
{
if (x < w)
{
for (i = 0; i < 8; i++)
{
mask = (byte)(0x80 >> i);
if (x < w && pSrc[sstep * y + x] == 0)
b &= (byte)(mask ^ 0xff);
else
b |= mask;
x++;
}
pDst[bytePos] = b;
}
}
x = 0;
pDst += stride;
}
break;
}
case PixelFormat.Format8bppIndexed:
case PixelFormat.Format24bppRgb:
case PixelFormat.Format32bppArgb:
if (sstep == dstep && !submat && continuous)
{
uint imageSize = (uint)(src.DataEnd.ToInt64() - src.Data.ToInt64());
Util.CopyMemory(pDst, pSrc, imageSize);
}
else
{
for (int y = 0; y < h; y++)
{
long offsetSrc = (y * sstep);
long offsetDst = (y * dstep);
// 一列ごとにコピー
Util.CopyMemory(pDst + offsetDst, pSrc + offsetSrc, w * ch);
}
}
break;
default:
throw new NotImplementedException();
}
}
finally
{
dst.UnlockBits(bd);
}
}
#endregion
}
}