forked from henricavalcante/openomr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openomr.php
212 lines (152 loc) · 5.43 KB
/
openomr.php
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
class OpenOMR
{
const EDGE = 4;
const DEBUG = 0;
const MATRIXROWS = 54;
const MATRIXCOLS = 38;
const ERROR_MARGIN = 0.5;
private $debugFolder;
private $img;
private $imgSizeW;
private $imgSizeH;
private $imgCelSizeW;
private $imgCelSizeH;
private $imgCelCompareSizeW;
private $imgCelCompareSizeH;
private function __construct($path) {
//initialize object
$this->img = new Imagick();
$this->readImage($path);
if (self::DEBUG)
{
$this->debugFolder = getdate()[0].'/';
mkdir($this->debugFolder);
}
$this->removeColors();
if (self::DEBUG) $this->img->writeImage($this->debugFolder.'removeColors.PNG');
$this->adjustImage();
if (self::DEBUG) $this->img->writeImage($this->debugFolder.'adjustImage.PNG');
$this->removeEdges();
if (self::DEBUG) $this->img->writeImage($this->debugFolder.'removeEdges.PNG');
}
public static function getInstance($path) {
if (!isset($path))
throw new Exception('Please inform the paths of the marks.');
return new self($path);
}
private function readImage($path) {
$this->img->readImage($path);
}
private function removeColors() {
$imginfo = $this->img->identifyImage();
if ($imginfo['type'] == 'TrueColor') {
// remove red from image
$this->img->separateImageChannel(Imagick::CHANNEL_RED);
} else {
// desaturate
$this->img->modulateImage(100, 0, 100);
}
}
private function adjustImage() {
$this->img->normalizeImage(Imagick::CHANNEL_ALL);
$this->img->enhanceImage();
$this->img->despeckleImage();
$this->img->blackthresholdImage('#808080');
$this->img->whitethresholdImage('#808080');
}
private function removeEdges() {
//remove edges and possible skew from image
$this->img->trimImage(85);
$this->img->deskewImage(15);
$this->img->trimImage(85);
$this->img->setImagePage(0, 0, 0, 0);
$this->calculateSizes();
}
private function calculateSizes() {
$imginfo = $this->img->identifyImage();
$this->imgSizeW = $imginfo['geometry']['width'];
$this->imgSizeH = $imginfo['geometry']['height'];
$this->imgCelSizeW = $this->imgSizeW/self::MATRIXCOLS;
$this->imgCelSizeH = $this->imgSizeH/self::MATRIXROWS;
$this->imgCelCompareSizeW = $this->imgCelSizeW - ( self::EDGE * 2 );
$this->imgCelCompareSizeH = $this->imgCelSizeH - ( self::EDGE * 2 );
if (self::DEBUG) print_r($this);
}
private function getRegionFromImage($row, $col) {
$sizeW = $this->imgCelCompareSizeW;
$sizeH = $this->imgCelCompareSizeH;
$pX = ($col * $this->imgCelSizeW)+self::EDGE;
$pY = ($row * $this->imgCelSizeW)+self::EDGE;
if (self::DEBUG) echo "\n" . $col . " - " . $row . " - " . $pX . " - " . $pY;
return $this->img->getImageRegion($sizeW, $sizeH, $pX, $pY);
}
public function getMatrixFromImage() {
// create reference block pattern
$imageToCompare = new Imagick();
$imageToCompare->newImage($this->imgCelCompareSizeW, $this->imgCelCompareSizeH, new ImagickPixel('black'));
$matrixResult = [];
for ($i = 0; $i < self::MATRIXROWS; $i++)
{
for ($j = 0; $j < self::MATRIXCOLS; $j++)
{
if (!isset($matrixResult[$i]))
$matrixResult[$i] = [];
$regionToCompare = $this->getRegionFromImage($i, $j);
$regionToCompare->setImagePage(0, 0, 0, 0);
if (self::DEBUG) $regionToCompare->writeImage($this->debugFolder.$i.'-'.$j.'.PNG');
$imageCompared = $regionToCompare->compareImages($imageToCompare, Imagick::METRIC_ROOTMEANSQUAREDERROR);
$matrixResult[$i][$j] = $imageCompared[1];
$regionToCompare->clear();
}
}
$imageToCompare->clear();
return $matrixResult;
}
public function getMarksFromPaths($paths) {
// create reference block pattern
$imageToCompare = new Imagick();
$imageToCompare->newImage($this->imgCelCompareSizeW, $this->imgCelCompareSizeH, new ImagickPixel('black'));
$result = [];
foreach ($paths as $path) {
if (!isset($result[$path['field']])) {
$result[$path['field']] = ['status' => 0,'value' => '', 'error_margin'=> 1];
}
else if ($result[$path['field']]['status'] == 3) {
//if marked with wrong dont search next path
continue;
}
else if ($result[$path['field']]['status'] == 2) {
//if a next char from field reset status
$result[$path['field']]['status'] = 0;
}
foreach ($path['marks'] as $mark) {
if ($result[$path['field']]['status'] == 0) {
$result[$path['field']]['status'] = 1;
}
$regionToCompare = $this->getRegionFromImage($mark[0], $mark[1]);
$regionToCompare->setImagePage(0, 0, 0, 0);
if (self::DEBUG) $regionToCompare->writeImage($this->debugFolder.$mark[0].'-'.$mark[1].'.PNG');
$differenceBetweenImages = $regionToCompare->compareImages($imageToCompare, Imagick::METRIC_ROOTMEANSQUAREDERROR);
if ($differenceBetweenImages[1] < $result[$path['field']]['error_margin'])
$result[$path['field']]['error_margin'] = $differenceBetweenImages[1];
if ($differenceBetweenImages[1] < self::ERROR_MARGIN)
{
//cade doesn't exists difference between black square and region marked concatenate the value
$result[$path['field']]['value'] .= $mark[2];
if ($result[$path['field']]['status'] == 2)
{
$result[$path['field']]['status'] = 3;
}
else
{
$result[$path['field']]['status'] = 2;
}
};
$regionToCompare->clear();
}
}
$imageToCompare->clear();
return $result;
}
}