forked from bytedeco/javacv
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RLSA.java
112 lines (101 loc) · 3.88 KB
/
RLSA.java
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
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.ByteBuffer;
import javax.imageio.ImageIO;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.opencv.opencv_core.*;
import org.bytedeco.opencv.opencv_imgproc.*;
import static org.bytedeco.opencv.global.opencv_core.*;
import static org.bytedeco.opencv.global.opencv_imgproc.*;
/**
* Based on "Implementation Run Length Smoothing Algorithm in C++":
* http://stackoverflow.com/questions/21554431/implementation-run-length-smoothing-algorithm-in-c
*
* @author Nicholas Woodward
*/
public class RLSA {
public static void main(String[] args) {
String imagePath = args[0].trim();
IplImage image = null;
try {
Java2DFrameConverter converter1 = new Java2DFrameConverter();
OpenCVFrameConverter.ToIplImage converter2 = new OpenCVFrameConverter.ToIplImage();
BufferedImage img = ImageIO.read(new File(imagePath));
image = converter2.convert(converter1.convert(img));
} catch (Exception ex) {
ex.printStackTrace();
}
if (image != null) {
IplImage rlsaImage = runLengthSmoothingAlgorithm(image);
// do something with the result image
image.release();
rlsaImage.release();
}
}
public static IplImage runLengthSmoothingAlgorithm(IplImage image) {
IplImage gry = image.clone();
cvThreshold(gry, gry, 128, 255, CV_THRESH_BINARY_INV);
CvMat tmpImg = gry.asCvMat();
ByteBuffer buffer = gry.getByteBuffer();
CvScalar temp = new CvScalar();
temp.val(255);
int hor_thres = 77; // adjust for your text size
int zero_count = 0;
int one_flag = 0;
for (int i = 0; i < tmpImg.rows(); i++) {
for (int j = 0; j < tmpImg.cols(); j++) {
int ind = i * gry.widthStep() + j * gry.nChannels() + 1;
double val = -1;
if (ind < buffer.capacity()) {
val = (buffer.get(ind) & 0xFF);
}
if (val == 255) {
if (one_flag == 255) {
if (zero_count <= hor_thres) {
tmpImg.put(i, j, 255);
for (int n = (j-zero_count); n < j; n++) {
tmpImg.put(i, n, 255);
}
} else {
one_flag = 0;
}
zero_count = 0;
}
one_flag = 255;
} else if (one_flag == 255) {
zero_count = zero_count + 1;
}
}
}
int ver_thres = 44; // adjustable
zero_count = 0;
one_flag = 0;
for (int i = 0; i < tmpImg.cols(); i++) {
for (int j = 0; j < tmpImg.rows(); j++) {
int ind = j * gry.widthStep() + i * gry.nChannels() + 1;
double val = -1;
if (ind < buffer.capacity()) {
val = (buffer.get(ind) & 0xFF);
}
if (val == 255) {
if (one_flag == 255) {
if (zero_count <= ver_thres) {
tmpImg.put(j, i, 255);
for (int n = ((j-zero_count) >= 0) ? (j-zero_count) : 0; n < j; n++) {
tmpImg.put(n, i, 255);
}
} else {
one_flag = 0;
}
zero_count = 0;
}
one_flag = 255;
} else if (one_flag == 255) {
zero_count = zero_count + 1;
}
}
}
return gry;
}
}