Italiano
HOME | PROGETTI | CLASSI C++ | Sorgenti c\c++ | Galleria | Guestbook | Server BW | Contattami | Login

<< Filtro Blur >>

visiste: 2438


Il filtro blur serve per sfuocare un'immagine. La matrice utilizzata nell'esempio č di grandezza 3x3

  1. //*************************************//
  2. //******** created by mamo139 *********//
  3. //*** http://mamo139.altervista.org ***//
  4. //*************************************//
  5.  
  6. #include <stdio.h>
  7. #include <windows.h>
  8. #include <math.h>
  9.  
  10. unsigned char* dammibit(unsigned char numero);
  11. unsigned char dammichar(unsigned char *bits);
  12.  
  13. int main (void){
  14.  
  15. char nomefileb[20];
  16. printf("nome del file base:");
  17. scanf("%s",nomefileb);
  18. printf("%s!\n",nomefileb);
  19. FILE * pFile;
  20. long lSize;
  21. pFile = fopen ( nomefileb , "rb" );
  22. fseek (pFile , 0 , SEEK_END);
  23. lSize = ftell (pFile);
  24. fclose (pFile);
  25.  
  26.  
  27. long lbuffer = lSize; //byte letti alla volta nella base
  28. printf("buffer impostato a %d bytes\n",lbuffer);
  29.  
  30. unsigned char *buf; //buffer di lettura
  31. buf = new unsigned char [(lbuffer)+100]; //grandezza del buffer della base
  32.  
  33. FILE *file, *f_out;
  34. if ((file = fopen(nomefileb, "rb")) && (f_out = fopen("cript.bmp", "wb"))){
  35.  
  36. printf("files aperti con successo!!\n");
  37.  
  38. long b; //byte letti
  39. b = fread( buf, 1, 54, file);
  40.  
  41. unsigned int coordx = buf[18]+buf[19]*256;
  42. unsigned int coordy = buf[22]+buf[23]*256;
  43. printf("caratteristiche immagine: %d x %d\n",coordx,coordy);
  44.  
  45.  
  46. fwrite( buf, 1, b, f_out);
  47. printf("header scritto...\n");
  48.  
  49. //******************************INIZIO LETTURA DEL FILE*********************************//
  50. b = fread( buf, 1, lbuffer, file);
  51. printf("file caricato nel buffer...\n");
  52.  
  53. long x,y,z=0;
  54. //caricamento in array bidimensionale...
  55. //unsigned char mappa[coordx][coordy][4];
  56. int x1=coordx, y1=coordy, z1=3; // Le dimensioni
  57. unsigned char ***mappa;
  58. int i, j;
  59. mappa=(unsigned char***)malloc(x1 * sizeof(unsigned char *));
  60. for(i=0; i<x1; i++) {
  61. mappa[i]=(unsigned char**)malloc(y1 * sizeof(unsigned char *));
  62. for (j=0; j<y1; j++)
  63. mappa[i][j]=(unsigned char*)malloc(z1 * sizeof(unsigned char *));
  64. }
  65. printf("mappa a due dimensioni creata...\n");
  66.  
  67. for(y=0; y < coordy ;y++){
  68. printf("loaded: y=%d\n",y);
  69. for(x=0;x < coordx;x++){
  70. mappa[x][y][0] = buf[z];
  71. mappa[x][y][1] = buf[(z+1)];
  72. mappa[x][y][2] = buf[(z+2)];
  73. z=z+3;
  74. }}
  75. printf("byte immagazzinati nella mappa: %d \n",z);
  76.  
  77. //*****************************INIZIO MODE DEL FILE**********************************//
  78.  
  79. //filtro blurr
  80. double kernel[3][3]; //kernel
  81. kernel[0][0]= 0.1111; kernel[0][1]= 0.1111; kernel[0][2]= 0.1111;
  82. kernel[1][0]= 0.1111; kernel[1][1]= 0.1111; kernel[1][2]= 0.1111;
  83. kernel[2][0]= 0.1111; kernel[2][1]= 0.1111; kernel[2][2]= 0.1111;
  84.  
  85.  
  86. double grigio,grigio2;
  87.  
  88. for(y=0; y < coordy ; y++){
  89. for(x=0; x < coordx ; x++){
  90. /*
  91.   unsigned char *wbuf;
  92.   wbuf = new unsigned char [4];
  93.   wbuf[0]= mappa[x][y][0];
  94.   wbuf[1]= mappa[x][y][1];
  95.   wbuf[2]= mappa[x][y][2];
  96.   */
  97. grigio = (mappa[x][y][0] + mappa[x][y][1] + mappa[x][y][2])/3;
  98. grigio2 = (mappa[x][y][0] + mappa[x][y][1] + mappa[x][y][2])/3;
  99. //printf("%f",grigio);
  100. if(x>=1 && y >=1 && x<coordx-1 && y<coordy-1){
  101.  
  102. //convoluzione
  103. grigio =
  104. ((mappa[x-1][y-1][0]+mappa[x-1][y-1][1]+mappa[x-1][y-1][2])/3) * kernel[0][0] +
  105. ((mappa[x][y-1][0]+mappa[x][y-1][1]+mappa[x][y-1][2])/3) * kernel[0][1] +
  106. ((mappa[x+1][y-1][0]+mappa[x+1][y-1][1]+mappa[x+1][y-1][2])/3) * kernel[0][2] +
  107.  
  108. ((mappa[x-1][y][0]+mappa[x-1][y][1]+mappa[x-1][y][2])/3) * kernel[1][0] +
  109. ((mappa[x][y][0]+mappa[x][y][1]+mappa[x][y][2])/3) * kernel[1][1] +
  110. ((mappa[x+1][y][0]+mappa[x+1][y][1]+mappa[x+1][y][2])/3) * kernel[1][2] +
  111.  
  112. ((mappa[x-1][y+1][0]+mappa[x-1][y+1][1]+mappa[x-1][y+1][2])/3) * kernel[2][0] +
  113. ((mappa[x][y+1][0]+mappa[x][y+1][1]+mappa[x][y+1][2])/3) * kernel[2][1] +
  114. ((mappa[x+1][y+1][0]+mappa[x+1][y+1][1]+mappa[x+1][y+1][2])/3) * kernel[2][2] ;
  115.  
  116. }
  117. //printf(" - %f\n",grigio);
  118. unsigned char *wbuf;
  119. wbuf = new unsigned char [3];
  120.  
  121. wbuf[0]= (int)grigio;
  122. wbuf[1]= wbuf[0];
  123. wbuf[2]= wbuf[0];
  124. fwrite( wbuf, 1, 3, f_out);
  125. }}
  126.  
  127. //fwrite( buf, 1, b, f_out);
  128.  
  129. }
  130.  
  131.  
  132. fclose(file);
  133. fclose(f_out);
  134.  
  135. system("PAUSE");
  136. return 1;
  137. }
  138.  
Versione sito: 1.03.01 by mamo139. - Tutti i sorgenti presenti su questo sito sono rilasciati sotto licenza "GNU GPL version 3".