-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegment.cs
More file actions
174 lines (151 loc) · 5.78 KB
/
Segment.cs
File metadata and controls
174 lines (151 loc) · 5.78 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
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Drawing.Imaging;
using System.Windows.Forms;
using AForge.Imaging;
using AForge.Imaging.Filters;
using System.Runtime.InteropServices;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
namespace VKR
{
internal class Segment
{
public static Bitmap SegmentWords(Bitmap originalBitmap)
{
byte[,] arr = GetBitMapMatrix(originalBitmap); //source array from originalBitmap
int w = arr.GetLength(0); //width of 2D array
int h = arr.GetLength(1);
List<int> l = new List<int>();
Image<Gray, Byte> img = originalBitmap.ToImage<Gray, byte>();//Image<Bgr, Byte> imageCV = bmp.ToImage<Bgr, byte>();
for (int i = 0; i < w; i++)
{
int count = 0;
for (int j = 0; j < h; j++)
if (arr[i, j] < 128)
count++;
l.Add(count);
}
List<Pair<int, int>> ld2 = new List<Pair<int, int>>();//
int c = 0, limit = 0;
//поиск границ строк
for (int i = 0; i < l.Count(); i = c)
{
Pair<int, int> p = new Pair<int, int>();
if (l[i] > limit)//->начлась новая строка
{
p.First = i;
c = i + 1;
while (l[c] > limit)
{
c++;
if (c >= l.Count())
break;
}
p.Second = c - 1;
ld2.Add(p);
}
else
c = i + 1;
}
//строки
List<Rectangle> lr = new List<Rectangle>();
for (int i = 0; i < ld2.Count(); i++)
{
Rectangle r = new Rectangle(ld2[i].First, 0, ld2[i].Second - ld2[i].First + 1, h);
lr.Add(r);
}
for (int i = 0; i < lr.Count(); i++)
{
Rectangle r = lr[i];
img.ROI = lr[i];//для вывода отдельных линий
CvInvoke.Imshow("roi" + i.ToString(), img);//CvInvoke.Imshow("roi", img);// CvInvoke.Imshow("roi"+i.ToString(), img);
CvInvoke.Rectangle(img, r, new MCvScalar(90, 0, 255), thickness: 0, lineType: LineType.EightConnected);
}
Bitmap res = img.ToBitmap();
return res;
}
public static Bitmap SegmentString(Bitmap originalBitmap)
{
byte[,] arr = GetBitMapMatrix(originalBitmap); //source array from originalBitmap
int w = arr.GetLength(0); //width of 2D array
int h = arr.GetLength(1);
List<int> l = new List<int>();
Image<Gray, Byte> img = originalBitmap.ToImage<Gray, byte>();//Image<Bgr, Byte> imageCV = bmp.ToImage<Bgr, byte>();
for (int i = 0; i < h; i++)
{
int count = 0;
for (int j = 0; j < w; j++)
if (arr[j, i] == 0)
count++;
l.Add(count);
}
List<Pair<int, int>> ld2 = new List<Pair<int, int>>();//
int cnt = 0, threshold = 15;
//поиск границ строк
for (int i = 0; i < l.Count(); i = cnt)
{
Pair<int, int> p = new Pair<int, int>();
if (l[i] > threshold)//->начлась новая строка
{
p.First = i;
cnt = i + 1;
while (l[cnt] > threshold)
{
cnt++;
if (cnt >= l.Count())
break;
}
p.Second = cnt - 1;
ld2.Add(p);
}
else
cnt = i + 1;
}
//строки
List<Rectangle> lr = new List<Rectangle>();
for (int i = 0; i < ld2.Count(); i++)
{
Rectangle r = new Rectangle(0, ld2[i].First, w, ld2[i].Second - ld2[i].First + (ld2[i].Second - ld2[i].First) / 2);
lr.Add(r);
}
for (int i = 0; i < lr.Count(); i++)
{
Rectangle r = lr[i];
img.ROI = lr[i];//для вывода отдельных линий
CvInvoke.Imshow("roi" + i.ToString(), img);//CvInvoke.Imshow("roi", img);// CvInvoke.Imshow("roi"+i.ToString(), img);
CvInvoke.Rectangle(img, r, new MCvScalar(90, 0, 255), thickness: 0, lineType: LineType.EightConnected);
}
/*using (Graphics gr = Graphics.FromImage(originalBitmap))
{
for (int y = 0; y < l.Count(); y++)
{
//scale each bin so that it is drawn 200 pixels wide from the left edge
gr.DrawLine(Pens.Black, new PointF(0, y), new PointF(l[y], y));
}
}*/
Bitmap res = img.ToBitmap();
return res;
}
private static byte[,] GetBitMapMatrix(Bitmap bm)
{
Bitmap b1 = new Bitmap(bm);
int hight = b1.Height;
int width = b1.Width;
byte[,] Matrix = new byte[width, hight];
for (int i = 0; i < width; i++)
{
for (int j = 0; j < hight; j++)
{
Matrix[i, j] = (byte)b1.GetPixel(i, j).ToArgb();
}
}
return Matrix;
}
}
}