-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathProgram.cs
More file actions
61 lines (51 loc) · 2.4 KB
/
Copy pathProgram.cs
File metadata and controls
61 lines (51 loc) · 2.4 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
using SharpCV;
using System;
using System.IO;
using static SharpCV.Binding;
namespace AppConsole
{
class Program
{
static void Main(string[] args)
{
string dataDir = Path.GetFullPath("../../../../../data");
/*string file = Path.GetFullPath("/solar.jpg");
var temp = cv2.imread(file);
var img = cv2.imread(file);
var cropped1 = cv2.imcrop(img, (150, 50, 200, 350));
cv2.imwrite("cropped1.jpg", cropped1);
var cropped2 = img[(50, 400), (150, 350)];
cv2.imwrite("cropped2.jpg", cropped2);*/
// remove border
var image = cv2.imread(Path.Combine(dataDir, "invoice.jpg"));
var gray = cv2.cvtColor(image, ColorConversionCodes.COLOR_BGR2GRAY);
var (_, thresh) = cv2.threshold(gray, 0, 255, ThresholdTypes.THRESH_BINARY_INV | ThresholdTypes.THRESH_OTSU);
// Remove horizontal lines
var horizontal_kernel = cv2.getStructuringElement(MorphShapes.MORPH_RECT, (20, 1));
var remove_horizontal = cv2.morphologyEx(thresh, MorphTypes.MORPH_OPEN, horizontal_kernel, iterations: 2);
var cnts = cv2.findContoursAsPoints(remove_horizontal, RetrievalModes.RETR_EXTERNAL, ContourApproximationModes.CHAIN_APPROX_SIMPLE);
foreach (var c in cnts)
cv2.drawContours(image, new[] { c }, -1, (255, 255, 255), 5);
// remove vertical border
var vertical_kernel = cv2.getStructuringElement(MorphShapes.MORPH_RECT, (1, 20));
var remove_vertical = cv2.morphologyEx(thresh, MorphTypes.MORPH_OPEN, vertical_kernel, iterations: 2);
cnts = cv2.findContoursAsPoints(remove_vertical, RetrievalModes.RETR_EXTERNAL, ContourApproximationModes.CHAIN_APPROX_SIMPLE);
foreach (var c in cnts)
cv2.drawContours(image, new[] { c }, -1, (255, 255, 255), 5);
cv2.imwrite("result-sharpcv.png", image);
/*for (var i =0; i < 3; i++)
{
temp = cv2.pyrDown(temp);
cv2.imshow("a", temp);
cv2.waitKey(0);
}*/
// GC will dispose automatically
/*for (int i = 0; i < 10000; i++)
{
using var img = cv2.imread(file);
}*/
Console.WriteLine("Completed");
Console.ReadLine();
}
}
}