From 94ea9b1a5a73087b72f8e90e19aa844a15f7edb8 Mon Sep 17 00:00:00 2001 From: piair Date: Fri, 16 Feb 2024 13:25:56 +0100 Subject: [PATCH] Upload files to "c" --- c/hough.c | 152 +++++++++++++++++++++++++++++++++++++++++++++ c/hough_multi.c | 161 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 313 insertions(+) create mode 100644 c/hough.c create mode 100644 c/hough_multi.c diff --git a/c/hough.c b/c/hough.c new file mode 100644 index 0000000..a7208e3 --- /dev/null +++ b/c/hough.c @@ -0,0 +1,152 @@ +// Objectif : utiliser du multithreading pour améliorer la rapidité +#include +#include +#include +#include + +int COLONNES; +int LIGNES; +int DISTANCE; + +// taille du fichier. update global variables +int* taille(char* filepath){ + FILE* ptr; + char ch; + ptr = fopen(filepath, "r"); + if (NULL == ptr) { + printf("file can't be opened \n"); + } + int lignes = 0; + int colonnes = 1; + int end = 1; + while (!feof(ptr)) { + ch = fgetc(ptr); + if (ch == '\n'){ + lignes ++; + end = 0; + } + else if(ch == ',' && end) { + colonnes ++; + } + } + + LIGNES = lignes; + COLONNES = colonnes; + DISTANCE = (int)round(sqrt(COLONNES *COLONNES + LIGNES *LIGNES)); +} + +//retourne la matrice +int* read_img(char* filepath){ + int* matrice = malloc((COLONNES * LIGNES) * sizeof(int)); + + FILE* ptr; + char ch; + ptr = fopen(filepath, "r"); + + if (NULL == ptr) { + printf("file can't be opened \n"); + } + int c = 0; + while (!feof(ptr)) { + ch = fgetc(ptr); + if (feof(ptr)){ + fclose(ptr); + return matrice; + } + if (ch == '\n'){ + c++; + } + else if(ch == ',') { + c++; + } + else if (ch == ' '){} + else { + //printf("%c\n", ch); + matrice[c] *= 10; + + matrice[c] += (int)ch - 48; + } + } +} + +void print_matrice(int* mat){ + for (int i = 0; i 250){ + for (int theta_deg = 0; theta_deg < 360 * precision ;theta_deg ++){ + theta_rad = theta_deg /(180.0 * precision) * M_PI; + v_rho = rho(x, y, theta_rad); + if (v_rho > 0){ + mat_hough[v_rho * 360 * precision + theta_deg] += 1; + } + } + } + } + } + return(mat_hough); +} + + +void print_hough(int* mat){ + for (int i = 0; i<360; i++){ + for (int j = 0; j < DISTANCE; j++){ + printf("%d | ", mat[j*360 + i]); + } + printf("\n"); + } +} + +void save_file(int* mat, int precision){ + FILE *ptr; + ptr = fopen("./out.txt","w"); + + for (int j = 0; j < DISTANCE; j++){ + for (int i = 0; i<360 * precision; i++){ + fprintf(ptr, "%d", mat[j*360 * precision + i]); + if (i != 360 * precision - 1){ + fprintf(ptr, "%s",","); + } + } + fprintf(ptr, "%s","\n"); + } + + fclose(ptr); + +} + + +int main() +{ + int precision = 100; + printf("precision : %d\n", precision); + char* filepath = "./in.txt"; + taille(filepath); + printf("Lignes : %d\nColonnes : %d\nnombre de pixels : %d\nDistance : %d\n", LIGNES, COLONNES,(LIGNES)*(COLONNES), DISTANCE); + int* image = read_img(filepath); + //print_matrice(image); + int* espace = espace_hough(image, precision); + //print_hough(espace); + + save_file(espace, precision); + return 0; +} + diff --git a/c/hough_multi.c b/c/hough_multi.c new file mode 100644 index 0000000..9eae57a --- /dev/null +++ b/c/hough_multi.c @@ -0,0 +1,161 @@ +// Objectif : utiliser du multithreading pour améliorer la rapidité +#include +#include +#include +#include + +#include + +int PRECISION = 10; +char* filepath = "./in.txt"; + +int COLONNES; +int LIGNES; +int DISTANCE; +int* IMAGE; +int* ESPACE; + +// taille du fichier. update global variables +int* taille(char* filepath){ + FILE* ptr; + char ch; + ptr = fopen(filepath, "r"); + if (NULL == ptr) { + printf("file can't be opened \n"); + } + int lignes = 0; + int colonnes = 1; + int end = 1; + while (!feof(ptr)) { + ch = fgetc(ptr); + if (ch == '\n'){ + lignes ++; + end = 0; + } + else if(ch == ',' && end) { + colonnes ++; + } + } + + LIGNES = lignes; + COLONNES = colonnes; + DISTANCE = (int)round(sqrt(COLONNES *COLONNES + LIGNES *LIGNES)); +} +//retourne la matrice +int* read_img(char* filepath){ + int* matrice = malloc((COLONNES * LIGNES) * sizeof(int)); + + FILE* ptr; + char ch; + ptr = fopen(filepath, "r"); + + if (NULL == ptr) { + printf("file can't be opened \n"); + } + int c = 0; + while (!feof(ptr)) { + ch = fgetc(ptr); + if (feof(ptr)){ + fclose(ptr); + IMAGE = matrice; + return NULL; + } + if (ch == '\n'){ + c++; + } + else if(ch == ',') { + c++; + } + else if (ch == ' '){} + else { + //printf("%c\n", ch); + matrice[c] *= 10; + + matrice[c] += (int)ch - 48; + } + } + +} + + +double rho(int x, int y, double theta){ + return(x * cos(theta) + y * sin(theta)); +} + + +void* espace_hough(void* arg){ + int thread = *(int*)arg; // numero du thread equivalent a la zone de degré qui va être parcourue + int v_rho; + double theta_rad; + for (int y = 0; y < LIGNES; y++){ + for (int x = 0; x < COLONNES; x++){ + if (IMAGE[y*COLONNES + x] > 250){ + for (int theta_deg = 360 * thread ; theta_deg < 360 * (thread+1) ;theta_deg ++){ + theta_rad = theta_deg /(180.0 * PRECISION) * M_PI; + v_rho = rho(x, y, theta_rad); // determine R + if (v_rho > 0){ + ESPACE[v_rho * 360 * PRECISION + theta_deg] += 1; + } + } + } + } + } + return(NULL); +} + + + +void save_file(int* mat, int PRECISION){ + FILE *ptr; + ptr = fopen("./out.txt","w"); + + for (int j = 0; j < DISTANCE; j++){ + for (int i = 0; i<360 * PRECISION; i++){ + fprintf(ptr, "%d", mat[j*360 * PRECISION + i]); + if (i != 360 * PRECISION - 1){ + fprintf(ptr, "%s",","); + } + } + fprintf(ptr, "%s","\n"); + } + + fclose(ptr); + +} + + +int main(int argc, char *argv[]) +{ + if (argc == 2){ + PRECISION = strtol(argv[1], NULL, 10); + } + taille(filepath); + + printf("Precision : %d\nLignes : %d\nColonnes : %d\nnombre de pixels : %d\nDistance : %d\n", PRECISION, LIGNES, COLONNES,(LIGNES)*(COLONNES), DISTANCE); + read_img(filepath); + + int* mat_hough = malloc(360 * PRECISION * DISTANCE * sizeof(int)); + for (int i = 0; i<360 * DISTANCE * PRECISION; i++){ + mat_hough[i] = 0; + } + ESPACE = mat_hough; + + pthread_t* t = (pthread_t*)malloc(sizeof(pthread_t)*(PRECISION)); + int* tab_int = malloc(PRECISION * sizeof(int)); + for(int i = 0; i < PRECISION; i++){ + tab_int[i] = i; + } + + for (int i=0; i < PRECISION; i++) { + pthread_create(&t[i], NULL, espace_hough, (void*)&tab_int[i]); + } + + for (int i=0; i < PRECISION; i++) { + pthread_join(t[i], NULL); + } + + save_file(ESPACE, PRECISION); + + return 0; +} +