Example #1
0
void DestroyFisher(MATRIX **M)
{
    matrix_destructor(M[0]);
//    matrix_destructor(M[1]);
//    matrix_destructor(M[2]);
//    matrix_destructor(M[3]);
    free(M);
}
Example #2
0
int main()
{
    char filename[255] = {}; // filename of the test image

    // We need to load the data structures that were created by the
    // CreateDatabase step
    if (MatrixRead_Binary() != 0) { //Load structures
        printf("Error!!!\n");
        exit(-1);
    }
    printf("Database Files Loaded From Disk...\n");
    PPMImage *TestImage; // pointer to the structure of our loaded test image

    int pass = 0, fail = 0, iterations; // Let's keep a few stats
    int i, j, k, x; // loop variables

    for (iterations = 1; iterations <= 30; iterations++) {
        /* 994 is the number of test images that we have sequentially numbered */
        // concat our filename together
        sprintf(filename, "../LDAIMAGES/Test2/%d.ppm", iterations);
        TestImage = ppm_image_constructor(filename);
        printf("Test Image Loaded From Disk and converted to grayscale...\n");

        // First let's allocate our difference array
        Difference = matrix_constructor(m_database.rows, 1);
        //Difference.rows = m_database.rows;
        //Difference.cols = 1;
        //Difference.data = (double**) malloc(Difference.rows * sizeof (double*));
        //for (i = 0; i < Difference.rows; i++) {
        //    Difference.data[i] = (double*) malloc(Difference.cols
        //            * sizeof (double));
        //}

        for (i = 0; i < m_database.rows; i++) {
            Difference.data[i][0] = TestImage->pixels[i].r
                    - m_database.data[i][0]; // mean database is a 1d vector
            // (using red channel for gray)
        }
        // Now let's multiply in the last matrix to calculate our ProjectedTestImage
        //v_fisherT_x_v_pcaT * Difference
        // Why is this commented out?

        ProjectedTestImage = matrix_constructor(rows, cols);
//        ProjectedTestImage.rows = v_fisherT_x_v_pcaT.rows;
//        ProjectedTestImage.cols = Difference.cols;
//        ProjectedTestImage.data = (double**) malloc(ProjectedTestImage.rows
//                * sizeof (double*));
        // allocate memory for our new array
//        for (i = 0; i < ProjectedTestImage.rows; i++) {
//            ProjectedTestImage.data[i] = (double*) malloc(ProjectedTestImage.cols
//                    * sizeof (double));
//            if (ProjectedTestImage.data[i] == 0) {
//                printf("Dynamic Allocation Failed!!!\n");
//                return -1;
//            }
//        }

        // clbas_dgemm();
        for (i = 0; i < v_fisherT_x_v_pcaT.rows; i++) { // perform matrix mult.
            // computation
            for (j = 0; j < Difference.cols; j++) { // This loop executes once
                ProjectedTestImage.data[i][j] = 0.0;
                for (k = 0; k < v_fisherT_x_v_pcaT.cols; k++) {
                    ProjectedTestImage.data[i][j] +=
                            v_fisherT_x_v_pcaT.data[i][k] * Difference.data[k][j];
                }
            }
        }

        unsigned long int Train_Number = 0;
        Train_Number = ProjectedImages_Fisher.cols; // Satisfies line 27
        double * q = 0; // Holds a column vector
        q = (double *) malloc(ProjectedImages_Fisher.rows * sizeof (double));

        double * Euc_dist = (double *)
                malloc(sizeof (double) * ProjectedImages_Fisher.cols);
        double temp = 0;

        for (i = 0; i < Train_Number; i++) { // line 44 Recognition.m
            for (j = 0; j < ProjectedImages_Fisher.rows; j++) { // create q
                q[j] = ProjectedImages_Fisher.data[j][i];
            } //q has been populated

            // At this point, ProjectedTestImage is 99x1 and q is 99x1
                    // (Based on testing database)
            for (x = 0; x < ProjectedImages_Fisher.rows; x++) {
                temp += ((ProjectedTestImage.data[x][0] - q[x]) *
                        (ProjectedTestImage.data[x][0] - q[x])); //line 46
            }
            Euc_dist[i] = temp;
            temp = 0; //reset our running count
        }
        // at this point, Euc_dist should be populated

        // we need to find the min euc_dist and its index
        //int Euc_dist_len = sizeof(*Euc_dist) / sizeof(double);
        int Euc_dist_len = ProjectedImages_Fisher.cols; // the length of euc_dist
        double min = Euc_dist[0];

        int Recognized_index = 0;
        for (i = 0; i < Euc_dist_len; i++) {
            if (Euc_dist[i] < min) { // reassign min
                min = Euc_dist[i];
                Recognized_index = i;
            }
        }
        int filename_index = Recognized_index + 1; // because our files are
        // named starting at 1 and not 0. Arrays start at 0 in C

        printf("Test %d : %d.ppm == %d.ppm\n", iterations, iterations,
                filename_index);
        printf("-----------------------------------\n");

        ////////////// for statistic tracking
        if (iterations == ((Recognized_index - 1) / 4 + 1)) {
            pass++;
        } else {
            fail++;
        }
        //////////////
        ppm_image_destructor(TestImage, 0); // Free the loaded image
        // Need to free the memory that was allocated...

        // free Difference matrix
        matrix_destructor(Difference);
//        for (i = 0; i < Difference.rows; i++) {
//            free(Difference.data[i]);
//        }
//        free(Difference.data);

        // Free ProjectedTestImage matrix...
        matrix_destructor(ProjectedTestImage);
//        for (i = 0; i < ProjectedTestImage.rows; i++) {
//            free(ProjectedTestImage.data[i]);
//        }
//        free(ProjectedTestImage.data);

        // Free q vector
        free(q); // wha? why exactly did i use malloc?

        // Free Euc_dist
        free(Euc_dist); // huh?
        ///////////Allocated Memory Freed//////////////////////
    }
    printf("%d Correct %d Wrong\n", pass, fail);
    return 0;
}
void operator_matrix() {
    matrix result;
    char in[USHRT_MAX];
    int m;
    int n;
    while (1) {
        printf("Entre com o número de linhas da 1ª matriz: ");
        scanf("%s", in);
        m = atoi(in);
        printf("\n");
        if (m == 0)
            printf("Valor inválido!\n\n");
        else
            break;
    }
    while (1) {
        printf("Entre com o número de colunas da 1ª matriz: ");
        scanf("%s", in);
        n = atoi(in);
        printf("\n");
        if (n == 0)
            printf("Valor inválido!\n\n");
        else
            break;
    }
    result = matrix_constructor(m, n);
    printf("Entre com os elementos da 1ª matriz, separando-os por espaços e/ou quebras de linha:\n");
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++) {
            scanf("%s", in);
            result.table[i][j] = atof(in);
        }
    printf("\n");
    int keep_going = 1;
    while (keep_going) {
        matrix next;
        switch (menu_matrix()) {
            case 1:
                // Add
                while (1) {
                    while (1) {
                        printf("Entre com o número de linhas da proxima matriz: ");
                        scanf("%s", in);
                        m = atoi(in);
                        printf("\n");
                        if (m == 0)
                            printf("Valor inválido!\n\n");
                        else
                            break;
                    }
                    while (1) {
                        printf("Entre com o número de colunas da proxima matriz: ");
                        scanf("%s", in);
                        n = atoi(in);
                        printf("\n");
                        if (n == 0)
                            printf("Valor inválido!\n\n");
                        else
                            break;
                    }
                    next = matrix_constructor(m, n);
                    if (!matrix_can_add(result, next))
                        printf("Não é possivel fazer a operação desejada com as matrizes de ordens previamente informadas!\n\n");
                    else
                        break;
                }
                printf("Entre com os elementos da proxima matriz, separando-os por espaços e/ou quebras de linha:\n");
                for (int i = 0; i < m; i++)
                    for (int j = 0; j < n; j++) {
                        scanf("%s", in);
                        next.table[i][j] = atof(in);
                    }
                printf("\n");
                matrix_add(&result, next);
                break;
            case 2:
                // Subtract
                while (1) {
                    while (1) {
                        printf("Entre com o número de linhas da proxima matriz: ");
                        scanf("%s", in);
                        m = atoi(in);
                        printf("\n");
                        if (m == 0)
                            printf("Valor inválido!\n\n");
                        else
                            break;
                    }
                    while (1) {
                        printf("Entre com o número de colunas da proxima matriz: ");
                        scanf("%s", in);
                        n = atoi(in);
                        printf("\n");
                        if (n == 0)
                            printf("Valor inválido!\n\n");
                        else
                            break;
                    }
                    next = matrix_constructor(m, n);
                    if (!matrix_can_subtract(result, next))
                        printf("Não é possivel fazer a operação desejada com as matrizes de ordens previamente informadas!\n\n");
                    else
                        break;
                }
                printf("Entre com os elementos da proxima matriz, separando-os por espaços e/ou quebras de linha:\n");
                for (int i = 0; i < m; i++)
                    for (int j = 0; j < n; j++) {
                        scanf("%s", in);
                        next.table[i][j] = atof(in);
                    }
                printf("\n");
                matrix_subtract(&result, next);
                break;
            case 3:
                // Multiply
                while (1) {
                    while (1) {
                        printf("Entre com o número de linhas da proxima matriz: ");
                        scanf("%s", in);
                        m = atoi(in);
                        printf("\n");
                        if (m == 0)
                            printf("Valor inválido!\n\n");
                        else
                            break;
                    }
                    while (1) {
                        printf("Entre com o número de colunas da proxima matriz: ");
                        scanf("%s", in);
                        n = atoi(in);
                        printf("\n");
                        if (n == 0)
                            printf("Valor inválido!\n\n");
                        else
                            break;
                    }
                    next = matrix_constructor(m, n);
                    if (!matrix_can_multiply(result, next))
                        printf("Não é possivel fazer a operação desejada com as matrizes de ordens previamente informadas!\n\n");
                    else
                        break;
                }
                printf("Entre com os elementos da proxima matriz, separando-os por espaços e/ou quebras de linha:\n");
                for (int i = 0; i < m; i++)
                    for (int j = 0; j < n; j++) {
                        scanf("%s", in);
                        next.table[i][j] = atof(in);
                    }
                printf("\n");
                matrix_multiply(&result, next);
                break;
            case 4:
                // Power
                if (matrix_can_power(result)) {
                    while (1) {
                        printf("Entre com o próximo valor: ");
                        scanf("%s", in);
                        printf("\n");
                        if (atoll(in) >= 0) {
                            matrix_power(&result, (unsigned long long int) atoll(in));
                            break;
                        } else
                            printf("Valor inválido!\n\n");
                    }
                } else
                    printf("Não é possivel realizar a operação desejada com a matrix atual!\n\n");
                break;
            default:
                keep_going = 0;
                break;
        }
        matrix_destructor(&next);
        printf("Resultado:\n\n");
        matrix_print(result);
        printf("\n");
        if (!keep_going)
            matrix_destructor(&result);
    }
}