-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompressWindow.h
1066 lines (770 loc) · 23.5 KB
/
CompressWindow.h
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef _COMPRESS_WINDOW_H
#define _COMPRESS_WINDOW_H
#include <QDialog>
#include <QImage>
#include <QPainter>
#include <QFileDialog>
#include <QLine>
#include <QVector>
#include <QEvent>
#include "PCX.h"
#include "Img_Function.h"
#include "VideoFunctions.h" // in this project
#include "VideoFunction.h" // In pcx project for jpeg
#include <vector>
#include <thread>
#include <mutex>
#include <cstdlib>
#include <functional>
#include "ui_CompressWindow.h"
using namespace video;
using namespace std::placeholders;
class CompressWindow : public QDialog
{
Q_OBJECT
public:
CompressWindow(QWidget *parent = Q_NULLPTR) : QDialog(parent)
{
ui.setupUi(this);
initializeCoordinateData();
QObject::connect(this, SIGNAL(showPreviousImage_signal()), this, SLOT(showPreviousImage_slot()));
QObject::connect(this, SIGNAL(showNowImage_signal()), this, SLOT(showNowImage_slot()));
QObject::connect(this, SIGNAL(showPreviousBlock_signal()), this, SLOT(showPreviousBlock_slot()));
QObject::connect(this, SIGNAL(showNowBlock_signal()), this, SLOT(showNowBlock_slot()));
QObject::connect(this, SIGNAL(showMotionVectorImage_signal()), this, SLOT(showMotionVectorImage_slot()));
QObject::connect(this, SIGNAL(myupdate()), this, SLOT(update()));
pen.setStyle(Qt::PenStyle::SolidLine);
pen.setWidth(2);
pen.setBrush(Qt::red);
pen.setCapStyle(Qt::PenCapStyle::RoundCap);
pen.setJoinStyle(Qt::PenJoinStyle::RoundJoin);
}
~CompressWindow()
{
}
void paintEvent(QPaintEvent *ev)
{
QPainter p{ this };
if(!previous_img.isNull())
p.drawImage(pre_img_pos, previous_img);
if (!now_img.isNull())
p.drawImage(now_img_pos, now_img);
if(!motion_vector_img.isNull())
p.drawImage(motion_vec_pos, motion_vector_img);
if (!previous_block.isNull())
p.drawImage(pre_block_pos, previous_block);
if (!now_block.isNull())
p.drawImage(now_block_pos, now_block);
vid_mutex.lock();
if (is_compressing)
{
p.setPen(pen);
p.drawRect(matching_block_pos.x(), matching_block_pos.y(), BLOCK_SIZE, BLOCK_SIZE);
p.drawRect(target_block_pos.x(), target_block_pos.y(), BLOCK_SIZE, BLOCK_SIZE);
for (auto &x : line_list)
{
if (x.dx() == 0 && x.dy() == 0)
p.drawPoint(x.p1());
else
p.drawLine(x);
}
}
vid_mutex.unlock();
}
signals: /* -------- show signal functions -------- */
void showPreviousImage_signal(void);
void showNowImage_signal(void);
void showPreviousBlock_signal(void);
void showNowBlock_signal(void);
void showMotionVectorImage_signal(void);
void myupdate();
public slots: /* -------- button slot functions --------*/
void openVideoSequence_slot(void)
{
auto file_names{ QFileDialog::getOpenFileNames(this, QStringLiteral("¿ï¾Ü¼v¹³") , QStringLiteral("../Resource/Images/PCX") , QStringLiteral("Image (*.pcx);")) };
if (file_names.size() != 0)
{
// reset vid_stream
vid_stream.clear();
vid_stream.reserve(file_names.size());
for (auto cnt = 0; cnt < file_names.size(); ++cnt)
{
auto &path = file_names[cnt];
if (!path.isNull())
{
std::string fname{ path.toStdString() };
vid_stream.emplace_back(img::PCX{ fname }.getImgPixelData_r());
}
}
// initialize motion vector
initializeMotionVectorImage();
// assign data
assignPreviousData(0);
assignNowData(1);
// show the initial image
showPreviousImage_slot();
showNowImage_slot();
showMotionVectorImage_slot();
}
}
void startCompressButton_slot(void)
{
if (vid_stream.size() == 0)
return;
if (is_compressing)
return;
vid_mutex.lock();
is_compressing = true;
is_stopped = false;
vid_mutex.unlock();
getOutputFilePath();
// reserve the buffer of output
output_buf.reserve(vid_stream.size() * 32 * 32 * 4 + 256 * 256 * 2);
// output the stream size
output_buf.emplace_back((unsigned char)vid_stream.size());
// output the GOP_SIZE
output_buf.emplace_back((unsigned char)GOP_SIZE);
std::thread td{ &CompressWindow::compress,this };
td.detach();
}
void stopButton_slot(void)
{
vid_mutex.lock();
is_stopped = true;
vid_mutex.unlock();
}
void continueButton_slot(void)
{
vid_mutex.lock();
is_stopped = false;
vid_mutex.unlock();
}
public slots: /* -------- show QImage functions -------- */
void showPreviousImage_slot(void)
{
if (!previous_data)
assignPreviousData(previous_idx);
transferDataToQImage(*previous_data, previous_img);
updatePreImgIdxLabel();
update();
}
void showNowImage_slot(void)
{
if (!now_data)
assignPreviousData(now_idx);
transferDataToQImage(*now_data, now_img);
updateNowImgIdxLabel();
update();
}
void showPreviousBlock_slot(void)
{
previous_data_block = copyBlock(previous_data, matching_block_show_index.x(), matching_block_show_index.y());
transferDataToQImage(previous_data_block, previous_block);
update();
}
void showNowBlock_slot(void)
{
now_data_block = copyBlock(now_data, target_block_show_index.x(), target_block_show_index.y());
transferDataToQImage(now_data_block, now_block);
update();
}
void showMotionVectorImage_slot(void)
{
transferDataToQImage(motion_vector_matrix, motion_vector_img);
update();
}
private: /* -------- private data -------- */
Ui::CompressWindow ui;
QImage previous_img;
QImage now_img;
QImage previous_block;
QImage now_block;
img::Matrix *previous_data{ nullptr };
img::Matrix *now_data{ nullptr };
img::Matrix previous_data_block;
img::Matrix now_data_block;
img::Matrix motion_vector_matrix;
QImage motion_vector_img;
std::vector<img::Matrix> vid_stream;
std::mutex vid_mutex;
QPoint pre_img_pos;
QPoint now_img_pos;
QPoint pre_block_pos;
QPoint now_block_pos;
QPoint motion_vec_pos;
QPoint matching_block_pos;
QPoint target_block_pos;
QPoint target_block_show_index;
QPoint matching_block_show_index;
QPen pen;
QVector<QLine> line_list;
std::string fpath;
std::vector<unsigned char> output_buf;
int previous_idx{ -1 };
int now_idx{ -1 };
bool is_compressing{ false };
bool is_stopped{ true };
private: /* -------- initialization functions --------- */
void initializeCoordinateData(void)
{
pre_img_pos = QPoint{ 50,100 };
now_img_pos = QPoint{ 580,100 };
pre_block_pos = QPoint{ 340,200 };
now_block_pos = QPoint{ 490, 200 };
motion_vec_pos = QPoint{ 50,450 };
}
void initializeMotionVectorImage(void)
{
motion_vector_matrix = img::Matrix(vid_stream[0].getRow(), vid_stream[0].getCol(), 3); // set to RGB, so we can draw color
unsigned r, c;
for(r = BLOCK_SIZE/2-1;r<motion_vector_matrix.getRow();r+=BLOCK_SIZE)
for (c = BLOCK_SIZE / 2 - 1; c < motion_vector_matrix.getCol(); c += BLOCK_SIZE)
{
motion_vector_matrix[r][c] = motion_vector_matrix[r][c + 1] = std::vector<img::BYTE>(3,(img::BYTE)0);
motion_vector_matrix[r + 1][c] = motion_vector_matrix[r + 1][c + 1] = std::vector<img::BYTE>(3, (img::BYTE)0);
}
}
private: /* -------- assignment and update data -------- */
void assignPreviousData(int idx)
{
if (!checkIndex(idx))
throw "index out of range";
previous_idx = idx;
previous_data = &vid_stream[idx];
}
void assignNowData(int idx)
{
if (!checkIndex(idx))
throw "index out of range";
now_idx = idx;
now_data = &vid_stream[idx];
}
void updatePreImgIdxLabel(void)
{
auto num = std::to_string(vid_stream.size());
auto str = std::to_string(previous_idx + 1) + " / " + num;
ui.pre_idx_label->setText(str.c_str());
}
void updateNowImgIdxLabel(void)
{
auto num = std::to_string(vid_stream.size());
auto str = std::to_string(now_idx + 1) + " / " + num;
ui.now_idx_label->setText(str.c_str());
}
private: /* -------- helper function -------- */
bool checkIndex(int idx) const
{
if (idx < 0 || idx >= vid_stream.size())
return false;
else
return true;
}
void resizeMotionVector(MOTION_VECTOR &motion_vector) const
{
unsigned row = static_cast<unsigned>(ceil(1.0*vid_stream[0].getRow() / BLOCK_SIZE));
unsigned col = static_cast<unsigned>(ceil(1.0*vid_stream[0].getCol() / BLOCK_SIZE));
motion_vector.clear();
motion_vector = MOTION_VECTOR(row, std::vector<M_VECTOR>(col));
}
img::Matrix copyBlock(img::Matrix *pdata, unsigned rt, unsigned ct) const
{
auto &data = *pdata;
img::Matrix b{ BLOCK_SIZE,BLOCK_SIZE,data.getColor() };
for(auto r=0;r<BLOCK_SIZE;++r)
for(auto c=0;c<BLOCK_SIZE;++c)
for (auto color = 0; color < data.getColor(); ++color)
b[r][c][color] = data[rt + r][ct + c][color];
return img::ImgFunction::resize(b, 1.0* COMPRESS_SHOW_BLOCK_SIZE / BLOCK_SIZE);
}
void getOutputFilePath(void)
{
QString path = QFileDialog::getSaveFileName(this, QStringLiteral("¿é¥XªºÄÒ¦W"), "../Resource/Output", "Images ( *.mymv )");
std::string file{ path.toStdString() };
auto idx = file.find_last_of('.');
if (idx == std::string::npos)
fpath = file + ".mymv";
else
fpath = file;
}
std::function< MOTION_VECTOR(const img::Matrix &, const img::Matrix &, const int)> getCompressionFunction(void)
{
std::function< MOTION_VECTOR(const img::Matrix &, const img::Matrix &, const int) > func;
switch (ui.comboBox->currentIndex())
{
case 0:
func = std::bind(&CompressWindow::exhaustedSearch, this, _1, _2, _3, true);
break;
case 1:
func = std::bind(&CompressWindow::exhaustedSearchQuick, this, _1, _2, _3, true);
break;
case 2:
func = std::bind(&CompressWindow::TDLSearch, this, _1, _2, _3, true);
break;
case 3:
func = std::bind(&CompressWindow::TSS, this, _1, _2, _3, true);
break;
case 4:
func = std::bind(&CompressWindow::CSASearch, this, _1, _2, _3, true);
break;
case 5:
func = std::bind(&CompressWindow::exhaustedSearch, this, _1, _2, _3, false);
break;
case 6:
func = std::bind(&CompressWindow::exhaustedSearchQuick, this, _1, _2, _3, false);
break;
case 7:
func = std::bind(&CompressWindow::TDLSearch, this, _1, _2, _3, false);
break;
case 8:
func = std::bind(&CompressWindow::TSS, this, _1, _2, _3, false);
break;
case 9:
func = std::bind(&CompressWindow::CSASearch, this, _1, _2, _3, false);
break;
}
return func;
}
private: /* -------- compression method -------- */
void compress(void)
{
auto func = getCompressionFunction();
for (unsigned idx = 0; idx < vid_stream.size(); ++idx)
{
if (idx % GOP_SIZE == 0)
{
auto seqs = my_video::VidFunction::jpegEncoding(vid_stream[idx]);
my_video::VidFunction::outputJpegCode(seqs, output_buf);
}
else
{
line_list.clear();
assignPreviousData(idx - 1);
assignNowData(idx);
showPreviousImage_signal();
showNowImage_slot();
auto motion_vector = func(vid_stream[idx - 1], vid_stream[idx], 0);
outputMotion(motion_vector);
generateImageWithMotionVector(motion_vector, vid_stream[idx - 1], vid_stream[idx]);
}
}
std::ofstream fout{ fpath,std::ios::binary };
if (!fout)
{
throw "output file error";
}
fout.write((const char*)output_buf.data(), output_buf.size());
fout.close();
}
void waitToContinue(void)
{
while (is_stopped)
std::this_thread::sleep_for(std::chrono::microseconds(100));
}
void storeAndDrawMotionVector(MOTION_VECTOR &motion_vector, unsigned r, unsigned c, unsigned r_dif, unsigned c_dif)
{
motion_vector[r / BLOCK_SIZE][c / BLOCK_SIZE] = M_VECTOR(r_dif, c_dif);
int start_y = motion_vec_pos.y() + BLOCK_SIZE / 2;
int start_x = motion_vec_pos.x() + BLOCK_SIZE / 2;
line_list.push_back(QLine(start_x + c, start_y + r, start_x + c + c_dif, start_y + r + r_dif));
myupdate();
}
void printBlockAndUpdate(void)
{
showNowBlock_signal();
showPreviousBlock_signal();
myupdate();
}
private: /* -------- exhausted search --------*/
MOTION_VECTOR exhaustedSearch(const img::Matrix &ref_data, const img::Matrix &data, const int color, bool display)
{
MOTION_VECTOR motion_vector;
resizeMotionVector(motion_vector);
unsigned r, c;
unsigned ref_r, ref_c;
unsigned min_r = -1, min_c = -1;
int difference;
int dif;
for (r = 0; r < data.getRow(); r += BLOCK_SIZE)
for (c = 0; c < data.getCol(); c += BLOCK_SIZE)
{
// draw
drawTargetBlock(display, r, c);
// using MAD to find less difference, which is the answer
min_r = 0; min_c = 0;
difference = 1000000;
for (ref_r = 0; ref_r <= ref_data.getRow() - BLOCK_SIZE; ++ref_r)
for (ref_c = 0; ref_c <= ref_data.getCol() - BLOCK_SIZE; ++ref_c)
{
// draw
drawMatchingBlock(display, ref_r, ref_c);
dif = MAD(ref_data, ref_r, ref_c, data, r, c, color);
if (dif < difference)
{
min_r = ref_r;
min_c = ref_c;
difference = dif;
}
if (is_stopped)
waitToContinue();
}
storeAndDrawMotionVector(motion_vector, r, c, min_r - r, min_c - c);
}
return motion_vector;
}
MOTION_VECTOR exhaustedSearchQuick(const img::Matrix &ref_data, const img::Matrix &data, const int color, bool display)
{
MOTION_VECTOR motion_vector;
resizeMotionVector(motion_vector);
auto func = [&](unsigned r_start, unsigned r_end)
{
unsigned r, c;
unsigned ref_r, ref_c;
unsigned min_r = -1, min_c = -1;
int difference;
int dif;
for (r = r_start; r < r_end; r += BLOCK_SIZE)
for (c = 0; c < data.getCol(); c += BLOCK_SIZE)
{
// draw
drawTargetBlock(display, r, c);
// check the position of itself
dif = PDC(ref_data, r, c, data, r, c, color);
if (dif >= BLOCK_SIZE * BLOCK_SIZE - COMPRESS_THRESHOLD)
{
storeAndDrawMotionVector(motion_vector, r, c, 0, 0);
continue;
}
// using MAD to find less difference, which is the answer
min_r = 0; min_c = 0;
difference = 1000000;
for (ref_r = 0; ref_r <= ref_data.getRow() - BLOCK_SIZE; ++ref_r)
for (ref_c = 0; ref_c <= ref_data.getCol() - BLOCK_SIZE; ++ref_c)
{
// draw
drawMatchingBlock(display, ref_r, ref_c);
dif = MAD(ref_data, ref_r, ref_c, data, r, c, color);
if (dif < difference)
{
min_r = ref_r;
min_c = ref_c;
difference = dif;
}
if (is_stopped)
waitToContinue();
}
storeAndDrawMotionVector(motion_vector, r, c, min_r - r, min_c - c);
}
};
std::thread td1{ func,0,data.getRow() };
//std::thread td2{ func,data.getRow() / 2,data.getRow() };
td1.join();
//td2.join();
return motion_vector;
}
private: /* -------- Two Dimensional Logarithmic Search -------- */
MOTION_VECTOR TDLSearch(const img::Matrix &ref_data, const img::Matrix &data, const int color, bool display)
{
MOTION_VECTOR motion_vector;
resizeMotionVector(motion_vector);
int r, c;
int min_r, min_c;
int step = 4;
for (r = 0; r < data.getRow(); r += BLOCK_SIZE)
for (c = 0; c < data.getCol(); c += BLOCK_SIZE)
{
// draw
drawTargetBlock(display, r, c);
step = 4;
min_r = r;
min_c = c;
while (step != 1)
{
if (is_stopped)
waitToContinue();
auto point = TDL_stage1(ref_data, data, color, min_r, min_c, step, display);
min_r = point.x();
min_c = point.y();
step /= 2;
}
auto point = TDL_stage3(ref_data, data, color, min_r, min_c, display);
std::this_thread::sleep_for(std::chrono::microseconds(10));
storeAndDrawMotionVector(motion_vector, r, c, min_r - r, min_c - c);
}
return motion_vector;
}
QPoint TDL_stage1(const img::Matrix &ref_data, const img::Matrix &data, const int color, int r, int c, int step, bool display)
{
int dif;
int min = 1000000000;
int min_r = -1, min_c = -1;
int r_t, c_t;
std::vector<M_VECTOR> arr{ {0,0},{0,-step},{-step,0},{0,step},{step,0} };
for (auto cnt = 0; cnt < arr.size(); ++cnt)
{
r_t = r + arr[cnt].first;
c_t = c + arr[cnt].second;
if (tdl_checkBoarder(r_t, c_t))
{
// draw
drawMatchingBlock(display, r_t, c_t);
dif = MAD(ref_data, r_t, c_t, data, r, c, color);
if (dif < min)
{
min = dif;
min_r = r_t;
min_c = c_t;
}
}
}
return QPoint{ min_r,min_c };
}
QPoint TDL_stage3(const img::Matrix &ref_data, const img::Matrix &data, const int color, int r, int c, bool display)
{
int dif;
int min = 1000000;
int min_r = -1, min_c = -1;
int r_t, c_t;
std::vector<M_VECTOR> arr{ {-1,-1},{-1,0},{-1,1},{0,-1},{0,0},{0,1},{1,-1},{1,0},{1,1} };
for (auto cnt = 0; cnt < arr.size(); ++cnt)
{
r_t = r + arr[cnt].first;
c_t = c + arr[cnt].second;
if (tdl_checkBoarder(r_t, c_t))
{
// draw
drawMatchingBlock(display, r_t, c_t);
dif = MAD(ref_data, r_t, c_t, data, r, c, color);
if (dif < min)
{
min = dif;
min_r = r_t;
min_c = c_t;
}
}
}
return QPoint{ min_r,min_c };
}
bool tdl_checkBoarder(int r, int c) const
{
if (r < 0 || c < 0)
return false;
if (r + BLOCK_SIZE > 256 || c + BLOCK_SIZE > 256)
return false;
return true;
}
private: /* -------- Three Step Search -------- */
MOTION_VECTOR TSS(const img::Matrix &ref_data, const img::Matrix &data, const int color, bool display)
{
MOTION_VECTOR motion_vector;
resizeMotionVector(motion_vector);
int r, c;
int min_r, min_c;
int step;
for (r = 0; r < data.getRow(); r += BLOCK_SIZE)
for (c = 0; c < data.getCol(); c += BLOCK_SIZE)
{
// draw
drawTargetBlock(display, r, c);
step = 3;
min_r = r;
min_c = c;
while (step != 0)
{
if (is_stopped)
waitToContinue();
auto point = TSS_stage(ref_data, data, color, min_r, min_c, step, display);
min_r = point.x();
min_c = point.y();
step -= 1;
}
std::this_thread::sleep_for(std::chrono::microseconds(10));
storeAndDrawMotionVector(motion_vector, r, c, min_r - r, min_c - c);
}
return motion_vector;
}
QPoint TSS_stage(const img::Matrix &ref_data, const img::Matrix &data, const int color, int r, int c, int step, bool display)
{
int dif;
int min = 1000000000;
int min_r = -1, min_c = -1;
int ref_r, ref_c;
std::vector<M_VECTOR> arr{ {0,0},{0,-step},{-step,0},{0,step},{step,0},{-step,-step}, {step,-step},{-step,step},{step,step} };
for (auto cnt = 0; cnt < arr.size(); ++cnt)
{
ref_r = r + arr[cnt].first;
ref_c = c + arr[cnt].second;
// draw
drawMatchingBlock(display, ref_r, ref_c);
if (tdl_checkBoarder(ref_r, ref_c))
{
dif = MAD(ref_data, ref_r, ref_c, data, r, c, color);
if (dif < min)
{
min = dif;
min_r = ref_r;
min_c = ref_c;
}
}
}
return QPoint{ min_r,min_c };
}
private: /* -------- Cross Search Algorithm -------- */
MOTION_VECTOR CSASearch(const img::Matrix &ref_data, const img::Matrix &data, const int color, bool display)
{
MOTION_VECTOR motion_vector;
resizeMotionVector(motion_vector);
int r, c;
int min_r, min_c;
int pre_r, pre_c;
int step = 4;
for (r = 0; r < data.getRow(); r += BLOCK_SIZE)
for (c = 0; c < data.getCol(); c += BLOCK_SIZE)
{
// draw
drawTargetBlock(display, r, c);
step = 4;
min_r = r;
min_c = c;
while (step != 1)
{
if (is_stopped)
waitToContinue();
auto point = CSA_stage1(ref_data, data, color, min_r, min_c, step, display);
pre_r = min_r;
pre_c = min_c;
min_r = point.x();
min_c = point.y();
step /= 2;
}
auto point = CSA_stage2(ref_data, data, color, min_r, min_c, pre_r, pre_c, display);
std::this_thread::sleep_for(std::chrono::microseconds(10));
storeAndDrawMotionVector(motion_vector, r, c, min_r - r, min_c - c);
}
return motion_vector;
}
QPoint CSA_stage1(const img::Matrix &ref_data, const img::Matrix &data, const int color, int r, int c, int step, bool display)
{
int dif;
int min = 1000000000;
int min_r = -1, min_c = -1;
int r_t, c_t;
std::vector<M_VECTOR> arr{ {-step,-step}, {step,-step},{-step,step},{step,step} };
for (auto cnt = 0; cnt < arr.size(); ++cnt)
{
r_t = r + arr[cnt].first;
c_t = c + arr[cnt].second;
if (tdl_checkBoarder(r_t, c_t))
{
// draw
drawMatchingBlock(display, r_t, c_t);
dif = MAD(ref_data, r_t, c_t, data, r, c, color);
if (dif < min)
{
min = dif;
min_r = r_t;
min_c = c_t;
}
}
}
return QPoint{ min_r,min_c };
}
QPoint CSA_stage2(const img::Matrix &ref_data, const img::Matrix &data, const int color, int r, int c,int pr, int pc, bool display)
{
int dif;
int min = 1000000;
int min_r = -1, min_c = -1;
int r_t, c_t;
std::vector<M_VECTOR> arr1{ {-1,-1},{-1,1},{1,-1},{1,1} };
std::vector<M_VECTOR> arr2{ {-1,0},{0,-1},{0,1},{1,0} };
std::vector<M_VECTOR> *parr;
int dr = r - pr;
int dc = c - pc;
if (dr*dc > 0)
parr = &arr1;
else
parr = &arr2;
auto &arr = *parr;
for (auto cnt = 0; cnt < arr.size(); ++cnt)
{
r_t = r + arr[cnt].first;
c_t = c + arr[cnt].second;
if (tdl_checkBoarder(r_t, c_t))
{
// draw
drawMatchingBlock(display, r_t, c_t);
dif = MAD(ref_data, r_t, c_t, data, r, c, color);
if (dif < min)
{
min = dif;
min_r = r_t;
min_c = c_t;
}
}
}
return QPoint{ min_r,min_c };
}
private: /* -------- output --------*/
void outputMatrix(const img::Matrix &data)
{
unsigned r, c;
for (r = 0; r < data.getRow(); ++r)
for (c = 0; c < data.getCol(); ++c)
output_buf.emplace_back(data[r][c][0]);
}
void outputMotion(const MOTION_VECTOR &data)
{