-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters.cpp
1342 lines (1008 loc) · 49.3 KB
/
filters.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 "time.h"
#include <QtGui>
#include <iostream>
// Include utility class
//#include "utility.h"
/***********************************************************************
This is the only file you need to change for your assignment. The
other files control the UI (in case you want to make changes.)
************************************************************************/
// The first four functions provide example code to help get you started
// Convert an image to grey-scale
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));
}
}
}
// Add random noise to the image
void MainWindow::AddNoise(QImage *image, double mag, bool colorNoise)
{
int r, c;
QRgb pixel;
int noiseMag = mag;
noiseMag *= 2;
for(r=0;r<image->height();r++)
{
for(c=0;c<image->width();c++)
{
pixel = image->pixel(c, r);
int red = qRed(pixel);
int green = qGreen(pixel);
int blue = qBlue(pixel);
// If colorNoise, add color independently to each channel
if(colorNoise)
{
red += rand()%noiseMag - noiseMag/2;
green += rand()%noiseMag - noiseMag/2;
blue += rand()%noiseMag - noiseMag/2;
}
// otherwise add the same amount of noise to each channel
else
{
int noise = rand()%noiseMag - noiseMag/2;
red += noise;
green += noise;
blue += noise;
}
// Make sure we don't over or under saturate
red = min(255, max(0, red));
green = min(255, max(0, green));
blue = min(255, max(0, blue));
image->setPixel(c, r, qRgb( red, green, blue));
}
}
}
// Here is an example of blurring an image using a mean or box filter with the specified radius.
// This could be implemented using separable filters to make it much more efficient, but it is not.
void MainWindow::MeanBlurImage(QImage *image, int radius)
{
if(radius == 0)
return;
int r, c, rd, cd, i;
QRgb pixel;
// This is the size of the kernel
int size = 2*radius + 1;
// Create a buffer image so we're not reading and writing to the same image during filtering.
QImage buffer;
int w = image->width();
int h = image->height();
// This creates an image of size (w + 2*radius, h + 2*radius) with black borders.
// This could be improved by filling the pixels using a different padding technique (reflected, fixed, etc.)
buffer = image->copy(-radius, -radius, w + 2*radius, h + 2*radius);
// Compute kernel to convolve with the image.
double *kernel = new double [size*size];
for(i=0;i<size*size;i++)
{
kernel[i] = 1.0;
}
// Make sure kernel sums to 1
double denom = 0.000001;
for(i=0;i<size*size;i++)
denom += kernel[i];
for(i=0;i<size*size;i++)
kernel[i] /= denom;
// For each pixel in the image...
for(r=0;r<h;r++)
{
for(c=0;c<w;c++)
{
double rgb[3];
rgb[0] = 0.0;
rgb[1] = 0.0;
rgb[2] = 0.0;
// Convolve the kernel at each pixel
for(rd=-radius;rd<=radius;rd++)
for(cd=-radius;cd<=radius;cd++)
{
// Get the pixel value
pixel = buffer.pixel(c + cd + radius, r + rd + radius);
// Get the value of the kernel
double weight = kernel[(rd + radius)*size + cd + radius];
rgb[0] += weight*(double) qRed(pixel);
rgb[1] += weight*(double) qGreen(pixel);
rgb[2] += weight*(double) qBlue(pixel);
}
// Store mean pixel in the image to be returned.
image->setPixel(c, r, qRgb((int) floor(rgb[0] + 0.5), (int) floor(rgb[1] + 0.5), (int) floor(rgb[2] + 0.5)));
}
}
// Clean up.
delete [] kernel;
}
// Downsample the image by 1/2
void MainWindow::HalfImage(QImage &image)
{
QImage buffer;
int w = image.width();
int h = image.height();
int r, c;
buffer = image.copy();
// Reduce the image size.
image = QImage(w/2, h/2, QImage::Format_RGB32);
// Copy every other pixel
for(r=0;r<h/2;r++)
for(c=0;c<w/2;c++)
{
image.setPixel(c, r, buffer.pixel(c*2, r*2));
}
}
#pragma region Utility
struct ImageMetaData {
int Width;
int Height;
int radius;
ImageMetaData():Width(0),Height(0),radius(0){}
ImageMetaData(int width_, int height_, int radius_):Width(width_),Height(height_),radius(radius_){}
ImageMetaData(const ImageMetaData& metaData):Width(metaData.Width),Height(metaData.Height),radius(metaData.radius){}
};
struct Position {
int XPixelPos;
int YPixelPos;
Position():XPixelPos(-1), YPixelPos(-1){}
Position(int X, int Y):XPixelPos(X), YPixelPos(Y){}
Position(const Position& position_):XPixelPos(position_.XPixelPos), YPixelPos(position_.YPixelPos){}
};
void ResetValues(double* imageRGB, double** tempStorageHorizConvolution, int size ) {
if(imageRGB != NULL) {
imageRGB[0] = 0.0;
imageRGB[1] = 0.0;
imageRGB[2] = 0.0;
}
if((tempStorageHorizConvolution != NULL) && size > 0) {
for(int i=0; i< size; i++)
for(int j=0; j<3; j++)
tempStorageHorizConvolution[i][j] = 0.0;
}
}
QImage InitializeOutputImageBuffer(const QImage* image, double sigma, ImageMetaData& metaData)
{
metaData.radius = (int)(3*sigma);
metaData.Width = image->width();
metaData.Height = image->height();
double* kernel = NULL;
QImage GaussImagebuffer = image->copy(-metaData.radius,-metaData.radius,
metaData.Width + 2*metaData.radius, metaData.Height + 2*metaData.radius);
return (GaussImagebuffer);
}
int GetPixelPositionForEdges(int currentPixel, int edgesize) {
if(currentPixel < 0)
return abs(currentPixel);
else if(currentPixel > (edgesize))
return abs(edgesize-(currentPixel - edgesize));
else
return edgesize;
}
void GetPixelWeight(QImage* image, const Position& pixelPosition, double* pixelWeight) {
int edgePixelCol = pixelPosition.YPixelPos;
int edgePixelRow = pixelPosition.XPixelPos;
if((edgePixelCol) < 0 || (edgePixelCol)> (image->width()-1))
edgePixelCol = GetPixelPositionForEdges(edgePixelCol,image->width()-1);
if((edgePixelRow) < 0 || (edgePixelRow)> (image->height()-1))
edgePixelRow = GetPixelPositionForEdges(edgePixelRow,image->height()-1);
QRgb pixel = image->pixel(edgePixelCol, edgePixelRow);
pixelWeight[0] = qRed(pixel);
pixelWeight[1] = qGreen(pixel);
pixelWeight[2] = qBlue(pixel);
}
#pragma endregion Utility
/////////////////////////////////////////////// GAUSSIAN BLUR ///////////////////////////////////////////////////
#pragma region GaussianBlurImage
#pragma region GaussianKernel
struct GaussianKernel {
int KernelSize;
double variance;
double* Kernel;
GaussianKernel():KernelSize(0), variance(0), Kernel(NULL){}
GaussianKernel(int kernelSize_, double variance_, double* Kernel):
KernelSize(kernelSize_), variance(variance_), Kernel(Kernel){}
GaussianKernel(const GaussianKernel& gaussianKernel_):
KernelSize(gaussianKernel_.KernelSize), variance(gaussianKernel_.variance), Kernel(gaussianKernel_.Kernel){}
};
void InitializeGaussianKernel(const int radius, GaussianKernel& gaussianKernel) {
gaussianKernel.KernelSize = 2 * radius + 1;
gaussianKernel.Kernel = new double[gaussianKernel.KernelSize * gaussianKernel.KernelSize];
}
void LoadvaluesOfkernelComponents(const int radius,GaussianKernel& gaussianKernel)
{
double kernelConstant = (double)1 / (double)(2 * M_PI * powf((float)gaussianKernel.variance,(float)2));
for(int row = -radius; row <= radius; ++row) {
for(int col = -radius; col <= radius; ++col) {
int Xvalue = (int)powf(float(row ), (float)2);
int Yvalue = (int)powf(float(col ), (float)2);
double expValue = (double)(-1)*((Xvalue + Yvalue)/(2*(powf((float)gaussianKernel.variance,(float)2))));
gaussianKernel.Kernel[(row + radius)*gaussianKernel.KernelSize + col + radius] = kernelConstant * exp(expValue);
} }
}
void NormalizeKernel(const GaussianKernel& kernel) {
double epsilon = 0.000001;
double magnitude = epsilon;
for(int pos = 0; pos < kernel.KernelSize * kernel.KernelSize ; pos++) {
magnitude += kernel.Kernel[pos];
}
for(int pos = 0; pos < kernel.KernelSize * kernel.KernelSize ; pos++)
kernel.Kernel[pos] /= magnitude;
}
void GetGaussianKernel(const int radius,GaussianKernel& gaussianKernel) {
InitializeGaussianKernel(radius, gaussianKernel);
LoadvaluesOfkernelComponents(radius, gaussianKernel);
NormalizeKernel(gaussianKernel);
}
#pragma endregion GaussianKernel
double* ApplyFilterToPixelAndReturnRGB(const ImageMetaData& metaData, Position position, const QImage& outputImageBuffer, const GaussianKernel& kernel)
{
int radius = metaData.radius;
double* imageRGB = new double[3]();
QRgb pixel;
for(int row = -radius; row <= radius; ++row) {
for(int col = -radius; col <= radius; ++col) {
pixel = outputImageBuffer.pixel(position.YPixelPos + col + radius, position.XPixelPos + row + radius);
double weight = kernel.Kernel[(row + radius)* kernel.KernelSize + (col + radius)];
imageRGB[0] += weight*(double) qRed(pixel);
imageRGB[1] += weight*(double) qGreen(pixel);
imageRGB[2] += weight*(double) qBlue(pixel);
}
}
return imageRGB;
}
void ConvolveImagewithGaussianKernel(const ImageMetaData& metaData, QImage* inputImage, QImage& outputImageBuffer,GaussianKernel& kernel) {
double* imageRGB = new double[3];
int radius = metaData.radius;
QRgb pixel;
for(int rowPixel = 0; rowPixel < metaData.Height; rowPixel++) {
for(int colPixel = 0; colPixel < metaData.Width; colPixel++) {
//imageRGB = ApplyFilterToPixelAndReturnRGB(metaData, Position(rowPixel, colPixel), outputImageBuffer, kernel);
ResetValues(imageRGB,NULL,0);
for(int row = -radius; row <= radius; ++row) {
for(int col = -radius; col <= radius; ++col) {
pixel = outputImageBuffer.pixel(colPixel + col + radius, rowPixel + row + radius);
double weight = kernel.Kernel[(row + radius)* kernel.KernelSize + (col + radius)];
imageRGB[0] += weight*(double) qRed(pixel);
imageRGB[1] += weight*(double) qGreen(pixel);
imageRGB[2] += weight*(double) qBlue(pixel);
}
}
// Store mean pixel in the image to be returned.
inputImage->setPixel(colPixel, rowPixel, qRgb((int) floor(imageRGB[0] + 0.5),
(int) floor(imageRGB[1] + 0.5), (int) floor(imageRGB[2] + 0.5)));
}
}
delete[] imageRGB;
imageRGB = NULL;
}
void MainWindow::GaussianBlurImage(QImage *image, double sigma)
{
// Add your code here. Look at MeanBlurImage to get yourself started.
// Assume radius to be one.
clock_t start, end;
ImageMetaData metaData(0,0,0);
GaussianKernel gaussianKernel(0,sigma,NULL);
QImage OutPutImageBuffer = InitializeOutputImageBuffer(image,sigma, metaData);
GetGaussianKernel(metaData.radius, gaussianKernel);
start = clock();
ConvolveImagewithGaussianKernel(metaData, image,OutPutImageBuffer,gaussianKernel);
end = clock();
qDebug() << "Time required for execution: " << (double)(end-start)/CLOCKS_PER_SEC ;
}
#pragma endregion GaussianBlurImage
/////////////////////////////////////////////// SEPERABLE GAUSSIAN BLUR ///////////////////////////////////////////
#pragma region SeperableGaussianBlurImage
void InitializeSeperableGaussianKernel(const int radius, GaussianKernel& gaussianKernel) {
gaussianKernel.KernelSize = 2 * radius + 1;
gaussianKernel.Kernel = new double[gaussianKernel.KernelSize];
}
void LoadvaluesOfSeperablekernelComponents(const int radius,GaussianKernel& gaussianKernel)
{
double kernelConstant = (double)1 / (double)(sqrtf(2 * M_PI) * (float)gaussianKernel.variance);
for(int pos = -radius; pos <= radius; ++pos) {
int value = (int)powf(float(pos), (float)2);
double expValue = (double)(-1)*((value)/(2*(powf((float)gaussianKernel.variance,(float)2))));
gaussianKernel.Kernel[(pos + radius)] = kernelConstant * exp(expValue);
}
}
void NormalizeSeperableKernel(const GaussianKernel& kernel) {
double epsilon = 0.000001;
double magnitude = epsilon;
for(int pos = 0; pos < kernel.KernelSize ; pos++)
magnitude += kernel.Kernel[pos];
for(int pos = 0; pos < kernel.KernelSize ; pos++)
kernel.Kernel[pos] /= magnitude;
}
void GetSeperableGaussianKernel(const int radius,GaussianKernel& gaussianKernel) {
InitializeSeperableGaussianKernel(radius, gaussianKernel);
LoadvaluesOfSeperablekernelComponents(radius, gaussianKernel);
NormalizeSeperableKernel(gaussianKernel);
}
void ApplyVerticalFilterToPixel(QImage* inputImage, QImage* tempImage,const ImageMetaData& metaData,const GaussianKernel& kernel ) {
QRgb pixel;
int edgePixel;
double* imageRGB = new double[3];
int radius = metaData.radius;
for(int rowPixel = 0; rowPixel < metaData.Height; rowPixel++) { // X
for(int colPixel = 0; colPixel < metaData.Width; colPixel++) { // Y
ResetValues(imageRGB, NULL, 0);
for(int row = -radius; row <= radius; ++row) {
if((rowPixel+row) < 0 || (rowPixel+row)> (metaData.Height-1)) {
edgePixel = GetPixelPositionForEdges(rowPixel+row,metaData.Height-1);
pixel = tempImage->pixel(colPixel, edgePixel);
}
else
pixel = tempImage->pixel(colPixel,rowPixel+row);
double weight = kernel.Kernel[row+radius];
imageRGB[0] += (double)qRed(pixel) * weight;
imageRGB[1] += (double)qGreen(pixel) * weight;
imageRGB[2] += (double)qBlue(pixel) * weight;
}
inputImage->setPixel(colPixel, rowPixel, qRgb((int) floor(imageRGB[0] + 0.5),
(int) floor(imageRGB[1] + 0.5), (int) floor(imageRGB[2] + 0.5)));
}
}
}
void ApplyHorizontalFilterToPixel(QImage* inputImage, QImage* tempImage,const ImageMetaData& metaData,const GaussianKernel& kernel ) {
QRgb pixel;
int edgePixel;
double* imageRGB = new double[3];
int radius = metaData.radius;
for(int rowPixel = 0; rowPixel < metaData.Height; rowPixel++) { // X
for(int colPixel = 0; colPixel < metaData.Width; colPixel++) { // Y
ResetValues(imageRGB, NULL, 0);
for(int col = -radius; col <= radius; ++col) {
if((colPixel+col) < 0 || (colPixel+col)> (metaData.Width-1)) {
edgePixel = GetPixelPositionForEdges(colPixel+col,metaData.Width-1);
pixel = inputImage->pixel(edgePixel, rowPixel);
}
else
pixel = inputImage->pixel(colPixel+col,rowPixel);
double weight = kernel.Kernel[col+radius];
imageRGB[0] += (double)qRed(pixel) * weight;
imageRGB[1] += (double)qGreen(pixel) * weight;
imageRGB[2] += (double)qBlue(pixel) * weight;
}
tempImage->setPixel(colPixel, rowPixel, qRgb((int) floor(imageRGB[0] ),
(int) floor(imageRGB[1] ), (int) floor(imageRGB[2] )));
}
}
}
void ConvolveImagewithSeperableGaussianKernel(const ImageMetaData& metaData, QImage* inputImage, QImage& outputImageBuffer,GaussianKernel& kernel) {
QImage tempImage = inputImage->copy();
ApplyHorizontalFilterToPixel(inputImage, &tempImage, metaData,kernel);
ApplyVerticalFilterToPixel(inputImage,&tempImage,metaData, kernel);
}
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.
clock_t start, end;
ImageMetaData metaData(0,0,0);
GaussianKernel gaussianKernel(0,sigma,NULL);
QImage OutPutImageBuffer = InitializeOutputImageBuffer(image,sigma,metaData);
GetSeperableGaussianKernel(metaData.radius, gaussianKernel);
start = clock();
ConvolveImagewithSeperableGaussianKernel(metaData, image,OutPutImageBuffer,gaussianKernel);
end = clock();
qDebug() << "Time required for execution: " << (double)(end-start)/CLOCKS_PER_SEC ;
}
#pragma endregion SeperableGaussianBlurImage
///////////////////////////////////////////// DERIVATIVE IMAGE ////////////////////////////////////////////////////
#pragma region DerivativeImage
#pragma region FirstDerivativeImage
double** GetSobelFilter(int radius) {
double** SobelFilter = new double* [2*radius +1];
for(int i = 0; i < (2*radius+1); i++) {
SobelFilter[i] = new double[3];
for(int j=0; j<3;j++)
SobelFilter[i][j] = 0.0;
}
// change from default to adapt to filter of any size.
SobelFilter[0][0] = -1; SobelFilter[1][0] = -2; SobelFilter[2][0] = -1;
SobelFilter[0][1] = -1; SobelFilter[1][1] = -2; SobelFilter[2][1] = -1;
SobelFilter[0][2] = -1; SobelFilter[1][2] = -2; SobelFilter[2][2] = -1;
return SobelFilter;
}
void GetIntensityDifferenceBetweenneighbourPixelsinXDirection(const QImage& OutPutImageBuffer,double** temporaryRowArray, int radius, int rowPixel) {
QRgb pixel;
for(int colPixel = radius; colPixel < OutPutImageBuffer.width() - radius; colPixel++) {
// If we want to include the differentiation filter size > 3
// double intensityDifference = 0.0;
// int XDerivativeFilter[2*radius];// = {-1, 0, 1};
// for( int pos = -radius; pos <= radius; pos++)
// intensityDifference += OutPutImageBuffer.pixel(colPixel+pos) * XDerivativeFilter (pos+radius);
pixel = OutPutImageBuffer.pixel(colPixel+1,rowPixel) - OutPutImageBuffer.pixel(colPixel -1, rowPixel);
temporaryRowArray[colPixel-radius][0] = qRed(pixel);
temporaryRowArray[colPixel-radius][1] = qGreen(pixel);
temporaryRowArray[colPixel-radius][2] = qBlue(pixel);
}
}
void SetFirstDerivativePixelIinXToImage(QImage& image,double** temporaryRowArray, int rowPixel, int width) {
for(int colPixel = 0; colPixel < width ; colPixel++) {
image.setPixel(colPixel,rowPixel,qRgb((int) floor(temporaryRowArray[colPixel][0] + 128),
(int) floor(temporaryRowArray[colPixel][1] + 128), (int) floor(temporaryRowArray[colPixel][2] + 128)));
temporaryRowArray[colPixel][0] = 0.0;
temporaryRowArray[colPixel][1] = 0.0;
temporaryRowArray[colPixel][2] = 0.0;
}
}
void ApplyFirstDerivativeinXDirectionToImage(QImage& OutPutImageBuffer,QImage& image,const int radius) {
QRgb pixel;
int width = image.width();
int height = image.height();
double** temporaryRowArray = new double*[width]();
for(int i = 0; i < width ; i++)
temporaryRowArray[i] = new double[3];
ResetValues(NULL, temporaryRowArray, width);
for(int rowPixel = radius; rowPixel < height+radius; rowPixel++) {
GetIntensityDifferenceBetweenneighbourPixelsinXDirection(OutPutImageBuffer,temporaryRowArray, radius, rowPixel);
SetFirstDerivativePixelIinXToImage(image, temporaryRowArray, rowPixel - radius, width);
}
for(int i = 0; i < width ; i++)
delete[] temporaryRowArray[i];
}
void MainWindow::FirstDerivImage(QImage *image, double sigma)
{
// Add your code here.
ImageMetaData metaData(0,0,0);
GaussianKernel gaussianKernel(0,sigma,NULL);
QImage OutPutImageBuffer = InitializeOutputImageBuffer(image,sigma,metaData);
ApplyFirstDerivativeinXDirectionToImage(OutPutImageBuffer, *image,metaData.radius);
SeparableGaussianBlurImage(image,sigma);
}
#pragma endregion FirstDerivativeImage
#pragma region SecondDerivativeImage
void SetSecondDerivativePixelIinYToImage(QImage& image,double** temporaryRowArray, int colPixel, int height) {
for(int rowPixel = 0; rowPixel < height ; rowPixel++) {
image.setPixel(colPixel,rowPixel,qRgb((int) floor(temporaryRowArray[rowPixel][0] + 128),
(int) floor(temporaryRowArray[rowPixel][1] + 128), (int) floor(temporaryRowArray[rowPixel][2] + 128)));
temporaryRowArray[rowPixel][0] = 0.0;
temporaryRowArray[rowPixel][1] = 0.0;
temporaryRowArray[rowPixel][2] = 0.0;
}
}
void GetSecondOrderDiffOfPixelsinYDirection(const QImage& OutPutImageBuffer,double** temporaryRowArray, int radius, int colPixel) {
QRgb pixel;
for(int rowPixel = radius; rowPixel < OutPutImageBuffer.height() - radius; rowPixel++) {
// If we want to include the differentiation filter size > 3
// double intensityDifference = 0.0;
// int XDerivativeFilter[2*radius];// = {-1, 0, 1};
// for( int pos = -radius; pos <= radius; pos++)
// intensityDifference += OutPutImageBuffer.pixel(colPixel+pos) * XDerivativeFilter (pos+radius);
pixel = OutPutImageBuffer.pixel(colPixel,rowPixel + 1) + OutPutImageBuffer.pixel(colPixel, rowPixel-1) - 2*OutPutImageBuffer.pixel(colPixel,rowPixel);
temporaryRowArray[rowPixel-radius][0] = qRed(pixel);
temporaryRowArray[rowPixel-radius][1] = qGreen(pixel);
temporaryRowArray[rowPixel-radius][2] = qBlue(pixel);
}
}
void ApplySecondDerivativeinYDirectionToImage(QImage& OutPutImageBuffer,QImage& image,const int radius) {
QRgb pixel;
int width = image.width();
int height = image.height();
double** temporaryRowArray = new double*[height]();
for(int i = 0; i < height ; i++)
temporaryRowArray[i] = new double[3];
ResetValues(NULL, temporaryRowArray, height);
for(int colPixel = radius; colPixel < width+radius; colPixel++) {
GetSecondOrderDiffOfPixelsinYDirection(OutPutImageBuffer,temporaryRowArray, radius, colPixel);
SetSecondDerivativePixelIinYToImage(image, temporaryRowArray, colPixel - radius, height);
}
for(int i = 0; i < height ; i++)
delete[] temporaryRowArray[i];
}
void GetSecondOrderDiffOfPixelsinXDirection(const QImage& OutPutImageBuffer,double** temporaryRowArray, int radius, int rowPixel) {
QRgb pixel;
for(int colPixel = radius; colPixel < OutPutImageBuffer.width() - radius; colPixel++) {
// If we want to include the differentiation filter size > 3
// double intensityDifference = 0.0;
// int XDerivativeFilter[2*radius];// = {-1, 0, 1};
// for( int pos = -radius; pos <= radius; pos++)
// intensityDifference += OutPutImageBuffer.pixel(colPixel+pos) * XDerivativeFilter (pos+radius);
pixel = OutPutImageBuffer.pixel(colPixel+1,rowPixel) + OutPutImageBuffer.pixel(colPixel -1, rowPixel) - 2*OutPutImageBuffer.pixel(colPixel,rowPixel);
temporaryRowArray[colPixel-radius][0] = qRed(pixel);
temporaryRowArray[colPixel-radius][1] = qGreen(pixel);
temporaryRowArray[colPixel-radius][2] = qBlue(pixel);
}
}
void SetSecondDerivativePixelIinXToImage(QImage& image,double** temporaryRowArray, int rowPixel, int width) {
for(int colPixel = 0; colPixel < width ; colPixel++) {
image.setPixel(colPixel,rowPixel,qRgb((int) floor(temporaryRowArray[colPixel][0] + 128),
(int) floor(temporaryRowArray[colPixel][1] + 128), (int) floor(temporaryRowArray[colPixel][2] + 128)));
temporaryRowArray[colPixel][0] = 0.0;
temporaryRowArray[colPixel][1] = 0.0;
temporaryRowArray[colPixel][2] = 0.0;
}
}
void ApplySecondDerivativeinXDirectionToImage(QImage& OutPutImageBuffer,QImage& image,const int radius) {
QRgb pixel;
int width = image.width();
int height = image.height();
double** temporaryRowArray = new double*[width]();
for(int i = 0; i < width ; i++)
temporaryRowArray[i] = new double[3];
ResetValues(NULL, temporaryRowArray, width);
for(int rowPixel = radius; rowPixel < height+radius; rowPixel++) {
GetSecondOrderDiffOfPixelsinXDirection(OutPutImageBuffer,temporaryRowArray, radius, rowPixel);
SetSecondDerivativePixelIinXToImage(image, temporaryRowArray, rowPixel - radius, width);
}
for(int i = 0; i < width ; i++)
delete[] temporaryRowArray[i];
}
QImage ReplicateImage(const QImage* originalImage) {
QImage replicatedImage = originalImage->copy(0, 0, originalImage->width() , originalImage->height());
return replicatedImage;
}
void LaplacianOfGaussian(QImage& image, const QImage& FirstDerivativeImage, const QImage& SecondDerivativeImage, const ImageMetaData& metaData) {
int width = metaData.Width;
int height = metaData.Height;
QRgb pixelFirstDerivImage;
QRgb pixelSecondDerivImage;
double* temporaryColorBuffer = new double[3];
ResetValues(temporaryColorBuffer, NULL, 0);
for(int rowPixel = 0; rowPixel < height; rowPixel++) {
for(int colPixel =0; colPixel < width; colPixel++) {
pixelFirstDerivImage = FirstDerivativeImage.pixel(colPixel,rowPixel);
pixelSecondDerivImage = SecondDerivativeImage.pixel(colPixel, rowPixel);
temporaryColorBuffer[0] = qRed(pixelFirstDerivImage) + qRed(pixelSecondDerivImage);
temporaryColorBuffer[1] = qGreen(pixelFirstDerivImage) + qGreen(pixelSecondDerivImage);
temporaryColorBuffer[2] = qBlue(pixelFirstDerivImage) + qBlue(pixelSecondDerivImage);
image.setPixel(colPixel,rowPixel,qRgb((int) floor(temporaryColorBuffer[0] + 128),
(int) floor(temporaryColorBuffer[1] + 128), (int) floor(temporaryColorBuffer[2] + 128)));
ResetValues(temporaryColorBuffer, NULL, 0);
}
}
delete[] temporaryColorBuffer;
}
// This is implemented with 3x3 Laplacian operator.
// Change to dynamic filter
void MainWindow::SecondDerivImage(QImage *image, double sigma)
{
QImage FirstDerivativeImage, SecondDerivativeImage ;
ImageMetaData metaData(0,0,0);
GaussianKernel gaussianKernel(0,sigma,NULL);
FirstDerivativeImage = ReplicateImage(image);
SecondDerivativeImage = ReplicateImage(image);
QImage OutputImageBuffer = InitializeOutputImageBuffer(image,sigma,metaData);
ApplySecondDerivativeinXDirectionToImage(OutputImageBuffer, FirstDerivativeImage,metaData.radius);
SeparableGaussianBlurImage(&FirstDerivativeImage,sigma);
OutputImageBuffer = InitializeOutputImageBuffer(image,sigma,metaData);
ApplySecondDerivativeinYDirectionToImage(OutputImageBuffer, SecondDerivativeImage,metaData.radius);
SeparableGaussianBlurImage(&SecondDerivativeImage,sigma);
LaplacianOfGaussian(*image, FirstDerivativeImage, SecondDerivativeImage, metaData);
}
#pragma endregion SecondDerivativeImage
#pragma endregion DerivativeImage
/////////////////////////////////////////// SHARPEN IMAGE /////////////////////////////////////////////////////////
#pragma region SharpernImage
void ApplySubtractionFilterToImage(QImage *image, QImage* derivativeImage, double alpha) {
int width = image->width();
int height = image->height();
QRgb imagepixel;
QRgb derivativePixel;
double* imageRGB = new double[3]();
for(int rowPixel = 0; rowPixel < height; rowPixel++)
for(int colPixel = 0; colPixel < width; colPixel++) {
imagepixel = image->pixel(colPixel,rowPixel);
derivativePixel = derivativeImage->pixel(colPixel,rowPixel);
imageRGB[0] = (double)(qRed(imagepixel) - (alpha * (double)(qRed(derivativePixel) - 128)));
imageRGB[1] = (double)(qGreen(imagepixel) - (alpha * (double)(qGreen(derivativePixel) - 128)));
imageRGB[2] = (double)(qBlue(imagepixel) - (alpha * (double)(qBlue(derivativePixel)-128)));
imageRGB[0] = min(255.0, max(0.0, imageRGB[0]));
imageRGB[1] = min(255.0, max(0.0, imageRGB[1]));
imageRGB[2] = min(255.0, max(0.0, imageRGB[2]));
image->setPixel(colPixel, rowPixel, qRgb((int) floor(imageRGB[0] ),
(int) floor(imageRGB[1] ), (int) floor(imageRGB[2] )));
ResetValues(imageRGB, NULL, 0);
}
}
void MainWindow::SharpenImage(QImage *image, double sigma, double alpha)
{
// Add your code here. It's probably easiest to call SecondDerivImage as a helper function.
int radius = (int)(3 * sigma);
QImage DerivativeImage = image->copy(0, 0, image->width() , image->height());
SecondDerivImage(&DerivativeImage,sigma);
ApplySubtractionFilterToImage(image, &DerivativeImage, alpha);
}
#pragma endregion SharpernImage
void MainWindow::BilateralImage(QImage *image, double sigmaS, double sigmaI)
{
// Add your code here. Should be similar to GaussianBlurImage.
}
struct SobelKernel {
double* XSobelKernel;
double* YSobelKernel;
int KernelSize;
SobelKernel():KernelSize(0), XSobelKernel(NULL), YSobelKernel(NULL){}
SobelKernel(int kernelSize_, double* XKernel_, double* YKernel_):
KernelSize(kernelSize_), XSobelKernel(XKernel_), YSobelKernel(YKernel_) {}
SobelKernel(const SobelKernel& SobelKernel_):
KernelSize(SobelKernel_.KernelSize), XSobelKernel(SobelKernel_.XSobelKernel),YSobelKernel(SobelKernel_.YSobelKernel){}
};
void InitializeSobelKernel(SobelKernel& sobelKernel) {
sobelKernel.KernelSize = 3;
sobelKernel.XSobelKernel = new double[3];
sobelKernel.YSobelKernel = new double[3];
}
void LoadvaluesOfkernelComponents(SobelKernel& sobelKernel)
{
double XKernel[3] = {1,0,-1};
double YKernel[3] = {1,2,1};
sobelKernel.XSobelKernel = XKernel;
sobelKernel.YSobelKernel = YKernel;
}
struct SobelImagePixel {
double magnitude;
double orientation;
double Gx;
double Gy;
SobelImagePixel(){}
SobelImagePixel(double magnitude_, double orientation_):magnitude(magnitude_), orientation(orientation_){}
};
void GetSobelKernel(SobelKernel& sobelKernel) {
InitializeSobelKernel(sobelKernel);
LoadvaluesOfkernelComponents(sobelKernel);
}
void ApplyVerticalFilterToPixelAndReturnRGB(double* imageRGB, double** tempStorageHorizConvolution, const double* kernel, int radius ) {
for(int pos = -radius; pos <= radius; pos++) {
imageRGB[0] += (tempStorageHorizConvolution[pos+radius][0] )* kernel[pos + radius];
imageRGB[1] += (tempStorageHorizConvolution[pos+radius][1] )* kernel[pos + radius];
imageRGB[2] += (tempStorageHorizConvolution[pos+radius][2] )* kernel[pos + radius];
}
}
void ApplyHorizontalFilterToPixelAndReturnRGB(const QImage& outputImageBuffer,double** tempStorageHorizConvolution,const double* kernel, Position position, int radius) {
QRgb pixel;
for(int row = -radius; row <= radius; ++row)
for(int col = -radius; col <= radius; ++col) {
pixel = outputImageBuffer.pixel(position.YPixelPos + col + radius,position.XPixelPos +row + radius);
double weight = kernel[col + radius];
tempStorageHorizConvolution[row+radius][0] += weight*(double) qRed(pixel);
tempStorageHorizConvolution[row+radius][1] += weight*(double) qGreen(pixel);
tempStorageHorizConvolution[row+radius][2] += weight*(double) qBlue(pixel);
}
}
void ApplySobelFilterToImage(const ImageMetaData& metaData, QImage* inputImage,SobelKernel& kernel) {
double* imageRGB = new double[3]();
int radius = metaData.radius;
double* tempStorageHorizConvolution = new double[kernel.KernelSize]();
double Gx, Gy, mag, orient;
double XKernel[3][3] = {{-1,0,1},{-2,0,2},{-1,0,1}};
double YKernel[3][3] = {{-1, -2, -1}, {0,0,0},{1, 2, 1}};
QImage tempImage = inputImage->copy();
QRgb pixel;
for(int rowPixel = 0; rowPixel < metaData.Height; rowPixel++) { // X
for(int colPixel = 0; colPixel < metaData.Width; colPixel++) { // Y
pixel = inputImage->pixel(colPixel, rowPixel);
int grayScale = qGray(pixel);
tempImage.setPixel(colPixel, rowPixel, qRgb((int)floor(grayScale), (int)floor(grayScale), (int)floor(grayScale)));
}
}
int rowEdgePixel, colEdgePixel;
for(int rowPixel = 0; rowPixel < metaData.Height; rowPixel++) { // X
for(int colPixel = 0; colPixel < metaData.Width; colPixel++) { // Y
Gx = 0.0; Gy = 0.0;
for(int row = -1; row < 2; row++) {
rowEdgePixel = rowPixel + row;
if((rowEdgePixel) < 0 || (rowEdgePixel) > (metaData.Height-1))
rowEdgePixel = GetPixelPositionForEdges(rowEdgePixel,metaData.Height-1);
for(int col = -1; col < 2; col++) {
colEdgePixel = colPixel + col;
if((colEdgePixel) < 0 || (colEdgePixel) > (metaData.Width-1))
colEdgePixel = GetPixelPositionForEdges(colEdgePixel,metaData.Width-1);
pixel = tempImage.pixel(colEdgePixel, rowEdgePixel);
double weight = XKernel[row+1][col+1];
Gx += (double)(qRed(pixel)) * weight;
weight = YKernel[row+1][col+1];
Gy += (double)(qRed(pixel)) * weight;
}
}
mag = sqrt(Gx * Gx + Gy * Gy);
orient = atan2f(Gy, Gx);
double red = (sin(orient) + 1.0)/2.0;
double green = (cos(orient) + 1.0)/2.0;
double blue = 1.0 - red - green;
red *= mag * 4;
green *= mag * 4;
blue *= mag * 4;
// Make sure the pixel values range from 0 to 255
red = min(255.0, max(0.0, red));
green = min(255.0, max(0.0, green));
blue = min(255.0, max(0.0, blue));
inputImage->setPixel(colPixel, rowPixel, qRgb((int) floor(red),
(int) floor(green), (int) floor(blue)));
}
}
}
void MainWindow::SobelImage(QImage *image)
{
// Add your code here.
double XSobelKernel[3][3] = {{1,0,-1},{2,0,-2},{1,0,-1}};
double YSobelKernel[3][3] = {{1,2,1},{0,0,0},{-1,-2,-1}};
double sigma = double(1) / double(3);
ImageMetaData metaData(0,0,0);
SobelKernel sobelKernel(0,NULL,NULL);
metaData.Height = image->height();
metaData.Width = image->width();
metaData.radius = 1;
GetSobelKernel(sobelKernel);
ApplySobelFilterToImage(metaData, image, sobelKernel);
/***********************************************************************
When displaying the orientation image I
recommend the following:
double mag; // magnitude of the gradient
double orien; // orientation of the gradient
double red = (sin(orien) + 1.0)/2.0;
double green = (cos(orien) + 1.0)/2.0;
double blue = 1.0 - red - green;
red *= mag*4.0;
green *= mag*4.0;
blue *= mag*4.0;
// Make sure the pixel values range from 0 to 255
red = min(255.0, max(0.0, red));
green = min(255.0, max(0.0, green));
blue = min(255.0, max(0.0, blue));
image->setPixel(c, r, qRgb( (int) (red), (int) (green), (int) (blue)));
************************************************************************/
}
void GetNeighbouringPixelPositionAndWeight(QImage *image, double colPixel, double rowPixel, Position* position, double** pixelWeight) {
int colPixel1 = (int) ((colPixel));
int rowPixel1 = (int) ((rowPixel));
position[0] = Position(rowPixel1,colPixel1);
GetPixelWeight(image, Position(rowPixel1,colPixel1), pixelWeight[0]);
position[1] = Position(rowPixel1,colPixel1+1);
GetPixelWeight(image, Position(rowPixel1,colPixel1+1), pixelWeight[1]);