Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<math.h>
- //matriz_pgm contém os pixels que constam no arquivo pgm que será usado neste programa;
- 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){
- if ( matriz_seg[x0][y0] == 1 || x0<0 || x0>(M-1) || y0<0 || y0> (N-1) ){
- return;
- }
- else{
- //printf("TESTANDO COM %d %d, CONTADOR = %d %I = %d MEDIA ATUAL = %f INTENSIDADE = %d...", x0, y0, *contador, *i, *media, matriz_pgm[x0][y0]);
- int chama_vizinho = 0;
- if (*i == 0)
- {
- *contador+=matriz_pgm[x0][y0];
- matriz_seg[x0][y0]=1;
- (*i)++;
- *media=((float) *contador)/(*i);
- //printf("PRIMEIRO CONQUISTADO!\n");
- chama_vizinho = 1;
- }
- else if ( fabs( matriz_pgm[x0][y0]- *media) <= criterio ) {
- *contador+=matriz_pgm[x0][y0];
- matriz_seg[x0][y0]=1;
- (*i)++;
- *media=((float) *contador)/(*i);
- //printf("CONQUISTADO!\n");
- chama_vizinho = 1;
- }
- if (chama_vizinho == 1) {
- if ( (x0 - 1) >= 0) {
- buscar_pixel(x0-1, y0, criterio, matriz_seg, i, matriz_pgm, contador, M, N, media);
- }
- if ( (y0+1)<N ) {
- buscar_pixel(x0, y0+1, criterio, matriz_seg, i, matriz_pgm, contador, M, N, media);
- }
- if( (x0+1)<M ){
- buscar_pixel(x0+1, y0, criterio, matriz_seg, i, matriz_pgm, contador, M, N, media);
- }
- if( (y0-1)>=0){
- buscar_pixel(x0, y0-1, criterio, matriz_seg, i, matriz_pgm, contador, M, N,media);
- }
- }
- //else printf("\n");
- }
- }
- int main(){
- char* nome_arquivo = malloc(200*sizeof(char)); // supõe-se
- //que o usuário não digite um nome de arquivo com mais de 200 caracteres;
- int num_consultas;//número de consultas desejadas pelo usuário;
- scanf("%s %d",nome_arquivo, &num_consultas);
- FILE *endereco_arquivo;
- endereco_arquivo=fopen(nome_arquivo, "r");
- if(endereco_arquivo==NULL){
- printf("erro de abertura do arquivo");
- return 0;
- }
- char* lixo=malloc(10*sizeof(char));
- 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,
- //por isso o nome "lixo"; ex: leitura de " P2\r\n ";
- *lixo = fgetc(endereco_arquivo);
- }
- free(lixo);
- int N, M, num_desprezivel;
- fscanf(endereco_arquivo, "%d %d", &N, &M );//N é a largura da imagem e M a altura;
- fscanf(endereco_arquivo, "%d", &num_desprezivel);
- int** matriz_pgm=malloc(M*sizeof(int*));
- for(int j=0; j<M; j++){
- matriz_pgm[j]= malloc(N*sizeof(int));
- }
- int** matriz_seg=malloc(M*sizeof(int*));
- for(int j=0; j<M; j++){
- matriz_seg[j]= malloc(N*sizeof(int));
- }
- for(int j=0 ; j<M; j++){
- for(int k=0; k<N ; k++){
- matriz_seg[j][k]=0;
- matriz_pgm[j][k]=0;
- }
- }
- //printf("LENDO RESTO DO ARQUIVO\n");
- for(int j=0 ; j<M; j++){
- for(int k=0; k<N ; k++){
- fscanf(endereco_arquivo, "%d", &matriz_pgm[j][k]);
- }
- }
- 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;
- int i=0, criterio=0;
- float media=0.0;
- int contador=0;//indica o valor da soma dos pixels que devem pertencer à segmentação, conforme
- //caminhamos sobre a matriz_pgm; o contador será utilizado posteriormente para calcular a média
- //dos pixels por onde "já caminhamos";
- for(int k=0; k<num_consultas; k++){
- //printf("CONSULTA %d\n", k + 1);
- scanf("%d %d %d",&x0, &y0, &criterio);
- contador = 0;
- i = 0;
- media = 0.0;
- buscar_pixel(x0, y0, criterio, matriz_seg, &i, matriz_pgm, &contador, M, N, &media);
- }
- printf("MATRIZ SEG:\n");
- for (int j = 0; j < M; ++j)
- {
- for (int k = 0; k < N; ++k)
- {
- printf("%d ", matriz_seg[j][k]);
- }
- printf("\n");
- }
- for(int k=0; k<M; k++){
- for( int j=0 ; j<N ; j++){
- if( (k - 1) >= 0 ){
- if( matriz_seg[k-1][j] != matriz_seg[k][j]){
- printf("(%d, %d)\n", k, j);
- continue;
- }
- }
- if ( (j+1)<N ){
- if( matriz_seg[k][j+1] != matriz_seg[k][j] ) {
- printf("(%d, %d)\n", k, j);
- continue;
- }
- }
- if( (k+1)<M ){
- if( matriz_seg[k+1][j] != matriz_seg[k][j] ){
- printf("(%d, %d)\n", k, j);
- continue;
- }
- }
- if(( j-1)>=0 ){
- if( matriz_seg[k][j-1] != matriz_seg[k][j]){
- printf("(%d, %d)\n", k, j);
- continue;
- }
- }
- //printf("NOT BORDER: %d %d\n", k, j);
- }
- }
- //ordenar a estrutura e imprimir
- //free nome_arquivo, matriz_pgm,
- //fclose
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement