forked from shimat/opencvsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatToBitmap.cs
More file actions
106 lines (91 loc) · 3.06 KB
/
Copy pathMatToBitmap.cs
File metadata and controls
106 lines (91 loc) · 3.06 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
using System;
using System.Diagnostics;
using System.Drawing;
using OpenCvSharp;
using OpenCvSharp.CPlusPlus;
using System.Windows.Forms;
using OpenCvSharp.Extensions;
using SampleBase;
namespace CppStyleSamplesCS
{
/// <summary>
///
/// </summary>
class MatToBitmap : ISample
{
public void Run()
{
ToMat();
ToMatGrayScale();
ToBitmap();
ToBitmapGrayScale();
}
public void ToBitmap()
{
Mat img = new Mat(FilePath.Image.Lenna511, LoadMode.Color); // width % 4 != 0
Bitmap bitmap = BitmapConverter.ToBitmap(img);
// Bitmap bitmap = img.ToBitmap();
using (var form = new Form())
using (var pb = new PictureBox())
{
pb.Image = bitmap;
var size = new System.Drawing.Size(bitmap.Width, bitmap.Height);
pb.ClientSize = size;
form.ClientSize = size;
form.Controls.Add(pb);
form.KeyPreview = true;
form.KeyDown += (sender, args) =>
{
if (args.KeyCode.HasFlag(Keys.Enter))
((Form)sender).Close();
};
form.Text = "Mat to Bitmap Test";
form.ShowDialog();
}
}
public void ToBitmapGrayScale()
{
Mat img = new Mat(FilePath.Image.Lenna511, LoadMode.GrayScale); // width % 4 != 0
Bitmap bitmap = BitmapConverter.ToBitmap(img);
// Bitmap bitmap = img.ToBitmap();
using (var form = new Form())
using (var pb = new PictureBox())
{
pb.Image = bitmap;
var size = new System.Drawing.Size(bitmap.Width, bitmap.Height);
pb.ClientSize = size;
form.ClientSize = size;
form.Controls.Add(pb);
form.KeyPreview = true;
form.KeyDown += (sender, args) =>
{
if (args.KeyCode.HasFlag(Keys.Enter))
((Form)sender).Close();
};
form.Text = "Grayscale Mat to Bitmap Test";
form.ShowDialog();
}
}
public void ToMat()
{
Bitmap bitmap = new Bitmap(FilePath.Image.Lenna511); // width % 4 != 0
Mat converted = BitmapConverter.ToMat(bitmap);
//Mat converted = Mat.FromBitmap(bitmap);
using (new Window("Bitmap to Mat test", converted))
{
Cv2.WaitKey();
}
}
public void ToMatGrayScale()
{
Mat img = new Mat(FilePath.Image.Lenna511, LoadMode.GrayScale);
Bitmap bitmap = img.ToBitmap();
Mat converted = BitmapConverter.ToMat(bitmap);
//Mat converted = Mat.FromBitmap(bitmap);
using (new Window("Grayscale Bitmap to Mat test", converted))
{
Cv2.WaitKey();
}
}
}
}