-
Notifications
You must be signed in to change notification settings - Fork 2
/
Project2.cpp
2551 lines (2043 loc) · 65.6 KB
/
Project2.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
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
#include "mainwindow.h"
#include "math.h"
#include "ui_mainwindow.h"
#include <QtGui>
#include "Matrix.h"
#include <vector>
#include <array>
#include <map>
#include <tuple>
#include <iostream>
#include <fstream>
#include <functional>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;
#define PI 3.14159265358979323846
template<typename T, typename T2>
inline
T saturate_cast(T2 num)
{
if (num < 0)
return 0;
if (num>255)
return 255;
return static_cast<uchar>(num);
}
void MainWindow::BlackWhiteImage(QImage *image)
{
int r, c;
QRgb pixel;
for (r = 0; r < image->height(); r++)
{
for (c = 0; c < image->width(); c++)
{
pixel = image->pixel(c, r);
double red = (double)qRed(pixel);
double green = (double)qGreen(pixel);
double blue = (double)qBlue(pixel);
// Compute intensity from colors - these are common weights
double intensity = 0.3*red + 0.6*green + 0.1*blue;
image->setPixel(c, r, qRgb((int)intensity, (int)intensity, (int)intensity));
}
}
}
void MainWindow::ResamplingImage(QImage &image,double scale)
{
QImage buffer;
int w = image.width();
int h = image.height();
int r, c;
buffer = image.copy();
// Reduce the image size.
int w2 = (w-2) / scale+1;
int h2 = (h-2) / scale+1;
image = QImage(w2, h2, QImage::Format_RGB32);
// Copy every other pixel
for (r = 0; r < h2; r++)
for (c = 0; c < w2; c++)
{
//BilinearInterpolation(&buffer, scale * c, scale * r, rgb);
QRgb pixel = buffer.pixel((int)(scale*c+0.5), (int)(scale*r+0.5));
int rgb[3];
rgb[0] = qRed(pixel);
rgb[1] = qGreen(pixel);
rgb[2] = qBlue(pixel);
image.setPixel(c, r, qRgb(rgb[0], rgb[1], rgb[2]));
}
}
std::vector<double> MainWindow::DownSampling(std::vector<double>& image, int& width, int& height, double scale)
{
int w2 = width / scale;
int h2 = height / scale;
vector<double> result(w2*h2);
for (int r = 0; r < h2; r++)
for (int c = 0; c < w2; c++)
{
result[r*w2 + c] = image[(int)(scale*r)*width+(int)(scale*c)];
}
width = w2;
height = h2;
return result;
}
std::vector<double> MainWindow::UpSampling(std::vector<double>& image, int& width, int& height, double scale)
{
int w2 = width*scale;
int h2 = height*scale;
vector<double> result(w2*h2);
for (int r = 0; r < h2; r++)
for (int c = 0; c < w2; c++)
{
result[r*w2 + c] = image[(int)(r/scale)*width + (int)(c/scale)];
}
/*
A B C
E F G
H I J
pixels A C H J are pixels from original image
pixels B E G I F are interpolated pixels
*/
// interpolate pixels B and I
for (int r = 0; r < h2; r += 2)
for (int c = 1; c < w2 - 1; c += 2)
result[r*w2 + c] = 0.5*(image[r/2*width + c/2] + image[r/2*width + c/2 + 1]);
// interpolate pixels E and G
for (int r = 1; r < h2 - 1; r += 2)
for (int c = 0; c < w2; c += 2)
result[r*w2 + c] = 0.5*(image[r/2*width + c/2] + image[(r/2+1)*width + c/2]);
// interpolate pixel F
for (int r = 1; r < h2 - 1; r += 2)
for (int c = 1; c < w2 - 1; c += 2)
result[r*w2+c] = 0.25*(image[r/2*width+c/2]+image[(r/2+1)*width+c/2]+image[r/2*width+c/2+1]+image[(r/2+1)*width+c/2+1]);
width = w2;
height = h2;
return result;
}
bool MainWindow::Bilinear(std::vector<double>& image,int width, int height, double x, double y, double& result)
{
if (x < 0 || x >= width - 1 || y < 0 || y >= height - 1)
return false;
int x_left = static_cast<int>(x);
int x_right = x_left + 1;
int y_left = static_cast<int>(y);
int y_right = y_left + 1;
double f00 = image[y_left*width + x_left];
double f10 = image[y_left*width + x_right];
double f01 = image[y_right*width + x_left];
double f11 = image[y_right*width + x_right];
double r0 = (x_right - x)*f00 + (x - x_left)*f10;
double r1 = (x_right - x)*f01 + (x - x_left)*f11;
result = (y_right - y)*r0 + (y - y_left)*r1;
return true;
}
/*******************************************************************************
Draw detected Harris corners
interestPts - interest points
numInterestsPts - number of interest points
imageDisplay - image used for drawing
Draws a red cross on top of detected corners
*******************************************************************************/
void MainWindow::DrawInterestPoints(CIntPt *interestPts, int numInterestsPts, QImage &imageDisplay)
{
int i;
int r, c, rd, cd;
int w = imageDisplay.width();
int h = imageDisplay.height();
for(i=0;i<numInterestsPts;i++)
{
c = (int) interestPts[i].m_X;
r = (int) interestPts[i].m_Y;
for(rd=-2;rd<=2;rd++)
if(r+rd >= 0 && r+rd < h && c >= 0 && c < w)
imageDisplay.setPixel(c, r + rd, qRgb(255, 0, 0));
for(cd=-2;cd<=2;cd++)
if(r >= 0 && r < h && c + cd >= 0 && c + cd < w)
imageDisplay.setPixel(c + cd, r, qRgb(255, 0, 0));
}
}
void MainWindow::DrawInterestPoints(std::vector<std::tuple<std::pair<double, double>, double, double, std::vector<double>>> & interest_points,
QImage &imageDisplay)
{
int w = imageDisplay.width();
int h = imageDisplay.height();
int num = interest_points.size();
for (int i = 0; i < num; i++)
{
int c = round(get<0>(interest_points[i]).second);
int r = round(get<0>(interest_points[i]).first);
for (int rd = -2; rd <= 2; rd++)
if (r + rd >= 0 && r + rd < h && c >= 0 && c < w)
imageDisplay.setPixel(c, r + rd, qRgb(255, 0, 0));
for (int cd = -2; cd <= 2; cd++)
if (r >= 0 && r < h && c + cd >= 0 && c + cd < w)
imageDisplay.setPixel(c + cd, r, qRgb(255, 0, 0));
}
}
/*******************************************************************************
Compute interest point descriptors
image - input image
interestPts - array of interest points
numInterestsPts - number of interest points
If the descriptor cannot be computed, i.e. it's too close to the boundary of
the image, its descriptor length will be set to 0.
I've implemented a very simple 8 dimensional descriptor. Feel free to
improve upon this.
*******************************************************************************/
void MainWindow::ComputeDescriptors(QImage image, CIntPt *interestPts, int numInterestsPts)
{
int r, c, cd, rd, i, j;
int w = image.width();
int h = image.height();
// double *buffer = new double [w*h];
vector<double> buffer(w*h);
QRgb pixel;
// Descriptor parameters
double sigma = 2.0;
int rad = 4;
// Computer descriptors from green channel
for(r=0;r<h;r++)
for(c=0;c<w;c++)
{
pixel = image.pixel(c, r);
buffer[r*w + c] = (double) qGreen(pixel);
}
// Blur
SeparableGaussianBlurImage(buffer, w, h, sigma);
// Compute the desciptor from the difference between the point sampled at its center
// and eight points sampled around it.
for(i=0;i<numInterestsPts;i++)
{
int c = (int) interestPts[i].m_X;
int r = (int) interestPts[i].m_Y;
if(c >= rad && c < w - rad && r >= rad && r < h - rad)
{
double centerValue = buffer[(r)*w + c];
int j = 0;
for(rd=-1;rd<=1;rd++)
for(cd=-1;cd<=1;cd++)
if(rd != 0 || cd != 0)
{
interestPts[i].m_Desc[j] = buffer[(r + rd*rad)*w + c + cd*rad] - centerValue;
j++;
}
interestPts[i].m_DescSize = DESC_SIZE;
}
else
{
interestPts[i].m_DescSize = 0;
}
}
// delete [] buffer;
}
/*******************************************************************************
Draw matches between images
matches - matching points
numMatches - number of matching points
image1Display - image to draw matches
image2Display - image to draw matches
Draws a green line between matches
*******************************************************************************/
void MainWindow::DrawMatches(CMatches *matches, int numMatches, QImage &image1Display, QImage &image2Display)
{
int i;
// Show matches on image
QPainter painter;
painter.begin(&image1Display);
QColor green(0, 250, 0);
QColor red(250, 0, 0);
for(i=0;i<numMatches;i++)
{
painter.setPen(green);
painter.drawLine((int) matches[i].m_X1, (int) matches[i].m_Y1, (int) matches[i].m_X2, (int) matches[i].m_Y2);
painter.setPen(red);
painter.drawEllipse((int) matches[i].m_X1-1, (int) matches[i].m_Y1-1, 3, 3);
}
QPainter painter2;
painter2.begin(&image2Display);
painter2.setPen(green);
for(i=0;i<numMatches;i++)
{
painter2.setPen(green);
painter2.drawLine((int) matches[i].m_X1, (int) matches[i].m_Y1, (int) matches[i].m_X2, (int) matches[i].m_Y2);
painter2.setPen(red);
painter2.drawEllipse((int) matches[i].m_X2-1, (int) matches[i].m_Y2-1, 3, 3);
}
}
void MainWindow::DrawMatches(std::vector<std::pair<double,double>>& feature0,
std::vector<std::pair<double, double>>& feature1,
QImage &image1Display,
QImage &image2Display)
{
int i;
// Show matches on image
QPainter painter;
painter.begin(&image1Display);
QColor green(0, 250, 0);
QColor red(250, 0, 0);
int num_match = feature0.size();
for (i = 0; i < num_match; i++)
{
painter.setPen(green);
painter.drawLine((int)feature0[i].second, (int)feature0[i].first, (int)feature1[i].second, (int)feature1[i].first);
painter.setPen(red);
painter.drawEllipse((int)feature0[i].second - 1, (int)feature0[i].first - 1, 3, 3);
}
QPainter painter2;
painter2.begin(&image2Display);
painter2.setPen(green);
for (i = 0; i < num_match; i++)
{
painter2.setPen(green);
painter2.drawLine((int)feature0[i].second, (int)feature0[i].first, (int)feature1[i].second, (int)feature1[i].first);
painter2.setPen(red);
painter2.drawEllipse((int)feature1[i].second - 1, (int)feature1[i].first - 1, 3, 3);
}
}
/*******************************************************************************
Given a set of matches computes the "best fitting" homography
matches - matching points
numMatches - number of matching points
h - returned homography
isForward - direction of the projection (true = image1 -> image2, false = image2 -> image1)
*******************************************************************************/
bool MainWindow::ComputeHomography(CMatches *matches, int numMatches, double h[3][3], bool isForward)
{
int error;
int nEq=numMatches*2;
dmat M=newdmat(0,nEq,0,7,&error);
dmat a=newdmat(0,7,0,0,&error);
dmat b=newdmat(0,nEq,0,0,&error);
double x0, y0, x1, y1;
for (int i=0;i<nEq/2;i++)
{
if(isForward == false)
{
x0 = matches[i].m_X1;
y0 = matches[i].m_Y1;
x1 = matches[i].m_X2;
y1 = matches[i].m_Y2;
}
else
{
x0 = matches[i].m_X2;
y0 = matches[i].m_Y2;
x1 = matches[i].m_X1;
y1 = matches[i].m_Y1;
}
//Eq 1 for corrpoint
M.el[i*2][0]=x1;
M.el[i*2][1]=y1;
M.el[i*2][2]=1;
M.el[i*2][3]=0;
M.el[i*2][4]=0;
M.el[i*2][5]=0;
M.el[i*2][6]=(x1*x0*-1);
M.el[i*2][7]=(y1*x0*-1);
b.el[i*2][0]=x0;
//Eq 2 for corrpoint
M.el[i*2+1][0]=0;
M.el[i*2+1][1]=0;
M.el[i*2+1][2]=0;
M.el[i*2+1][3]=x1;
M.el[i*2+1][4]=y1;
M.el[i*2+1][5]=1;
M.el[i*2+1][6]=(x1*y0*-1);
M.el[i*2+1][7]=(y1*y0*-1);
b.el[i*2+1][0]=y0;
}
int ret=solve_system (M,a,b);
if (ret!=0)
{
freemat(M);
freemat(a);
freemat(b);
return false;
}
else
{
h[0][0]= a.el[0][0];
h[0][1]= a.el[1][0];
h[0][2]= a.el[2][0];
h[1][0]= a.el[3][0];
h[1][1]= a.el[4][0];
h[1][2]= a.el[5][0];
h[2][0]= a.el[6][0];
h[2][1]= a.el[7][0];
h[2][2]= 1;
}
freemat(M);
freemat(a);
freemat(b);
return true;
}
bool MainWindow::ComputeHomography(std::vector<std::pair<double, double>>& feature0,
std::vector<std::pair<double, double>>& feature1,
double h[3][3],
bool isForward)
{
int error;
int nEq = feature0.size() * 2;
dmat M = newdmat(0, nEq, 0, 7, &error);
dmat a = newdmat(0, 7, 0, 0, &error);
dmat b = newdmat(0, nEq, 0, 0, &error);
double x0, y0, x1, y1;
for (int i = 0; i < nEq / 2; i++)
{
if (isForward == false)
{
x0 = feature0[i].second;
y0 = feature0[i].first;
x1 = feature1[i].second;
y1 = feature1[i].first;
}
else
{
x0 = feature1[i].second;
y0 = feature1[i].first;
x1 = feature0[i].second;
y1 = feature0[i].first;
}
//Eq 1 for corrpoint
M.el[i * 2][0] = x1;
M.el[i * 2][1] = y1;
M.el[i * 2][2] = 1;
M.el[i * 2][3] = 0;
M.el[i * 2][4] = 0;
M.el[i * 2][5] = 0;
M.el[i * 2][6] = (x1*x0*-1);
M.el[i * 2][7] = (y1*x0*-1);
//M.el[i * 2][6] = -x1*x0;
//M.el[i * 2][7] = -y1*x0;
b.el[i * 2][0] = x0;
//Eq 2 for corrpoint
M.el[i * 2 + 1][0] = 0;
M.el[i * 2 + 1][1] = 0;
M.el[i * 2 + 1][2] = 0;
M.el[i * 2 + 1][3] = x1;
M.el[i * 2 + 1][4] = y1;
M.el[i * 2 + 1][5] = 1;
M.el[i * 2 + 1][6] = (x1*y0*-1);
M.el[i * 2 + 1][7] = (y1*y0*-1);
//M.el[i * 2 + 1][6] = -x1*y0;
//M.el[i * 2 + 1][7] = -y1*y0;
b.el[i * 2 + 1][0] = y0;
}
int ret = solve_system(M, a, b);
if (ret != 0)
{
freemat(M);
freemat(a);
freemat(b);
return false;
}
else
{
h[0][0] = a.el[0][0];
h[0][1] = a.el[1][0];
h[0][2] = a.el[2][0];
h[1][0] = a.el[3][0];
h[1][1] = a.el[4][0];
h[1][2] = a.el[5][0];
h[2][0] = a.el[6][0];
h[2][1] = a.el[7][0];
h[2][2] = 1;
}
freemat(M);
freemat(a);
freemat(b);
return true;
}
/*******************************************************************************
*******************************************************************************
*******************************************************************************
The routines you need to implement are below
*******************************************************************************
*******************************************************************************
*******************************************************************************/
/*******************************************************************************
Blur a single channel floating point image with a Gaussian.
image - input and output image
w - image width
h - image height
sigma - standard deviation of Gaussian
This code should be very similar to the code you wrote for assignment 1.
*******************************************************************************/
std::vector<double> MainWindow::SeparableGaussianBlurImage(vector<double>& image, int w, int h, double sigma)
{
// Add your code here
if (sigma == 0) return vector<double>();
int radius = 3*sigma;
int size = 2 * radius + 1;
sigma /= sqrt(2.0);
std::vector<double> buffer((w + 2 * radius)*(h + 2 * radius));
vector<double> result(w*h);
for (int r = 0; r < h; ++r)
{
for (int c = 0; c < w; ++c)
{
buffer[(r + radius)*(w + 2 * radius) + c + radius] = image[r*w + c];
}
}
double* kernel = new double[size];
double Z = sqrt(2 * PI)*sigma;
for (int x = -radius; x <= radius; ++x)
{
kernel[x + radius] = exp(-(pow(x, 2.0)) / (2 * pow(sigma, 2))) / Z;
}
double denom = 0.000001;
for (int i = 0; i < size; ++i)
{
denom += kernel[i];
}
for (int i = 0; i < size; i++)
{
kernel[i] /= denom;
}
for (int r = 0; r < h; ++r)
{
for (int c = 0; c < w; ++c)
{
double rgb[3];
rgb[0] = 0.0;
rgb[1] = 0.0;
rgb[2] = 0.0;
double intensity = 0;
// Convolve the kernel at each pixel
for (int rd = -radius; rd <= radius; ++rd)
{
// Get the pixel value
//QRgb pixel = buffer.pixel(c + rd + radius, r);
double pixel = buffer[(r + radius)*(w + 2 * radius) + c + rd + radius];
// Get the value of the kernel
double weight = kernel[rd + radius];
intensity += weight*pixel;
}
buffer[(r + radius)*(w + 2 * radius) + c + radius] = intensity;
}
}
for (int r = 0; r < h; ++r)
{
for (int c = 0; c < w; ++c)
{
double rgb[3];
rgb[0] = 0.0;
rgb[1] = 0.0;
rgb[2] = 0.0;
double intensity = 0;
for (int cd = -radius; cd <= radius; ++cd)
{
// Get the pixel value
//QRgb pixel = buffer.pixel(c, r + cd + radius);
double pixel = buffer[(r + cd + radius)*(w + 2 * radius) + c + radius];
// Get the value of the kernel
double weight = kernel[cd + radius];
intensity += weight*pixel;
}
result[r*w + c] = intensity;
}
}
delete[] kernel;
return result;
// To access the pixel (c,r), use image[r*width + c].
}
void MainWindow::SeparableGaussianBlurImage(QImage *image, double sigma)
{
// Add your code here. Done right, you should be able to copy most of the code from GaussianBlurImage.
if (sigma == 0) return;
if (!image->isGrayscale())
BlackWhiteImage(image);
int radius = 3 * sigma;
int size = 2 * radius + 1;
QImage buffer;
int w = image->width();
int h = image->height();
buffer = image->copy(-radius, -radius, w + 2 * radius, h + 2 * radius);
double* kernel = new double[size];
double Z = sqrt(2 * PI)*sigma;
for (int rd = -radius; rd <= radius; ++rd)
{
kernel[rd + radius] = exp(-(pow(rd, 2.0)) / (2 * pow(sigma, 2))) / Z;
}
double denom = 0.000001;
double y_denom = 0.000001;
for (int i = 0; i < size; ++i)
{
denom += kernel[i];
}
for (int i = 0; i < size; i++)
{
kernel[i] /= denom;
}
for (int r = 0; r < h; ++r)
{
for (int c = 0; c < w; ++c)
{
double intensity = 0;
for (int rd = -radius; rd <= radius; ++rd)
{
// Get the pixel value
QRgb pixel = buffer.pixel(c + radius, r + rd + radius);
// Get the value of the kernel
double weight = kernel[rd + radius];
intensity += weight*(double)qRed(pixel);
}
// Store mean pixel in the image to be returned.
buffer.setPixel(c + radius, r + radius, qRgb((int)floor(intensity + 0.5), (int)floor(intensity + 0.5), (int)floor(intensity + 0.5)));
}
}
for (int r = 0; r < h; ++r)
{
for (int c = 0; c < w; ++c)
{
double intensity = 0;
for (int cd = -radius; cd <= radius; ++cd)
{
// Get the pixel value
QRgb pixel = buffer.pixel(c + cd + radius, r + radius);
// Get the value of the kernel
double weight = kernel[cd + radius];
intensity += weight*(double)qRed(pixel);
}
// Store mean pixel in the image to be returned.
image->setPixel(c, r, qRgb((int)floor(intensity + 0.5), (int)floor(intensity + 0.5), (int)floor(intensity + 0.5)));
}
}
delete[] kernel;
}
void MainWindow::FristDerivate(const vector<double>&image,int w, int h, double sigma, Direction direction, vector<double>& gradient)
{
if (sigma == 0) return;
int radius = 1;
int size = 2 * radius + 1;
std::vector<double> buffer2((w + 2 * radius)*(h + 2 * radius));
for (int r = 0; r < h; ++r)
{
for (int c = 0; c < w; ++c)
{
buffer2[(r + radius)*(w + 2 * radius) + c + radius] = image[r*w + c];
}
}
double*x_kernel = new double[size];
double*y_kernel = new double[size];
double Z = sqrt(2 * PI)*sigma;
for (int x = -radius; x <= radius; ++x)
{
x_kernel[x + radius] = -x / pow(sigma, 2)*exp(-pow(x, 2.0) / (2 * pow(sigma, 2))) / Z;
}
for (int y = -radius; y <= radius; ++y)
{
y_kernel[y + radius] = exp(-pow(y, 2.0) / (2 * pow(sigma, 2))) / Z;
}
double x_denom = 0.000001;
double y_denom = 0.000001;
for (int i = 0; i < size; ++i)
{
x_denom += abs(x_kernel[i]);
y_denom += y_kernel[i];
}
for (int i = 0; i < size; i++)
{
x_kernel[i] /= x_denom;
y_kernel[i] /= y_denom;
}
for (int r = 0; r < h; ++r)
{
for (int c = 0; c < w; ++c)
{
double intensity = 0;
// Convolve the kernel at each pixel
for (int cd = -radius; cd <= radius; ++cd)
{
double pixel = 0;
// Get the pixel value
if (direction == Direction::Y)
//pixel = buffer.pixel(c, r + cd + radius);
pixel = buffer2[(r + cd + radius)*(w + 2 * radius) + c + radius];
else
//pixel = buffer.pixel(c + cd + radius, r);
pixel = buffer2[(r + radius)*(w + 2 * radius) + c + cd + radius];
// Get the value of the kernel
double weight = y_kernel[cd + radius];
intensity += weight*pixel;
}
buffer2[(r + radius)*(w + 2 * radius) + c + radius] = intensity;
}
}
for (int r = 0; r < h; ++r)
{
for (int c = 0; c < w; ++c)
{
double intensity = 0;
// Convolve the kernel at each pixel
for (int rd = -radius; rd <= radius; ++rd)
{
// Get the pixel value
double pixel = 0;
if (direction == Direction::Y)
pixel = buffer2[(r + radius)*(w + 2 * radius) + c + rd + radius];
else
pixel = buffer2[(r + rd + radius)*(w + 2 * radius) + c + radius];
// Get the value of the kernel
double weight = x_kernel[rd + radius];
intensity += weight*pixel;
}
//intensity += 128;
intensity = floor(intensity + 0.5);
gradient[r*w+c] = intensity;
}
}
delete[] x_kernel;
delete[] y_kernel;
}
/*******************************************************************************
Detect Harris corners.
image - input image
sigma - standard deviation of Gaussian used to blur corner detector
thres - Threshold for detecting corners
interestPts - returned interest points
numInterestsPts - number of interest points returned
imageDisplay - image returned to display (for debugging)
*******************************************************************************/
void MainWindow::HarrisCornerDetector(QImage image, double sigma, double thres, CIntPt **interestPts, int &numInterestsPts, QImage &imageDisplay)
{
/**sift detector*/
DrawInterestPoints(SiftDetector(image), imageDisplay);
return;
/**harris corner detector*/
int w = image.width();
int h = image.height();
vector<double> buffer(w*h);
QRgb pixel;
numInterestsPts = 0;
// Compute the corner response using just the green channel
for(int r=0;r<h;r++)
for(int c=0;c<w;c++)
{
pixel = image.pixel(c, r);
buffer[r*w + c] = (double) qGreen(pixel);
}
// Write your Harris corner detection code here.
vector<double> dx(w*h);
vector<double> dy(w*h);
FristDerivate(buffer, w, h, sigma, Direction::X, dx);
FristDerivate(buffer, w, h, sigma, Direction::Y, dy);
vector<double> ddx(w*h);
vector<double> ddy(w*h);
vector<double> dxdy(w*h);
for (int r = 0; r < h; r++)
for (int c = 0; c < w; c++)
{
ddx[r*w + c] = dx[r*w + c] * dx[r*w + c];
ddy[r*w + c] = dy[r*w + c] * dy[r*w + c];
dxdy[r*w + c] = dx[r*w + c] * dy[r*w + c];
}
ddx=SeparableGaussianBlurImage(ddx, w, h, sigma+0.5);
ddy=SeparableGaussianBlurImage(ddy, w, h, sigma+0.5);
dxdy=SeparableGaussianBlurImage(dxdy, w, h, sigma+0.5);
int radius = static_cast<int>(0.02*std::min(w, h) / 2 + 0.5);
int size = 2 * radius + 1;
vector<double> r_value((w + 2 * radius)*(h + 2 * radius));
const double alpha = 0.05; // constant value 0.04~0.06
double r_min = 1000000;
double r_max = 0;
for (int r = 0; r < h;++r)
{
for (int c = 0; c < w;++c)
{
double delta = ddx[r*w + c] * ddy[r*w + c] - pow(dxdy[r*w + c], 2);
double trace = ddx[r*w + c] + ddy[r*w + c];
double R = delta - alpha*pow(trace, 2);
if (R>0)
{
if (r_max < R) r_max = R;
if (r_min>R) r_min = R;
r_value[(r + radius)*(w + 2 * radius) + c + radius] = R;
}
}
}
thres = r_min + 0.2*(r_max - r_min);
vector<CIntPt> interest_points;
int corner_row = 0;
int corner_col = 0;
// non maximum suppression
const int LowNum = 1000;
const int HighNum = 2000;
int iterations = 0;
while (!(numInterestsPts>LowNum && numInterestsPts<HighNum) && iterations<10)
{
numInterestsPts = 0;
interest_points.clear();
for (int r = 0; r < h; ++r)
{
for (int c = 0; c < w; ++c)
{
double max_value = r_value[(r + radius)*(w + 2 * radius) + c + radius];
if (max_value>thres)
{
for (int rd = -radius; rd <= radius; ++rd)
for (int cd = -radius; cd <= radius; ++cd)
{
if (max_value < r_value[(r + rd + radius)*(w + 2 * radius) + c + cd + radius])
{
max_value = r_value[(r + rd + radius)*(w + 2 * radius) + c + cd + radius];
corner_row = r + rd;