This repository has been archived by the owner on Aug 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
dft_homomorphic.cc
492 lines (356 loc) · 15.5 KB
/
dft_homomorphic.cc
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
// Example : apply homomorphic filtering to input image/video
// usage: prog {<image_name> | <video_name>}
// Author : Toby Breckon, [email protected]
// Copyright (c) 2009 School of Engineering, Cranfield University
// License : LGPL - http://www.gnu.org/licenses/lgpl.html
// portions based on OpenCV library example dft.c
#include "cv.h" // open cv general include file
#include "highgui.h" // open cv GUI include file
#include <stdio.h>
#include <algorithm> // contains max() function (amongst others)
using namespace cv; // use c++ namespace so the timing stuff works consistently
using namespace std;
/******************************************************************************/
// Rearrange the quadrants of Fourier image so that the origin is at
// the image center
// src & dst arrays of equal size & type
void cvShiftDFT(CvArr * src_arr, CvArr * dst_arr );
/******************************************************************************/
// setup the camera index / file capture properly based on OS platform
// 0 in linux gives first camera for v4l
//-1 in windows gives first device or user dialog selection
#ifdef linux
#define CAMERA_INDEX 0
#define VIDEOCAPTURE cvCaptureFromFile
#else
#define CAMERA_INDEX -1
#define VIDEOCAPTURE cvCaptureFromAVI
#endif
/******************************************************************************/
// return a floating point spectrum magnitude image scaled for user viewing
// dft_A - input dft (2 channel floating point, Real + Imaginary fourier image)
// rearrange - perform rearrangement of DFT quadrants if > 0
// return value - pointer to output spectrum magnitude image scaled for user viewing
IplImage* create_spectrum_magnitude_display(CvMat* dft_A, int rearrange);
/******************************************************************************/
// create a 2-channel butterworth-based homomorphic filter with radius D, order
// n and homomorphic upper and lower as specified
void create_butterworth_homomorphic_filter(CvMat* dft_Filter,
int D, int n, float upper, float lower)
{
CvMat* single = cvCreateMat(dft_Filter->rows, dft_Filter->cols, CV_64FC1 );
CvPoint centre = cvPoint(dft_Filter->rows / 2, dft_Filter->cols / 2);
double radius;
// create essentially create a butterworth highpass filter
// with additional scaling and offset
// details based on Gonzalez/Woods 3rd Edition, p293 w/ guidance from
// MATLAB implementation on MATLAB File Exchange
for(int i = 0; i < dft_Filter->rows; i++)
{
for(int j = 0; j < dft_Filter->cols; j++)
{
radius = (double) sqrt(pow((i - centre.x), 2.0) + pow((double) (j - centre.y), 2.0));
CV_MAT_ELEM(*single, double, i, j) =
((upper - lower) * ( 1 / (1 + pow((double) (D / radius), (double) (2 * n))))) + lower;
}
}
// N.B. perhaps need to check that all elements of single are within a reasonable range
// - NOT DONE
cvMerge(single, single, NULL, NULL, dft_Filter);
cvReleaseMat(&single);
}
/******************************************************************************/
int main( int argc, char** argv )
{
IplImage* img = NULL; // image object
CvCapture* capture = NULL; // capture object
IplImage* dft_spec_mag = NULL;
char const * originalName = "Original Image (grayscale)"; // window name
char const * homomorphicName = "Homomorphic Filtered (grayscale)"; // window name
char const * spectrumMagName = "Magnitude Image (log transformed)"; // window name
char const * filterName = "Filter Image"; // window name
bool keepProcessing = true; // loop control flag
char key; // user input
int EVENT_LOOP_DELAY = 40; // delay for GUI window
// 40 ms equates to 1000ms/25fps = 40ms per frame
int radiusD = 10; // radius of band pass filter parameter
int order = 2; // order of band pass filter parameter
int high_h_v_TB = 101; // trackbar control value upper
int low_h_v_TB = 99; // trackbar control value lower
bool doHistEq = false; // flag for hist. eq. output
// if command line arguments are provided try to read image/video_name
// otherwise default to capture from attached H/W camera
if(
( argc == 2 && (img = cvLoadImage( argv[1], CV_LOAD_IMAGE_UNCHANGED)) != 0 ) ||
( argc == 2 && (capture = VIDEOCAPTURE( argv[1] )) != 0 ) ||
( argc != 2 && (capture = cvCreateCameraCapture( 0 )) != 0 )
)
{
// create window objects (use flag=0 to allow resize, 1 to auto fix size)
cvNamedWindow(originalName, 0);
cvNamedWindow(homomorphicName, 0);
cvNamedWindow(spectrumMagName, 0);
cvNamedWindow(filterName, 0);
// define required floating point images for DFT processing
// (if using a capture object we need to get a frame first to get the size)
if (capture) {
// cvQueryFrame s just a combination of cvGrabFrame
// and cvRetrieveFrame in one call.
img = cvQueryFrame(capture);
if(!img){
if (argc == 2){
printf("End of video file reached\n");
} else {
printf("ERROR: cannot get next fram from camera\n");
}
exit(0);
}
}
// do setup for required DFT images and arrays
IplImage* realInput = cvCreateImage( cvGetSize(img), IPL_DEPTH_64F, 1);
IplImage* imaginaryInput = cvCreateImage( cvGetSize(img), IPL_DEPTH_64F, 1);
IplImage* complexInput = cvCreateImage( cvGetSize(img), IPL_DEPTH_64F, 2);
int dft_M = cvGetOptimalDFTSize( img->height - 1 );
int dft_N = cvGetOptimalDFTSize( img->width - 1 );
printf("Optimal size for DFT is height = %i, width = %i\n", dft_M, dft_N);
CvMat* dft_A = cvCreateMat( dft_M, dft_N, CV_64FC2 );
CvMat* dft_Filter = cvCreateMat( dft_M, dft_N, CV_64FC2 );
CvMat tmp;
double m, M;
IplImage* image_Re = cvCreateImage( cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
IplImage* image_Im = cvCreateImage( cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
IplImage* image_Re_Filter = cvCreateImage( cvGetSize(dft_Filter), IPL_DEPTH_64F, 1);
IplImage* outputImg = cvCreateImage( cvSize(dft_N, dft_M), img->depth, 1);
// add adjustable trackbar for low pass filter threshold parameter
cvCreateTrackbar("Radius D", homomorphicName, &radiusD, (min(dft_M, dft_N) / 2), NULL);
cvCreateTrackbar("upper H (* 0.01)", homomorphicName, &high_h_v_TB, 200, NULL);
cvCreateTrackbar("lower H (*0.01)", homomorphicName, &low_h_v_TB, 100, NULL);
cvCreateTrackbar("Order n", homomorphicName, &order, 10, NULL);
// define grayscale image
IplImage* grayImg =
cvCreateImage(cvSize(img->width,img->height), img->depth, 1);
grayImg->origin = img->origin;
// start main loop
while (keepProcessing) {
int64 timeStart = getTickCount(); // get time at start of loop
// if capture object in use (i.e. video/camera)
// get image from capture object
if (capture) {
// cvQueryFrame s just a combination of cvGrabFrame
// and cvRetrieveFrame in one call.
img = cvQueryFrame(capture);
if(!img){
if (argc == 2){
printf("End of video file reached\n");
} else {
printf("ERROR: cannot get next fram from camera\n");
}
exit(0);
}
} //else {
// THIS SECTION IS COMMENTED OUT TO ALLOW FOR INTERACTIVE
// PARAMETER CHANGES ON A SINGLE IMAGE
// if not a capture object set event delay to zero so it waits
// indefinitely (as single image file, no need to loop)
// EVENT_LOOP_DELAY = 0;
//}
// *** Fourier processing
// if input is not already grayscale, convert to grayscale
if (img->nChannels > 1){
cvCvtColor(img, grayImg, CV_BGR2GRAY);
} else {
grayImg = img;
}
// convert grayscale image to real part of DTF input
cvScale(grayImg, realInput, 1.0, 0.0);
// take the natural log of the input (compute log(1 + Mag)
cvAddS( realInput, cvScalarAll(1.0), realInput, NULL ); // 1 + Mag
cvLog( realInput, realInput ); // log(1 + Mag)
// merge with imaginary part (initialised to all zeros)
cvZero(imaginaryInput);
cvMerge(realInput, imaginaryInput, NULL, NULL, complexInput);
// copy A to dft_A and pad dft_A with zeros
cvGetSubRect( dft_A, &tmp, cvRect(0,0, grayImg->width,grayImg->height));
cvCopy( complexInput, &tmp, NULL );
cvGetSubRect( dft_A, &tmp, cvRect(img->width,0, dft_A->cols - grayImg->width, grayImg->height));
if ((dft_A->cols - grayImg->width) > 0)
{
cvZero( &tmp );
}
// set up filter (first channel is real part / second is imaginary)
create_butterworth_homomorphic_filter(dft_Filter, radiusD, order,
(high_h_v_TB * 0.01), (low_h_v_TB * 0.01));
// copy and scale for display
cvSplit(dft_Filter, image_Re_Filter, NULL, NULL, NULL);
cvMinMaxLoc(image_Re_Filter, &m, &M, NULL, NULL, NULL);
cvScale(image_Re_Filter, image_Re_Filter, 1.0/(M-m), 1.0*(-m)/(M-m));
// get the DFT of the original image (scaled) (and shirt quadrants)
//cvDFT( dft_A, dft_A, CV_DXT_FORWARD, complexInput->height );
cvDFT( dft_A, dft_A, (CV_DXT_FORWARD | CV_DXT_SCALE), complexInput->height );
cvShiftDFT( dft_A, dft_A );
// apply filter (and shift quadrants back)
cvMulSpectrums( dft_A, dft_Filter, dft_A, 0);
cvShiftDFT( dft_A, dft_A );
// compute spectrum magnitude for display
if (dft_spec_mag != NULL){
cvReleaseImage(&dft_spec_mag);
}
dft_spec_mag = create_spectrum_magnitude_display(dft_A, 1);
// invert dft
cvDFT( dft_A, dft_A, CV_DXT_INV_SCALE, complexInput->height );
//cvDFT( dft_A, dft_A, CV_DXT_INVERSE, complexInput->height );
// take the ABS of result (as per MATLAB implementation ??)
// (appears not to effect result - 29/5/09
// cvAbsDiffS(dft_A, dft_A, cvScalarAll(0));
// Split Fourier in real and imaginary parts
cvSplit( dft_A, image_Re, image_Im, 0, 0 );
// take the exp (inverse of natural log of the input)
cvExp( image_Re, image_Re );
cvMinMaxLoc(image_Re, &m, &M, NULL, NULL, NULL);
cvScale(image_Re, image_Re, 1.0/(M-m), 1.0*(-m)/(M-m));
// convert to 8-bit 255
cvMinMaxLoc(image_Re, &m, &M, NULL, NULL, NULL);
cvScale(image_Re, outputImg, 255.0 / (M-m), 1.0*(-m)/(M-m));
outputImg->origin = img->origin;
if (doHistEq){
cvEqualizeHist(outputImg, outputImg);
}
// display image in window
cvShowImage( originalName, grayImg );
cvShowImage( homomorphicName, outputImg ); // N.B. floating point image
cvShowImage( spectrumMagName, dft_spec_mag );
cvShowImage( filterName, image_Re_Filter );
// start event processing loop (very important,in fact essential for GUI)
// 40 ms roughly equates to 100ms/25fps = 40ms per frame
// here we take account of processing time for the loop by subtracting the time
// taken in ms. from this (1000ms/25fps = 40ms per frame) value whilst ensuring
// we get a +ve wait time
key = cvWaitKey((int) std::max(2.0, EVENT_LOOP_DELAY -
(((getTickCount() - timeStart) / getTickFrequency()) * 1000)));
if (key == 'x'){
// if user presses "x" then exit
printf("Keyboard exit requested : exiting now - bye!\n");
keepProcessing = false;
} else if (key == 's'){
cvSaveImage("homomorphic_output.bmp", outputImg);
} else if (key == 'e'){
// toggle hist. eq.
doHistEq = (!doHistEq);
}
}
// destroy window objects
// (triggered by event loop *only* window is closed)
cvDestroyAllWindows();
// destroy image object (if it does not originate from a capture object)
if (!capture){
if (grayImg == img) {grayImg = NULL;}
cvReleaseImage( &img );
}
// release other images
cvReleaseMat( &dft_A);
cvReleaseMat( &dft_Filter);
if (grayImg) {cvReleaseImage( &grayImg );}
cvReleaseImage( &realInput );
cvReleaseImage( &imaginaryInput );
cvReleaseImage( &complexInput );
cvReleaseImage( &image_Re );
cvReleaseImage( &image_Im );
cvReleaseImage( &dft_spec_mag );
cvReleaseImage(&image_Re_Filter);
cvReleaseImage(&outputImg);
// all OK : main returns 0
return 0;
}
// not OK : main returns -1
return -1;
}
/******************************************************************************/
// Rearrange the quadrants of Fourier image so that the origin is at
// the image center
// src & dst arrays of equal size & type
void cvShiftDFT(CvArr * src_arr, CvArr * dst_arr )
{
CvMat * tmp = NULL;
CvMat q1stub, q2stub;
CvMat q3stub, q4stub;
CvMat d1stub, d2stub;
CvMat d3stub, d4stub;
CvMat * q1, * q2, * q3, * q4;
CvMat * d1, * d2, * d3, * d4;
CvSize size = cvGetSize(src_arr);
CvSize dst_size = cvGetSize(dst_arr);
int cx, cy;
if(dst_size.width != size.width ||
dst_size.height != size.height){
cvError( CV_StsUnmatchedSizes,
"cvShiftDFT", "Source and Destination arrays must have equal sizes",
__FILE__, __LINE__ );
}
if(src_arr==dst_arr){
tmp = cvCreateMat(size.height/2, size.width/2, cvGetElemType(src_arr));
}
cx = size.width/2;
cy = size.height/2; // image center
q1 = cvGetSubRect( src_arr, &q1stub, cvRect(0,0,cx, cy) );
q2 = cvGetSubRect( src_arr, &q2stub, cvRect(cx,0,cx,cy) );
q3 = cvGetSubRect( src_arr, &q3stub, cvRect(cx,cy,cx,cy) );
q4 = cvGetSubRect( src_arr, &q4stub, cvRect(0,cy,cx,cy) );
d1 = cvGetSubRect( src_arr, &d1stub, cvRect(0,0,cx,cy) );
d2 = cvGetSubRect( src_arr, &d2stub, cvRect(cx,0,cx,cy) );
d3 = cvGetSubRect( src_arr, &d3stub, cvRect(cx,cy,cx,cy) );
d4 = cvGetSubRect( src_arr, &d4stub, cvRect(0,cy,cx,cy) );
if(src_arr!=dst_arr){
if( !CV_ARE_TYPES_EQ( q1, d1 )){
cvError( CV_StsUnmatchedFormats,
"cvShiftDFT", "Source and Destination arrays must have the same format",
__FILE__, __LINE__ );
}
cvCopy(q3, d1, 0);
cvCopy(q4, d2, 0);
cvCopy(q1, d3, 0);
cvCopy(q2, d4, 0);
}
else{
cvCopy(q3, tmp, 0);
cvCopy(q1, q3, 0);
cvCopy(tmp, q1, 0);
cvCopy(q4, tmp, 0);
cvCopy(q2, q4, 0);
cvCopy(tmp, q2, 0);
cvReleaseMat(&tmp);
}
}
/******************************************************************************/
// return a floating point spectrum magnitude image scaled for user viewing
// dft_A - input dft (2 channel floating point, Real + Imaginary fourier image)
// rearrange - perform rearrangement of DFT quadrants if > 0
// return value - pointer to output spectrum magnitude image scaled for user viewing
IplImage* create_spectrum_magnitude_display(CvMat* dft_A, int rearrange)
{
double m, M;
IplImage* image_Re = cvCreateImage( cvSize(dft_A->cols, dft_A->rows), IPL_DEPTH_64F, 1);
IplImage* image_Im = cvCreateImage( cvSize(dft_A->cols, dft_A->rows), IPL_DEPTH_64F, 1);
// Split Fourier in real and imaginary parts
cvSplit( dft_A, image_Re, image_Im, 0, 0 );
// Compute the magnitude of the spectrum Mag = sqrt(Re^2 + Im^2)
cvPow( image_Re, image_Re, 2.0);
cvPow( image_Im, image_Im, 2.0);
cvAdd( image_Re, image_Im, image_Re, NULL);
cvPow( image_Re, image_Re, 0.5 );
// Compute log(1 + Mag)
cvAddS( image_Re, cvScalarAll(1.0), image_Re, NULL ); // 1 + Mag
cvLog( image_Re, image_Re ); // log(1 + Mag)
// Rearrange the quadrants of Fourier image so that the origin is at
// the image center
if (rearrange){
cvShiftDFT( image_Re, image_Re );
}
// scale image for display
cvMinMaxLoc(image_Re, &m, &M, NULL, NULL, NULL);
cvScale(image_Re, image_Re, 1.0/(M-m), 1.0*(-m)/(M-m));
// release imaginary image part
cvReleaseImage(&image_Im);
// return DFT spectrum
return image_Re;
}
/******************************************************************************/