-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
668 lines (537 loc) · 18.7 KB
/
main.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
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <vector>
#include <cstring>
#include <map>
//#include <math.h>
#include "image.h"
#include "WriteImage.h"
#include "ReadImage.h"
#include "freeman_arg_parse.h" //A small utility I wrote for extracting command line args.
#define SWAP(a,b) tempr=(a);(a)=(b);(b)=tempr
#define PI 3.14159265
bool FLAG_DEBUG = true;
std::string output_path = "C:\\Users\\Adam\\source\\repos\\CS474-Project-3\\Q 3a\\images";
// std::string image_input_path = "../images/";
int ClampPxVal(int val, int lo, int hi);
void WriteImageToFile(std::string filename, ImageType& img);
void ReadImageFromFile(std::string filename, ImageType& img);
void generateTestImage(int size, double** arr, int innerSize);
int ProcessTestImages(int argc, char** argv);
void fft(double data[], unsigned long nn, int isign);
void normalizeArray(double arr[], int valCount, int size);
void DFT_WriteToCSV(double arr[], int SIZE, std::string filepath);
void fft2D(unsigned int N, unsigned int M, ImageType& i_real, ImageType& i_imag, int isign);
void computeMagnitude(unsigned int N, unsigned int M, ImageType& i_real, ImageType& i_imag, ImageType& i_mag);
void shiftToCenter(unsigned int N, unsigned int M, ImageType& image, int isign);
void stretchMagnitude(unsigned int N, unsigned int M, ImageType& i_mag);
void zeroPhase(unsigned int N, unsigned int M, ImageType& i_real, ImageType& i_imag, ImageType& i_mag);
// NOTES: This program does NOT check if images have power-of-two dimensions.
// fft() will only work under these conditions.
// Furthermore, as of 11/7/2021 this program has not been tested with N=/=M dimensioned images yet.
int main(int argc, char** argv)
{
//create test image. create empty imaginary image object (init vals to 0)
//Test using generated test image ////////////////////////////////////////////////
/*
const int TEST_SIZE = 512;
int whiteSquare = 256;
double** testImage;
testImage = new double* [TEST_SIZE];
for (int i = 0; i < TEST_SIZE; i++)
testImage[i] = new double[TEST_SIZE];
generateTestImage(TEST_SIZE, testImage, whiteSquare);
*/
ImageType image;
ReadImageFromFile(output_path + "\\lenna.pgm", image);
int N, M, Q;
double q;
image.getImageInfo(N, M, Q);
ImageType img_real(N, M, 255);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
image.getPixelVal(i, j, q);
img_real.setPixelVal(i, j, q);
}
}
ImageType img_imag;
img_imag.CopyImageData(img_real);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
img_imag.setPixelVal(i, j, 0);
}
}
// Magnitude
ImageType img_mag;
img_mag.CopyImageData(img_real);
WriteImageToFile(output_path + "\\lenna_raw.pgm", img_real);
//2d ffts:
//forward t
//shiftToCenter(TEST_SIZE, TEST_SIZE, img_real, img_imag, img_mag, 0); // 0 for image, 1 for FT
fft2D(N, M, img_real, img_imag, -1);
//WriteImageToFile(output_path + "\\test_image_real_frequency_domain.pgm", img_real);
computeMagnitude(N, M, img_real, img_imag, img_mag);
//stretchMagnitude(N, M, img_mag);
//WriteImageToFile(output_path + "\\lenna_mag_stretched.pgm", img_mag);
zeroPhase(N, M, img_real, img_imag, img_mag);
WriteImageToFile(output_path + "\\lenna_zeroPhase.pgm", img_real);
//shiftToCenter(N, M, img_mag, 1); // 0 for image, 1 for FT
//WriteImageToFile(output_path + "\\test_image_magnitude_shifted.pgm", img_mag);
//stretchMagnitude(N, M, img_mag);
//WriteImageToFile(output_path + "\\test_image_magnitude_stretched.pgm", img_mag);
//shiftToCenter(N, M, img_mag, 1); // undo centering
//backward t
fft2D(N, M, img_real, img_imag, 1);
WriteImageToFile(output_path + "\\lenna_fwd_bck_transformed.pgm", img_real);
// Test using input file: ////////////////////////////////////////////////
ProcessTestImages(argc, argv);
return 0;
}
//TODO: PROBABLY only works if N==M currently.
//Need to double check all ranges and uses of those vars to ensure they're correct
//Also the work array would need to be resized (SIZE == 2N + 1) for the columns fft
void fft2D(unsigned int N, unsigned int M, ImageType& i_real, ImageType& i_imag, int isign)
{
unsigned int SIZE = 2 * M + 1;
double* arr = new double[SIZE];
for (int k = 0; k < SIZE; k++) arr[k] = 0.0f;
//compute rows
for (int i = 0; i < N; i++)
{
// std::cout << i << std::endl;
//load values into work array
double* ptr_r = arr;
ptr_r += 1;
for (int j = 0, k = 1, l = 2; j < M; j++, k += 2, l += 2)
{
double real, imag;
i_real.getPixelVal(i, j, real);
i_imag.getPixelVal(i, j, imag);
// *ptr_r = (double)real;
// ptr_r += 2;
arr[k] = real;
arr[l] = imag;
// if(i==255) std::cout << real << " ";
// if(i == 255 && j==N-1)
// {
// for(int b=0; b<50; b++) {std::cout << arr[b] << " ";}
// printArrayReal(arr, SIZE);
// printArrayImag(arr, SIZE);
// }
}
//compute dft (row)
fft(arr, M, isign);
//copy values back into image row
for (int j = 0; j < M; j++)
{
double real, imag;
real = arr[2 * j + 1];
imag = arr[2 * j + 2];
if (isign < 0) real /= N;
if (isign < 0) imag /= N;
i_real.setPixelVal(i, j, real);
i_imag.setPixelVal(i, j, imag);
}
//clear work array
for (int k = 0; k < SIZE; k++) arr[k] = 0;
}
//compute columns
for (int j = 0; j < M; j++)
{
//load values into work array
for (int i = 0, k = 1, l = 2; i < N; i++, k += 2, l += 2)
{
double real, imag;
i_real.getPixelVal(i, j, real);
i_imag.getPixelVal(i, j, imag);
if (isign < 0) real /= N;
if (isign < 0) imag /= N;
arr[k] = real;
arr[l] = imag;
}
//compute dft (column)
fft(arr, N, isign);
// if(i==255) std::cout << real << " ";
// if(true)
// {
// printArrayReal(arr, SIZE);
// printArrayImag(arr, SIZE);
// std::cout << std::endl;
// }
//copy values back into image col
for (int i = 0; i < N; i++)
{
double real, imag;
real = arr[2 * i + 1];
imag = arr[2 * i + 2];
i_real.setPixelVal(i, j, real);
i_imag.setPixelVal(i, j, imag);
}
//clear work array
for (int k = 0; k < SIZE; k++) arr[k] = 0;
}
return;
//TODO: reenable
if (isign < 0)
{
for (int i = 0; i < N; i++)
{
//clear work array
for (int k = 0; k < SIZE; k++) arr[k] = 0;
//copy into work array
for (int j = 0; j < M; j++)
{
double real, imag;
real = arr[2 * j + 1];
imag = arr[2 * j + 2];
i_real.setPixelVal(i, j, real);
i_imag.setPixelVal(i, j, imag);
}
//normalize
normalizeArray(arr, N * M, SIZE);
//copy back into image storage
for (int j = 0; j < M; j++)
{
double real, imag;
real = arr[2 * j + 1];
imag = arr[2 * j + 2];
i_real.setPixelVal(i, j, real);
i_imag.setPixelVal(i, j, imag);
}
}
}
delete[] arr;
}
void generateTestImage(int size, double** arr, int innerSize)
{
int leftBound = ((size / 2) - (innerSize / 2));
int upperBound = ((size / 2) - (innerSize / 2));
int rightBound = ((size / 2) + (innerSize / 2)) - 1;
int lowerBound = ((size / 2) + (innerSize / 2)) - 1;
// DEBUG ///////////////////////////////////////////
// for(int i=0; i<size; i++)
// {
// for (int j = 0; j < size; j++)
// {
// arr[i][j] = 255;
// // std::cout << i << ", " << j << ", val: " ;
// // std::cout << arr[i][j] << " | ";
// }
// }
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if ((i >= upperBound && i <= lowerBound) && (j >= leftBound && j <= rightBound))
arr[i][j] = 255;
else
arr[i][j] = 0;
}
}
}
void DFT_WriteToCSV(double arr[], int SIZE, std::string filepath)
{
std::cout << "Writing DFT values as .csv files in directory: " << filepath << "\n";
std::ofstream os;
os.open(filepath + "\\DFT_Real.csv");
for (int i = 1; i < SIZE; i = i + 2)
os << arr[i] << "\n";
os.close();
os.open(filepath + "\\DFT_Imaginary.csv");
for (int i = 2; i < SIZE; i = i + 2)
os << arr[i] << "\n";
os.close();
os.open(filepath + "\\DFT_Magnitude.csv");
int i = 1;
while (i < SIZE)
{
os << sqrt(pow(arr[i], 2) + pow(arr[i + 1], 2));
// |F(u)| = sqrt (R(u)^2 + I(u)^2)
i += 2;
os << "\n";
}
os.close();
os.open(filepath + "\\DFT_Phase.csv");
i = 1;
while (i < SIZE)
{
os << atan2(arr[i], arr[i + 1]);
i += 2;
os << "\n";
}
os.close();
}
void printArrayReal(double arr[], int SIZE)
{
std::cout << "Array, real components: \n";
for (int i = 1; i < SIZE; i = i + 2)
std::cout << arr[i] << " ";
std::cout << "\n\n";
}
void printArrayImag(double arr[], int SIZE)
{
std::cout << "Array, imagninary components: \n";
for (int i = 2; i < SIZE; i = i + 2)
std::cout << arr[i] << " ";
std::cout << "\n\n";
}
void fft(double data[], unsigned long nn, int isign)
{
unsigned long n, mmax, m, j, istep, i;
double wtemp, wr, wpr, wpi, wi, theta;
double tempr, tempi;
n = nn << 1;
j = 1;
for (i = 1; i < n; i += 2) {
if (j > i) {
SWAP(data[j], data[i]);
SWAP(data[j + 1], data[i + 1]);
}
m = n >> 1;
while (m >= 2 && j > m) {
j -= m;
m >>= 1;
}
j += m;
}
mmax = 2;
while (n > mmax) {
istep = mmax << 1;
theta = isign * (6.28318530717959 / mmax);
wtemp = sin(0.5 * theta);
wpr = -2.0 * wtemp * wtemp;
wpi = sin(theta);
wr = 1.0;
wi = 0.0;
for (m = 1; m < mmax; m += 2) {
for (i = m; i <= n; i += istep) {
j = i + mmax;
tempr = wr * data[j] - wi * data[j + 1];
tempi = wr * data[j + 1] + wi * data[j];
data[j] = data[i] - tempr;
data[j + 1] = data[i + 1] - tempi;
data[i] += tempr;
data[i + 1] += tempi;
}
wr = (wtemp = wr) * wpr - wi * wpi + wr;
wi = wi * wpr + wtemp * wpi + wi;
}
mmax = istep;
}
}
void normalizeArray(double arr[], int valCount, int SIZE)
{
for (int i = 0; i < SIZE; i++)
arr[i] = arr[i] * 1 / valCount;
}
void printMagnitude(double arr[], int size)
{
int i = 1;
std::cout << "Magnitude of array: \n";
while (i < size)
{
std::cout << sqrt(pow(arr[i], 2) + pow(arr[i + 1], 2));
// |F(u)| = sqrt (R(u)^2 + I(u)^2)
i += 2;
std::cout << " ";
}
std::cout << "\n\n";
}
void WriteImageToFile(std::string filename, ImageType& img)
{
std::string out_file = filename;
char* cstr = new char[out_file.length() + 1];
strcpy(cstr, out_file.c_str());
std::writeImage(cstr, img);
std::cout << " * Saved image: " << out_file << "\n";
delete[] cstr;
}
void ReadImageFromFile(std::string filename, ImageType& img)
{
std::string in_file = filename;
char* cstr = new char[in_file.length() + 1];
strcpy(cstr, in_file.c_str());
std::readImage(cstr, img);
std::cout << " * Saved image: " << in_file << "\n";
delete[] cstr;
}
int ClampPxVal(int val, int lo, int hi)
{
if (val < lo) return lo;
else if (val > hi) return hi;
else return val;
}
int ProcessTestImages(int argc, char** argv)
{
//TODO: Print description of process to console (per program)
//Extract args into vector
std::vector<std::string> args;
for (int i = 1; i < argc; i++)
{
std::string next_element(argv[i]);
args.push_back(next_element);
}
//Fill data structures using args
std::vector<std::string> imagePaths = ExtractArgs("-in", args);
std::vector<std::string> outputPaths = ExtractArgs("-out", args);
if (outputPaths.size() > 0 && outputPaths[0].length() > 0)
{
if (outputPaths[0][outputPaths[0].length() - 1] != '/')
{
outputPaths[0] = outputPaths[0] + "/";
//std::cout << outputPaths[0] << "\n";
}
}
if (outputPaths.size() > 0)
{
std::cout << "Output paths specification is currently disabled for this program. Instead change the string \"output_path\" in main.cpp.\n";
}
//Process each image
for (int i = 0; i < imagePaths.size(); i++)
{
std::cout << "_________________________\n";
std::cout << "image " << i << ": \"" << imagePaths[i] << "\"\n";
ImageType next_image;
char* cstr = new char[imagePaths[i].length() + 1];
strcpy(cstr, imagePaths[i].c_str());
std::readImage(cstr, next_image);
// DO STUFF
int N, M, Q;
next_image.getImageInfo(N, M, Q);
ImageType img_imag;
img_imag.CopyImageData(next_image);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
img_imag.setPixelVal(i, j, 0);
}
}
// GENERATE OUTPUT FILENAME CONVENTION
//determine original file name from path string
std::string original_filename = "";
for (int l = imagePaths[i].length(); imagePaths[i][l] != '/' && l >= 0; l--)
{
if (imagePaths[i][l] != '/')
{
//std::cout << imagePaths[i][l] << std::endl;
std::string temp;
temp += imagePaths[i][l];
original_filename.insert(0, temp);
}
//throw out extension
if (imagePaths[i][l] == '.')
{
original_filename = "";
}
}
std::string out_file = output_path + "/" + original_filename;
WriteImageToFile(out_file + "_test_image_raw.pgm", next_image);
//2d ffts:
//forward t
fft2D(N, M, next_image, img_imag, -1);
WriteImageToFile(out_file + "_test_image_real_frequency_domain.pgm", next_image);
//backward t
fft2D(N, M, next_image, img_imag, 1);
WriteImageToFile(out_file + "_test_image_fwd_bck_transformed.pgm", next_image);
// END DO STUFF
delete[] cstr;
std::cout << "\n";
}
return 0;
}
void computeMagnitude(unsigned int N, unsigned int M, ImageType& i_real, ImageType& i_imag, ImageType& i_mag)
{ // PRE: 2D FFT has been computed with i_real and i_imag
// POST: Magnitude (sqrt of R^2 and I^2) is calculated and saved into i_mag
double mag, real, imag;
// Compute magnitude
for (int i = 0; i < M; i++)
{
for (int j = 0; j < M; j++)
{
i_real.getPixelVal(i, j, real);
i_imag.getPixelVal(i, j, imag);
mag = sqrt(pow(real, 2) + pow(imag, 2));
// F|u, v| = sqrt (R^2 (u, v) + I^2 (u, v))
i_mag.setPixelVal(i, j, mag);
}
}
}
void shiftToCenter(unsigned int N, unsigned int M, ImageType& image, int isign)
{ // PRE: 2D FFT has been computed with i_real and i_imag
// POST: Magnitude (sqrt of R^2 and I^2) is calculated and shifted to center of freq. domain (or inverse if applied twice)
if (isign == 0)
{ // f(x, y) * -1^(x + y)
double pixelVal, newPixelVal;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
image.getPixelVal(i, j, pixelVal);
newPixelVal = pixelVal * pow(-1, (i + j));
image.setPixelVal(i, j, newPixelVal);
}
}
}
else
{
// TODO: Get this branch to work. Indicies seem to be going out-of-bounds but clamping them does not seem to help.
// |F(u - N/2, v - N/2)|
ImageType temp;
temp.CopyImageData(image);
double mag;
int newRowIndex, newColIndex, horzFactor, vertFactor;
horzFactor = ((M / 2) % M) - 1;
vertFactor = (N / 2) - 1;
for (int u = 0; u < N; u++)
{
for (int v = 0; v < N; v++)
{
temp.getPixelVal(u, v, mag);
// Get new indices and check for out-of-bounds errors
//newIndexHorz = u - horzFactor;
newRowIndex = u - ((M / 2) % M);
newColIndex = v - ((N / 2) % N);
//newIndexHorz = ClampPxVal((u - horzFactor), 0, N);
//newIndexVert = ClampPxVal((v - vertFactor), 0, M);
//image.setPixelVal(newRowIndex, newColIndex, mag);
}
}
} //end if-else
}
void stretchMagnitude(unsigned int N, unsigned int M, ImageType& i_mag)
{
double mag, new_mag;
for (int u = 0; u < M; u++)
{
for (int v = 0; v < N; v++)
{
// |D(u, v)| = c * log (1 + |F(u, v)|), where c = 1
i_mag.getPixelVal(u, v, mag);
new_mag = log10(1 + mag);
i_mag.setPixelVal(u, v, new_mag);
}
}
i_mag.RemapPixelValues();
// re-scale |D(u, v)| to range [0, 255]
}
void zeroPhase(unsigned int N, unsigned int M, ImageType& i_real, ImageType& i_imag, ImageType& i_mag)
{
double Q;
for (int u = 0; u < N; u++)
{
for (int v = 0; v < M; v++)
{
i_mag.getPixelVal(u, v, Q);
i_real.setPixelVal(u, v, Q);
i_imag.setPixelVal(u, v, 0);
}
}
}
#undef SWAP