-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.h
More file actions
executable file
·1832 lines (1689 loc) · 54.1 KB
/
function.h
File metadata and controls
executable file
·1832 lines (1689 loc) · 54.1 KB
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
// function.h: interface for the function class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_FUNCTION_H__6E194843_FEB3_491F_8062_765AA3465CBC__INCLUDED_)
#define AFX_FUNCTION_H__6E194843_FEB3_491F_8062_765AA3465CBC__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "Dib.h"
#include <math.h>
// 常数π
#define pi 3.1415926535
#define WIDTHBYTES(bits) (((bits) + 31) / 32 * 4)
#include <direct.h>
#include <complex>
using namespace std;
#define PI 3.14159265358979323846
//#endif // !defined(AFX_FUNCTION_H__6E194843_FEB3_491F_8062_765AA3465CBC__INCLUDED_)
typedef struct{
int Value;
int Dist;
int AngleNumber;
} MaxValue;
/*************************************************************************
* \函数名称:
* GrayLinTrans()
* \输入参数:
* LPSTR lpDIBBits //指向源图像的像素指针
* LONG lmageWidth //源图像的宽度
* LONG lmageHeight // 源图像的高度
* int oralow // 原始图像的灰度值集中区域-低值
* int orahig // 原始图像的灰度值集中区域-高值
* int newlow // 变换后图像灰度的动态范围-低值
* int newhig // 变换后图像灰度的动态范围-高值
* \返回值:
* BOOL // 成功返回TRUE,否则返回FALSE。
* \说明:
* 该函数用来对图像指定的灰度分布进行灰度变换主要用来实现灰度分布的扩展
*************************************************************************/
BOOL GrayLinTrans(LPSTR lpDIBBits, LONG lmageWidth, LONG lmageHeight,int oralow, int orahig, int newlow, int newhig)
{
unsigned char* lpSrc; // 指向源图像的指针
int i; // 循环变量
int j; // 循环变量
BYTE byMap[256]; // 定义灰度映射表
for (i = 0; i <= oralow; i++) //当灰度级小于要增强的灰度级时
{
if (newlow > 0) //判断新灰度级是否大于0
{
byMap[i] = newlow; //赋予新值
}
else
{
byMap[i] = 0; //直接赋值为0
}
}
for (; i <= orahig; i++)
{
if (orahig != oralow) // 判断orahig是否等于oralow(防止
//分母为0)
{
//线性变换
byMap[i] = newlow + (BYTE) ((newhig - newlow) * (i - oralow) / (orahig - oralow));
}
else
{
byMap[i] = newlow; //直接赋值为newlow
}
}
for (; i < newhig; i++)
{
if (newhig <= 255) //判断d是否大于255
{
byMap[i] = newhig; //直接赋值为newhig
}
else
{
byMap[i] = 255; //直接赋值为255
}
}
for(i = 0; i < lmageHeight; i++) //对图像的每个像素值进行变换
{
for(j = 0; j < lmageWidth; j++) //每列
{
// 指向DIB第i行,第j个象素的指针
lpSrc = (unsigned char*)lpDIBBits + lmageWidth * (lmageHeight - 1 - i) + j;
*lpSrc = byMap[*lpSrc]; //用新的灰度替代原有的灰度
}
}
return TRUE; //返回
}
/*************************************************************************
* \函数名称:
* GrayHistEnhance()
* \输入参数:
* LPSTR lpDIBBits //指向源图像的像素指针
* LONG lmageWidth //源图像的宽度
* LONG lmageHeight //源图像的高度
* \返回值:
* BOOL //成功则返回TRUE,否则返回FALSE
* \说明:
* 该函数对指定的图像进行直方图均衡化处理
*************************************************************************/
BOOL GrayHistEnhance(LPSTR lpDIBBits, LONG lmageWidth, LONG lmageHeight)
{
unsigned char* lpSrc; //指向源图像的指针
int nTemp; //临时变量
int j; //循环变量
int i; //循环变量
BYTE byMap[256]; //累积直方图,即灰度映射表
int nCount[256]; //直方图
for (i = 0; i < 256; i ++) //重置计数为0
{
nCount[i] = 0; //清零
}
for(i = 0; i < lmageHeight; i++) //每行
{
for(j = 0; j < lmageWidth; j++) //每列
{
// 指向DIB第i行,第j个象素的指针
lpSrc = (unsigned char*)lpDIBBits + lmageWidth * (lmageHeight - 1 - i) + j;
nCount[*(lpSrc)]++; //计数加1
}
}
for (i = 0; i < 256; i++) //计算累积直方图
{
nTemp = 0; //初始为0
for (j = 0; j <= i ; j++)
{
nTemp += nCount[j];
}
byMap[i] = (BYTE) (nTemp * 255 / lmageHeight / lmageWidth); //计算对应的新灰度值
//说明:* 255实际上就是将新的灰度值也转换为0~255的范围。
}
for(i = 0; i < lmageHeight; i++)
{
for(j = 0; j < lmageWidth; j++) //每列
{
//指向DIB第i行,第j个象素的指针
lpSrc = (unsigned char*)lpDIBBits + lmageWidth * (lmageHeight - 1 - i) + j;
*lpSrc = byMap[*lpSrc]; //计算新的灰度值
}
}
return TRUE; //返回
}
/*************************************************************************
* 函数名称:
* MedianValue()
* 参数:
* unsigned char * FiltValue - 指向要获取中值的数组指针
* int iFilterLen - 数组长度
* 返回值:
* unsigned char - 返回指定数组的中值。
* 说明:
* 该函数用冒泡法对一维数组进行排序,并返回数组元素的中值。
************************************************************************/
unsigned char MedianValue(unsigned char * FiltValue, int mFilterLen)
{
int i; // 循环变量
int j; // 循环变量
unsigned char Temp; // 中间变量
// 用冒泡法对数组进行排序
for (j = 0; j < mFilterLen - 1; j ++)
{
for (i = 0; i < mFilterLen - j - 1; i ++)
{
if (FiltValue[i] > FiltValue[i + 1]) // 前面的值大于后面的
{
Temp = FiltValue[i];
FiltValue[i] = FiltValue[i + 1]; // 二者的值互换
FiltValue[i + 1] = Temp;
}
}
}
if ((mFilterLen & 1) > 0) // 计算中值
{
//数组有奇数个元素,返回中间一个元素
Temp = FiltValue[(mFilterLen + 1) / 2];
}
else
{
//数组有偶数个元素,返回中间两个元素平均值
Temp = (FiltValue[mFilterLen / 2] + FiltValue[mFilterLen / 2 + 1]) / 2;
}
return Temp; // 返回中值
}
/*************************************************************************
* 函数名称:
* MedianFilter()
* 参数:
* LPSTR lpDIBBits - 指向源DIB图像指针
* LONG lmageWidth - 源图像宽度(象素数)
* LONG lmageHeight - 源图像高度(象素数)
* int mFilterH - 滤波器的高度
* int mFilterW - 滤波器的宽度
* int mFilterMX - 滤波器的中心元素X坐标
* int mFilterMY - 滤波器的中心元素Y坐标
* 返回值:
* BOOL - 成功返回TRUE,否则返回FALSE。
* 说明:
* 该函数对DIB图像进行中值滤波。
************************************************************************/
BOOL MedianFilter(LPSTR lpDIBBits, LONG lmageWidth, LONG lmageHeight,
int mFilterH, int mFilterW, int mFilterMX, int mFilterMY)
{
unsigned char* lpSrc; //指向原图像的指针
unsigned char* lpDst; //指向要复制区域的指针
LPBYTE lpImage; //指向复制图像的指针
unsigned char* FiltValue; //指向滤波器数组的指针
int i; //循环变量
int j; //循环变量
int k;
int l;
lpImage = (LPBYTE) new char[lmageWidth *lmageHeight];
if(lpImage == NULL)
{
return FALSE;
}
memcpy(lpImage, lpDIBBits, lmageWidth *lmageHeight);
FiltValue = new unsigned char[mFilterH *mFilterW]; // 暂时分配内存,以保存滤波器数组
if (FiltValue == NULL) // 判断是否内存分配失败
{
delete[]lpImage; // 释放已分配内存
return FALSE; // 分配内存失败
}
// 开始中值滤波
for(i = mFilterMY; i < lmageHeight - mFilterH + mFilterMY + 1; i++)
{
for(j = mFilterMX; j < lmageWidth - mFilterW + mFilterMX + 1; j++)
{
// 指向新DIB第i行,第j个象素的指针
lpDst = (unsigned char*)lpImage + lmageWidth * (lmageHeight - 1 - i) + j;
for (k = 0; k < mFilterH; k++) // 读取滤波器数组
{
for (l = 0; l < mFilterW; l++)
{
// 指向DIB第i - iFilterMY + k行,第j - iFilterMX + l个象素的指针
lpSrc = (unsigned char*)lpDIBBits + lmageWidth * (lmageHeight - 1 - i + mFilterMY
- k) + j - mFilterMX + l;
FiltValue[k * mFilterW + l] = *lpSrc; // 保存象素值
}
}
*lpDst= MedianValue(FiltValue, (mFilterH * mFilterW)); // 获取中值
}
}
memcpy(lpDIBBits, lpImage, lmageWidth*lmageHeight); // 复制变换后的图像
delete[]lpImage; // 释放内存
delete[]FiltValue;
return TRUE; // 返回
}
/*************************************************************************
*
* \函数名称:
* FFT_1D()
*
* \输入参数:
* complex<double> * pCTData - 指向时域数据的指针,输入的需要变换的数据
* complex<double> * pCFData - 指向频域数据的指针,输出的经过变换的数据
* nLevel -傅立叶变换蝶形算法的级数,2的幂数,
*
* \返回值:
* 无
*
* \说明:
* 一维快速傅立叶变换。
*
*************************************************************************
*/
void FFT_1D(complex<double> * pCTData, complex<double> * pCFData, int nLevel)
{
// 循环控制变量
int i;
int j;
int k;
// 傅立叶变换点数
int nCount =0 ;
// 计算傅立叶变换点数
nCount =(int)pow(2,nLevel) ;
// 某一级的长度
int nBtFlyLen;
nBtFlyLen = 0 ;
// 变换系数的角度 =2 * PI * i / nCount
double dAngle;
complex<double> *pCW ;
// 分配内存,存储傅立叶变化需要的系数表
pCW = new complex<double>[nCount / 2];
// 计算傅立叶变换的系数
for(i = 0; i < nCount / 2; i++)
{
dAngle = -2 * PI * i / nCount;
pCW[i] = complex<double> ( cos(dAngle), sin(dAngle) );
}
// 变换需要的工作空间
complex<double> *pCWork1,*pCWork2;
// 分配工作空间
pCWork1 = new complex<double>[nCount];
pCWork2 = new complex<double>[nCount];
// 临时变量
complex<double> *pCTmp;
// 初始化,写入数据
memcpy(pCWork1, pCTData, sizeof(complex<double>) * nCount);
// 临时变量
int nInter;
nInter = 0;
// 蝶形算法进行快速傅立叶变换
for(k = 0; k < nLevel; k++)
{
for(j = 0; j < (int)pow(2,k); j++)
{
//计算长度
nBtFlyLen = (int)pow( 2,(nLevel-k) );
//倒序重排,加权计算
for(i = 0; i < nBtFlyLen/2; i++)
{
nInter = j * nBtFlyLen;
pCWork2[i + nInter] =
pCWork1[i + nInter] + pCWork1[i + nInter + nBtFlyLen / 2];
pCWork2[i + nInter + nBtFlyLen / 2] =
(pCWork1[i + nInter] - pCWork1[i + nInter + nBtFlyLen / 2])
* pCW[(int)(i * pow(2,k))];
}
}
// 交换 pCWork1和pCWork2的数据
pCTmp = pCWork1 ;
pCWork1 = pCWork2 ;
pCWork2 = pCTmp ;
}
// 重新排序
for(j = 0; j < nCount; j++)
{
nInter = 0;
for(i = 0; i < nLevel; i++)
{
if ( j&(1<<i) )
{
nInter += 1<<(nLevel-i-1);
}
}
pCFData[j]=pCWork1[nInter];
}
// 释放内存空间
delete pCW;
delete pCWork1;
delete pCWork2;
pCW = NULL ;
pCWork1 = NULL ;
pCWork2 = NULL ;
}
/*************************************************************************
*
* \函数名称:
* FFT_2D()
*
* \输入参数:
* complex<double> * pCTData - 图像数据
* int nWidth - 数据宽度
* int nHeight - 数据高度
* complex<double> * pCFData - 傅立叶变换后的结果
*
* \返回值:
* 无
*
* \说明:
* 二维傅立叶变换。
*
************************************************************************
*/
void FFT_2D(complex<double> * pCTData, int nWidth, int nHeight, complex<double> * pCFData)
{
// 循环控制变量
int x;
int y;
// 临时变量
double dTmpOne;
double dTmpTwo;
// 进行傅立叶变换的宽度和高度,(2的整数次幂)
// 图像的宽度和高度不一定为2的整数次幂
int nTransWidth;
int nTransHeight;
// 计算进行傅立叶变换的宽度 (2的整数次幂)
dTmpOne = log(nWidth)/log(2);
dTmpTwo = ceil(dTmpOne) ;
dTmpTwo = pow(2,dTmpTwo) ;
nTransWidth = (int) dTmpTwo ;
// 计算进行傅立叶变换的高度 (2的整数次幂)
dTmpOne = log(nHeight)/log(2);
dTmpTwo = ceil(dTmpOne) ;
dTmpTwo = pow(2,dTmpTwo) ;
nTransHeight = (int) dTmpTwo ;
// x,y(行列)方向上的迭代次数
int nXLev;
int nYLev;
// 计算x,y(行列)方向上的迭代次数
nXLev = (int) ( log(nTransWidth)/log(2) + 0.5 );
nYLev = (int) ( log(nTransHeight)/log(2) + 0.5 );
for(y = 0; y < nTransHeight; y++)
{
// x方向进行快速傅立叶变换
FFT_1D(&pCTData[nTransWidth * y], &pCFData[nTransWidth * y], nXLev);
}
// pCFData中目前存储了pCTData经过行变换的结果
// 为了直接利用FFT_1D,需要把pCFData的二维数据转置,再一次利用FFT_1D进行
// 傅立叶行变换(实际上相当于对列进行傅立叶变换)
for(y = 0; y < nTransHeight; y++)
{
for(x = 0; x < nTransWidth; x++)
{
pCTData[nTransHeight * x + y] = pCFData[nTransWidth * y + x];
}
}
for(x = 0; x < nTransWidth; x++)
{
// 对x方向进行快速傅立叶变换,实际上相当于对原来的图象数据进行列方向的
// 傅立叶变换
FFT_1D(&pCTData[x * nTransHeight], &pCFData[x * nTransHeight], nYLev);
}
// pCFData中目前存储了pCTData经过二维傅立叶变换的结果,但是为了方便列方向
// 的傅立叶变换,对其进行了转置,现在把结果转置回来
for(y = 0; y < nTransHeight; y++)
{
for(x = 0; x < nTransWidth; x++)
{
pCTData[nTransWidth * y + x] = pCFData[nTransHeight * x + y];
}
}
memcpy(pCTData, pCFData, sizeof(complex<double>) * nTransHeight * nTransWidth );
}
/*************************************************************************
*
* \函数名称:
* IFFT_2D()
*
* \输入参数:
* complex<double> * pCFData - 频域数据
* complex<double> * pCTData - 时域数据
* int nWidth - 图像数据宽度
* int nHeight - 图像数据高度
*
* \返回值:
* 无
*
* \说明:
* 二维傅立叶反变换。
*
************************************************************************
*/
void IFFT_2D(complex<double> * pCFData, complex<double> * pCTData, int nWidth, int nHeight)
{
// 循环控制变量
int x;
int y;
// 临时变量
double dTmpOne;
double dTmpTwo;
// 进行傅立叶变换的宽度和高度,(2的整数次幂)
// 图像的宽度和高度不一定为2的整数次幂
int nTransWidth;
int nTransHeight;
// 计算进行傅立叶变换的宽度 (2的整数次幂)
dTmpOne = log(nWidth)/log(2);
dTmpTwo = ceil(dTmpOne) ;
dTmpTwo = pow(2,dTmpTwo) ;
nTransWidth = (int) dTmpTwo ;
// 计算进行傅立叶变换的高度 (2的整数次幂)
dTmpOne = log(nHeight)/log(2);
dTmpTwo = ceil(dTmpOne) ;
dTmpTwo = pow(2,dTmpTwo) ;
nTransHeight = (int) dTmpTwo ;
// 分配工作需要的内存空间
complex<double> *pCWork= new complex<double>[nTransWidth * nTransHeight];
//临时变量
complex<double> *pCTmp ;
// 为了利用傅立叶正变换,可以把傅立叶频域的数据取共轭
// 然后直接利用正变换,输出结果就是傅立叶反变换结果的共轭
for(y = 0; y < nTransHeight; y++)
{
for(x = 0; x < nTransWidth; x++)
{
pCTmp = &pCFData[nTransWidth * y + x] ;
pCWork[nTransWidth * y + x] = complex<double>( pCTmp->real() , -pCTmp->imag() );
}
}
// 调用傅立叶正变换
FFT_2D(pCWork, nWidth, nHeight, pCTData) ;
// 求时域点的共轭,求得最终结果
// 根据傅立叶变换原理,利用这样的方法求得的结果和实际的时域数据
// 相差一个系数
for(y = 0; y < nTransHeight; y++)
{
for(x = 0; x < nTransWidth; x++)
{
pCTmp = &pCTData[nTransWidth * y + x] ;
pCTData[nTransWidth * y + x] =
complex<double>( pCTmp->real()/(nTransWidth*nTransHeight),
-pCTmp->imag()/(nTransWidth*nTransHeight) );
}
}
delete pCWork ;
pCWork = NULL ;
}
/*************************************************************************
* \函数名称:
* ButterWorthHighPass()
* \输入参数:
* LPSTR lpDIBBits - 指向需要滤波的图像像素指针
* int nWidth - 数据宽度
* int nHeight - 数据高度
* int nRadius - ButterWorth高通滤波的"半功率"点
* \返回值:
* BOOL // 成功返回TRUE,否则返回FALSE。
* \说明:
* pDIBBits 是指向需要滤波的图像像素指针。
* 经过ButterWorth高通滤波的数据仍然存储在lpDIBBits 当中。
**************************************************************************/
BOOL ButterWorthHighPass(LPSTR lpDIBBits, LONG nWidth, LONG nHeight, int nRadius)
{
unsigned char* lpSrc; // 指向源图像的指针
int y ; // 循环控制变量
int x ; // 循环控制变量
double dTmpOne ; //存放临时变量
double dTmpTwo ; //存放临时变量
double H ; // ButterWorth 滤波系数
int nTransWidth ; // 傅立叶变换的宽度(2的整数次幂)
int nTransHeight; // 傅立叶变换的高度(2的整数次幂)
double dReal ; // 傅立叶变换的实部
double dImag; // 傅立叶变换的虚部
double unchValue; // 存贮图像各像素灰度的临时变量
complex<double> * pCTData ; // 指向时域数据的指针
complex<double> * pCFData ; // 指向频域数据的指针
// 计算进行傅立叶变换的点数-横向(2的整数次幂)
dTmpOne = log(nWidth)/log(2);
dTmpTwo = ceil(dTmpOne);
dTmpTwo = pow(2,dTmpTwo);
nTransWidth = (int) dTmpTwo;
// 计算进行傅立叶变换的点数-纵向 (2的整数次幂)
dTmpOne = log(nHeight)/log(2);
dTmpTwo = ceil(dTmpOne) ;
dTmpTwo = pow(2,dTmpTwo);
nTransHeight = (int) dTmpTwo;
pCTData=new complex<double>[nTransWidth * nTransHeight]; // 分配内存
pCFData=new complex<double>[nTransWidth * nTransHeight]; // 分配内存
// 图像数据的宽和高不一定是2的整数次幂,所以pCTData有一部分数据需要补0
for(y=0; y<nTransHeight; y++)
{
for(x=0; x<nTransWidth; x++)
{
pCTData[y*nTransWidth + x]=complex<double>(0,0); // 补零
}
}
for(y=0; y<nHeight; y++) // 把图像数据传给pCTData
{
for(x=0; x<nWidth; x++)
{
// 指向DIB第y行,第x个象素的指针
lpSrc = (unsigned char*)lpDIBBits + nWidth * (nHeight - 1 - y) + x;
unchValue = *lpSrc;
pCTData[y*nTransWidth + x]=complex<double>(unchValue,0);
}
}
FFT_2D(pCTData, nWidth, nHeight, pCFData) ; // 傅立叶正变换
for(y=0; y<nTransHeight; y++) // 下面开始实施ButterWorth高通滤波
{
for(x=0; x<nTransWidth; x++)
{
H = (double)(y*y+x*x) ;
H = (nRadius * nRadius) / H ;
H = 1/(1+H); // 求H值
pCFData[y*nTransWidth + x]=complex<double>(H*(pCFData[y*nTransWidth + x].real()),
H*(pCFData[y*nTransWidth + x].imag()) );
}
}
// 经过ButterWorth高通滤波的图象进行反变换
IFFT_2D(pCFData, pCTData, nWidth, nHeight);
for(y=0; y<nHeight; y++) // 反变换的数据传给lpDIBBits
{
for(x=0; x<nWidth; x++)
{
dReal = pCTData[y*nTransWidth + x].real() ;
dImag = pCTData[y*nTransWidth + x].imag() ;
unchValue = max(0,min(255,sqrt(dReal*dReal+dImag*dImag)+100 ));
// 指向DIB第y行,第x个象素的指针
lpSrc = (unsigned char*)lpDIBBits + nWidth * (nHeight - 1 - y) + x;
*lpSrc =unchValue ;
}
}
delete pCTData; // 释放内存
delete pCFData; // 释放内存
pCTData = NULL;
pCFData = NULL;
return (true); // 返回结果
}
/*************************************************************************
* \函数名称:
* ButterWorthLowPass()
* \输入参数:
* LPSTR lpDIBBits - 指向需要滤波的图像像素指针
* int nWidth - 数据宽度
* int nHeight - 数据高度
* int nRadius - ButterWorth低通滤波的"半功率"点
* \返回值:
* BOOL // 成功返回TRUE,否则返回FALSE。
* \说明:
* pDIBBits 是指向需要滤波的图像像素指针。
* 经过ButterWorth低通滤波的数据仍然存储在lpDIBBits 当中。
**************************************************************************/
BOOL ButterWorthLowPass(LPSTR lpDIBBits, LONG nWidth, LONG nHeight, int nRadius)
{
unsigned char* lpSrc; // 指向源图像的指针
int y ; // 循环控制变量
int x ; // 循环控制变量
double dTmpOne ; //存放临时变量
double dTmpTwo ; //存放临时变量
double H ; // ButterWorth 滤波系数
int nTransWidth ; // 傅立叶变换的宽度(2的整数次幂)
int nTransHeight; // 傅立叶变换的高度(2的整数次幂)
double dReal ; // 傅立叶变换的实部
double dImag; // 傅立叶变换的虚部
double unchValue; // 存贮图像各像素灰度的临时变量
complex<double> * pCTData ; // 指向时域数据的指针
complex<double> * pCFData ; // 指向频域数据的指针
// 计算进行傅立叶变换的点数-横向 (2的整数次幂)
dTmpOne = log(nWidth)/log(2);
dTmpTwo = ceil(dTmpOne) ;
dTmpTwo = pow(2,dTmpTwo);
nTransWidth = (int) dTmpTwo;
// 计算进行傅立叶变换的点数-纵向 (2的整数次幂)
dTmpOne = log(nHeight)/log(2);
dTmpTwo = ceil(dTmpOne) ;
dTmpTwo = pow(2,dTmpTwo);
nTransHeight = (int) dTmpTwo;
pCTData=new complex<double>[nTransWidth * nTransHeight]; // 分配内存
pCFData=new complex<double>[nTransWidth * nTransHeight]; // 分配内存
// 图像数据的宽和高不一定是2的整数次幂,所以pCTData有一部分数据需要补0
for(y=0; y<nTransHeight; y++)
{
for(x=0; x<nTransWidth; x++)
{
pCTData[y*nTransWidth + x]=complex<double>(0,0); // 补零
}
}
for(y=0; y<nHeight; y++) // 把图像数据传给pCTData
{
for(x=0; x<nWidth; x++)
{
// 指向DIB第y行,第x个象素的指针
lpSrc = (unsigned char*)lpDIBBits + nWidth * (nHeight - 1 - y) + x;
unchValue = *lpSrc;
pCTData[y*nTransWidth + x]=complex<double>(unchValue,0);
}
}
FFT_2D(pCTData, nWidth, nHeight, pCFData) ; // 傅立叶正变换
for(y=0; y<nTransHeight; y++) // 开始实施ButterWorth低通滤波
{
for(x=0; x<nTransWidth; x++)
{
H = (double)(y*y+x*x) ;
H = H / (nRadius * nRadius);
H = 1/(1+H) ; // 求H值
pCFData[y*nTransWidth + x]=complex<double>(pCFData[y*nTransWidth + x].real()*H,
pCFData[y*nTransWidth + x].imag()*H);
}
}
// 经过ButterWorth低通滤波的图象进行反变换
IFFT_2D(pCFData, pCTData, nWidth, nHeight);
for(y=0; y<nHeight; y++) // 反变换的数据传给lpDIBBits
{
for(x=0; x<nWidth; x++)
{
dReal = pCTData[y*nTransWidth + x].real() ;
dImag = pCTData[y*nTransWidth + x].imag() ;
unchValue = max(0,min(255,sqrt(dReal*dReal+dImag*dImag) ));
// 指向DIB第y行,第x个象素的指针
lpSrc = (unsigned char*)lpDIBBits + nWidth * (nHeight - 1 - y) + x;
*lpSrc =unchValue ;
}
}
delete pCTData; // 释放内存
delete pCFData; // 释放内存
pCTData = NULL;
pCFData = NULL;
return (true); //返回结果
}
/*************************************************************************
* \函数名称:
* Template()
* \输入参数:
* LPSTR lpDIBBits - 指向源图像的像素指针
* LONG lmageWidth - 源图像的宽度
* LONG lmageHeight - 源图像的高度
* int nTempWidth - 模板的宽度
* int nTempHeight - 模板的高度
* int nTempCenX - 模板中心的X坐标(相对于模板)
* int nTempCenY - 模板中心的Y坐标(相对于模板)
* double* Tempdata - 模板数组的指针
* double* TempCoef - 模板的系数
* \返回值:
* BOOL - 成功则返回TRUE,否则返回FALSE
* \说明:
* 该函数用指定的模板对lpDIBBits指向的图象进行模板操作。模板的定义了宽度,高度,中心坐标
*和系数,模板的数据存放在Tempdata中。对图象进行模板操作后,仍然存放在lpDIBBits指向的对象
*中。需要注意的是,该函数只能处理8位的图象,否则,指向的数据将出错。
**************************************************************************/
BOOL Template(LPSTR lpDIBBits, LONG lmageWidth, LONG lmageHeight,
int nTempWidth, int nTempHeight,
int nTempCenX, int nTempCenY,
double* Tempdata, double TempCoef)
{
LPBYTE lpImage; // 临时存放图像数据的指针
int i; // 循环变量
int j; // 循环变量
int k; // 循环变量
int l; // 循环变量
unsigned char* lpSrc; // 指向源图像的指针
unsigned char* lpDst; // 指向要复制区域的指针
double dbResult; // 计算结果
lpImage = (LPBYTE) new char[lmageWidth*lmageHeight]; // 分配内存
if (lpImage == NULL) // 判断是否内存分配失败
{
return FALSE; // 分配内存失败
}
// 将原始图像的数据拷贝到临时存放内存中
memcpy(lpImage, lpDIBBits, lmageWidth*lmageHeight);
// 进行模板计算,行(除去边缘几行)
for(i = nTempCenY ; i <lmageHeight - nTempHeight + nTempCenY + 1; i++)
{
// 列(除去边缘几列)
for(j = nTempCenX; j < lmageWidth - nTempWidth + nTempCenX + 1; j++)
{
// 指向新DIB第i行,第j个象素的指针
lpDst = (unsigned char*)lpImage + lmageWidth * (lmageHeight - 1 - i) + j;
dbResult = 0; // 置0
for (k = 0; k < nTempHeight; k++)
{
for (l = 0; l < nTempWidth; l++)
{
// 指向DIB第i - iFilterMY + k行,第j - iFilterMX + l个象素的指针
lpSrc = (unsigned char*)lpDIBBits + lmageWidth * (lmageHeight - 1 - i + nTempCenY - k) + j - nTempCenX + l;
dbResult += (* lpSrc) * Tempdata[k * nTempWidth + l]; // 保存象素值
}
}
dbResult *= TempCoef; // 乘上系数
dbResult = (double ) fabs(dbResult); // 取绝对值
if(dbResult > 255) // 判断是否超过255
{
* lpDst = 255; // 直接赋值为255
}
else
{
* lpDst = (unsigned char) (dbResult + 0.5); // 赋值
}
}
}
memcpy(lpDIBBits, lpImage, lmageWidth*lmageHeight); //复制变换后的图像
delete[] lpImage; // 释放内存
return TRUE; // 返回
}
/*************************************************************************
* 函数名称:
* RegionDivideThreshold()
* 参数:
* LPSTR lpDIBBits - 指向源DIB图像指针
* LONG lWidth - 源图像宽度(象素数,必须是4的倍数)
* LONG lHeight - 源图像高度(象素数)
* int threshold - 分割门限
* 返回值:
* BOOL - 运算成功返回TRUE,否则返回FALSE。
* 说明:
* 该函数用于对图像进行固定阈值分割。
************************************************************************/
BOOL RegionDivideThreshold(LPSTR lpDIBBits, LONG lmageWidth, LONG lmageHeight, int threshold)
{
int i; //循环变量
int j; //循环变量
// 指向源图像的指针
unsigned char* lpSrc;
//像素值
unsigned char pixeltemp;
for(j = 0; j <lmageHeight; j++)
{
for(i = 0;i <lmageWidth; i++)
{
// 指向源图像倒数第j行,第i个象素的指针
lpSrc = (unsigned char *)lpDIBBits + lmageWidth * j + i;
//取得当前指针处的像素值
pixeltemp = (unsigned char)*lpSrc;
//目标图像中灰度值小于门限则设置为黑点
if(pixeltemp <= threshold)
{
*lpSrc=(unsigned char)0;
}
else //否则设置为白点
{
*lpSrc=(unsigned char)255;
}
}
}
return true;
}
/*************************************************************************
* \函数名称:
* RegionDivideAdaptive()
* 参数:
* LPSTR lpDIBBits - 指向源DIB图像指针
* LONG lmageWidth - 源图像宽度(象素数,必须是4的倍数)
* LONG lmageHeight - 源图像高度(象素数)
* 返回值:
* BOOL - 运算成功返回TRUE,否则返回FALSE。
* 说明:
* 该函数用于对图像进行自适应阈值分割。把图像分成四个子图象,然后计算每个子图象的均值,
* 根据均值设置阈值,阈值只是应用在对应的子图像
**************************************************************************/
BOOL RegionDivideAdaptive(LPSTR lpDIBBits, LONG lmageWidth, LONG lmageHeight)
{
int i; //循环变量
int j; //循环变量
// 指向源图像的指针
unsigned char* lpSrc;
//像素值
unsigned char pixeltemp;
// 子图像灰度的平均值
int nAvg ;
nAvg = 0 ; //初始化为0
// 对左下图像逐点扫描:
for(j = 0; j <lmageHeight/2; j++)
{
for(i = 0;i <lmageWidth/2; i++)
{
// 指向源图像倒数第j行,第i个象素的指针
lpSrc = (unsigned char *)lpDIBBits + lmageWidth * j + i;
//取得当前指针处的像素值
pixeltemp = (unsigned char)*lpSrc;
//灰度的累计
nAvg = (int)pixeltemp+nAvg;
}
}
// 计算均值
nAvg = nAvg /((lmageHeight/2) * (lmageWidth/2)) ;
// 对左下图像逐点扫描:
for(j = 0; j <lmageHeight/2; j++)
{
for(i = 0;i <lmageWidth/2; i++)
{
// 指向源图像倒数第j行,第i个象素的指针
lpSrc = (unsigned char *)lpDIBBits + lmageWidth * j + i;
//取得当前指针处的像素值
pixeltemp = (unsigned char)*lpSrc;
//目标图像中灰度值小于门限则设置为黑点
if(pixeltemp <= nAvg)
{
*lpSrc=(unsigned char)0;
}
else //否则设置为白点
{
*lpSrc=(unsigned char)255;
}
}
}
nAvg =0; //初始化为0
// 对左上图像逐点扫描:
for(j = lmageHeight/2; j <lmageHeight; j++)
{
for(i = 0;i <lmageWidth/2; i++)
{
// 指向源图像倒数第j行,第i个象素的指针
lpSrc = (unsigned char *)lpDIBBits + lmageWidth * j + i;
//取得当前指针处的像素值
pixeltemp = (unsigned char)*lpSrc;
//灰度的累计
nAvg = (int)pixeltemp+nAvg;
}
}
// 计算均值
nAvg = nAvg /((lmageHeight/2) * (lmageWidth/2)) ;
// 对左上图像逐点扫描:
for(j = lmageHeight/2; j <lmageHeight; j++)
{
for(i = 0;i <lmageWidth/2; i++)
{
// 指向源图像倒数第j行,第i个象素的指针
lpSrc = (unsigned char *)lpDIBBits + lmageWidth * j + i;
//取得当前指针处的像素值
pixeltemp = (unsigned char)*lpSrc;
//目标图像中灰度值小于门限则设置为黑点
if(pixeltemp <= nAvg)
{
*lpSrc=(unsigned char)0;
}
else //否则设置为白点
{
*lpSrc=(unsigned char)255;
}
}
}
nAvg =0; //初始化为0
// 对右上图像逐点扫描:
for(j = lmageHeight/2; j <lmageHeight; j++)
{
for(i = lmageWidth/2;i <lmageWidth; i++)
{
// 指向源图像倒数第j行,第i个象素的指针
lpSrc = (unsigned char *)lpDIBBits + lmageWidth * j + i;
//取得当前指针处的像素值
pixeltemp = (unsigned char)*lpSrc;
//灰度的累计
nAvg = (int)pixeltemp+nAvg;
}
}
// 计算均值
nAvg = nAvg /((lmageHeight/2) * (lmageWidth/2)) ;
// 对右上图像逐点扫描:
for(j = lmageHeight/2; j <lmageHeight; j++)
{
for(i = lmageWidth/2;i <lmageWidth; i++)
{
// 指向源图像倒数第j行,第i个象素的指针
lpSrc = (unsigned char *)lpDIBBits + lmageWidth * j + i;
//取得当前指针处的像素值
pixeltemp = (unsigned char)*lpSrc;
//目标图像中灰度值小于门限则设置为黑点
if(pixeltemp <= nAvg)
{
*lpSrc=(unsigned char)0;
}
else //否则设置为白点
{
*lpSrc=(unsigned char)255;
}
}
}
nAvg =0; //初始化为0
// 对右下图像逐点扫描:
for(j = 0; j <lmageHeight/2; j++)
{
for(i = lmageWidth/2;i <lmageWidth; i++)