-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveArtifact.cs
More file actions
144 lines (128 loc) · 6.59 KB
/
RemoveArtifact.cs
File metadata and controls
144 lines (128 loc) · 6.59 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
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 RemoveArtifact
{
public static Bitmap RemoveNoiseWithAforge(Bitmap src, int width, int height)
{
Invert InvertFilter;
InvertFilter = new AForge.Imaging.Filters.Invert();
InvertFilter.ApplyInPlace(src);
Image<Gray, Byte> imageCV = src.ToImage<Gray, byte>();//Image<Bgr, Byte> imageCV = bmp.ToImage<Bgr, byte>();
Mat srcM = imageCV.Mat;
BlobCounter bc = new BlobCounter();
bc.ProcessImage(src);
Blob[] blobs = bc.GetObjectsInformation();
foreach (var b in blobs)
{
if (b.Rectangle.Width < width || b.Rectangle.Height < height)
CvInvoke.Rectangle(srcM, b.Rectangle, new MCvScalar(0, 0, 0), thickness: -1, lineType: LineType.EightConnected);
}
Bitmap res = srcM.ToBitmap();
InvertFilter.ApplyInPlace(res);
return res;
}
public static Bitmap RemoveNoiseAforge(Bitmap bmp, int w, int h)
{
Invert InvertFilter;
InvertFilter = new AForge.Imaging.Filters.Invert();
InvertFilter.ApplyInPlace(bmp);
// create filter
BlobsFiltering filter = new BlobsFiltering();
// configure filter
filter.CoupledSizeFiltering = true;
filter.MinWidth = w;
filter.MinHeight = h;
// apply the filter
filter.ApplyInPlace(bmp);
InvertFilter.ApplyInPlace(bmp);
return bmp;
}
private static Bitmap RemoveLines(Bitmap bmp)
{
Image<Gray, Byte> imageCV = bmp.ToImage<Gray, byte>();//Image<Bgr, Byte> imageCV = bmp.ToImage<Bgr, byte>();
Mat src = imageCV.Mat;
Mat gray = new Mat();
if (imageCV.NumberOfChannels == 3)
CvInvoke.CvtColor(src, gray, Emgu.CV.CvEnum.ColorConversion.Rgb2Gray);
else
gray = src;
// Apply adaptiveThreshold at the bitwise_not of gray, notice the ~ symbol
Mat bw = new Mat();
CvInvoke.AdaptiveThreshold(~gray, bw, 255, Emgu.CV.CvEnum.AdaptiveThresholdType.MeanC, Emgu.CV.CvEnum.ThresholdType.Binary, 15, -2);
Mat fin = bw.Clone();
// Show binary image
CvInvoke.Imshow("binary", bw);
// Create the images that will use to extract the horizontal and vertical lines
Mat horizontal = bw.Clone();
Mat vertical = bw.Clone();
fin = ~bw;
// Specify size on horizontal axis
int horizontalsize = horizontal.Cols / 30;
// Create structure element for extracting horizontal lines through morphology operations
Mat horizontalKernel = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(horizontalsize, 1), new System.Drawing.Point(-1, -1));
// Apply morphology operations
CvInvoke.Erode(horizontal, horizontal, horizontalKernel, new System.Drawing.Point(-1, -1), 1, BorderType.Default, new MCvScalar(255, 255, 255));
//horizontal = eroseMat(horizontal.ToBitmap(), horizontalKernel);
CvInvoke.Imshow("erode", horizontal);
CvInvoke.Dilate(horizontal, horizontal, horizontalKernel, new System.Drawing.Point(-1, -1), 1, BorderType.Default, new MCvScalar(255, 255, 255));
//horizontal = dilateMat(horizontal.ToBitmap(), horizontalKernel);
CvInvoke.Imshow("horizontal_lines", horizontal);
//Need to find horizontal contours, so as to not damage letters
Mat hierarchy = new Mat();
Emgu.CV.Util.VectorOfVectorOfPoint contours = new Emgu.CV.Util.VectorOfVectorOfPoint();
CvInvoke.FindContours(horizontal, contours, hierarchy, RetrType.Tree, ChainApproxMethod.ChainApproxNone);
int contCount = contours.Size;
for (int i = 0; i < contCount; i++)
{
Rectangle r = CvInvoke.BoundingRectangle(contours[i]);
float percentage_height = (float)r.Height / (float)src.Rows;
float percentage_width = (float)r.Width / (float)src.Cols;
//These exclude contours that probably are not dividing lines
if (percentage_height > 0.10)
continue;
if (percentage_width < 0.15)//если ширина меньше 5%, то не удаляем
continue;
//fills in line with white rectange
CvInvoke.Rectangle(fin, r, new MCvScalar(255, 255, 255), thickness: -1, lineType: LineType.EightConnected);
}
int verticalsize = vertical.Rows / 30;
Mat verticalKernel = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(1, verticalsize), new System.Drawing.Point(-1, -1));
CvInvoke.Erode(vertical, vertical, verticalKernel, new System.Drawing.Point(-1, -1), 1, BorderType.Default, new MCvScalar(255, 255, 255));
//vertical = eroseMat(horizontal.ToBitmap(), horizontalKernel);
CvInvoke.Dilate(vertical, vertical, verticalKernel, new System.Drawing.Point(-1, -1), 1, BorderType.Default, new MCvScalar(255, 255, 255));
//vertical = dilateMat(horizontal.ToBitmap(), horizontalKernel);
CvInvoke.Imshow("verticalal", vertical);
CvInvoke.FindContours(vertical, contours, hierarchy, RetrType.Tree, ChainApproxMethod.ChainApproxNone);
contCount = contours.Size;
for (int i = 0; i < contCount; i++)
{
Rectangle r = CvInvoke.BoundingRectangle(contours[i]);
float percentage_height = (float)r.Height / (float)src.Rows;
float percentage_width = (float)r.Width / (float)src.Cols;
//These exclude contours that probably are not dividing lines
if (percentage_width > 0.10)
continue;
if (percentage_height < 0.15)// больше лучше
continue;
//fills in line with white rectange
CvInvoke.Rectangle(fin, r, new MCvScalar(255, 255, 255), thickness: -1, lineType: LineType.EightConnected);
}
Bitmap res = fin.ToBitmap();
return res;
}
}
}