-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImage.cpp
1121 lines (943 loc) · 26 KB
/
Image.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: Image.cpp
* Author: Joy_Admin
*
* Created on 17 octobre 2016, 12:35
*/
#include "Image.h"
#include <fstream> // ifstream
#include <sstream> //sstream
#include <vector> //vector
#include <cstdlib>
#include <cmath>
using namespace std;
Image::Image(){
}
bool Image::test(string file)
{
int heig = 0, wid = 0;
ifstream infile(file.c_str());
stringstream ss;
string inputLine = "";
// First line : version
getline(infile,inputLine);
if(inputLine.compare("P2") != 0)
{
cerr << "Erreur!!!!!!, Impossible de travailler sur le fichier." << endl;
SetValide(false) ;
}
cout << "\n" << endl;
// Second line : comment
getline(infile,inputLine);
// Continue with a stringstream
ss << infile.rdbuf();
// Third line : size
ss >> wid >> heig;
infile.close();
return wid != 0 && heig!= 0;
}
Image::Image(bool val)
{
valide = val;
}
Image::Image(string file)
{
reload(file);
}
//*/
/**
* on charge une image
* @param output
* @return
*/
bool Image::reload(string output) {
string file = output;
valide = test(file);
if(valide)
{
int row = 0, col = 0, height = 0, width = 0;
ifstream infile(file.c_str());
stringstream ss;
string inputLine = "";
// First line : version
getline(infile,inputLine);
cout << "\t*****Analyse de votre fichier.*****\n" << endl;
if(inputLine.compare("P2") != 0) cerr << "Erreur!!!!!!!, Impossible de travailler sur le fichier.\n" << endl;
else
{
version = inputLine;
cout << "NomFichier : " << file << endl;
file = file.substr(0,file.size()-4);
SetfileName(file);
cout << "Version : " << inputLine << endl;
}
// Second line : comment
getline(infile,inputLine);
cout << "Comment : " << inputLine << endl;
// Continue with a stringstream
ss << infile.rdbuf();
// Third line : size
ss >> width >> height;
Setwidth(width);
Setheight(height);
cout << width << " columns and " << height << " rows" << endl;
// Fourth line : size
ss >> niveau;
SetNiveau(niveau);
cout << "Niveau " << niveau << endl;
array = new int* [height];
minval = niveau;
maxval = 0;
// Following lines : data
for(row = 0; row < height; ++row)
{
array[row] = new int[width];
for (col = 0; col < width; ++col)
{
ss >> array[row][col];
if ( array[row][col] > maxval )
{
maxval = array[row][col];
}
if ( array[row][col] < minval )
{
minval = array[row][col];
}
}
}
Setmaxval(maxval);
Setminval(minval);
/*/ Now print the array to see the result
for(row = 0; row < height; ++row) {
for(col = 0; col < width; ++col) {
cout << array[row][col] << " ";
}
cout << endl;
}//*/
infile.close();
cout << "\t*****Fin de l'analyse.*****\n" << endl;
SetLuminance(CalLuminance());
}
}
//on ecrit l'image de sortie
bool Image::writeImage(string output){
ofstream Flux(output.c_str());
if(Flux)
{
Flux<<version<<endl;
Flux<<"#Creator : Joy Jedidja Ndjama Version 1.1"<<endl;
Flux<<width<<" "<<height<<endl;
Flux<<niveau<<endl;
for(int row = 0; row < height; ++row)
for (int col = 0; col < width; ++col) Flux << array[row][col]<< endl;
return true;
}
else
{
cout<<"Erreur lors de l'ouverture du fichier"<<endl;
return false;
}
}
/**
*
* on calcule la luminance
* @return
*/
double Image::CalLuminance()
{
return CalLuminance(array);
}
/**
*
* on calcule la luminance
* @return
*/
double Image::CalLuminance(int** arr)
{
double luminance = 0;
for(int row = 0; row < height; ++row)
for (int col = 0; col < width; ++col) luminance += arr[row][col];
luminance /= (height * width);
return luminance;
}
/**
* on calcule le contraste
* @return
*/
double Image::CalContraste()
{
return CalContraste(array);
}
/**
* on calcule le contraste
* @return
*/
double Image::CalContraste(int** arr)
{
double contraste = 0.0;
for(int row = 0; row < height; ++row)
for (int col = 0; col < width; ++col) contraste += pow(arr[row][col] - luminance,2) ;
contraste = sqrt(contraste * 1/(width * height));
return contraste;
}
/**
* On construit une image connaissant tous ses pixels
* @param arr
* @return
*/
bool Image::writeImage(int** arr,int zoom1,int zoom2){
writeImage(arr,zoom1,zoom2,false);
}
bool Image::writeImage(int** arr, int i, int j, bool val) {
int wid = j * width;
int hei = i * height;
ofstream Flux(output.c_str());
if(Flux)
{
Flux<<version<<endl;
Flux<<"#Creator : Joy Jedidja Ndjama Version 1.1"<<endl;
Flux<<wid<<" "<<hei<<endl;
Flux<<niveau<<endl;
if(val)cout<<"\tEcriture du fichier resultat en cours ";
for(int row = 0; row < hei; ++row)
{ if(val) cout<<" ... ";
for (int col = 0; col < wid; ++col)
{
Flux <<arr[row][col]<<endl;
}
}
if(val) cout<<"\n Terminee "<<endl;
//Flux.close();
return true;
}
else
{
cout<<"Erreur lors de l'ouverture du fichier"<<endl;
//Flux.close();
return false;
}
}
/**
* On construit une image connaissant tous ses pixels
* @param arr
* @return
*/
bool Image::writeImage(int** arr){
writeImage(arr,1,1);
}
/**
* transformation linéaire
* @return
*/
bool Image::transformationLineaire()
{
SetOutput(GetfileName()+"Transformation_Lineaire.pgm");
transformationLineaireSaturation(minval,maxval);
}
/**
* transformation lineaire par saturation
* @param sMin
* @param sMax
* @return
*/
bool Image::transformationLineaireSaturation(int sMin, int sMax)
{
int pas = sMax - sMin;
if(pas == 255)
{
cout<<"\t Pas besoin d'effectuer de transformation lineaire puisse que le "
"pas optimal de cet image est de 255\n"<<endl;
cout<<"\t Vous obtiendrez la meme image en sortie\n"<<endl;
return false;
}
else
{
int** arr = new int* [width];
for(int row = 0; row < height; ++row)
{
arr[row] = new int[width];
for (int col = 0; col < width; ++col)
{
arr[row][col] = (255 * (array[row][col] - minval)) / (pas);
if ( arr[row][col] <= 0)
{
arr[row][col] = 0;
}
if (arr[row][col] >= 255)
{
arr[row][col] = 255;
}
}
}
if(sMin != minval)SetOutput(GetfileName()+"Transformation_Lineaire_Sat.pgm");
writeImage(arr);
cout<<"\tTransformation lineaire terminee\n"<<endl;
cout<<"\tConsultez l'image "<<output <<" pour voir le resultat\n"<<endl;
}
}
/**
* egalisation des histogrammes
* @return
*/
bool Image::egalisationHistogramme() {
//etape une: calcul de l'histogramme
int* histogramme = constructionHistogramme(array);
//etape deux: normalisation de l'histogramme
double* histogramme1 = normalistationHistogramme(histogramme);
//etape trois: densité de probalité normalisé
histogramme1 = densiteHistogramme(histogramme1);
//etape quatre: transformation des niveaux de gris de l'image
int** arr = new int* [width];
for(int row = 0; row < height; ++row)
{
arr[row] = new int[width];
for (int col = 0; col < width; ++col)
{
arr[row][col] = histogramme1[array[row][col]] * 255 ;
}
}
SetOutput(GetfileName()+"Egalisation.pgm");
writeImage(arr);
cout<<"\tEgalisation des histogrammes terminee\n"<<endl;
cout<<"\tConsultez l'image "<<output <<" pour voir le resultat\n"<<endl;
return true;
}
/**
* on construit l'histogramme de base
* @param arr
* @return
*/
int* Image::constructionHistogramme(int** arr) {
int* histogramme = new int[256];
for (int col = 0; col < 255; col++)
{
histogramme[col] = 0;
}
for(int row = 0; row < height; ++row)
for (int col = 0; col < width; ++col)
{
{
for(int i = 0; i < 255; i++)
{
if (array[row][col] == i)
{
++histogramme[i];
}
}
}
}
return histogramme;
}
/**
* on normalise l'histogramme
* @param histogramme
* @return
*/
double* Image::normalistationHistogramme(int* histogramme) {
double* histogramme1 = new double[256];
double pixel = width * height;
for (int col = 0; col < 255; col++)
{
histogramme1[col] = ( histogramme[col] / pixel);
}
return histogramme1;
}
/**
* on calcule la densité de probalité normalisé
* @param histogramme
* @return
*/
double* Image::densiteHistogramme(double* histogramme) {
double* histogramme1 = new double[256];
for (int col = 0; col < 255; col++)
{
for (int c = 0; c <= col; c++)
{
histogramme1[col] += histogramme[c];
}
}
return histogramme1;
}
/**
* on additionne deux images
* @param image
* @return
*/
bool Image::additionImage(Image& image) {
int** arr = new int* [width];
int** arr1 = image.GetArray();
for(int row = 0; row < height; ++row)
{
arr[row] = new int[width];
for (int col = 0; col < width; ++col)
{
arr[row][col] = arr1[row][col] + array[row][col] ;
if (arr[row][col] >= 255)
{
arr[row][col] = 255;
}
}
}
SetOutput(GetfileName()+"Addition.pgm");
bool result = writeImage(arr);
cout<<"\tAddition des deux images terminee\n"<<endl;
cout<<"\tConsultez l'image "<<output <<" pour voir le resultat\n"<<endl;
return result;
}
/**
* on additionne deux images
* @param image
* @return
*/
int** Image::additionImage(int** arr1,int** arr2) {
int** arr = new int* [width];
for(int row = 0; row < height; ++row)
{
arr[row] = new int[width];
for (int col = 0; col < width; ++col)
{
arr[row][col] = arr1[row][col] + arr2[row][col] ;
if (arr[row][col] >= 255)
{
arr[row][col] = 255;
}
}
}
return (arr);
}
/**
* on soustrait deux images
* @param image
* @return
*/
bool Image::soustractionImage(Image& image) {
int** arr = new int* [width];
int** arr1 = image.GetArray();
for(int row = 0; row < height; ++row)
{
arr[row] = new int[width];
for (int col = 0; col < width; ++col)
{
arr[row][col] = array[row][col] - arr1[row][col] ;
if (arr[row][col] <= 0)
{
arr[row][col] = 0;
}
}
}
SetOutput(GetfileName()+"Soustraction.pgm");
bool result = writeImage(arr);
cout<<"\tSoustraction des deux images terminee\n"<<endl;
cout<<"\tConsultez l'image "<<output <<" pour voir le resultat\n"<<endl;
return result;
}
/**
* on multiplie une image par un facteur
* @param fac
* @return
*/
bool Image::multiplicationImage(double fac) {
int** arr = new int* [width];
for(int row = 0; row < height; ++row)
{
arr[row] = new int[width];
for (int col = 0; col < width; ++col)
{
arr[row][col] = array[row][col] * fac ;
if (arr[row][col] <= 0)
{
arr[row][col] = 0;
}
if (arr[row][col] >= 255)
{
arr[row][col] = 255;
}
}
}
SetOutput(GetfileName()+"Multiplication.pgm");
bool result = writeImage(arr);
cout<<"\tMultiplication de l'image terminee\n"<<endl;
cout<<"\tConsultez l'image "<<output <<" pour voir le resultat\n"<<endl;
return result;
}
/**
* on augmente la luminosite de l'image
* @param fac
* @return
*/
bool Image::luminositeImage(double fac) {
int** arr = new int* [width];
for(int row = 0; row < height; ++row)
{
arr[row] = new int[width];
for (int col = 0; col < width; ++col)
{
arr[row][col] = array[row][col] + fac ;
if (arr[row][col] <= 0)
{
arr[row][col] = 0;
}
if (arr[row][col] >= 255)
{
arr[row][col] = 255;
}
}
}
SetOutput(GetfileName()+"Luminosite.pgm");
bool result = writeImage(arr);
cout<<"\tAncienne luminance: "<<GetLuminance() <<" \n"<<endl;
cout<<"\tNouvelle luminance: "<<CalLuminance(arr) <<" \n"<<endl;
if(fac > 0 )cout<<"\tAugmentation de la luminosite terminee\n"<<endl;
if(fac < 0 )cout<<"\tDimunition de la luminosite terminee\n"<<endl;
cout<<"\tConsultez l'image "<<output <<" pour voir le resultat\n"<<endl;
return result;
}
/**
* on augmente la luminosite de l'image
* @param fac
* @return
*/
bool Image::contrasteImage(double fac) {
double contraste = CalContraste();
double luminance = GetLuminance();
double coef = 0;
coef = ( contraste + fac ) / contraste;
int** arr = new int* [width];
for(int row = 0; row < height; ++row)
{
arr[row] = new int[width];
for (int col = 0; col < width; ++col)
{
arr[row][col] = coef*array[row][col] ;
if (arr[row][col] <= 0)
{
arr[row][col] = 0;
}
if (arr[row][col] >= 255)
{
arr[row][col] = 255;
}
}
}
SetOutput(GetfileName()+"Contraste.pgm");
bool result = writeImage(arr);
cout<<"\tAncienne contraste: "<<contraste <<" \n"<<endl;
double cont = CalContraste(arr),diff;
cout<<"\tNouveau contraste: "<< cont <<" \n"<<endl;
string res = "";
if(fac > 0 )res =" L'augmentation" ;
if(fac < 0 )res = "La dimunition" ;
diff = abs(contraste - cont);
if(diff < abs(fac - 2))
{
cout<<"\tDesole, nous n'avons pas atteint "<<res<<" de "<<fac<<" que vous souhaitiez\n"<<endl;
if(fac > 0 )cout<<"\tRessayez avec une valeur plus petite.\n"<<endl;
if(fac < 0 )cout<<"\tRessayez avec une valeur plus grande.\n"<<endl;
}
cout<<"\t"<<res<<" de "<< diff<<" du contraste est terminee\n"<<endl;
cout<<"\tConsultez l'image "<<output <<" pour voir le resultat\n"<<endl;
return result;
}
/**
* on inverse l'image
* @return
*/
bool Image::inversionImage() {
int** arr = new int* [width];
for(int row = 0; row < height; ++row)
{
arr[row] = new int[width];
for (int col = 0; col < width; ++col)
{
arr[row][col] = 255 - array[row][col] ;
if (arr[row][col] <= 0)
{
arr[row][col] = 0;
}
}
}
SetOutput(GetfileName()+"Inversion.pgm");
bool result = writeImage(arr);
cout<<"\tInversion terminee\n"<<endl;
cout<<"\tConsultez l'image "<<output <<" pour voir le resultat\n"<<endl;
return result;
}
/**
* on renverse l'image
* @return
*/
bool Image::renverseImage() {
int** arr = new int* [width];
for(int row = 0; row < height; ++row)
{
arr[row] = new int[width];
for (int col = 0; col < width; ++col)
{
arr[row][col] = array[height - row - 1][width - col -1] ;
}
}
SetOutput(GetfileName()+"Renverse.pgm");
bool result = writeImage(arr);
cout<<"\tL'image a ete renverse\n"<<endl;
cout<<"\tConsultez l'image "<<output <<" pour voir le resultat\n"<<endl;
return result;
}
/**
* on fait le zoom sur l'image
* @return
*/
bool Image::zoomImage(int zoom1,int zoom2) {
int hei = zoom1* height;
int wid = zoom2 * width;
int** arr1 = new int* [hei];
int col1 = 0, row1 = 0;
// Following lines : data
for(int row = 0; row < height; ++row)
{
for(int j = 0; j < zoom1; ++j)
{
arr1[row1+j] = new int[wid];
}
for (int col = 0; col < width; ++col)
{
for(int i = 0; i < zoom1; ++i)
{ for(int j = 0; j < zoom2; ++j)
{
arr1[row1+i][col1+j] = array[row][col];
}
}
col1 += zoom2;
}//*/
col1 = 0;
row1 += zoom1;
}
SetOutput(GetfileName()+"zoom.pgm");//GetfileName()
writeImage(arr1,zoom1,zoom2,true);
cout<<"\tL'image a ete zoome\n"<<endl;
cout<<"\tConsultez l'image "<<output <<" pour voir le resultat\n"<<endl;
}
/**
* convulution avec filtre moyenneur
* @param dim
* @return
*/
bool Image::convolutionFiltreMoyenneur(int dim) {
int** R;
R = new int* [height];
for(int i=0; i < height; i++){
R[i] = new int[width];
for(int j=0; j < width; j++){
R[i][j] = 0;
}
}
int m(dim/2);
for(int i=m; i < height - m; i++){
for(int j=m; j < width - m; j++){
for(int k=0; k < dim; k++){
for(int l=0; l < dim; l++){
R[i][j] += array[i-k+m][j-l+m];
}
}
R[i][j] /= pow(dim,2);
}
}
SetOutput(GetfileName()+"FiltreMoyenneur.pgm");
bool result = writeImage(R);
cout<<"\tLe filtre moyenneur a ete applique a l'image\n"<<endl;
cout<<"\tConsultez l'image "<<output <<" pour voir le resultat\n"<<endl;
return result;
}
/**
* la convolution avec filtre gaussien
* @param dim
* @param sigma
* @return
*/
bool Image::convolutionFiltreGaussien(int dim) {
int** R;
double K[dim][dim];
//construction de la matrice gaussienne
double facteurGaussien = 0.0;
if(dim == 3)
{
K[0][0] = 1;
K[0][1] = 2;
K[0][2] = 1;
K[1][0] = 2;
K[1][1] = 4;
K[1][2] = 2;
K[2][0] = 1;
K[2][1] = 2;
K[2][2] = 1;
}
else
{
K[0][0] = K[4][4] = K[4][0] = K[0][4] = 1;
K[0][1] = K[1][0] = K[4][1] = K[4][3] = K[3][0] = K[3][4] = K[0][3] = K[1][4] = 2;
K[1][2] = K[1][3] = K[3][2] = K[3][3] = 6;
K[2][0] = K[2][4] = K[0][2] = K[4][2] = 3;
K[2][1] = K[2][3] = K[1][1] = K[3][1] = 8;
K[2][2] = 10;
}
for(int i = 0; i < dim; i++){
for(int j = 0; j < dim; j++){
facteurGaussien += K[i ][j ] ;
}
}
R = new int* [height];
for(int i=0; i < height; i++){
R[i] = new int[width];
for(int j=0; j < width; j++){
R[i][j] = 0;
}
}
int m(dim/2);
for(int i=m; i < height - m; i++){
for(int j=m; j < width - m; j++){
for(int k=0; k < dim; k++){
for(int l=0; l < dim; l++){
R[i][j] += array[i-k+m][j-l+m]*K[dim-1-k][dim-1-l];
}
}
R[i][j] /= facteurGaussien ;
}
}
SetOutput(GetfileName()+"FiltreGaussien.pgm");
bool result = writeImage(R);
cout<<"\tLe filtre gaussien a ete applique a l'image\n"<<endl;
cout<<"\tConsultez l'image "<<output <<" pour voir le resultat\n"<<endl;
return result;
}
//*/
/**
* convolution filtre median
* @param dim
* @return
*/
bool Image::convolutionFiltreMedian(int dim) {
int** R;
int** K;
//construction de la matrice gaussienne
R = new int* [height];
for(int i=0; i < height; i++){
R[i] = new int[width];
for(int j=0; j < width; j++){
R[i][j] = 0;
}
}
K = new int* [dim];
for(int i=0; i < dim; i++){
K[i] = new int[dim];
}
int m(dim/2);
for(int i=m; i < height - m; i++){
for(int j=m; j < width - m; j++){
for(int k=0; k < dim; k++){
for(int l=0; l < dim; l++){
K[dim-1-k][dim-1-l] = array[i-k+m][j-l+m] ;
}
}
R[i][j] = mediane (K,dim);
}
}
SetOutput(GetfileName()+"FiltreMedian.pgm");
bool result = writeImage(R);
cout<<"\tLe filtre median a ete applique a l'image\n"<<endl;
cout<<"\tConsultez l'image "<<output <<" pour voir le resultat\n"<<endl;
return result;
}
/**
* renvoit la valeur mediane pour un pixel donné
* @param K
* @param dim
* @return
*/
int Image::mediane(int** K,int dim) {
int dim1 = dim*dim;
int temp[dim1],temp1[dim1];
int k = 0, echange = 0 , aux = 0;
for(int i=0; i < dim; i++){
for(int j=0; j < dim; j++){
temp[k] = K[i][j];
temp1 [k] = 255;
k++;
}
}
for(int i=0; i < dim1; i++){
for(int j = i; j < dim1; j++){
if(temp[j] < temp1[i])
{
temp1[i] = temp[j];
echange = j;
}
}
aux = temp[i];
temp[i] = temp[echange];
temp[echange] = aux;
}
// for(int j = 0; j < dim1; j++){
// cout<<temp[j]<< " "<<temp1[j]<<endl;
// }
//
return temp1[dim];
}
/**
* On applique le filtre de Roberts
* @return
*/
bool Image::contourRoberts() {
int dim = 2;
double** K = new double* [dim];
for(int i=0; i < dim; i++){
K[i] = new double[dim];
}
K[0][0] = 1;
K[1][1] = -1;
K[0][1] = K[1][0] = 0;
int** R = convolution(array,K,2);
K[0][0] = K[1][1] = 0;
K[0][1] = 1;
K[1][0] = -1;
int** R1 = convolution(R,K,2);
R = additionImage(R,R1);
SetOutput(GetfileName()+"ContourRoberts.pgm");
bool result = writeImage(R);
cout<<"\tLes contours de l'image ont pu etre ressorti.\n"<<endl;
cout<<"\tConsultez l'image "<<output <<" pour voir le resultat\n"<<endl;
return result;
}
/**
* filtre de Prewitt
* @return
*/
bool Image::contourPrewitt() {
int dim = 3;
double** K = new double* [dim];
for(int i=0; i < dim; i++){
K[i] = new double[dim];
for(int j=0; j < dim; j++){
if(i == 0)
{
K[i][j] = -1;
}
if(i == 1)
{
K[i][j] = 0;
}
if(i == 2)
{
K[i][j] = 1;
}
}
}
int** R = convolution(array,K,2);
for(int i=0; i < dim; i++){
for(int j=0; j < dim; j++){
if(j == 0)
{
K[i][j] = -1;
}
if(j == 1)
{
K[i][j] = 0;
}
if(j == 2)
{
K[i][j] = 1;