Repository navigation
Expand file tree
/
Copy pathZJU-ImageProcessing-C Language.c
More file actions
1313 lines (1170 loc) · 30.4 KB
/
Copy pathZJU-ImageProcessing-C Language.c
File metadata and controls
1313 lines (1170 loc) · 30.4 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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define BITMAPFILEHEADERLENGTH 14 // The bmp FileHeader length is 14
#define BM 19778 // The ASCII code for BM
//================My file 1 start=============//
#define MAXSIZE 10000
unsigned char Y[MAXSIZE][MAXSIZE],output_Y[MAXSIZE][MAXSIZE];
unsigned char U[MAXSIZE][MAXSIZE],output_U[MAXSIZE][MAXSIZE];
unsigned char V[MAXSIZE][MAXSIZE],output_V[MAXSIZE][MAXSIZE];
void RGBtoYUV();
void YUVtoGray();
void YUVtoRGB();
void ChangeTheLuminance();
#define SourceFile "C:\\Users\\Administrator\\Desktop\\air.bmp"
#define OutputFile1 "C:\\Users\\Administrator\\Desktop\\air1.bmp"
#define OutputFile2 "C:\\Users\\Administrator\\Desktop\\air2.bmp"
#define OutputFile3 "C:\\Users\\Administrator\\Desktop\\air3.bmp"
#define OutputFile4 "C:\\Users\\Administrator\\Desktop\\air4.bmp"
#define OutputFile5 "C:\\Users\\Administrator\\Desktop\\air5.bmp"
#define OutputFile6 "C:\\Users\\Administrator\\Desktop\\air6.bmp"
#define OutputFile7 "C:\\Users\\Administrator\\Desktop\\air7.bmp"
//================My file 1 end=============//
//================My file 2 start=============//
void YUVtoBinarization();
#define elementline 3
void Dilation_Lining(char dowhat);
void Erosion();
void Opening_Closing(char dowhat);
void BetweenTwoOperationProcessing();
//================My file 2 end=============//
//================My file 3 end=============//
void VisibilityEnhancement();
void HistogramEqualization_r();
void HistogramEqualization_g();
void HistogramEqualization_b();
void HistogramEqualization_Y();
//================My file 3 end=============//
//================My file 4 start==========//
void Scaling(float ratioX,float ratioY);
void Rotation(double angle);
double Gaussian(int radial,double lamda);
void RBF_Gaussian(unsigned char temple[],int x,int y,int paramenter);
void Shearing(float dx,float dy);
void mirror(char character);
void translation(int sizeX,int sizeY,int sizeX1,int sizeY1,int x,int y);
void translation1(int sizeX,int sizeY,int sizeX1,int sizeY1,int x,int y);
//================My file 4 end=============//
//================My file 5 start==========//
void meanFiltering(int paramenter);
void MeanFilter(unsigned char temple[],int x,int y,int paramenter);
unsigned char Laplacian_filter_Y(int x,int y);
void LaplacianFilter_Y(double paramenter);
void Meanfilter_Y(int paramenter);
//================My file 5 end=============//
//================My file 6 start==========//
void BilateralFilter(int paramenter,double lamda_range,double lamda_color);
void RBF_Bilateral(unsigned char temple[],int x,int y,int paramenter,double lamda_range,double lamda_color);
//================My file 6 end=============//
//================Someone's work===========//
/* Test the file is bmp file or not */
void bmpFileTest(FILE* fpbmp);
/* To get the OffSet of header to data part */
void bmpHeaderPartLength(FILE* fpbmp);
/* To get the width and height of the bmp file */
void BmpWidthHeight(FILE* fpbmp);
//get r,g,b data
void bmpDataPart(FILE* fpbmp);
// output data to corresponding txt file
void bmpoutput(FILE *fpout);
//===========Someone's work end===========//
//===========Rewrite File ================//
void Initialization();
//open a picture
FILE *openfile(char filename[]);
//creat a picture
FILE *writefile(char filename[]);
//add head to file
void addHeadertofile(FILE *input,FILE *output);
//===========Rewrite File End================//
//============TEST FUNCTION=================//
void testInputYUV();
void testOutputYUV();
void testRGB();
void NoneProcess();
//=============TEST FUNCTION END============//
unsigned int OffSet = 0; // OffSet from Header part to Data Part
long width; // The Width of the Data Part
long height; // The Height of the Data Part
unsigned char r[MAXSIZE][MAXSIZE], output_r[MAXSIZE][MAXSIZE];
unsigned char g[MAXSIZE][MAXSIZE], output_g[MAXSIZE][MAXSIZE];
unsigned char b[MAXSIZE][MAXSIZE], output_b[MAXSIZE][MAXSIZE];
unsigned char records[MAXSIZE][MAXSIZE];
int main(int argc, char* argv[])
{
/* Open bmp file */
unsigned char *fp_temp;
FILE *fpbmp;
FILE *fpout;
Initialization();
//===============first picture -- Bilateral Filter==========
/*Standerd operations for file i/o*/
fpbmp=openfile(SourceFile);
bmpDataPart(fpbmp);
fpout=writefile(OutputFile1);
addHeadertofile(fpbmp,fpout);
/*your operations for your picture*/
bmpoutput(fpout);
/*Standerd operations for close*/
fclose(fpbmp);
fclose(fpout);
return 0;
}
void bmpoutput(FILE* fpout)
{
long i, j = 0;
long stride;
unsigned char* pixout = NULL;
stride = (24 * width + 31) / 8;
stride = stride / 4 * 4;
pixout = malloc(stride);
fseek(fpout, OffSet, SEEK_SET);
for (j = 0; j<height; j++)
{
for (i = 0; i<width; i++)
{
pixout[i * 3 + 2] = output_r[height - 1 - j][i];
pixout[i * 3 + 1] = output_g[height - 1 - j][i];
pixout[i * 3] = output_b[height - 1 - j][i];
}
fwrite(pixout, 1, stride, fpout);
}
}
void bmpDataPart(FILE* fpbmp)
{
int i, j = 0;
int stride;
unsigned char* pix = NULL;
fseek(fpbmp, OffSet, SEEK_SET);
stride = (24 * width + 31) / 8;
stride = stride / 4 * 4;
pix = malloc(stride);
for (j = 0; j<height; j++)
{
fread(pix, 1, stride, fpbmp);
for (i = 0; i<width; i++)
{
r[height - 1 - j][i] = pix[i * 3 + 2];
g[height - 1 - j][i] = pix[i * 3 + 1];
b[height - 1 - j][i] = pix[i * 3];
output_r[height - 1 - j][i] = 255;
output_g[height - 1 - j][i] = 255;
output_b[height - 1 - j][i] = 255;
}
}
}
void bmpFileTest(FILE* fpbmp)
{
unsigned short bfType = 0;
fseek(fpbmp, 0L, SEEK_SET);//seek_set 起始位置
fread(&bfType, sizeof(char), 2, fpbmp);
if (BM != bfType)
{
printf("This file is not bmp file.!!!\n");
exit(1);
}
}
/* To get the OffSet of header to data part */
void bmpHeaderPartLength(FILE* fpbmp)
{
fseek(fpbmp, 10L, SEEK_SET);
fread(&OffSet, sizeof(char), 4, fpbmp);
printf("The Header Part is of length %d.\n", OffSet);
}
/* To get the width and height of the bmp file */
void BmpWidthHeight(FILE* fpbmp)
{
int size;
fseek(fpbmp, 18L, SEEK_SET);
fread(&width, sizeof(char), 4, fpbmp);
fseek(fpbmp, 2L, SEEK_SET);
fread(&size, sizeof(char), 4, fpbmp);
printf("The Size of the bmp file is %ld.\n", size);
fseek(fpbmp, 22L, SEEK_SET);
fread(&height, sizeof(char), 4, fpbmp);
printf("The Width of the bmp file is %ld.\n", width);
printf("The Height of the bmp file is %ld.\n", height);
}
//=====================here is what I write=================
void RGBtoYUV()
{
//convert
for (int j = 0; j<height; j++)
{
for (int i = 0; i<width; i++)
{
Y[j][i]= 0.299*r[j][i] + 0.587*g[j][i] + 0.114*b[j][i];
U[j][i]= -0.147*r[j][i] - 0.289*g[j][i] + 0.436*b[j][i] ;
V[j][i]= 0.615*r[j][i] - 0.515*g[j][i] - 0.100*b[j][i];
}
}
}
void YUVtoGray()//output YUV
{
for (int j = 0; j<height; j++)
{
for (int i = 0; i<width; i++)
{
output_Y[j][i]=Y[j][i];
output_U[j][i]=0;
output_V[j][i]=0;
}
}
}
void YUVtoRGB()
{
for (int j = 0; j<height; j++)
{
for (int i = 0; i<width; i++)
{
output_r[j][i] = output_Y[j][i] + 1.140*output_V[j][i];
output_g[j][i] = output_Y[j][i] - 0.394*output_U[j][i] - 0.581*output_V[j][i];
output_b[j][i] = output_Y[j][i] + 2.032*output_U[j][i];
}
}
}
void ChangeTheLuminance()
{
for (int j = 0; j<height; j++)
{
for (int i = 0; i<width; i++)
{
output_Y[j][i]=255-Y[j][i];
output_U[j][i]=U[j][i];
output_V[j][i]=V[j][i];
}
}
}
void YUVtoBinarization()//output YUV
{
//OTSU althrithm '大津算法'
unsigned char T=0,perfect_T=0;//Threshold
double w0,w1,u0,u1,u;//paramenter
long number_below_T = 0L,number_above_T = 0L;//paramenter
double g[255]={0.0},max_g=0.0;//result;
for(int num=0;num<=255;num++,T++)//after this there is a max g
{
/*Pre calcu*/
long all_below_T=0L,all_above_T=0L;
number_below_T=0L,number_above_T=0L;
for (int j = 0; j<height; j++)
{
for (int i = 0; i<width; i++)
{
if(T>=Y[j][i]){
number_below_T++;
all_below_T+=Y[j][i];
}
else{
number_above_T++;
all_above_T+=Y[j][i];
}
}
}
if(number_below_T==0||number_above_T==0) continue;
u0=(double)all_below_T/number_below_T;
u1=(double)all_above_T/number_above_T;
/*Step2*/
w0=(double)number_below_T/(height*width);
w1=(double)number_above_T/(height*width);
u=w0*u0+w1*u1;
g[num]=w0*pow(abs(u0-u),2.0)+w1*pow(abs(u1-u),2.0);
/*Step 3 find max g*/
if(g[num]>max_g){
max_g=g[num];
perfect_T=T;
}
}
//'用算出来的perfect_T解出二值图'
for (int j = 0; j<height; j++)
{
for (int i = 0; i<width; i++)
{
if(Y[j][i]<perfect_T)
output_Y[j][i]=0;
else
output_Y[j][i]=255;
output_U[j][i]=0;
output_V[j][i]=0;
}
}
printf("Binarization finished!\n");
}
void Dilation_Lining(char dowhat)
{
int Struture_Element[elementline][elementline]={{0}};//假设是十字型
int centre=(elementline-1)/2;
/*自动生成十字形element*/
for (int j = 0; j<elementline; j++)
{
for (int i = 0; i<elementline; i++)
{
if(j==centre||i==centre)
Struture_Element[j][i]=1;
else
Struture_Element[j][i]=0;
}
}
/*叠加原图*/
for (int j = 0; j<height; j++)
{
for (int i = 0; i<width; i++)
{
output_Y[j][i]=Y[j][i];
output_U[j][i]=0;
output_V[j][i]=0;
}
}
/*膨胀一小圈*/
for (int j = 0; j<height-elementline; j++)
{
for (int i = 0; i<width-elementline; i++)
{
int Istrue=0;
for(int i1=0;i1<elementline;i1++)
for(int j1=0;j1<elementline;j1++)
{
if(Struture_Element[i1][j1]==1&&Y[j+i1][i+j1]==0)
Istrue=1;
}
/*大概,YUV中黑色是。。。Y=0 (T^T)*/
if(Istrue==1)
output_Y[j+centre][i+centre]=dowhat=='d'?0:255;
}
}
printf("Dilation successed!\n");
}
void Erosion()
{
int Struture_Element[elementline][elementline]={{0}};//假设是十字型
int centre=(elementline-1)/2;
/*自动生成十字形element*/
for (int j = 0; j<elementline; j++)
{
for (int i = 0; i<elementline; i++)
{
if(j==centre||i==centre)
Struture_Element[j][i]=1;
else
Struture_Element[j][i]=0;
}
}
/*叠加原图*/
for (int j = 0; j<height; j++)
{
for (int i = 0; i<width; i++)
{
output_Y[j][i]=255;
output_U[j][i]=0;
output_V[j][i]=0;
}
}
/*缩小一小圈*/
for (int j = 0; j<height-elementline; j++)
{
for (int i = 0; i<width-elementline; i++)
{
int Istrue=0;
for(int i1=0;i1<elementline;i1++)
for(int j1=0;j1<elementline;j1++)
{
if(Struture_Element[i1][j1]==0)
continue;
else if(Struture_Element[i1][j1]==1&&Y[j+i1][i+j1]!=0)//不同时黑色
Istrue=1;//不全等
}
//printf("%d",Istrue);
/*大概,YUV中黑色是。。。Y=0 (T^T)*/
if(Istrue==0)
output_Y[j+centre][i+centre]=0;//所有情况都成立!
}
}
printf("Erosion successed!\n");
}
void Opening_Closing(char dowhat)
{
if(dowhat=='o'){
Erosion();
BetweenTwoOperationProcessing();
Dilation_Lining('d');
printf("Opening finished, Now outer noises are removed!\n");
}
else if(dowhat=='c'){
Dilation_Lining('d');
BetweenTwoOperationProcessing();
Erosion();
printf("Closing finished, Now inner noises are removed!\n");
}
else
printf("Opening_Closing fail!\n");
}
void BetweenTwoOperationProcessing()
{
for (int j = 0; j<height; j++)
{
for (int i = 0; i<width; i++)
{
Y[j][i] = output_Y[j][i];
U[j][i] = output_U[j][i];
V[j][i] = output_V[j][i];
}
}
}
//============================3==============================
void VisibilityEnhancement()
{
int maxLumnance = 0;
for (int j = 0; j<height; j++)
{
for (int i = 0; i<width; i++)
{
if(Y[j][i]>maxLumnance)
maxLumnance=Y[j][i];
}
}
for (int j = 0; j<height; j++)
{
for (int i = 0; i<width; i++)
{
output_Y[j][i]=(unsigned char)255*(log10(Y[j][i]+1))/(log10(maxLumnance+1));
output_U[j][i]=0;
output_V[j][i]=0;
}
}
}
void HistogramEqualization_r()
{
//number 统计
long n0[256]={0.0};
/*0-255 直方图X3 RGB*/
for (int j = 0; j<height; j++)
{
for (int i = 0; i<width; i++)
{
n0[r[j][i]]++;
}
}
/*0-1/255-...-254/255,1*/
double p0[256]={0.0};
for(int i=0;i<256;i++)
p0[i]=n0[i]/(1.0*height*width);
/*n[k]0-255:number*/
/*Pn[n]*/
double s0[256]={0.0};
s0[0]=p0[0];
for (int i=1;i<256;i++)
s0[i]=s0[i-1]+p0[i];
//---------------------------------
/*s[k]=求和Pn[0+..+k]*/
/*找距离s[k]最近的像素点s[x]*/
double minmize=1.0;
int min_number[256]={0};
double diff=0.0;
for(int j = 0;j<256;j++)
{
//printf("!! == %f\n",s0[j]);
diff=0.0;minmize=1.0;
for(int i=0;i<256;i++)
{
double pix0=1.0*i/256;
diff = fabs(s0[j]-pix0);
//printf("%d == %f\n",i,diff);
if(diff<minmize){
minmize=diff;
min_number[j]=i;//min_number[j] --> i ;
}
}
//printf("%d == %d\n",j,min_number[j]);
// break;
}
//输出
for (int j = 0; j<height; j++)
{
for (int i = 0; i<width; i++)
{
output_r[j][i]=min_number[r[j][i]];
}
}
/*统计临近的相同的像素点*/
/*计算最终的而结果并画直方图*/
}void HistogramEqualization_g()
{
//number 统计
long n0[256]={0.0};
/*0-255 直方图X3 RGB*/
for (int j = 0; j<height; j++)
{
for (int i = 0; i<width; i++)
{
n0[g[j][i]]++;
}
}
/*0-1/255-...-254/255,1*/
double p0[256]={0.0};
for(int i=0;i<256;i++)
p0[i]=n0[i]/(1.0*height*width);
/*n[k]0-255:number*/
/*Pn[n]*/
double s0[256]={0.0};
s0[0]=p0[0];
for (int i=1;i<256;i++)
s0[i]=s0[i-1]+p0[i];
//---------------------------------
/*s[k]=求和Pn[0+..+k]*/
/*找距离s[k]最近的像素点s[x]*/
double minmize=1.0;
int min_number[256]={0};
double diff=0.0;
for(int j = 0;j<256;j++)
{
//printf("!! == %f\n",s0[j]);
diff=0.0;minmize=1.0;
for(int i=0;i<256;i++)
{
double pix0=1.0*i/256;
diff = fabs(s0[j]-pix0);
//printf("%d == %f\n",i,diff);
if(diff<minmize){
minmize=diff;
min_number[j]=i;//min_number[j] --> i ;
}
}
//printf("%d == %d\n",j,min_number[j]);
// break;
}
//输出
for (int j = 0; j<height; j++)
{
for (int i = 0; i<width; i++)
{
output_g[j][i]=min_number[g[j][i]];
}
}
/*统计临近的相同的像素点*/
/*计算最终的而结果并画直方图*/
}void HistogramEqualization_b()
{
//number 统计
long n0[256]={0.0};
/*0-255 直方图X3 RGB*/
for (int j = 0; j<height; j++)
{
for (int i = 0; i<width; i++)
{
n0[b[j][i]]++;
}
}
/*0-1/255-...-254/255,1*/
double p0[256]={0.0};
for(int i=0;i<256;i++)
p0[i]=n0[i]/(1.0*height*width);
/*n[k]0-255:number*/
/*Pn[n]*/
double s0[256]={0.0};
s0[0]=p0[0];
for (int i=1;i<256;i++)
s0[i]=s0[i-1]+p0[i];
//---------------------------------
/*s[k]=求和Pn[0+..+k]*/
/*找距离s[k]最近的像素点s[x]*/
double minmize=1.0;
int min_number[256]={0};
double diff=0.0;
for(int j = 0;j<256;j++)
{
//printf("!! == %f\n",s0[j]);
diff=0.0;minmize=1.0;
for(int i=0;i<256;i++)
{
double pix0=1.0*i/256;
diff = fabs(s0[j]-pix0);
//printf("%d == %f\n",i,diff);
if(diff<minmize){
minmize=diff;
min_number[j]=i;//min_number[j] --> i ;
}
}
//printf("%d == %d\n",j,min_number[j]);
// break;
}
//输出
for (int j = 0; j<height; j++)
{
for (int i = 0; i<width; i++)
{
output_b[j][i]=min_number[b[j][i]];
}
}
/*统计临近的相同的像素点*/
/*计算最终的而结果并画直方图*/
}
void HistogramEqualization_Y()
{
//number 统计
long n0[256]={0.0};
/*0-255 直方图X3 RGB*/
for (int j = 0; j<height; j++)
{
for (int i = 0; i<width; i++)
{
n0[Y[j][i]]++;
}
}
/*0-1/255-...-254/255,1*/
double p0[256]={0.0};
for(int i=0;i<256;i++)
p0[i]=n0[i]/(1.0*height*width);
/*n[k]0-255:number*/
/*Pn[n]*/
double s0[256]={0.0};
s0[0]=p0[0];
for (int i=1;i<256;i++)
s0[i]=s0[i-1]+p0[i];
//---------------------------------
/*s[k]=求和Pn[0+..+k]*/
/*找距离s[k]最近的像素点s[x]*/
double minmize=1.0;
int min_number[256]={0};
double diff=0.0;
for(int j = 0;j<256;j++)
{
//printf("!! == %f\n",s0[j]);
diff=0.0;minmize=1.0;
for(int i=0;i<256;i++)
{
double pix0=1.0*i/256;
diff = fabs(s0[j]-pix0);
//printf("%d == %f\n",i,diff);
if(diff<minmize){
minmize=diff;
min_number[j]=i;//min_number[j] --> i ;
}
}
//printf("%d == %d\n",j,min_number[j]);
// break;
}
//输出
for (int j = 0; j<height; j++)
{
for (int i = 0; i<width; i++)
{
output_Y[j][i]=min_number[Y[j][i]];
}
}
/*统计临近的相同的像素点*/
/*计算最终的而结果并画直方图*/
}
//--------------------------4-----------------------------
void translation(int sizeX,int sizeY,int sizeX1,int sizeY1,int x,int y)
{
int translatingMatrix[3][3] = {{1,0,x},
{0,1,y},
{0,0,1}};
//int x1 = translatingMatrix[0][0]*x0+translatingMatrix[0][1]*y0+translatingMatrix[0][2];
//int y1 = translatingMatrix[1][0]*x0+translatingMatrix[1][1]*y0+translatingMatrix[1][2];
if(sizeX>sizeX1||sizeY>sizeY1){
printf("Be careful to mentain this: sizeX<=sizeX1 and sizeY<=sizeY1.\n");
return;
}
for (int y0 = 0; y0<height; y0++)
{
for (int x0 = 0; x0<width; x0++)
{
output_r[y0][x0]= r[y0][x0];
output_g[y0][x0]= g[y0][x0];
output_b[y0][x0]= b[y0][x0];
}
}
for (int y0 = sizeY; y0<sizeY1; y0++)
{
for (int x0 = sizeX; x0<sizeX1; x0++)
{
int x1 = translatingMatrix[0][0]*x0+translatingMatrix[0][1]*y0+translatingMatrix[0][2];
int y1 = translatingMatrix[1][0]*x0+translatingMatrix[1][1]*y0+translatingMatrix[1][2];
output_r[y1][x1]= r[y0][x0];
output_g[y1][x1]= g[y0][x0];
output_b[y1][x1]= b[y0][x0];
}
}
for (int y0 = sizeY+y; y0<sizeY1+y; y0++)
{
for (int x0 = sizeX+x; x0<sizeX1+x; x0++)
{
int x1 = translatingMatrix[0][0]*x0+translatingMatrix[0][1]*y0-translatingMatrix[0][2];
int y1 = translatingMatrix[1][0]*x0+translatingMatrix[1][1]*y0-translatingMatrix[1][2];
output_r[y1][x1]= r[y0][x0];
output_g[y1][x1]= g[y0][x0];
output_b[y1][x1]= b[y0][x0];
}
}
}
void translation1(int sizeX,int sizeY,int sizeX1,int sizeY1,int x,int y)
{
int translatingMatrix[3][3] = {{1,0,x},
{0,1,y},
{0,0,1}};
//int x1 = translatingMatrix[0][0]*x0+translatingMatrix[0][1]*y0+translatingMatrix[0][2];
//int y1 = translatingMatrix[1][0]*x0+translatingMatrix[1][1]*y0+translatingMatrix[1][2];
if(sizeX>sizeX1||sizeY>sizeY1){
printf("Be careful to mentain this: sizeX<=sizeX1 and sizeY<=sizeY1.\n");
return;
}
for (int y0 = 0; y0<height; y0++)
{
for (int x0 = 0; x0<width; x0++)
{
output_r[y0][x0]= 255;
output_g[y0][x0]= 255;
output_b[y0][x0]= 255;
}
}
for (int y0 = sizeY; y0<sizeY1; y0++)
{
for (int x0 = sizeX; x0<sizeX1; x0++)
{
int x1 = translatingMatrix[0][0]*x0+translatingMatrix[0][1]*y0+translatingMatrix[0][2];
int y1 = translatingMatrix[1][0]*x0+translatingMatrix[1][1]*y0+translatingMatrix[1][2];
output_r[y1][x1]= r[y0][x0];
output_g[y1][x1]= g[y0][x0];
output_b[y1][x1]= b[y0][x0];
}
}
}
void mirror(char character)
{
if(character=='x')
for (int y0 = 0; y0<height; y0++)
{
for (int x0 = 0; x0<width; x0++)
{
output_r[y0][x0]= r[y0][width-x0];
output_g[y0][x0]= g[y0][width-x0];
output_b[y0][x0]= b[y0][width-x0];
}
}
else if(character=='y')
for (int y0 = 0; y0<height; y0++)
{
for (int x0 = 0; x0<width; x0++)
{
output_r[y0][x0]= r[height-y0][x0];
output_g[y0][x0]= g[height-y0][x0];
output_b[y0][x0]= b[height-y0][x0];
}
}
else
printf("something wrong with your input.");
}
void Scaling(float ratioX,float ratioY)
{
if(ratioX<=0||ratioY<=0){
printf("Scaling paramenter wrong!\n");
return;
}
if(ratioX<=1&&ratioY<=1){
for (int y0 = 0; y0<height; y0++)
{
for (int x0 = 0; x0<width; x0++)
{
int x1 = ratioX*x0;
int y1 = ratioY*y0;
output_r[y1][x1]= r[y0][x0];
output_g[y1][x1]= g[y0][x0];
output_b[y1][x1]= b[y0][x0];
}
}
}
else if(ratioX>1||ratioY>1){
for (int y0 = 0; y0<height; y0++)
{
for (int x0 = 0; x0<width; x0++)
{
int x1 = ratioX*x0;
int y1 = ratioY*y0;
output_r[y1][x1]= r[y0][x0];
output_g[y1][x1]= g[y0][x0];
output_b[y1][x1]= b[y0][x0];
records[y1][x1]=1;
}
}
for (int y0 = 0; y0<height*ratioY; y0++)
{
for (int x0 = 0; x0<width*ratioX; x0++)
{
if(records[y0][x0]==0){
if(ratioX>1&&ratioY>1){
output_r[y0][x0]= r[(int)(y0/ratioY)][(int)(x0/ratioX)];
output_g[y0][x0]= g[(int)(y0/ratioY)][(int)(x0/ratioX)];
output_b[y0][x0]= b[(int)(y0/ratioY)][(int)(x0/ratioX)];
}
else if(ratioX<=1&&ratioY>1){
output_r[y0][x0]= r[(int)(y0/ratioY)][x0];
output_g[y0][x0]= g[(int)(y0/ratioY)][x0];
output_b[y0][x0]= b[(int)(y0/ratioY)][x0];
}
else if(ratioX>1&&ratioY<=1){
output_r[y0][x0]= r[y0][(int)(x0/ratioX)];
output_g[y0][x0]= g[y0][(int)(x0/ratioX)];
output_b[y0][x0]= b[y0][(int)(x0/ratioX)];
}
}
}
}
}
}
double Gaussian(int radial,double lamda)
{
return exp(-pow(radial,2)/(2*pow(lamda,2)));
}
void RBF_Gaussian(unsigned char temple[],int x,int y,int paramenter)//3
{
/*Paramenter is used to control how many points are included included in this process*/
//L2 fomular
double lamda = 0.4;//paramenter_X
double w0 = 0.0;
for (int y0 = y-paramenter; y0<=y+paramenter; y0++)
{
for (int x0 = x-paramenter; x0<=x+paramenter; x0++)
{
if(x0==x&&y0==y) continue;
double radial = abs(x-x0)+abs(y-y0);//sqrt(pow((x-x0),2)+pow((y-y0),2));
w0 += Gaussian(radial,lamda);
}
}
double pixelout[3] = {0.0};
for (int y0 = y-paramenter; y0<=y+paramenter; y0++)
{
for (int x0 = x-paramenter; x0<=x+paramenter; x0++)
{
if(x0==x&&y0==y)
continue;
double radial = abs(x-x0)+abs(y-y0);//sqrt(pow((x-x0),2)+pow((y-y0),2));
double w1 = Gaussian(radial,lamda)/w0;
pixelout[0] += w1*output_r[y0][x0];
pixelout[1] += w1*output_g[y0][x0];
pixelout[2] += w1*output_b[y0][x0];
}
}
for(int i=0;i<3;i++) temple[i] = (unsigned char)pixelout[i];//output!
//printf(" %d\n",temple[0]);
}
void Rotation(double angle)
{
angle = (angle-90)/180*3.1415;
for (int y0 = 0; y0<height; y0++)
{
for (int x0 = 0; x0<width; x0++)
{
//int x1 = cos(angle)*x0-sin(angle)*y0;
//int y1 = sin(angle)*x0+cos(angle)*y0;
int x1 = sin(angle)*(width/2-x0)+cos(angle)*(height/2-y0);
int y1 = sin(angle)*(height/2-y0)-cos(angle)*(width/2-x0);
y1+=height/2;x1+=width/2;
records[y1][x1] = 1;
output_r[y1][x1]= r[y0][x0];
output_g[y1][x1]= g[y0][x0];
output_b[y1][x1]= b[y0][x0];
}
}
for (int y0 = 0; y0<height; y0++)
{
for (int x0 = 0; x0<width; x0++)
{
if(records[y0][x0] != 1)
{
/*指针传递三个参数值*/
unsigned char temple[3];
RBF_Gaussian(temple,x0,y0,3);
output_r[y0][x0]=temple[0];
output_g[y0][x0]=temple[1];
output_b[y0][x0]=temple[2];
}
}
}
}
void Shearing(float dx,float dy)
{
for (int y0 = 0; y0<height; y0++)
{
for (int x0 = 0; x0<width; x0++)
{
int x1=0;