Next: Listing for corrcalc.c Up: Computer programs for Previous: Computer programs for


Listing for statsimp.c

/************************************************************************/
/*                                                                      */
/*      Program: statsimp.c                                             */
/*      Purpose: To compute the area and surface areas                  */
/*              for one or more phases of a cement particle image       */
/*      Programmer: Dale P. Bentz                                       */
/*                  NIST                                                */
/*                  Building 226 Room B-350                             */
/*                  Gaithersburg, MD  20899-0001                        */
/*                  Phone: (301) 975-5865                               */
/*                  E-mail: dale.bentz@nist.gov                         */
/*                                                                      */
/************************************************************************/
#include <stdio.h>
#include <math.h>

int flag[5];

/* Note that program is limited to a 512*512 image size */
main()
{
        int mask,i,j,image [512] [512],xsize,ysize;
        int inval;
        long int sum,edgesum;
        float fsum;
        char filen[80];
        FILE *infile;

        do{
                printf("Enter size of image in x and y directions \n");
                scanf("%d %d",&xsize,&ysize);
                printf("%d %d \n",xsize,ysize);
        } while ((xsize>512)||(ysize>512));

        printf("Enter name of image file \n");
        scanf("%s",filen);

        infile=fopen(filen,"r");

        mask=1;
        /* Read in the image and assign powers of two as phase values */
        for(i=0;i<xsize;i++){
        for(j=0;j<ysize;j++){
                fscanf(infile,"%d\n",&inval);
                image [i] [j]=(int)(pow(2.,(float)inval));
        }
        }
        fclose(infile);

/* Now perform calculation */
/* Allow user to execute counting as many times as desired */
        while(mask!=0){
                printf("Phase assignments are assumed to be as follows: \n\n");
                printf("Phase      Image ID   Mask value \n \n");
                printf("Pores          0          1\n");
                printf("C3S            1          2\n");
                printf("C2S            2          4\n");
                printf("C3A            3          8\n");
                printf("C4AF           4         16\n");

            printf("Enter composite value for mask to use during this run \n");
            printf("Example: for C3S and C2S, composite would be 2+4=6 \n");
                printf("Enter 0 to exit program \n");
                scanf("%d",&mask);
                printf("%d\n",mask);

                /* Determine area and perimeter counts for this phase(s) */
                /* Perimeter count is number of pixel edges of phase(s) */
                /* in contact with porosity */
                /* Non-periodic so ignore a one-pixel layer around the edge */
                if(mask!=0){
                sum=edgesum=0;
                for(i=1;i<(xsize-1);i++){
                for(j=1;j<(ysize-1);j++){

                if((mask&(image[i][j]))!=0){
                        sum+=1;
                        /* Check immediate 4 neighbors for edges */
                        if(((image[i-1][j]))==1){
                                edgesum+=1;
                        }
                        if(((image[i+1][j]))==1){
                                edgesum+=1;
                        }
                        if(((image[i][j-1]))==1){
                                edgesum+=1;
                        }
                        if(((image[i][j+1]))==1){
                                edgesum+=1;
                        }
                }

                }
                }

                printf("Area- %ld pixels Perimeter- %ld \n",sum,edgesum);
                fsum=(float)sum/((float)(xsize-2)*(float)(ysize-2));
                printf("Phase fraction is %f \n",fsum);
                }
        } /* end of while mask loop */
}



Dale P Bentz
Fri Feb 21 08:44:14 EST 1997