Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.15 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4.  
  5. //matriz_pgm contém os pixels que constam no arquivo pgm que será usado neste programa;
  6.  
  7. void buscar_pixel(int x0, int y0, int criterio, int** matriz_seg, int* i, int** matriz_pgm, int* contador, int M, int N, float* media){
  8.  
  9.  
  10.    if ( matriz_seg[x0][y0] == 1 || x0<0 || x0>(M-1) || y0<0 || y0> (N-1) ){
  11.  
  12.         return;
  13.    }
  14.  
  15.    else{
  16.  
  17.         //printf("TESTANDO COM %d %d, CONTADOR = %d %I = %d MEDIA ATUAL = %f INTENSIDADE = %d...", x0, y0, *contador, *i, *media, matriz_pgm[x0][y0]);
  18.  
  19.             int chama_vizinho = 0;
  20.             if (*i == 0)
  21.             {
  22.                 *contador+=matriz_pgm[x0][y0];
  23.  
  24.                 matriz_seg[x0][y0]=1;
  25.                 (*i)++;
  26.                 *media=((float) *contador)/(*i);
  27.                 //printf("PRIMEIRO CONQUISTADO!\n");
  28.                 chama_vizinho = 1;
  29.  
  30.             }
  31.             else if ( fabs( matriz_pgm[x0][y0]- *media) <= criterio ) {
  32.                 *contador+=matriz_pgm[x0][y0];
  33.  
  34.                 matriz_seg[x0][y0]=1;
  35.                 (*i)++;
  36.                 *media=((float) *contador)/(*i);
  37.                 //printf("CONQUISTADO!\n");
  38.                 chama_vizinho = 1;
  39.             }
  40.  
  41.             if (chama_vizinho == 1) {
  42.  
  43.                if ( (x0 - 1) >= 0) {
  44.  
  45.                     buscar_pixel(x0-1, y0, criterio, matriz_seg, i, matriz_pgm, contador, M, N, media);
  46.                }
  47.  
  48.                 if ( (y0+1)<N ) {
  49.  
  50.                     buscar_pixel(x0, y0+1, criterio, matriz_seg, i, matriz_pgm, contador, M, N, media);
  51.  
  52.                 }
  53.  
  54.                 if( (x0+1)<M ){
  55.  
  56.                         buscar_pixel(x0+1, y0, criterio, matriz_seg, i, matriz_pgm, contador, M, N, media);
  57.  
  58.                 }
  59.  
  60.                 if( (y0-1)>=0){
  61.  
  62.                         buscar_pixel(x0, y0-1, criterio, matriz_seg, i, matriz_pgm, contador, M, N,media);
  63.  
  64.                 }
  65.             }
  66.             //else printf("\n");
  67.    }
  68.  
  69. }
  70.  
  71.  
  72.  
  73.  
  74. int main(){
  75.  
  76. char* nome_arquivo = malloc(200*sizeof(char)); // supõe-se
  77. //que o usuário não digite um nome de arquivo com mais de 200 caracteres;
  78.  
  79. int num_consultas;//número de consultas desejadas pelo usuário;
  80.  
  81.  
  82. scanf("%s %d",nome_arquivo, &num_consultas);
  83.  
  84. FILE *endereco_arquivo;
  85. endereco_arquivo=fopen(nome_arquivo, "r");
  86.  
  87. if(endereco_arquivo==NULL){
  88.     printf("erro de abertura do arquivo");
  89.     return 0;
  90. }
  91.  
  92. char* lixo=malloc(10*sizeof(char));
  93.  
  94. for(int j=0 ; j<3; j++){//lê os quatro primeiros caracteres do documento, incluindo \n e \r; estes quatro primeiros caracteres não serão usados,
  95.     //por isso o nome "lixo"; ex: leitura de " P2\r\n ";
  96.  
  97.     *lixo = fgetc(endereco_arquivo);
  98. }
  99.  
  100. free(lixo);
  101.  
  102.  
  103. int N, M, num_desprezivel;
  104.  
  105.  
  106. fscanf(endereco_arquivo, "%d %d", &N, &M );//N é a largura da imagem e M a altura;
  107. fscanf(endereco_arquivo, "%d", &num_desprezivel);
  108.  
  109. int** matriz_pgm=malloc(M*sizeof(int*));
  110. for(int j=0; j<M; j++){
  111.     matriz_pgm[j]= malloc(N*sizeof(int));
  112. }
  113.  
  114. int** matriz_seg=malloc(M*sizeof(int*));
  115. for(int j=0; j<M; j++){
  116.     matriz_seg[j]= malloc(N*sizeof(int));
  117. }
  118.  
  119. for(int j=0 ; j<M; j++){
  120.     for(int k=0; k<N ; k++){
  121.  
  122.         matriz_seg[j][k]=0;
  123.         matriz_pgm[j][k]=0;
  124.     }
  125.  
  126. }
  127.  
  128. //printf("LENDO RESTO DO ARQUIVO\n");
  129. for(int j=0 ; j<M; j++){
  130.     for(int k=0; k<N ; k++){
  131.  
  132.         fscanf(endereco_arquivo, "%d", &matriz_pgm[j][k]);
  133.     }
  134.  
  135. }
  136.  
  137. int x0=-1, y0=-1; // o par ordenado (x0, y0) representa as coordenadas na matriz_pgm por onde o usuário deseja iniciar a busca;
  138. int i=0, criterio=0;
  139. float media=0.0;
  140. int contador=0;//indica o valor da soma dos pixels que devem pertencer à segmentação, conforme
  141. //caminhamos sobre a matriz_pgm; o contador será utilizado posteriormente para calcular a média
  142. //dos pixels por onde "já caminhamos";
  143.  
  144.  
  145. for(int k=0; k<num_consultas; k++){
  146.     //printf("CONSULTA %d\n", k + 1);
  147.     scanf("%d %d %d",&x0, &y0, &criterio);
  148.     contador = 0;
  149.     i = 0;
  150.     media = 0.0;
  151.     buscar_pixel(x0, y0, criterio, matriz_seg, &i, matriz_pgm, &contador, M, N, &media);
  152.  
  153. }
  154.  
  155. printf("MATRIZ SEG:\n");
  156. for (int j = 0; j < M; ++j)
  157. {
  158.     for (int k = 0; k < N; ++k)
  159.     {
  160.         printf("%d ", matriz_seg[j][k]);
  161.     }
  162.     printf("\n");
  163. }
  164.  
  165. for(int k=0; k<M; k++){
  166.     for( int j=0 ; j<N ; j++){
  167.         if( (k - 1) >= 0 ){
  168.             if( matriz_seg[k-1][j] != matriz_seg[k][j]){
  169.                 printf("(%d, %d)\n", k, j);
  170.                 continue;
  171.             }
  172.         }
  173.  
  174.         if ( (j+1)<N ){
  175.            if(  matriz_seg[k][j+1] != matriz_seg[k][j] ) {
  176.  
  177.             printf("(%d, %d)\n", k, j);
  178.             continue;
  179.            }
  180.         }
  181.  
  182.         if( (k+1)<M ){
  183.             if(  matriz_seg[k+1][j] != matriz_seg[k][j] ){
  184.  
  185.                 printf("(%d, %d)\n", k, j);
  186.                 continue;
  187.             }
  188.         }
  189.  
  190.         if(( j-1)>=0 ){
  191.             if( matriz_seg[k][j-1] != matriz_seg[k][j]){
  192.  
  193.                 printf("(%d, %d)\n", k, j);
  194.                 continue;
  195.             }
  196.         }
  197.  
  198.         //printf("NOT BORDER: %d %d\n", k, j);
  199.     }
  200.  
  201.  
  202. }
  203.  
  204.  
  205. //ordenar a estrutura e imprimir
  206.    //free nome_arquivo, matriz_pgm,
  207.    //fclose
  208.  
  209.     return 0;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement