-
Notifications
You must be signed in to change notification settings - Fork 3
/
optlabapp.cpp
649 lines (547 loc) · 16.3 KB
/
optlabapp.cpp
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#include "optlabapp.h"
#include "./ui_optlabapp.h"
#include <QtCore/QDebug>
#include <QtGui/QImageReader>
#include <QtGui/QImageWriter>
#include <QtGui/QPixmap>
#include <QtGui/QGuiApplication>
#include <QtGui/QScreen>
#include <QtWidgets/QFileDialog>
#include <QtWidgets/QMessageBox>
#include <leptonica/allheaders.h>
#include <avir.h>
static PIX* QImage2PIX(QImage& src_image)
{
src_image = src_image.rgbSwapped();
const int width = src_image.width();
const int height = src_image.height();
const int depth = src_image.depth();
const int wpl = src_image.bytesPerLine() / 4;
PIX* dst_image = pixCreate(width, height, depth);
pixSetWpl(dst_image, wpl);
pixSetColormap(dst_image, nullptr);
l_uint32* raw_data = dst_image->data;
for (int y = 0; y < height; y++)
{
l_uint32* lines = raw_data + y * wpl;
QByteArray arr(reinterpret_cast<const char*>(src_image.scanLine(y)), src_image.bytesPerLine());
for (int j = 0; j < arr.size(); ++j)
{
*(reinterpret_cast<l_uint8*>(lines) + j) = arr[j];
}
}
return pixEndianByteSwapNew(dst_image);
}
static QImage PIX2QImage(PIX* src_image)
{
const int width = pixGetWidth(src_image);
const int height = pixGetHeight(src_image);
const int depth = pixGetDepth(src_image);
const int bytesPerLine = pixGetWpl(src_image) * 4;
l_uint32* s_data = pixGetData(pixEndianByteSwapNew(src_image));
QImage::Format format;
if (depth == 1)
format = QImage::Format_Mono;
else if (depth == 0)
format = QImage::Format_Indexed8;
else
format = QImage::Format_RGB32;
QImage result(reinterpret_cast<uchar*>(s_data), width, height, bytesPerLine, format);
// Handle pallete
QVector<QRgb> _bwCT;
_bwCT.append(qRgb(255, 255, 255));
_bwCT.append(qRgb(0, 0, 0));
QVector<QRgb> _grayscaleCT(256);
for (int i = 0; i < 256; ++i)
{
_grayscaleCT.append(qRgb(i, i, i));
}
if (depth == 1)
{
result.setColorTable(_bwCT);
}
else if (depth == 8)
{
result.setColorTable(_grayscaleCT);
}
else
{
result.setColorTable(_grayscaleCT);
}
if (result.isNull())
{
QImage none(0, 0, QImage::Format_Invalid);
return none;
}
return result.rgbSwapped();
}
binarization_operation::binarization_operation(QImage image, QWidget* parent)
: QDialog(parent),
image_(image),
ui_(new Ui::binarization_operation_dialog)
{
ui_->setupUi(this);
QObject::connect(
ui_->local_binarization_push_button,
SIGNAL(clicked()),
this,
SLOT(on_local_binarization_push_button_clicked())
);
}
binarization_operation::~binarization_operation()
{
delete ui_;
}
void binarization_operation::on_local_binarization_push_button_clicked()
{
if (ui_->sauvola_radio_button->isChecked())
{
qDebug() << "Binarisation Image";
if (image_.isNull())
{
return;
}
QImage img = image_.convertToFormat(QImage::Format_Grayscale8);
auto src_image = QImage2PIX(img);
l_int32 w, h;
PIX* dst_image = nullptr;
pixGetDimensions(src_image, &w, &h, nullptr);
pixSauvolaBinarize(src_image, 7, 0.34f, 1, nullptr, nullptr, nullptr, &dst_image);
image_ = PIX2QImage(dst_image);
pixDestroy(&src_image);
pixDestroy(&dst_image);
emit(export_image(image_));
}
}
OptLabApp::OptLabApp(QWidget* parent)
: QMainWindow(parent),
ui_(new Ui::OptLabApp),
current_image_file_name_(""),
scale_factor_(1.0)
{
ui_->setupUi(this);
}
OptLabApp::~OptLabApp()
{
delete ui_;
}
void OptLabApp::setup_app_ui()
{
ui_->image_label->setScaledContents(true);
this->resize(QGuiApplication::primaryScreen()->availableSize() * 3 / 5);
}
bool OptLabApp::load_file(const QString& file_name)
{
QImageReader reader(file_name);
reader.setAutoTransform(true);
const QImage new_image = reader.read();
if (new_image.isNull())
{
QMessageBox::information(this,
QGuiApplication::applicationDisplayName(),
tr(u8"无法载入图片 %1: %2")
.arg(QDir::toNativeSeparators(file_name), reader.errorString()));
return false;
}
set_image(new_image);
this->setWindowFilePath(file_name);
const QString message = tr(u8"打开文件 \"%1\"\t分辨率 %2x%3\t位深度 %4")
.arg(QDir::toNativeSeparators(file_name))
.arg(image_.width()).arg(image_.height()).arg(image_.depth());
ui_->status_bar->showMessage(message);
return true;
}
void OptLabApp::set_image(const QImage& new_image)
{
image_ = new_image;
ui_->image_label->setPixmap(QPixmap::fromImage(image_));
scale_factor_ = 1.0;
}
bool OptLabApp::save_file(const QString& file_name)
{
QImageWriter writer(file_name);
if (!writer.write(image_))
{
QMessageBox::information(this, QGuiApplication::applicationDisplayName(),
tr(u8"无法保存 %1: %2")
.arg(QDir::toNativeSeparators(file_name)), writer.errorString());
return false;
}
const QString message = tr(u8"已保存 \"%1\"").arg(QDir::toNativeSeparators(file_name));
ui_->status_bar->showMessage(message);
return true;
}
void OptLabApp::on_action_open_triggered()
{
current_image_file_name_ = QFileDialog::getOpenFileName(this,
tr(u8"打开文件"),
"",
tr(u8"图像文件 (*.png;*.jpg;*.jpeg)"));
qDebug() << u8"正在打开文件" << current_image_file_name_;
load_file(current_image_file_name_);
}
void OptLabApp::on_action_save_triggered()
{
const QString image_file_name = current_image_file_name_;
qDebug() << u8"正在保存文件" << image_file_name;
if (!image_file_name.isNull() || !image_file_name.isEmpty())
save_file(image_file_name);
}
void OptLabApp::on_action_save_as_triggered()
{
const QString image_file_name = QFileDialog::getSaveFileName(this,
tr(u8"保存文件"),
"",
tr(u8"图像文件 (*.png;*.jpg;*.jpeg)"));
qDebug() << u8"正在保存文件" << image_file_name;
if (!image_file_name.isNull() || !image_file_name.isEmpty())
save_file(image_file_name);
}
void OptLabApp::on_inverting_push_button_clicked()
{
qDebug() << "Inverting Image";
if (image_.isNull())
{
return;
}
auto src_image = QImage2PIX(image_);
const auto w = pixGetWidth(src_image);
const auto h = pixGetHeight(src_image);
const auto d = pixGetDepth(src_image);
PIX* dst_image = pixCreate(w, h, d);
pixInvert(dst_image, src_image);
image_ = PIX2QImage(dst_image);
ui_->image_label->setPixmap(QPixmap::fromImage(image_));
pixDestroy(&src_image);
pixDestroy(&dst_image);
}
void OptLabApp::on_rotate_it_push_button_clicked()
{
const float deg2rad = 3.1415926535f / 180.f;
float angle = ui_->set_angle_line_edit->text().toFloat();
qDebug() << "Rotate Image";
if (image_.isNull())
{
return;
}
auto src_image = QImage2PIX(image_);
auto format = pixGetInputFormat(src_image);
if (format == IFF_UNKNOWN) format = IFF_PNG;
if (angle == 90.0f || angle == 180.0f || angle == 270.0f)
{
const auto quad = static_cast<l_int32>((angle + 0.5f) / 90.0f);
auto dst_image = pixRotateOrth(src_image, quad);
image_ = PIX2QImage(dst_image);
ui_->image_label->setPixmap(QPixmap::fromImage(image_));
pixDestroy(&src_image);
pixDestroy(&dst_image);
return;
}
const float angle_rad = deg2rad * angle;
const auto i_color = L_BRING_IN_WHITE;
const auto i_type = L_ROTATE_AREA_MAP;
auto dst_image = pixRotate(src_image, angle_rad, i_type, i_color, 0, 0);
image_ = PIX2QImage(dst_image);
ui_->image_label->setPixmap(QPixmap::fromImage(image_));
pixDestroy(&src_image);
pixDestroy(&dst_image);
}
void OptLabApp::on_rescaling_push_button_clicked()
{
qDebug() << "Rescaling Image";
if (image_.isNull())
{
return;
}
float x_factor = ui_->set_width_factor_line_edit->text().toFloat();
float y_factor = ui_->set_height_factor_line_edit->text().toFloat();
const int width = image_.width();
const int height = image_.height();
const int scan_line_size = image_.bytesPerLine();
const int depth = image_.depth();
const int new_width = x_factor * width;
const int new_height = y_factor * height;
QImage dst_image(new_width, new_height, image_.format());
dst_image.fill(0);
avir::CImageResizer<> image_resizer(depth, depth);
image_resizer.resizeImage(image_.bits(), width, height, scan_line_size,
dst_image.bits(), new_width, new_height,
depth / 8, 0);
image_ = dst_image;
ui_->image_label->setPixmap(QPixmap::fromImage(image_));
}
void OptLabApp::on_binarisation_push_button_clicked()
{
binarization_operation binarization_dialog(image_, this);
QObject::connect(&binarization_dialog, SIGNAL(export_image(QImage)), this, SLOT(receive_image(QImage)));
binarization_dialog.exec();
}
void OptLabApp::on_deskew_push_button_clicked()
{
static const l_float32 DEFAULT_SWEEP_RANGE = 90.;
qDebug() << "Deskew Image";
if (image_.isNull())
{
return;
}
l_float32 angle, conf;
auto src_image = QImage2PIX(image_);
auto dst_image = pixDeskewGeneral(src_image, 0, DEFAULT_SWEEP_RANGE, 1.0, 0, 0, &angle, &conf);
image_ = PIX2QImage(dst_image);
ui_->image_label->setPixmap(QPixmap::fromImage(image_));
pixDestroy(&src_image);
pixDestroy(&dst_image);
}
void OptLabApp::on_despeckle_push_button_clicked()
{
qDebug() << "Despeckle Image";
if (image_.isNull())
{
return;
}
/* HMT (with just misses) for speckle up to 2x2 */
static auto selstr2 = "oooo"
"oC o"
"o o"
"oooo";
/* Normalize for rapidly varying background */
auto pixa_1 = pixaCreate(0);
auto img = image_.convertToFormat(QImage::Format_Grayscale8);
auto src_image = QImage2PIX(img);
pixaAddPix(pixa_1, src_image, L_INSERT);
auto pix_1 = pixBackgroundNormFlex(src_image, 7, 7, 1, 1, 10);
pixaAddPix(pixa_1, pix_1, L_INSERT);
/* Remove the background */
auto pix_2 = pixGammaTRCMasked(nullptr, pix_1, nullptr, 1.0, 100, 175);
/* Binarize */
auto pix_3 = pixThresholdToBinary(pix_2, 180);
pixaAddPix(pixa_1, pix_3, L_INSERT);
/* Remove the speckle noise up to 2x2 */
auto sel_1 = selCreateFromString(selstr2, 4, 4, "speckle2");
auto pix_4 = pixHMT(nullptr, pix_3, sel_1);
pixaAddPix(pixa_1, pix_4, L_INSERT);
auto sel_2 = selCreateBrick(2, 2, 0, 0, SEL_HIT);
auto pix_5 = pixDilate(nullptr, pix_4, sel_2);
pixaAddPix(pixa_1, pix_5, L_INSERT);
auto dst_image = pixSubtract(nullptr, pix_3, pix_5);
pixaAddPix(pixa_1, dst_image, L_INSERT);
image_ = PIX2QImage(dst_image);
ui_->image_label->setPixmap(QPixmap::fromImage(image_));
selDestroy(&sel_1);
selDestroy(&sel_2);
pixDestroy(&pix_2);
pixaDestroy(&pixa_1);
}
void OptLabApp::on_dilation_push_button_clicked()
{
qDebug() << "Dilation";
if (image_.isNull())
{
return;
}
auto img = image_.convertToFormat(QImage::Format_Mono);
auto src_image = QImage2PIX(img);
auto sela = selaAddBasic(nullptr);
auto nsels = selaGetCount(sela);
l_int32 idx = 1;
auto sel = selaGetSel(sela, idx);
auto selname = selGetName(sel);
auto dst_image = pixDilate(nullptr, src_image, sel);
image_ = PIX2QImage(dst_image);
ui_->image_label->setPixmap(QPixmap::fromImage(image_));
pixDestroy(&src_image);
pixDestroy(&dst_image);
}
void OptLabApp::on_erosion_push_button_clicked()
{
qDebug() << "Erosion";
if (image_.isNull())
{
return;
}
auto img = image_.convertToFormat(QImage::Format_Mono);
auto src_image = QImage2PIX(img);
auto sela = selaAddBasic(nullptr);
auto nsels = selaGetCount(sela);
l_int32 idx = 0;
auto sel = selaGetSel(sela, idx);
auto selname = selGetName(sel);
auto dst_image = pixErode(nullptr, src_image, sel);
image_ = PIX2QImage(dst_image);
ui_->image_label->setPixmap(QPixmap::fromImage(image_));
pixDestroy(&src_image);
pixDestroy(&dst_image);
}
/*
* Use these variable abbreviations:
*
* pap1: distance from left edge to the page
* txt1: distance from left edge to the text
* Identify pap1 by (a) 1st downward transition in intensity (nait).
* (b) start of 1st lowpass interval (nail)
* Identify txt1 by (a) end of 1st lowpass interval (nail)
* (b) first upward transition in reversals (nart)
*
* pap2: distance from right edge to beginning of last upward transition,
* plus some extra for safety.
* txt1: distance from right edge to the text
* Identify pap2 by 1st downward transition in intensity.
* Identify txt2 by (a) beginning of 1st lowpass interval from bottom
* (b) last downward transition in reversals from bottom
*/
static l_int32
GetLeftCut(NUMA* narl,
NUMA* nart,
NUMA* nait,
l_int32 w,
l_int32* pleft)
{
l_int32 nrl, nrt, nit, start, end, sign, pap1, txt1, del;
nrl = numaGetCount(narl);
nrt = numaGetCount(nart);
nit = numaGetCount(nait);
/* Check for small max number of reversals or no edge */
numaGetSpanValues(narl, 0, nullptr, &end);
if (end < 20 || nrl <= 1)
{
*pleft = 0;
return 0;
}
/* Where is text and page, scanning from the left? */
pap1 = 0;
txt1 = 0;
if (nrt >= 4)
{
/* beginning of first upward transition */
numaGetEdgeValues(nart, 0, &start, nullptr, nullptr);
txt1 = start;
}
if (nit >= 4)
{
/* end of first downward trans in (inverse) intensity */
numaGetEdgeValues(nait, 0, nullptr, &end, &sign);
if (end < txt1 && sign == -1)
pap1 = end;
else
pap1 = 0.5 * txt1;
}
del = txt1 - pap1;
if (del > 20)
{
txt1 -= L_MIN(20, 0.5 * del);
pap1 += L_MIN(20, 0.5 * del);
}
lept_stderr("txt1 = %d, pap1 = %d\n", txt1, pap1);
*pleft = pap1;
return 0;
}
static l_int32
GetRightCut(NUMA* narl,
NUMA* nart,
NUMA* nait,
l_int32 w,
l_int32* pright)
{
l_int32 nrt, ntrans, start, end, sign, txt2, pap2, found, trans;
nrt = numaGetCount(nart);
/* Check for small max number of reversals or no edge */
/* Where is text and page, scanning from the right? */
ntrans = nrt / 3;
if (ntrans > 1)
{
found = FALSE;
for (trans = ntrans - 1; trans > 0; --trans)
{
numaGetEdgeValues(nart, trans, &start, &end, &sign);
if (sign == -1)
{
/* end of textblock */
txt2 = end;
found = TRUE;
}
}
if (!found)
{
txt2 = w - 1; /* take the whole thing! */
pap2 = w - 1;
}
else
{
/* found textblock; now find right side of page */
found = FALSE;
for (trans = ntrans - 1; trans > 0; --trans)
{
numaGetEdgeValues(nart, trans, &start, &end, &sign);
if (sign == 1 && start > txt2)
{
pap2 = start; /* start of textblock on other page */
found = TRUE;
}
}
if (!found)
{
/* no text from other page */
pap2 = w - 1; /* refine later */
}
}
}
else
{
txt2 = w - 1;
pap2 = w - 1;
}
lept_stderr("txt2 = %d, pap2 = %d\n", txt2, pap2);
*pright = pap2;
return 0;
}
void OptLabApp::on_scanning_border_removal_push_button_clicked()
{
static const l_int32 mindif = 60;
qDebug() << "Scanning Border Removal";
if (image_.isNull())
{
return;
}
l_int32 w, h;
auto src_image = QImage2PIX(image_);
auto gray_image = pixConvertTo8(src_image, 0);
pixGetDimensions(gray_image, &w, &h, nullptr);
/* Get info on vertical reversal profile */
auto nar = pixReversalProfile(gray_image, 0.8, L_VERTICAL_LINE,
0, h - 1, mindif, 1, 1);
auto naro = numaOpen(nar, 11);
auto pix1 = gplotSimplePix1(naro, "Reversals Opened");
auto narl = numaLowPassIntervals(naro, 0.1, 0.0);
auto nart = numaThresholdEdges(naro, 0.1, 0.5, 0.0);
numaDestroy(&nar);
numaDestroy(&naro);
/* Get info on vertical intensity profile */
auto pixgi = pixInvert(nullptr, gray_image);
auto nai = pixAverageIntensityProfile(pixgi, 0.8, L_VERTICAL_LINE,
0, h - 1, 1, 1);
auto naio = numaOpen(nai, 11);
auto pix2 = gplotSimplePix1(naio, "Intensities Opened");
auto nait = numaThresholdEdges(naio, 0.4, 0.6, 0.0);
numaDestroy(&nai);
numaDestroy(&naio);
/* Analyze profiles for left/right edges */
l_int32 left, right;
GetLeftCut(narl, nart, nait, w, &left);
GetRightCut(narl, nart, nait, w, &right);
/* Output visuals */
auto pixa2 = pixaCreate(3);
pixaAddPix(pixa2, src_image, L_INSERT);
pixaAddPix(pixa2, pix1, L_INSERT);
pixaAddPix(pixa2, pix2, L_INSERT);
auto pixd = pixaDisplayTiledInColumns(pixa2, 2, 1.0, 25, 0);
pixaDestroy(&pixa2);
image_ = PIX2QImage(pixd);
ui_->image_label->setPixmap(QPixmap::fromImage(image_));
//pixDestroy(&src_image);
//pixDestroy(&gray_image);
}
void OptLabApp::receive_image(QImage image)
{
image_ = image;
ui_->image_label->setPixmap(QPixmap::fromImage(image_));
}