-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathImageOperations.cpp
924 lines (717 loc) · 22 KB
/
ImageOperations.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
#include "ImageOperations.h"
#include <stdio.h>
#include <QMessageBox>
ImageOperations::ImageOperations(QImage image)
{
ImageOperations::image = image;
}
ImageOperations::~ImageOperations()
{
}
QImage ImageOperations::FFTPowerSpectrum(Channels forColor)
{
int height = image.height();
int width = image.width();
if (height == 0 || width == 0)
return QImage();
int newHeightAndWidth = firstPowerOfTwoOfLargerDimension(width, height);
cArray2D array2D(newHeightAndWidth, newHeightAndWidth);
int mean = getMeanValue(forColor);
array2D.fill(complex(mean,0));
cImageToArray2D(&array2D, forColor);
cExecuteFFTOnArray2D(&array2D);
cPowerSpectrumToRealPart(&array2D);
double min = (array2D.findMin()).re();
double max = (array2D.findMax()).re();
cScaleRealPartLogarithmically(&array2D, min, max);
cSwapBlocks(&array2D);
QImage newImage = cRealPartToImage(&array2D);
return newImage;
}
void ImageOperations::cImageToArray2D(cArray2D *pArray, Channels forColor)
{
if (image.format() != QImage::Format_ARGB32)
image = image.convertToFormat(QImage::Format_ARGB32);
int newHeight = pArray->getHeight();
int newWidth = pArray->getWidth();
int positionY = 0;
int positionX = 0;
for (int i=0; i < image.height(); i++)
{
QRgb *line = (QRgb*)image.scanLine(i);
for (int j=0; j < image.width(); j++)
{
positionY = i + (newHeight - image.height()) / 2;
positionX = j + (newWidth - image.width())/2;
pArray->setAt(positionY, positionX, getIntensity(line[j], forColor));
}
}
}
int ImageOperations::getIntensity(QRgb color, Channels forColor)
{
int intensity = 0;
switch (forColor)
{
case Red:
intensity = qRed(color);
break;
case Green:
intensity = qGreen(color);
break;
case Blue:
intensity = qBlue(color);
break;
}
return intensity;
}
int ImageOperations::getMeanValue(Channels forColor)
{
double all = (double)image.height() * (double)image.width();
double sum = 0;
for (int i=0; i < image.height(); i++)
{
QRgb *line = (QRgb*)image.scanLine(i);
for (int j=0; j < image.width(); j++)
{
sum += (double)getIntensity(line[j], forColor);
}
}
double mean = sum / all;
int fl = (int)floor(mean);
return fl;
}
void ImageOperations::cExecuteFFTOnArray2D(cArray2D *pArray)
{
int newHeight = pArray->getHeight();
int newWidth = pArray->getWidth();
cArray1D row(newWidth);
for (int i=0; i < newHeight; i++)
{
for (int j = 0; j < newWidth; j++)
row.setAt(j, pArray->getAt(i, j));
FFT::Forward(&row);
for (int j = 0; j < newWidth; j++)
pArray->setAt(i, j, row.getAt(j));
}
cArray1D column(newHeight);
for (int j=0; j < newWidth; j++)
{
for (int i = 0; i < newHeight; i++)
column.setAt(i, pArray->getAt(i,j));
FFT::Forward(&column);
for (int i = 0; i < newHeight; i++)
pArray->setAt(i, j, column.getAt(i));
}
}
void ImageOperations::cPowerSpectrumToRealPart(cArray2D *pArray)
{
int newHeight = pArray->getHeight();
int newWidth = pArray->getWidth();
for (int i = 0; i < newHeight; i++)
for (int j = 0; j < newWidth; j++)
{
double value = pArray->getAt(i, j).power();
pArray->setAt(i, j, value);
}
}
void ImageOperations::cScaleRealPartLogarithmically(cArray2D *pArray, double min, double max)
{
int newHeight = pArray->getHeight();
int newWidth = pArray->getWidth();
double A = (double)255 / log(max - min + 1);
double B = min - 1;
for (int i = 0; i < newHeight; i++)
for (int j =0 ; j < newWidth; j++)
{
double rezultat = floor( A * log( pArray->getAt(i,j).re() - B));
pArray->setAt(i, j, rezultat);
}
}
void ImageOperations::cSwapBlocks(cArray2D *pArray)
{
int newHeight = pArray->getHeight();
int newWidth = pArray->getWidth();
complex temp;
for (int i =0 ; i < newHeight/2; i++)
{
for (int j = 0; j < newWidth/2; j++)
{
temp = pArray->getAt(i, j);
pArray->setAt(i, j, pArray->getAt(i + newHeight/2, j + newWidth/2));
pArray->setAt(i + newHeight/2, j + newWidth/2, temp);
}
}
for (int i = newHeight/2; i < newHeight; i++)
{
for (int j = 0; j < newWidth/2; j++)
{
temp = pArray->getAt(i, j);
pArray->setAt(i, j, pArray->getAt(i - newHeight/2, j + newWidth/2));
pArray->setAt(i - newHeight/2, j + newWidth/2, temp);
}
}
}
QImage ImageOperations::cRealPartToImage(cArray2D *pArray)
{
int newHeight = pArray->getHeight();
int newWidth = pArray->getWidth();
//QImage::QImage ( int width, int height, Format format )
QImage newImage(newWidth, newHeight, QImage::Format_RGB32);
for (int i = 0; i < newHeight; i++)
{
QRgb *line = (QRgb*)newImage.scanLine(i);
for (int j = 0; j < newWidth; j++)
{
int col = (int)pArray->getAt(i,j).re();
//QColor ( int r, int g, int b, int a = 255 )
QColor color( col, col, col, 255 );
//QRgb QColor::rgba () const
line[j] = color.rgb();
}
}
return newImage;
}
int ImageOperations::nextPowerOfTwo(int currentDimension)
{
double potenca = (int)ceil( log((double)currentDimension)/log((double)2) );
return (int)pow( 2, potenca );
}
bool ImageOperations::isAPowerOfTwo(unsigned int x)
{
return ((x != 0) && ((x & (~x + 1)) == x));
}
int ImageOperations::firstPowerOfTwoOfLargerDimension(int width, int height)
{
int newHeightAndWidth = 0;
if (height > width)
{
if (!isAPowerOfTwo(height))
height = nextPowerOfTwo( height );
newHeightAndWidth = height;
}
else
{
if (!isAPowerOfTwo(width))
width = nextPowerOfTwo( width );
newHeightAndWidth = width;
}
return newHeightAndWidth;
}
void ImageOperations::cDebugToFile(QString fileName, cArray2D *pArray, int height, int width)
{
if (DEBUG)
{
QString temp;
fileName += " " + temp.setNum( height, 10 ) + "x" + temp.setNum( width, 10 ) + ".txt";
QFile file( fileName );
file.open(QIODevice::ReadWrite);
QString re;
QString im;
QString msg;
for (int i=0; i < height; i++)
{
//QString & QString::setNum ( int n, int base = 10 )
temp.setNum( i, 10 );
temp = "Row: " + temp + "\n";
file.write(temp.toAscii(), temp.length());
for (int j=0; j < width; j++)
{
//QString & QString::setNum ( double n, char format = 'g', int precision = 6 )
re.setNum( pArray->getAt(i,j).re(), 'g', 6);
im.setNum( pArray->getAt(i,j).im(), 'g', 6);
msg = "(" + re + ", " + im + ")\n";
file.write(msg.toAscii(), msg.length());
}
file.write("\n", qstrlen("\n"));
}
file.close();
}
}
QImage ImageOperations::FFTConvolution(QImage mask)
{
int height = image.height();
int width = image.width();
int maskHeight = mask.height();
int maskWidth = mask.width();
if (height == 0 || width == 0 || maskHeight == 0 || maskWidth == 0)
return QImage();
int newHeightAndWidth = firstPowerOfTwoOfLargerDimension(width, height);
if (newHeightAndWidth != maskHeight || newHeightAndWidth != maskWidth)
return QImage();
//Load image
cArray2D array2DRed(newHeightAndWidth, newHeightAndWidth);
cArray2D array2DGreen(newHeightAndWidth, newHeightAndWidth);
cArray2D array2DBlue(newHeightAndWidth, newHeightAndWidth);
int meanRed = getMeanValue(Red);
array2DRed.fill(complex(meanRed,0));
int meanGreen = getMeanValue(Green);
array2DGreen.fill(complex(meanGreen,0));
int meanBlue = getMeanValue(Blue);
array2DBlue.fill(complex(meanBlue,0));
cImageToArray2D(&array2DRed, Red);
cImageToArray2D(&array2DGreen, Green);
cImageToArray2D(&array2DBlue, Blue);
//Load mask
cArray2D array2DMaskRed(newHeightAndWidth, newHeightAndWidth);
cArray2D array2DMaskGreen(newHeightAndWidth, newHeightAndWidth);
cArray2D array2DMaskBlue(newHeightAndWidth, newHeightAndWidth);
cMaskToArray2D(&array2DMaskRed, mask, Red);
cMaskToArray2D(&array2DMaskGreen, mask, Green);
cMaskToArray2D(&array2DMaskBlue, mask, Blue);
//FFT on image
cExecuteFFTOnArray2D(&array2DRed);
cExecuteFFTOnArray2D(&array2DGreen);
cExecuteFFTOnArray2D(&array2DBlue);
cSwapBlocks(&array2DRed);
cSwapBlocks(&array2DGreen);
cSwapBlocks(&array2DBlue);
cPointwiseProduct(&array2DRed, &array2DMaskRed);
cPointwiseProduct(&array2DGreen, &array2DMaskGreen);
cPointwiseProduct(&array2DBlue, &array2DMaskBlue);
cSwapBlocks(&array2DRed);
cSwapBlocks(&array2DGreen);
cSwapBlocks(&array2DBlue);
cExecuteIFFTOnArray2D(&array2DRed);
cExecuteIFFTOnArray2D(&array2DGreen);
cExecuteIFFTOnArray2D(&array2DBlue);
/*Linear scale
double minRed = (array2DRed.findMin()).re();
double maxRed = (array2DRed.findMax()).re();
double minGreen = (array2DGreen.findMin()).re();
double maxGreen = (array2DGreen.findMax()).re();
double minBlue = (array2DBlue.findMin()).re();
double maxBlue = (array2DBlue.findMax()).re();
cScaleRealPartLinearly(&array2DRed, minRed, maxRed);
cScaleRealPartLinearly(&array2DGreen, minGreen, maxGreen);
cScaleRealPartLinearly(&array2DBlue, minBlue, maxBlue);
*/
cRealIntensityCrop(&array2DRed);
cRealIntensityCrop(&array2DGreen);
cRealIntensityCrop(&array2DBlue);
QImage newImage = cRealPartToImageWithCrop(&array2DRed, &array2DGreen, &array2DBlue);
return newImage;
}
void ImageOperations::cRealIntensityCrop(cArray2D *pArray)
{
for (int i = 0; i < pArray->getHeight(); i++)
for (int j = 0; j < pArray->getWidth(); j++)
if (pArray->getAt(i,j).re() > (double)255)
pArray->setAt(i, j, (double)255);
else if (pArray->getAt(i,j).re() < (double)0)
pArray->setAt(i, j, (double)0);
}
QImage ImageOperations::cRealPartToImageWithCrop(cArray2D *pArrayRed, cArray2D *pArrayGreen, cArray2D *pArrayBlue)
{
int newHeight = pArrayRed->getHeight();
int newWidth = pArrayRed->getWidth();
//QImage::QImage ( int width, int height, Format format )
QImage newImage(image.width(), image.height(), QImage::Format_RGB32);
for (int i = 0; i < image.height(); i++)
{
QRgb *line = (QRgb*)newImage.scanLine(i);
for (int j = 0; j < image.width(); j++)
{
int red = (int)pArrayRed->getAt(i + (newHeight - image.height())/2, j + (newWidth - image.width())/2).re();
int green = (int)pArrayGreen->getAt(i + (newHeight - image.height())/2, j + (newWidth - image.width())/2).re();
int blue = (int)pArrayBlue->getAt(i + (newHeight - image.height())/2, j + (newWidth - image.width())/2).re();
//QColor ( int r, int g, int b, int a = 255 )
QColor color( red, green, blue, 255 );
//QRgb QColor::rgba () const
line[j] = color.rgb();
}
}
return newImage;
}
void ImageOperations::cMaskToArray2D(cArray2D *pArray, QImage mask, Channels forColor)
{
if (mask.format() != QImage::Format_ARGB32)
mask = mask.convertToFormat(QImage::Format_ARGB32);
for (int i=0; i < mask.height(); i++)
{
QRgb *line = (QRgb*)mask.scanLine(i);
for (int j=0; j < mask.width(); j++)
{
pArray->setAt(i, j, getIntensity(line[j], forColor) / (double)255);
}
}
}
void ImageOperations::cPointwiseProduct(cArray2D *pArrayA, cArray2D *pArrayB)
{
for (int i = 0; i < pArrayA->getHeight(); i++)
for (int j = 0; j < pArrayA->getWidth(); j++)
{
complex a = pArrayA->getAt(i,j);
complex b = pArrayB->getAt(i,j);
pArrayA->setAt(i,j, a * b);
}
}
void ImageOperations::cExecuteIFFTOnArray2D(cArray2D *pArray)
{
int newHeight = pArray->getHeight();
int newWidth = pArray->getWidth();
cArray1D row(newWidth);
for (int i=0; i < newHeight; i++)
{
for (int j = 0; j < newWidth; j++)
row.setAt(j, pArray->getAt(i, j));
FFT::Inverse(&row);
for (int j = 0; j < newWidth; j++)
pArray->setAt(i, j, row.getAt(j));
}
cArray1D column(newHeight);
for (int j=0; j < newWidth; j++)
{
for (int i = 0; i < newHeight; i++)
column.setAt(i, pArray->getAt(i,j));
FFT::Inverse(&column);
for (int i = 0; i < newHeight; i++)
pArray->setAt(i, j, column.getAt(i));
}
}
void ImageOperations::cScaleRealPartLinearly(cArray2D *pArray, double min, double max)
{
int newHeight = pArray->getHeight();
int newWidth = pArray->getWidth();
double k = (double)255 / (max - min);
double n = (double)255 * min / (min - max);
for (int i = 0; i < newHeight; i++)
for (int j =0 ; j < newWidth; j++)
{
double rezultat = round( k * pArray->getAt(i,j).re() + n );
pArray->setAt(i, j, rezultat);
}
}
double ImageOperations::round(double x)
{
double fl = floor(x);
double decimals = x - fl;
if(decimals >= 0.5)
return ceil(x);
else
return floor(x);
}
QImage ImageOperations::DCTPowerSpectrum(Channels forColor)
{
dArray2D array2D = DCTPowerSpectrumArray(forColor);
if (array2D.getHeight() == 0 || array2D.getWidth() == 0)
return QImage();
dPowerSpectrum(&array2D);
QImage newImage = DCTPowerSpectrumQImage(array2D);
return newImage;
}
dArray2D ImageOperations::DCTPowerSpectrumArray(Channels forColor)
{
int height = image.height();
int width = image.width();
if (height == 0 || width == 0)
return dArray2D(0,0);
int newHeightAndWidth = firstPowerOfTwoOfLargerDimension(width, height);
dArray2D array2D(newHeightAndWidth, newHeightAndWidth);
int mean = getMeanValue(forColor);
array2D.fill(mean);
dImageToArray2D(&array2D, forColor);
//dDebugToFile("D_Image.txt", &array2D, newHeight, newWidth);
dExecuteDCTOnArray2D(&array2D);
//dDebugToFile("D_DCT.txt", &array2D, newHeightAndWidth, newHeightAndWidth);
return array2D;
}
QImage ImageOperations::DCTPowerSpectrumQImage(dArray2D array2D)
{
double min = array2D.findMin();
double max = array2D.findMax();
//printf("min: %f, max: %f\n", min, max);
dScaleLogarithmically(&array2D, min, max);
//dDebugToFile("D_Scaled.txt", &array2D, newHeight, newWidth);
QImage newImage = dArrayToImage(&array2D);
return newImage;
}
QImage ImageOperations::DCTConvolution(QImage mask)
{
int height = image.height();
int width = image.width();
int maskHeight = mask.height();
int maskWidth = mask.width();
if (height == 0 || width == 0 || maskHeight == 0 || maskWidth == 0)
return QImage();
int newHeightAndWidth = firstPowerOfTwoOfLargerDimension(width, height);
if (newHeightAndWidth != maskHeight || newHeightAndWidth != maskWidth)
return QImage();
//Load image
dArray2D array2DRed(newHeightAndWidth, newHeightAndWidth);
dArray2D array2DGreen(newHeightAndWidth, newHeightAndWidth);
dArray2D array2DBlue(newHeightAndWidth, newHeightAndWidth);
int meanRed = getMeanValue(Red);
array2DRed.fill(meanRed);
int meanGreen = getMeanValue(Green);
array2DGreen.fill(meanGreen);
int meanBlue = getMeanValue(Blue);
array2DBlue.fill(meanBlue);
dImageToArray2D(&array2DRed, Red);
dImageToArray2D(&array2DGreen, Green);
dImageToArray2D(&array2DBlue, Blue);
//Load mask
dArray2D array2DMaskRed(newHeightAndWidth, newHeightAndWidth);
dArray2D array2DMaskGreen(newHeightAndWidth, newHeightAndWidth);
dArray2D array2DMaskBlue(newHeightAndWidth, newHeightAndWidth);
dMaskToArray2D(&array2DMaskRed, mask, Red);
dMaskToArray2D(&array2DMaskGreen, mask, Green);
dMaskToArray2D(&array2DMaskBlue, mask, Blue);
//DCT on image
dExecuteDCTOnArray2D(&array2DRed);
dExecuteDCTOnArray2D(&array2DGreen);
dExecuteDCTOnArray2D(&array2DBlue);
//Pointwise product
dPointwiseProduct(&array2DRed, &array2DMaskRed);
dPointwiseProduct(&array2DGreen, &array2DMaskGreen);
dPointwiseProduct(&array2DBlue, &array2DMaskBlue);
//Swap blocks?
//IDCT on image
dExecuteIDCTOnArray2D(&array2DRed);
dExecuteIDCTOnArray2D(&array2DGreen);
dExecuteIDCTOnArray2D(&array2DBlue);
dIntensityCrop(&array2DRed);
dIntensityCrop(&array2DGreen);
dIntensityCrop(&array2DBlue);
QImage newImage = dImageWithCrop(&array2DRed, &array2DGreen, &array2DBlue);
return newImage;
}
void ImageOperations::dImageToArray2D(dArray2D *pArray, Channels forColor)
{
/*
//Repeat image
if (image.format() != QImage::Format_ARGB32)
image = image.convertToFormat(QImage::Format_ARGB32);
int height = image.height();
int width = image.width();
int newHeight = pArray->getHeight();
int newWidth = pArray->getWidth();
for (int i=0; i < height; i++)
{
QRgb *line = (QRgb*)image.scanLine(i);
for (int j=0; j < width; j++)
{
pArray->setAt(i, j, getIntensity(line[j], forColor));
if (j + width < newWidth)
{
pArray->setAt(i, j+width, getIntensity(line[j], forColor));
}
if (i + height < newHeight)
{
pArray->setAt(i+height, j, getIntensity(line[j], forColor));
}
if (j + width < newWidth && i + height < newHeight)
{
pArray->setAt(i+height, j+width, getIntensity(line[j], forColor));
}
}
}
*/
//Image in the middle
if (image.format() != QImage::Format_ARGB32)
image = image.convertToFormat(QImage::Format_ARGB32);
int newHeight = pArray->getHeight();
int newWidth = pArray->getWidth();
int positionY = 0;
int positionX = 0;
for (int i=0; i < image.height(); i++)
{
QRgb *line = (QRgb*)image.scanLine(i);
for (int j=0; j < image.width(); j++)
{
positionY = i + (newHeight - image.height()) / 2;
positionX = j + (newWidth - image.width())/2;
pArray->setAt(positionY, positionX, getIntensity(line[j], forColor));
}
}
}
void ImageOperations::dExecuteDCTOnArray2D(dArray2D *pArray)
{
int newHeight = pArray->getHeight();
int newWidth = pArray->getWidth();
dArray1D row(newWidth);
for (int i=0; i < newHeight; i++)
{
for (int j = 0; j < newWidth; j++)
row.setAt(j, pArray->getAt(i, j));
DCT::Forward(&row);
for (int j = 0; j < newWidth; j++)
pArray->setAt(i, j, row.getAt(j));
}
dArray1D column(newHeight);
for (int j=0; j < newWidth; j++)
{
for (int i = 0; i < newHeight; i++)
column.setAt(i, pArray->getAt(i,j));
DCT::Forward(&column);
for (int i = 0; i < newHeight; i++)
pArray->setAt(i, j, column.getAt(i));
}
}
void ImageOperations::dExecuteIDCTOnArray2D(dArray2D *pArray)
{
int newHeight = pArray->getHeight();
int newWidth = pArray->getWidth();
dArray1D row(newWidth);
for (int i=0; i < newHeight; i++)
{
for (int j = 0; j < newWidth; j++)
row.setAt(j, pArray->getAt(i, j));
DCT::Inverse(&row);
for (int j = 0; j < newWidth; j++)
pArray->setAt(i, j, row.getAt(j));
}
dArray1D column(newHeight);
for (int j=0; j < newWidth; j++)
{
for (int i = 0; i < newHeight; i++)
column.setAt(i, pArray->getAt(i,j));
DCT::Inverse(&column);
for (int i = 0; i < newHeight; i++)
pArray->setAt(i, j, column.getAt(i));
}
}
void ImageOperations::dPowerSpectrum(dArray2D *pArray)
{
int newHeight = pArray->getHeight();
int newWidth = pArray->getWidth();
for (int i = 0; i < newHeight; i++)
for (int j = 0; j < newWidth; j++)
{
double value = pow(pArray->getAt(i, j), 2);
pArray->setAt(i, j, value);
}
}
void ImageOperations::dScaleLogarithmically(dArray2D *pArray, double min, double max)
{
int newHeight = pArray->getHeight();
int newWidth = pArray->getWidth();
double A = (double)255 / log(max - min + 1);
double B = min - 1;
for (int i = 0; i < newHeight; i++)
for (int j =0 ; j < newWidth; j++)
{
double rezultat = floor( A * log( pArray->getAt(i,j) - B));
pArray->setAt(i, j, rezultat);
}
}
QImage ImageOperations::dArrayToImage(dArray2D *pArray)
{
int newHeight = pArray->getHeight();
int newWidth = pArray->getWidth();
QImage newImage(newWidth, newHeight, QImage::Format_RGB32);
for (int i = 0; i < newHeight; i++)
{
QRgb *line = (QRgb*)newImage.scanLine(i);
for (int j = 0; j < newWidth; j++)
{
int col = (int)pArray->getAt(i,j);
QColor color( col, col, col, 255 );
line[j] = color.rgb();
}
}
return newImage;
}
void ImageOperations::dSwapBlocks(dArray2D *pArray)
{
int newHeight = pArray->getHeight();
int newWidth = pArray->getWidth();
double temp;
for (int i =0 ; i < newHeight/2; i++)
{
for (int j = 0; j < newWidth/2; j++)
{
temp = pArray->getAt(i, j);
pArray->setAt(i, j, pArray->getAt(i + newHeight/2, j + newWidth/2));
pArray->setAt(i + newHeight/2, j + newWidth/2, temp);
}
}
for (int i = newHeight/2; i < newHeight; i++)
{
for (int j = 0; j < newWidth/2; j++)
{
temp = pArray->getAt(i, j);
pArray->setAt(i, j, pArray->getAt(i - newHeight/2, j + newWidth/2));
pArray->setAt(i - newHeight/2, j + newWidth/2, temp);
}
}
}
void ImageOperations::dIntensityRound(dArray2D *pArray)
{
for (int i = 0; i < pArray->getHeight(); i++)
for (int j = 0; j < pArray->getWidth(); j++)
pArray->setAt(i,j, round(pArray->getAt(i,j)));
}
void ImageOperations::dIntensityCrop(dArray2D *pArray)
{
for (int i = 0; i < pArray->getHeight(); i++)
for (int j = 0; j < pArray->getWidth(); j++)
if (pArray->getAt(i,j) > (double)255)
pArray->setAt(i, j, (double)255);
else if (pArray->getAt(i,j) < (double)0)
pArray->setAt(i, j, (double)0);
}
QImage ImageOperations::dImageWithCrop(dArray2D *pArrayRed, dArray2D *pArrayGreen, dArray2D *pArrayBlue)
{
int newHeight = pArrayRed->getHeight();
int newWidth = pArrayRed->getWidth();
QImage newImage(image.width(), image.height(), QImage::Format_RGB32);
for (int i = 0; i < image.height(); i++)
{
QRgb *line = (QRgb*)newImage.scanLine(i);
for (int j = 0; j < image.width(); j++)
{
int red = (int)pArrayRed->getAt(i + (newHeight - image.height())/2, j + (newWidth - image.width())/2);
int green = (int)pArrayGreen->getAt(i + (newHeight - image.height())/2, j + (newWidth - image.width())/2);
int blue = (int)pArrayBlue->getAt(i + (newHeight - image.height())/2, j + (newWidth - image.width())/2);
QColor color( red, green, blue, 255 );
line[j] = color.rgb();
}
}
return newImage;
}
void ImageOperations::dMaskToArray2D(dArray2D *pArray, QImage mask, Channels forColor)
{
if (mask.format() != QImage::Format_ARGB32)
mask = mask.convertToFormat(QImage::Format_ARGB32);
for (int i=0; i < mask.height(); i++)
{
QRgb *line = (QRgb*)mask.scanLine(i);
for (int j=0; j < mask.width(); j++)
{
pArray->setAt(i, j, getIntensity(line[j], forColor) / (double)255);
}
}
}
void ImageOperations::dPointwiseProduct(dArray2D *pArrayA, dArray2D *pArrayB)
{
for (int i = 0; i < pArrayA->getHeight(); i++)
for (int j = 0; j < pArrayA->getWidth(); j++)
{
double a = pArrayA->getAt(i,j);
double b = pArrayB->getAt(i,j);
pArrayA->setAt(i,j, a * b);
}
}
void ImageOperations::dDebugToFile(QString fileName, dArray2D *pArray, int height, int width)
{
if (DEBUG)
{
QString temp;
fileName += " " + temp.setNum( height, 10 ) + "x" + temp.setNum( width, 10 ) + ".txt";
QFile file( fileName );
file.open(QIODevice::ReadWrite);
QString re;
QString msg;
for (int i=0; i < height; i++)
{
for (int j=0; j < width; j++)
{
re.setNum( pArray->getAt(i,j), 'g', 6);
msg = re + "\t";
file.write(msg.toAscii(), msg.length());
}
file.write("\n", qstrlen("\n"));
}
file.close();
}
}