{{ message }}
Repository navigation
Expand file tree
/
Copy pathfunction.h
More file actions
5779 lines (5335 loc) · 167 KB
/
Copy pathfunction.h
File metadata and controls
5779 lines (5335 loc) · 167 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;
struct CplexNum
{
double re;
double im;
};
/////////////////////////////////////////////////////////
//用于复数运算
CplexNum Add(CplexNum c1,CplexNum c2)
{
CplexNum c;
c.re=c1.re+c2.re;
c.im=c1.im+c2.im;
return c;
}
CplexNum Sub(CplexNum c1,CplexNum c2)
{
CplexNum c;
c.re=c1.re-c2.re;
c.im=c1.im-c2.im;
return c;
}
CplexNum Mul(CplexNum c1,CplexNum c2)
{
CplexNum c;
c.re=c1.re*c2.re-c1.im*c2.im;
c.im=c1.re*c2.im+c2.re*c1.im;
return c;
}
////////////////////////////////////////////////////////
/*************************************************************************
* 函数名称:AngleToRadian(int angle)
* 函数参数:
* int angle
* 函数类型:float
* 函数功能:用来角度到弧度的转换
************************************************************************/
double AngleToRadian(int angle)
{
return ((angle)*pi/180.0);
}
/*************************************************************************
* 函数名称:ThreeCrossMat(double *pMatrix, int rank, double *pQMatrix,
double *pMainCross, double *pHypoCross)
* 函数参数:
* double *pMatrix ,指向矩阵的指针
* int rank ,矩阵阶数
* double *pQMatrix , householder变换后的的矩阵的指针
* double *pMainCross ,对称三角阵中的主对角元素的指针
* double *pHypoCross , 对称三角阵中的次对角元素的指针
* 函数类型:BOOL
*函数功能:将n阶实对称矩阵化为对称三角阵
************************************************************************/
BOOL ThreeCrossMat(double *pMatrix, int rank, double *pQMatrix,
double *pMainCross, double *pHypoCross)
{
int i, j, k, u;//变量声明
double h, f, g, h2;
for(i = 0; i <= rank-1; i++)// 将矩阵pQMatrix初始化
{
for(j = 0; j <= rank-1; j++)
{
u = i*rank + j;
pQMatrix[u] = pMatrix[u];
}
}
for (i = rank-1; i >= 1; i--)
{
h = 0.0;
if (i > 1)
for (k = 0; k <= i-1; k++)
{
u = i*rank + k;
h = h + pQMatrix[u]*pQMatrix[u];
}
if (h + 1.0 == 1.0)// 如果一行全部为零
{
pHypoCross[i] = 0.0;
if (i == 1)
{
pHypoCross[i] = pQMatrix[i*rank+i-1];
}
pMainCross[i] = 0.0;
}
else
{// 否则求正交矩阵的值
pHypoCross[i] = sqrt(h);// 求次对角元素的值
u = i*rank + i - 1;
if (pQMatrix[u] > 0.0) // 判断i行i-1列元素是不是大于零
{
pHypoCross[i] = -pHypoCross[i];
}
h = h - pQMatrix[u]*pHypoCross[i];
pQMatrix[u] = pQMatrix[u] - pHypoCross[i];
f = 0.0;
for (j = 0; j <= i-1; j++)// householder变换
{
pQMatrix[j*rank+i] = pQMatrix[i*rank+j] / h;
g = 0.0;
for (k = 0; k <= j; k++)
{
g = g + pQMatrix[j*rank+k]*pQMatrix[i*rank+k];
}
if (j+1 <= i-1)
for (k = j+1; k <= i-1; k++)
{
g = g + pQMatrix[k*rank+j]*pQMatrix[i*rank+k];
}
pHypoCross[j] = g / h;
f = f + g*pQMatrix[j*rank+i];
}
h2 = f / (h + h);
for (j = 0; j <= i-1; j++)// 求正交矩阵的值
{
f = pQMatrix[i*rank + j];
g = pHypoCross[j] - h2*f;
pHypoCross[j] = g;
for (k = 0; k <= j; k++)
{
u = j*rank + k;
pQMatrix[u] = pQMatrix[u] - f*pHypoCross[k] - g*pQMatrix[i*rank + k];
}
}
pMainCross[i] = h;
}
}
for (i = 0; i <= rank-2; i++) // 赋零值
{
pHypoCross[i] = pHypoCross[i + 1];
}
pHypoCross[rank - 1] = 0.0;
pMainCross[0] = 0.0;
for (i = 0; i <= rank-1; i++)
{ // 主对角元素的计算
if ((pMainCross[i] != 0.0) && (i-1 >= 0))
for (j = 0; j <= i-1; j++)
{
g = 0.0;
for (k = 0; k <= i-1; k++)
{g = g + pQMatrix[i*rank + k]*pQMatrix[k*rank + j];
}
for (k = 0; k <= i-1; k++)
{
u = k*rank + j;
pQMatrix[u] = pQMatrix[u] - g*pQMatrix[k*rank + i];
}
}
u = i*rank + i;// 存储主对角线的元素
pMainCross[i] = pQMatrix[u];
pQMatrix[u] = 1.0;
if (i-1 >= 0)// 将三对角外所有的元素赋零值
for (j = 0; j <= i-1; j++)
{
pQMatrix[i*rank + j] = 0.0;
pQMatrix[j*rank+i] = 0.0;
}
}
return(TRUE);// 返回
}
/*************************************************************************
*
* 函数名称:EigenvalueVector(int rank, double *pMainCross, double *pHypoCross,
double *pMatrix, double Precision, int MaxT)
* 函数参数:
* int rank ,矩阵A的阶数
* double *pMainCross ,对称三角阵中的主对角元素的指针,返回时存放A的特征值
* double *pHypoCross ,对称三角阵中的次对角元素的指针
* double *pMatrix ,对称矩阵A的特征向量的指针
* double Precision ,控制精度
* int MaxT , 最大迭代次数
* 函数类型:BOOL
* 函数功能:用计算实对称三角矩阵的全部特征值以及相应的特征向量
************************************************************************/
BOOL EigenvalueVector(int rank, double *pMainCross, double *pHypoCross,
double *pMatrix, double Precision, int MaxT)
{
int i, j, k, m, it, u, v;// 变量声明
double d, f, h, g, p, r, e, s;
pHypoCross[rank - 1] = 0.0; // 初始化
d = 0.0;
f = 0.0;
for(j = 0; j <= rank-1; j++)
{ // 迭代精度的控制
it = 0;
h = Precision * (fabs(pMainCross[j]) + fabs(pHypoCross[j]));
if(h > d)
{
d = h;
}
m = j;
while((m <= rank-1) && (fabs(pHypoCross[m]) > d))
{
m = m + 1;
}
if(m != j)
{
// 迭代求矩阵A的特征值和特征向量
do
{
// 超过迭代次数,迭代失败
if(it == MaxT)
{
return(FALSE);
}
it = it + 1;
g = pMainCross[j];
p = (pMainCross[j + 1] - g) / (2.0 * pHypoCross[j]);
r = sqrt(p*p + 1.0);
// 如果p大于0
if (p >= 0.0)
{
pMainCross[j] = pHypoCross[j]/(p + r);
}
else
{
pMainCross[j] = pHypoCross[j]/(p - r);
}
h = g - pMainCross[j];
// 计算主对角线的元素
for (i = j + 1; i <= rank - 1; i++)
{
pMainCross[i] = pMainCross[i] - h;
}
// 赋值
f = f + h;
p = pMainCross[m];
e = 1.0; s = 0.0;
for(i = m - 1; i >= j; i--)
{
g = e * pHypoCross[i];
h = e * p;
// 主对角线元素的绝对值是否大于次对角线元素的
if(fabs(p) >= fabs(pHypoCross[i]))
{
e = pHypoCross[i] / p;
r = sqrt(e*e + 1.0);
pHypoCross[i + 1] = s*p*r;
s = e / r; e = 1.0 / r;
}
else
{
e = p / pHypoCross[i];
r = sqrt(e*e + 1.0);
pHypoCross[i+1] = s * pHypoCross[i] * r;
s = 1.0 / r; e = e / r;
}
p = e*pMainCross[i] - s*g;
pMainCross[i + 1] = h + s*(e*g + s*pMainCross[i]);
// 重新存储特征向量
for(k = 0; k <= rank - 1; k++)
{
u = k*rank + i + 1; v = u - 1;
h = pMatrix[u];
pMatrix[u] = s*pMatrix[v] + e*h;
pMatrix[v] = e*pMatrix[v] - s*h;
}
}
// 将主对角线和次对角线元素重新赋值
pHypoCross[j] = s * p;
pMainCross[j] = e * p;
}
while (fabs(pHypoCross[j]) > d);
}
pMainCross[j] = pMainCross[j] + f;
}
for (i = 0; i <= rank-1; i++)
{// 返回A的特征值
k = i; p = pMainCross[i];
if(i+1 <= rank-1)
{// 将A特征值赋给p
j = i + 1;
while((j <= rank-1) && (pMainCross[j] <= p))
{ k = j;
p = pMainCross[j];
j = j+1;
}
}
if (k != i)
{// 存储A的特征值和特征向量
pMainCross[k] = pMainCross[i];
pMainCross[i] = p;
for(j = 0; j <= rank-1; j++)
{
u = j*rank + i;
v = j*rank + k;
p = pMatrix[u];
pMatrix[u] = pMatrix[v];
pMatrix[v] = p;
}
}
}
return(TRUE);// 返回
}
/*************************************************************************
* 函数名称:
* Transpose(LPSTR lpSrcDib,LPSTR lpDibBits,long lWidth,long lHeight,
long lLineBytes,long lDstLineBytes)
* 函数参数:
* LPSTR lpSrcDib,指向源DIB的指针
LPSTR lpSrcStartBits,指向DIB起始像素的指针
long lWidth,DIB图象的宽度
long lHeight,DIB图象的高度
long lLineBytes,DIB图象的行字节数,为4的倍数
long lDstLineBytes,临时DIB图象的行字节数,为4的倍数
* 函数类型:BOOL
* 函数功能:该函数用来转置DIB图象
************************************************************************/
BOOL Transpose(LPSTR lpSrcDib,LPSTR lpSrcStartBits,long lWidth,long lHeight,
long lLineBytes,long lDstLineBytes)
{
long i; //行循环变量
long j; //列循环变量
LPSTR lpSrcDIBBits; //指向源像素的指针
LPSTR lpDstDIBBits; //指向临时图象对应像素的指针
LPSTR lpDstStartBits; //指向临时图象对应像素的指针
HLOCAL hDstDIBBits; //临时图象句柄
LPBITMAPINFOHEADER lpbmi; // 指向BITMAPINFOHEADER结构的指针
lpbmi = (LPBITMAPINFOHEADER)lpSrcDib;
hDstDIBBits= LocalAlloc(LHND, lWidth * lDstLineBytes);// 分配临时内存
if (hDstDIBBits== NULL) // 判断是否内存分配
{
return FALSE; // 分配内存失败
}
lpDstStartBits= (char * )LocalLock(hDstDIBBits); // 锁定内存
for(i = 0; i < lHeight; i++) // 针对图象每行进行操作
{
for(j = 0; j < lWidth; j++) // 针对每行图象每列进行操作
{
lpSrcDIBBits= (char *)lpSrcStartBits + lLineBytes * (lHeight - 1 - i) + j;// 指向源DIB第i行,第j个像素的指针
lpDstDIBBits= (char *)lpDstStartBits + lDstLineBytes * (lWidth - 1 - j) + i;// 指向转置DIB第j行,第i个像素的指针
*(lpDstDIBBits)= *(lpSrcDIBBits); // 复制像素
}
}
memcpy(lpSrcStartBits, lpDstStartBits, static_cast<size_t>(lWidth) * lDstLineBytes);// 复制转置后的图象
lpbmi->biWidth = lHeight;
lpbmi->biHeight = lWidth;
LocalUnlock(hDstDIBBits); // 释放内存
LocalFree(hDstDIBBits);
return TRUE; // 返回
}
/*************************************************************************
* 函数名称:Mirror(LPSTR lpSrcStartBits, long lWidth, long lHeight,long lLineBytes)
* 函数参数:
LPSTR lpSrcStartBits,指向DIB起始像素的指针
long lWidth,DIB图象的宽度
long lHeight,DIB图象的高度
long lLineBytes,DIB图象的行字节数,为4的倍数
* 函数类型:BOOL
* 函数功能:该函数用来镜像DIB图象,本程序只实现了水平镜像,垂直镜像的原理书中也谈到。 很容易实现
************************************************************************/
BOOL Mirror(LPSTR lpSrcStartBits, long lWidth, long lHeight,long lLineBytes)
{
long i; //行循环变量
long j; //列循环变量
LPSTR lpSrcDIBBits; //指向源像素的指针
LPSTR lpDstDIBBits; //指向临时图象对应像素的指针
HLOCAL hDstDIBBits; //临时图象句柄
LPSTR lpBits; // 指向中间像素的指针,当复制图象时,提供临时的像素内存空间
hDstDIBBits= LocalAlloc(LHND, lLineBytes);// 分配临时内存保存行图象
if (hDstDIBBits == NULL)
{
return FALSE; // 分配内存失败
}
lpDstDIBBits= (char * )LocalLock(hDstDIBBits);// 锁定
for(i = 0; i < lHeight; i++)// 水平镜像,针对图象每行进行操作
{
for(j = 0; j < lWidth / 2; j++)// 针对每行图象左半部分进行操作
{
lpSrcDIBBits= (char *)lpSrcStartBits + lLineBytes * i + j;// 指向倒数第i行,第j个像素的指针
lpBits= (char *)lpSrcStartBits + lLineBytes * (i + 1) - j;// 指向倒数第i+1行,倒数第j个像素的指针
*lpDstDIBBits=*lpBits;//保存中间像素
*lpBits = *lpSrcDIBBits;// 将倒数第i行,第j个像素复制到倒数第i行,倒数第j个像素
*lpSrcDIBBits=*lpDstDIBBits;// 将倒数第i行,倒数第j个像素复制到倒数第i行,第j个像素
}
}
LocalUnlock(hDstDIBBits);// 释放内存
LocalFree(hDstDIBBits);
return TRUE;
}
/*************************************************************************
* 函数名称:GrayEqualize(LPSTR lpSrcStartBits, long lWidth, long lHeight,long lLineBytes)
* 函数参数:
LPSTR lpSrcStartBits,指向DIB起始像素的指针
long lWidth,DIB图象的宽度
long lHeight,DIB图象的高度
long lLineBytes,DIB图象的行字节数,为4的倍数
* 函数类型:BOOL
* 函数功能:该函数用来进行直方图均衡
************************************************************************/
BOOL GrayEqualize(LPSTR lpSrcStartBits, long lWidth, long lHeight,long lLineBytes)
{
long i; //行循环变量
long j; //列循环变量
unsigned char* lpSrcUnChr; //指向像素的指针
BYTE bGrayMap[256];// 灰度映射
long lGrayNum[256];// 灰度映射
for (i = 0; i < 256; i ++)// 置0
{
lGrayNum[i] = 0;
}
for (i = 0; i < lHeight; i ++)// 各灰度值计数
{
for (j = 0; j < lWidth; j ++)
{
lpSrcUnChr=(unsigned char*)lpSrcStartBits + lLineBytes * i + j;
lGrayNum[*(lpSrcUnChr)]++;// 加1
}
}
for (i = 0; i < 256; i++)// 计算灰度映射表
{
long varLong; //临时变量
varLong = 0;// 初始为0
for (j = 0; j <= i ; j++)
{
varLong += lGrayNum[j];
}
bGrayMap[i] = (BYTE) (varLong * 255 / lHeight / lWidth);// 计算对应的新灰度值
}
for(i = 0; i < lHeight; i++)// 行
{
for(j = 0; j < lWidth; j++)// 列
{
// 指向DIB第i行,第j个像素的指针(unsigned char*)
lpSrcUnChr= (unsigned char*)lpSrcStartBits + lLineBytes * (lHeight - 1 - i) + j;
*lpSrcUnChr= bGrayMap[*lpSrcUnChr];// 计算新的灰度值
}
}
return TRUE;
}
/*************************************************************************
* 函数名称:LineTranslation(LPSTR lpSrcStartBits, long lWidth, long lHeight, float fSlope, float fIntercept,long lLineBytes)
* 函数参数:
* LPSTR lpSrcStartBits,指向源DIB图象起始像素的指针
* long lWidth,DIB图象宽度
* long lHeight,DIB图象高度
* float fSlope,斜率
* float fIntercept,截距
* LONGlLineBytes,DIB图象的行字节数,为4的倍数
* 函数类型:BOOL
* 函数功能:该函数用来对图象进行灰度的线性变换操作
************************************************************************/
BOOL LineTranslation(LPSTR lpSrcStartBits, long lWidth, long lHeight, float fSlope, float fIntercept,long lLineBytes)
{
long i; //行循环变量
long j; //列循环变量
float varFloat; //浮点参数变量
unsigned char* lpSrcUnChr; //指向像素的指针
for(i=0;i<lHeight;i++)// 行
{
for(j=0; j<lWidth; j++)// 列
{
lpSrcUnChr=(unsigned char*)lpSrcStartBits+lLineBytes*(lHeight-1-i)+j;// 指向DIB第i行,第j个像素的指针
varFloat=fSlope*(*lpSrcUnChr)+fIntercept;// 线性变换
if (varFloat>255)// 超出范围直接赋值为255
{
*lpSrcUnChr=255;
}
else if(varFloat<0)
{
*lpSrcUnChr=0;// 直接赋值为0
}
else
{
*lpSrcUnChr=(unsigned char)(varFloat+0.5);// 近似
}
}
}
return TRUE;
}
/*************************************************************************
* 函数名称:ThresholdTranslation(LPSTR lpSrcStartBits, long lWidth, long lHeight, BYTE bThreshold,long lLineBytes)
* 函数参数:
LPSTR lpSrcStartBits,指向DIB起始像素的指针
long lWidth,DIB图象的宽度
long lHeight,DIB图象的高度
long lLineBytes,DIB图象的行字节数,为4的倍数
* 函数类型:BOOL
* 函数功能:该函数用来对图象进行阈值变换
************************************************************************/
BOOL ThresholdTranslation(LPSTR lpSrcStartBits, long lWidth, long lHeight,
BYTE bThreshold,long lLineBytes)
{
long i; //行循环变量
long j; //列循环变量
unsigned char* lpSrcUnChr; //指向像素的指针
for(i = 0; i < lHeight; i++)// 行
{
for(j = 0; j < lWidth; j++)// 列
{
lpSrcUnChr= (unsigned char*)lpSrcStartBits + lLineBytes * (lHeight - 1 - i) + j;// 指向DIB第i行,第j个像素的指针
if ((*lpSrcUnChr) <bThreshold)// 小于阈值直接赋值为0
{
*lpSrcUnChr= 0;
}
else
{
*lpSrcUnChr= 255;// 赋值为255
}
}
}
return TRUE;
}
/*************************************************************************
* 函数名称:LogTranslation(LPSTR lpSrcStartBits, long lWidth, long lHeight, long lLineBytes)
* 函数参数:
LPSTR lpSrcStartBits,指向DIB起始像素的指针
long lWidth,DIB图象的宽度
long lHeight,DIB图象的高度
long lLineBytes,DIB图象的行字节数,为4的倍数
* 函数类型:BOOL
* 函数功能:该函数用来对图象进行对数变换
************************************************************************/
BOOL LogTranslation(LPSTR lpSrcStartBits, long lWidth, long lHeight, long lLineBytes)
{
long i; //行循环变量
long j; //列循环变量
unsigned char* lpSrcUnChr; //指向像素的指针
for(i = 0; i < lHeight; i++)// 行
{
for(j = 0; j < lWidth; j++)// 列
{
lpSrcUnChr= (unsigned char*)lpSrcStartBits + lLineBytes * (lHeight - 1 - i) + j;// 指向DIB第i行,第j个像素的指针
//*lpSrcUnChr=50*(unsigned char)log((*lpSrcUnChr+1));
*lpSrcUnChr = 255 * (unsigned char)log((*lpSrcUnChr + 1))/log(255);
}
}
return TRUE;
}
/*************************************************************************
* 函数名称:WindowTranslation(LPSTR lpSrcStartBits, long lWidth, long lHeight, BYTE bLower, BYTE bUpper,long lLineBytes)
* 函数参数:
* LPSTR lpSrcStartBits,指向源DIB图象起始像素的指针
* long lWidth,DIB图象宽度
* long lHeight,DIB图象高度
* BYTE bLower,窗口变换下限
* BYTE bUpper,窗口变换上限
* LONGl LineBytes,DIB图象的行字节数,为4的倍数
* 函数类型:BOOL
* 函数功能:该函数用来对图象进行窗口变换
************************************************************************/
BOOL WindowTranslation(LPSTR lpSrcStartBits, long lWidth, long lHeight, BYTE bLower, BYTE bUpper,long lLineBytes)
{
long i; //行循环变量
long j; //列循环变量
unsigned char* lpSrcUnChr; //指向像素的指针
for(i = 0; i < lHeight; i++)// 行
{
for(j = 0; j < lWidth; j++)// 列
{
lpSrcUnChr= (unsigned char*)lpSrcStartBits + lLineBytes * (lHeight - 1 - i) + j;// 指向DIB第i行,第j个像素的指针
if ((*lpSrcUnChr) < bLower)// 超出范围直接赋值
{
*lpSrcUnChr= 0;
}
else if ((*lpSrcUnChr) > bUpper)
{
*lpSrcUnChr= 255;// 直接赋值为255
}
}
}
return TRUE;
}
/*************************************************************************
* 函数名称:GrayStretch(LPSTR lpSrcStartBits, long lWidth, long lHeight, BYTE bPointX1, BYTE bPointY1, BYTE bPointX2, BYTE bPointY2,long lLineBytes)
* 函数参数:
* LPSTR lpSrcStartBits,指向源DIB图象起始像素的指针
* long lWidth,DIB图象宽度
* long lHeight,DIB图象高度
* BYTE bPointX1,第一个点的X坐标
* BYTE bPointY1,第一个点的Y坐标
* BYTE bPointX2,第二个点的X坐标
* BYTE bPointY2,第二个点的Y坐标
* LONGl LineBytes,DIB图象的行字节数,为4的倍数
* 函数类型:BOOL
* 函数功能:该函数用来对图象进行灰度拉伸
************************************************************************/
BOOL GrayStretch(LPSTR lpSrcStartBits, long lWidth, long lHeight, BYTE bPointX1, BYTE bPointY1, BYTE bPointX2, BYTE bPointY2,long lLineBytes)
{
long i; //行循环变量
long j; //列循环变量
unsigned char* lpSrcUnChr; //指向像素的指针
BYTE bMap[256];// 灰度映射
for (i = 0; i <= bPointX1; i++)// 计算灰度映射
{
if (bPointX1 > 0)// 判断bPointX1是否大于0
{
bMap[i] = (BYTE) bPointY1 * i / bPointX1;// 变换
}
else
{
bMap[i] = 0;// 赋值为0
}
}
for (; i <= bPointX2; i++)
{
if (bPointX2 != bPointX1)// 判断bPointX1是否等于bPointX2
{
bMap[i] = bPointY1 + (BYTE) ((bPointY2 - bPointY1) * (i - bPointX1) / (bPointX2 - bPointX1));// 变换
}
else
{
bMap[i] = bPointY1;// 赋值为bPointY1
}
}
for (; i < 256; i++)
{
if (bPointX2 != 255)// 判断bPointX2是否等于255
{
bMap[i] = bPointY2 + (BYTE) ((255 - bPointY2) * (i - bPointX2) / (255 - bPointX2));//变换
}
else
{
bMap[i] = 255;// 赋值为255
}
}
for(i = 0; i < lHeight; i++)// 行
{
for(j = 0; j < lWidth; j++)// 列
{
lpSrcUnChr= (unsigned char*)lpSrcStartBits + lLineBytes * (lHeight - 1 - i) + j;// 指向DIB第i行,第j个像素的指针
*lpSrcUnChr= bMap[*lpSrcUnChr];// 新灰度值
}
}
return TRUE;
}
/*************************************************************************
* 函数名称:Translation(LPSTR lpSrcStartBits, long lWidth, long lHeight, long lXOffset,
long lYOffset,long lLineBytes,long lDstLineBytes)
* 函数参数:
* LPSTR lpSrcStartBits,指向源DIB起始像素的指针
* long lWidth,DIB图象的宽度
* long lHeight,DIB图象的高度
* long lXOffset,X方向偏移量
* long lYOffset,Y方向偏移量
* long lLineBytes,DIB图象的行字节数,为4的倍数
* long lDstLineBytes,临时DIB图象的行字节数,为4的倍数
* 函数类型:BOOL
* 函数功能:该函数用来平移DIB图象
************************************************************************/
BOOL Translation(LPSTR lpSrcStartBits, long lWidth, long lHeight,
long lXOffset, long lYOffset,long lLineBytes,long lDstLineBytes)
{
long i; //行循环变量
long j; //列循环变量
LPSTR lpSrcDIBBits; //指向源像素的指针
LPSTR lpDstDIBBits; //指向临时图象对应像素的指针
LPSTR lpDstStartBits; //指向临时图象对应像素的指针
HLOCAL hDstDIBBits; //临时图象句柄
hDstDIBBits= LocalAlloc(LHND, lWidth * lDstLineBytes);// 分配临时内存
lpDstStartBits= (char * )LocalLock(hDstDIBBits);// 锁定内存
if (hDstDIBBits== NULL)// 判断是否内存分配
return FALSE;// 分配内存失败
for(i = 0; i < lHeight; i++)// 行
{
for(j = 0; j < lWidth; j++) // 列
{
lpDstDIBBits=(char*)lpDstStartBits+lLineBytes*(lHeight-1-i)
+j;// 指向新DIB第i行,第j个像素的指针
if( (j-lYOffset>= 0) && (j-lYOffset< lWidth) && // 像素在源DIB中的坐标j-lXOffset
(i-lXOffset>= 0) && (i-lXOffset < lHeight))// 判断是否在源图范围内
{
lpSrcDIBBits=(char *)lpSrcStartBits+lLineBytes*(lHeight-1-
(i-lXOffset))+(j-lYOffset);// 指向源DIB第i0行,第j0个像素的指针
*lpDstDIBBits= *lpSrcDIBBits;// 复制像素
}
else
{
* ((unsigned char*)lpDstDIBBits) = 255;// 源图中没有的像素,赋为255
}
}
}
memcpy(lpSrcStartBits, lpDstStartBits, lLineBytes * lHeight);// 复制图象
LocalUnlock(hDstDIBBits);// 释放内存
LocalFree(hDstDIBBits);
return TRUE;
}
/*************************************************************************
* 函数名称:Rotate(LPSTR lpSrcDib, LPSTR lpSrcStartBits,long lWidth, long lHeight,
long lLineBytes, WORD palSize, long lDstWidth,
long lDstHeight,long lDstLineBytes,float fSina, float fCosa)
* 函数参数:
* LPSTR lpSrcDib,指向源DIB的指针
* LPSTR lpSrcStartBits,指向源DIB的起始像素的指针
* long lWidth,源DIB图象宽度
* long lHeight,源DIB图象高度
* long lLineBytes,源DIB图象字节宽度(4的倍数)
* WORD palSize,源DIB图象调色板大小
* long lDstWidth,目标图象宽度
* long lDstHeight,目标DIB图象高度
* long lDstLineBytes,目标DIB图象行字节数(4的倍数)
* float fSina,旋转角的余弦,说明:为了避免两次求取正余弦,这里作为两个函数参数来用
* float fCosa,旋转角的正弦
* 函数类型:HGLOBAL
* 函数功能:用来旋转DIB图象
************************************************************************/
HGLOBAL Rotate(LPSTR lpSrcDib, LPSTR lpSrcStartBits,long lWidth, long lHeight,
long lLineBytes, WORD palSize, long lDstWidth,
long lDstHeight,long lDstLineBytes,float fSina, float fCosa)
{
float varFloat1; //浮点参数变量1
float varFloat2; //浮点参数变量2
LPSTR lpDstDib; //指向临时图象的指针
long i; //行循环变量
long j; //列循环变量
long i1; //行循环变量
long j1; //列循环变量
LPSTR lpSrcDIBBits; //指向源像素的指针
LPSTR lpDstDIBBits; //指向临时图象对应像素的指针
LPSTR lpDstStartBits; //指向临时图象对应像素的指针
LPBITMAPINFOHEADER lpbmi;// 指向BITMAPINFOHEADER结构的指针
varFloat1= (float) (-0.5 * (lDstWidth - 1) * fCosa - 0.5 * (lDstHeight - 1) * fSina// 将经常用到的两个常数事先求出,以便作为常数使用
+ 0.5 * (lDstWidth - 1));
varFloat2= (float) ( 0.5 * (lDstWidth - 1) * fSina - 0.5 * (lDstHeight - 1) * fCosa
+ 0.5 * (lDstHeight - 1));
HGLOBAL hDIB = (HGLOBAL) ::GlobalAlloc(GHND, lDstLineBytes * lDstHeight + *(LPDWORD)lpSrcDib +palSize);// 分配内存,以保存新DIB
if (hDIB == NULL)// 判断是否是有效的DIB对象
{
return FALSE;// 不是,则返回
}
lpDstDib= (char * )::GlobalLock((HGLOBAL) hDIB);// 锁定内存
memcpy(lpDstDib,lpSrcDib, *(LPDWORD)lpSrcDib +palSize);// 复制DIB信息头和调色板
lpbmi = (LPBITMAPINFOHEADER)lpDstDib;// 获取指针
lpbmi->biHeight=lDstHeight;// 更新DIB中图象的高度和宽度
lpbmi->biWidth =lDstWidth;
lpDstStartBits=lpDstDib+ *(LPDWORD)lpDstDib
+palSize;// 求像素起始位置,作用如同::FindDIBBits(gCo.lpSrcDib),这里尝试使用了这种方法,以避免对全局函数的调用
for(i = 0; i < lDstHeight; i++)// 行操作
{
for(j = 0; j < lDstWidth; j++)// 列操作
{
lpDstDIBBits= (char *)lpDstStartBits+ lDstLineBytes * (lDstHeight - 1 - i) + j;// 指向新DIB第i行,第j个像素的指针
i1= (long) (-((float) j) * fSina + ((float) i) * fCosa + varFloat2 + 0.5);// 计算该像素在源DIB中的坐标
j1= (long) ( ((float) j) * fCosa + ((float) i) * fSina + varFloat1 + 0.5);
if( (j1>= 0) && (j1< lWidth) && (i1>= 0) && (i1< lHeight))
{// 判断是否在源图内
lpSrcDIBBits= (char *)lpSrcStartBits+ lLineBytes * (lHeight - 1 -i1) + j1;// 指向源DIB第i0行,第j0个像素的指针
*lpDstDIBBits= *lpSrcDIBBits;// 复制像素
}
else
{
* ((unsigned char*)lpDstDIBBits) = 255;// 源图中不存在的像素,赋为255
}
}
}
return hDIB;
}
/*************************************************************************
* 函数名称:Zoom(LPSTR lpSrcDib, LPSTR lpSrcStartBits,long lWidth, long lHeight,
long lLineBytes, WORD palSize, long lDstWidth,
long lDstHeight,long lDstLineBytes,float fXZoomRatio, float fYZoomRatio)
* 函数参数:
* LPSTR lpSrcDib,指向源DIB的指针
* LPSTR lpSrcStartBits,指向源DIB的起始像素的指针
* long lWidth,源DIB图象宽度
* long lHeight,源DIB图象高度
* long lLineBytes,源DIB图象字节宽度(4的倍数)
* WORD palSize,源DIB图象调色板大小
* long lDstWidth,目标图象宽度
* long lDstHeight,目标DIB图象高度
* long lDstLineBytes,目标DIB图象行字节数(4的倍数)
* float fhorRatio,水平缩放比率
* float fverRatio,垂直缩放比率
* 函数类型:HGLOBAL
* 函数功能:用来缩放DIB图象
************************************************************************/
HGLOBAL Zoom(LPSTR lpSrcDib, LPSTR lpSrcStartBits,long lWidth, long lHeight,
long lLineBytes, WORD palSize,long lDstWidth,long lDstLineBytes,long lDstHeight,
float fhorRatio,float fverRatio)
{
LPSTR lpDstDib; //指向临时图象的指针
long i; //行循环变量
long j; //列循环变量
long i1; //行循环变量
long j1; //列循环变量
LPSTR lpSrcDIBBits; //指向源像素的指针
LPSTR lpDstDIBBits; //指向临时图象对应像素的指针
LPSTR lpDstStartBits; //指向临时图象对应像素的指针
LPBITMAPINFOHEADER lpbmi;// 指向BITMAPINFO结构的指针
// 分配内存,以保存缩放后的DIB
HGLOBAL hDIB = (HGLOBAL) ::GlobalAlloc(GHND, lDstLineBytes* lDstHeight + *(LPDWORD)lpSrcDib +palSize);
if (hDIB == NULL)// 判断是否是有效的DIB对象
{
return FALSE;// 不是,则返回
}
lpDstDib= (char * )::GlobalLock((HGLOBAL) hDIB);// 锁定内存
memcpy(lpDstDib, lpSrcDib, *(LPDWORD)lpSrcDib +palSize);// 复制DIB信息头和调色板
lpDstStartBits=lpDstDib+ *(LPDWORD)lpDstDib// 找到新DIB像素起始位置
+palSize;// 求像素起始位置,作用如同::FindDIBBits(lpSrcDib),这里尝试使用了这种方法,以避免对全局函数的调用
lpbmi = (LPBITMAPINFOHEADER)lpDstDib;// 获取指针
lpbmi->biWidth = lDstWidth;// 更新DIB中图象的高度和宽度
lpbmi->biHeight =lDstHeight;
for(i = 0; i < lDstHeight; i++)// 行操作
{
for(j = 0; j < lDstWidth; j++)// 列操作
{
// 指向新DIB第i行,第j个像素的指针
lpDstDIBBits= (char *)lpDstStartBits + lDstLineBytes * (lDstHeight-1-i)+j;
i1= (long) (i / fverRatio + 0.5);// 计算该像素在源DIB中的坐标
j1= (long) (j / fhorRatio + 0.5);
if( (j1>= 0) && (j1< lWidth) && (i1>= 0) && (i1< lHeight))
{// 判断是否在源图内
lpSrcDIBBits= (char *)lpSrcStartBits+ lLineBytes * (lHeight - 1 -i1) + j1;// 指向源DIB第i行,第j个像素的指针
*lpDstDIBBits= *lpSrcDIBBits;// 复制像素
}
else
{
* ((unsigned char*)lpDstDIBBits) = 255;// 源图中不存在的像素,赋为255
}
}
}
return hDIB;
}
/*************************************************************************
* 函数名称:FastFourierTran(CplexNum * pTd, CplexNum* pFd, int power)
* 函数参数:
* CplexNum * pTd,指向时域数组的指针
* CplexNum * pFd,指向频域数组的指针
* int power,2的幂数,即迭代次数
* 函数类型:void
函数功能:用来实现快速付立叶变换
************************************************************************/
void FastFourierTran(CplexNum * pTd, CplexNum * pFd, int power)
{
long i; //行循环变量
long j; //列循环变量
long dotCount;// 付立叶变换点数
int k;// 循环变量
int bfsize,p;// 中间变量
double angle;// 角度
CplexNum *pWn,*temReg1,*temReg2,*temReg;
dotCount= 1 <<power;// 计算付立叶变换点数
pWn= new CplexNum[sizeof(CplexNum)*dotCount/ 2];// 分配运算所需存储器
temReg1 = new CplexNum[sizeof(CplexNum)*dotCount];
temReg2 = new CplexNum[sizeof(CplexNum)*dotCount];
for(i = 0; i < dotCount/ 2; i++)// 计算加权系数
{
angle = -i * pi* 2 / dotCount;
pWn[i].re = cos(angle);
pWn[i].im=sin(angle);
}
memcpy(temReg1, pTd, sizeof(CplexNum)*dotCount);// 将时域点写入temReg1
for(k = 0; k < power; k++)// 采用蝶形算法进行快速付立叶变换
{
for(j = 0; j < 1 << k; j++)
{
bfsize = 1 << (power-k);
for(i = 0; i < bfsize / 2; i++)
{
p = j * bfsize;
temReg2[i+p]=Add(temReg1[i+p],temReg1[i+p+bfsize/2]);
temReg2[i+p+bfsize/2]=Mul(Sub(temReg1[i+p],temReg1[i+p+bfsize/2]),
pWn[i*(1<<k)]);
}
}
temReg = temReg1;
temReg1 = temReg2;
temReg2 = temReg;
}
for(j = 0; j <dotCount; j++)// 重新排序
{
p = 0;
for(i = 0; i <power; i++)
{
if (j&(1<<i))
{
p+=1<<(power-i-1);
}
}
pFd[j]=temReg1[p];
}
delete pWn;// 释放内存
delete temReg1;
delete temReg2;
}
/*************************************************************************
*
* 函数名称:InverFastFourierTran(CplexNum * pFd, CplexNum * pTd, int power)
* 函数参数:
* CplexNum * pFd,指向频域值的指针
* CplexNum * pTd,指向时域值的指针
* int power ,2的幂数
*函数类型:void
*函数功能:用来实现快速付立叶反变换
************************************************************************/
void InverFastFourierTran(CplexNum * pFd, CplexNum * pTd, int power)
{
LONG dotCount; // 付立叶变换点数
int i; // 循环变量
CplexNum *pX;
dotCount= 1 << power; // 计算付立叶变换点数
pX = new CplexNum[sizeof(CplexNum)*dotCount];// 分配运算所需存储器
memcpy(pX, pFd, sizeof(CplexNum)*dotCount);// 将频域点写入X
for(i = 0; i <dotCount; i++)// 求共轭
{
pX[i].re= pX[i].re;
pX[i].im=-pX[i].im;
}
FastFourierTran(pX, pTd, power);// 调用快速付立叶变换
for(i = 0; i <dotCount; i++)// 求时域点的共轭
{
pTd[i].re =pTd[i].re/dotCount;
pTd[i].im=-pTd[i].im/dotCount;
}
delete pX; // 释放内存
}
/*************************************************************************
* 函数名称:Fourier(CplexNum * pTd, int lWidth, int lHeight, CplexNum * pFd)
* 函数参数:
* CplexNum * pTd,指向时域值的指针
* int lWidth,图象宽度
* int lHeight,图象高度
* CplexNum * pFd ,指向频域值的指针
*
You can’t perform that action at this time.
