From 8fdc8c7a06c2ec9367945c32516d64ca0f59bd18 Mon Sep 17 00:00:00 2001 From: matthieu BARONNET Date: Sun, 25 Oct 2020 17:42:37 +0100 Subject: [PATCH 1/3] adding some segmentation function, no .h file yet --- src/segmentation/segmentation.c | 236 ++++++++++++++++++++++++++++++++ 1 file changed, 236 insertions(+) create mode 100644 src/segmentation/segmentation.c diff --git a/src/segmentation/segmentation.c b/src/segmentation/segmentation.c new file mode 100644 index 0000000..20bad94 --- /dev/null +++ b/src/segmentation/segmentation.c @@ -0,0 +1,236 @@ +#include +#include +#include +#include + + +void hori_histo(unsigned int ext[], size_t sizex, size_t sizey, unsigned char img[sizey][sizex]); +void vert_histo(size_t upper,size_t lower,unsigned int ext[], size_t sizex, size_t sizey, unsigned char img[sizey][sizex]); +void word_seg(size_t upper,size_t lower,size_t sizex, size_t sizey, unsigned char img[sizey][sizex]); +void line_seg(size_t sizex, size_t sizey, unsigned char img[sizey][sizex]); + + +/** + * + * testing function that generates a random binarized image to experiment with histogram + * genaration(sadly it appear to be sligthly broken at the moment) + * + */ +void generate( size_t sizex, size_t sizey,unsigned char image[sizey][sizex]) +{ + srand((unsigned int)time(NULL)); + size_t i, j; + for ( i=1; i < sizey-1; ++i) + { + for ( j=1; j < sizex-1; j++) + { + image[i][j] = rand() % 2; + + } + } + + for (size_t y = 1; y < sizex-1; y++) + { + image[0][y]=0; + image[sizey-1][y]=0; + } +} + + +int main() +{ + size_t sizex = 60; + size_t sizey = 60; + unsigned char image[sizey][sizex]; + + //unsigned char image[][4]={ { 0,1,0,1 } , { 1,1,1,1 } ,{ 1,0,0,0 }}; + + //unsigned int test[60]; + //unsigned int ptest = test; + + generate(sizex,sizey,image); + line_seg(sizex,sizey,image); + + //printf("%s\n",ext); + + return 0; +} + + +/** + * + * Modify array into a histogram of a bit multidimensionnal array. + * horizontal lecture. + * @author matthieu + * @param img the image analysed + * @param ext empty array that will be modified + * @param sizex horizontal size of the treated image + * @param sizey verticale size of the treated image + * + */ +void hori_histo(unsigned int ext[], size_t sizex, size_t sizey, unsigned char img[sizey][sizex]) +{ + + for(unsigned int y = 0 ; y < sizey ; y++) + { + ext[y]=0; + for(unsigned int x = 0; x < sizex ; x++) + { + ext[y] += img[y][x]; + //printf("%u",img[y][x]); + } + //printf("\n"); + } +} + +/** + * + * Uses hori_histo's horizontal histogram to segment different lines in img, + * then calls word_seg for each segmented line. + * @author matthieu + * @param img analysed image + * @param sizex horizontal size of img + * @param sizey verticale size of img + * + */ +void line_seg(size_t sizex, size_t sizey, unsigned char img[sizey][sizex]) +{ + unsigned int ho_histo[sizey]; + hori_histo(ho_histo,sizex,sizey,img); + + size_t y=0; + size_t upper=0; + size_t lower=0; + while (y4) + { + space_finder = 0; + right = x-nbspace; + //printf("%lu,%lu",left,right); + } + } + x+=1; + } + } + else + { + x+=1; + } + } + printf("%lu,%lu\n",left,right); + + /* + for(size_t i =0; i< sizex; i++) + { + + for (unsigned int y = 0; y < ver_histo[i]; y++) + { + printf("|"); + } + + printf("histo(%lu) = %u\n",i,ver_histo[i]); + //printf("\n"); + } + */ + +} + + +/** + * + * Modify array into a histogram of a bit multidimensionnal array. + * vertical lecture + * @author matthieu + * @param img the image analysed + * @param ext empty array that will be modified + * @param sizex horizontal size of img + * @param sizey verticale size of img + * @param upper limit the histogram to a precise max in img[][] + * @param lower limit the histogram to a precise min in img[][] + * + */ +void vert_histo(size_t upper,size_t lower,unsigned int ext[], size_t sizex, size_t sizey, unsigned char img[sizey][sizex]) +{ + size_t x, y; + for(x = 0 ; x < sizex ; x++) + { + ext[x]=0; + //printf("ext(%lu) = %u\n",x,ext[x]); + for(y = upper; y < lower ; y++) + { + + ext[x] += img[y][x]; + //printf("%u",img[y][x]); + } + //printf("\n"); + //printf("ext(%lu) = %u\n",x,ext[x]); + } +} + + From 602575ed79719fdcb04c21fbb53c12cdaacaa823 Mon Sep 17 00:00:00 2001 From: matthieu BARONNET Date: Sun, 25 Oct 2020 19:12:50 +0100 Subject: [PATCH 2/3] seg structure done, added segmentation.h, program can segment but return not yet functionnal --- src/segmentation/segmentation.c | 61 +++++++++++++++++++++++++++++---- src/segmentation/segmentation.h | 10 ++++++ 2 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 src/segmentation/segmentation.h diff --git a/src/segmentation/segmentation.c b/src/segmentation/segmentation.c index 20bad94..28f4318 100644 --- a/src/segmentation/segmentation.c +++ b/src/segmentation/segmentation.c @@ -5,7 +5,8 @@ void hori_histo(unsigned int ext[], size_t sizex, size_t sizey, unsigned char img[sizey][sizex]); -void vert_histo(size_t upper,size_t lower,unsigned int ext[], size_t sizex, size_t sizey, unsigned char img[sizey][sizex]); +void vert_histo(size_t upper,size_t lower,unsigned int ext[], size_t sizex, size_t sizey, unsigned char img[sizey][sizex], size_t left, size_t right); +void letter_seg(size_t upper,size_t lower, size_t left, size_t right, size_t sizex, size_t sizey, unsigned char img[sizey][sizex]); void word_seg(size_t upper,size_t lower,size_t sizex, size_t sizey, unsigned char img[sizey][sizex]); void line_seg(size_t sizex, size_t sizey, unsigned char img[sizey][sizex]); @@ -16,6 +17,7 @@ void line_seg(size_t sizex, size_t sizey, unsigned char img[sizey][sizex]); * genaration(sadly it appear to be sligthly broken at the moment) * */ +/* void generate( size_t sizex, size_t sizey,unsigned char image[sizey][sizex]) { srand((unsigned int)time(NULL)); @@ -35,8 +37,8 @@ void generate( size_t sizex, size_t sizey,unsigned char image[sizey][sizex]) image[sizey-1][y]=0; } } - - +*/ +/* int main() { size_t sizex = 60; @@ -55,7 +57,7 @@ int main() return 0; } - +*/ /** * @@ -148,7 +150,7 @@ void line_seg(size_t sizex, size_t sizey, unsigned char img[sizey][sizex]) void word_seg(size_t upper,size_t lower,size_t sizex, size_t sizey, unsigned char img[sizey][sizex]) { unsigned int ver_histo[sizex]; - vert_histo(upper,lower,ver_histo,sizex,sizey,img); + vert_histo(upper,lower,ver_histo,sizex,sizey,img,0,sizex); size_t x = 0; @@ -213,12 +215,15 @@ void word_seg(size_t upper,size_t lower,size_t sizex, size_t sizey, unsigned cha * @param sizey verticale size of img * @param upper limit the histogram to a precise max in img[][] * @param lower limit the histogram to a precise min in img[][] + * @param left specify from which indexe to start histogram + * @param right specify from which indexe to end histogram * */ -void vert_histo(size_t upper,size_t lower,unsigned int ext[], size_t sizex, size_t sizey, unsigned char img[sizey][sizex]) +void vert_histo(size_t upper,size_t lower,unsigned int ext[], size_t sizex, size_t sizey, + unsigned char img[sizey][sizex],size_t left, size_t right) { size_t x, y; - for(x = 0 ; x < sizex ; x++) + for(x = left ; x < right ; x++) { ext[x]=0; //printf("ext(%lu) = %u\n",x,ext[x]); @@ -234,3 +239,45 @@ void vert_histo(size_t upper,size_t lower,unsigned int ext[], size_t sizex, size } + +/** + * + * Uses vert_histo vertical histogram to segment letters in a word. + * the neural network is called for each segmented letter to analyse them. + * @author matthieu + * @param img analysed image + * @param sizex horizontal size of img + * @param sizey verticale size of img + * @param upper limit the histogram to a precise max in img[][] + * @param lower limit the histogram to a precise min in img[][] + * @param left specify from which indexe to start each word + * @param right specify from which indexe to end each word + * + */ +void letter_seg(size_t upper,size_t lower, size_t left, size_t right, size_t sizex, + size_t sizey, unsigned char img[sizey][sizex]) +{ + unsigned int word_histo[right-left+1]; + vert_histo(upper,lower,word_histo,sizex,sizey,img,left,right); + + size_t x = 0; + size_t y = 0; + while (x Date: Sun, 25 Oct 2020 21:11:11 +0100 Subject: [PATCH 3/3] chore: Reformat code --- CMakeLists.txt | 7 +- src/segmentation/segmentation.c | 328 ++++++++++++++++---------------- src/segmentation/segmentation.h | 30 ++- 3 files changed, 198 insertions(+), 167 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 221c5c6..b0167f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.17) +cmake_minimum_required(VERSION 3.16) project(CText C) set(CMAKE_C_STANDARD 99) @@ -21,6 +21,11 @@ add_executable( src/test/test_method.c src/test/test_method.h ) +add_executable( + CText-seg + src/segmentation/segmentation.c src/segmentation/segmentation.h +) + target_link_libraries(CText m) target_link_libraries(CText-tests m) diff --git a/src/segmentation/segmentation.c b/src/segmentation/segmentation.c index 28f4318..0138c39 100644 --- a/src/segmentation/segmentation.c +++ b/src/segmentation/segmentation.c @@ -3,11 +3,30 @@ #include #include - -void hori_histo(unsigned int ext[], size_t sizex, size_t sizey, unsigned char img[sizey][sizex]); -void vert_histo(size_t upper,size_t lower,unsigned int ext[], size_t sizex, size_t sizey, unsigned char img[sizey][sizex], size_t left, size_t right); -void letter_seg(size_t upper,size_t lower, size_t left, size_t right, size_t sizex, size_t sizey, unsigned char img[sizey][sizex]); -void word_seg(size_t upper,size_t lower,size_t sizex, size_t sizey, unsigned char img[sizey][sizex]); +void hori_histo(unsigned int ext[], + size_t sizex, + size_t sizey, + unsigned char img[sizey][sizex]); +void vert_histo(size_t upper, + size_t lower, + unsigned int ext[], + size_t sizex, + size_t sizey, + unsigned char img[sizey][sizex], + size_t left, + size_t right); +void letter_seg(size_t upper, + size_t lower, + size_t left, + size_t right, + size_t sizex, + size_t sizey, + unsigned char img[sizey][sizex]); +void word_seg(size_t upper, + size_t lower, + size_t sizex, + size_t sizey, + unsigned char img[sizey][sizex]); void line_seg(size_t sizex, size_t sizey, unsigned char img[sizey][sizex]); @@ -17,7 +36,7 @@ void line_seg(size_t sizex, size_t sizey, unsigned char img[sizey][sizex]); * genaration(sadly it appear to be sligthly broken at the moment) * */ -/* + void generate( size_t sizex, size_t sizey,unsigned char image[sizey][sizex]) { srand((unsigned int)time(NULL)); @@ -37,27 +56,26 @@ void generate( size_t sizex, size_t sizey,unsigned char image[sizey][sizex]) image[sizey-1][y]=0; } } -*/ -/* -int main() -{ - size_t sizex = 60; - size_t sizey = 60; - unsigned char image[sizey][sizex]; - //unsigned char image[][4]={ { 0,1,0,1 } , { 1,1,1,1 } ,{ 1,0,0,0 }}; - - //unsigned int test[60]; - //unsigned int ptest = test; - generate(sizex,sizey,image); - line_seg(sizex,sizey,image); +int main() { + size_t sizex = 60; + size_t sizey = 60; + unsigned char image[sizey][sizex]; + + //unsigned char image[][4]={ { 0,1,0,1 } , { 1,1,1,1 } ,{ 1,0,0,0 }}; - //printf("%s\n",ext); + //unsigned int test[60]; + //unsigned int ptest = test; - return 0; + generate(sizex, sizey, image); + line_seg(sizex, sizey, image); + + //printf("%s\n",ext); + + return 0; } -*/ + /** * @@ -70,19 +88,19 @@ int main() * @param sizey verticale size of the treated image * */ -void hori_histo(unsigned int ext[], size_t sizex, size_t sizey, unsigned char img[sizey][sizex]) -{ - - for(unsigned int y = 0 ; y < sizey ; y++) - { - ext[y]=0; - for(unsigned int x = 0; x < sizex ; x++) - { - ext[y] += img[y][x]; - //printf("%u",img[y][x]); - } - //printf("\n"); +void hori_histo(unsigned int ext[], + size_t sizex, + size_t sizey, + unsigned char img[sizey][sizex]) { + + for (unsigned int y = 0; y < sizey; y++) { + ext[y] = 0; + for (unsigned int x = 0; x < sizex; x++) { + ext[y] += img[y][x]; + //printf("%u",img[y][x]); } + //printf("\n"); + } } /** @@ -95,45 +113,38 @@ void hori_histo(unsigned int ext[], size_t sizex, size_t sizey, unsigned char im * @param sizey verticale size of img * */ -void line_seg(size_t sizex, size_t sizey, unsigned char img[sizey][sizex]) -{ - unsigned int ho_histo[sizey]; - hori_histo(ho_histo,sizex,sizey,img); - - size_t y=0; - size_t upper=0; - size_t lower=0; - while (y4) - { - space_finder = 0; - right = x-nbspace; - //printf("%lu,%lu",left,right); - } - } - x+=1; - } - } - else - { - x+=1; + size_t x = 0; + size_t left = 0; + size_t right = 0; + int space_finder; + int nbspace; + while (x < sizex) { + if (ver_histo[x] != 0) { + left = x; + space_finder = 1; + nbspace = 0; + while ((space_finder) && (x < sizex)) { + if (ver_histo[x] == 0) { + nbspace += 1; + if (nbspace > 4) { + space_finder = 0; + right = x - nbspace; + //printf("%lu,%lu",left,right); + } } + x += 1; + } + } else { + x += 1; } - printf("%lu,%lu\n",left,right); + } + printf("%lu,%lu\n", left, right); - /* - for(size_t i =0; i< sizex; i++) - { - - for (unsigned int y = 0; y < ver_histo[i]; y++) - { - printf("|"); - } - - printf("histo(%lu) = %u\n",i,ver_histo[i]); - //printf("\n"); - } - */ + /* + for(size_t i =0; i< sizex; i++) + { -} + for (unsigned int y = 0; y < ver_histo[i]; y++) + { + printf("|"); + } + + printf("histo(%lu) = %u\n",i,ver_histo[i]); + //printf("\n"); + } + */ +} /** * @@ -219,27 +224,28 @@ void word_seg(size_t upper,size_t lower,size_t sizex, size_t sizey, unsigned cha * @param right specify from which indexe to end histogram * */ -void vert_histo(size_t upper,size_t lower,unsigned int ext[], size_t sizex, size_t sizey, - unsigned char img[sizey][sizex],size_t left, size_t right) -{ - size_t x, y; - for(x = left ; x < right ; x++) - { - ext[x]=0; - //printf("ext(%lu) = %u\n",x,ext[x]); - for(y = upper; y < lower ; y++) - { - - ext[x] += img[y][x]; - //printf("%u",img[y][x]); - } - //printf("\n"); - //printf("ext(%lu) = %u\n",x,ext[x]); +void vert_histo(size_t upper, + size_t lower, + unsigned int ext[], + size_t sizex, + size_t sizey, + unsigned char img[sizey][sizex], + size_t left, + size_t right) { + size_t x, y; + for (x = left; x < right; x++) { + ext[x] = 0; + //printf("ext(%lu) = %u\n",x,ext[x]); + for (y = upper; y < lower; y++) { + + ext[x] += img[y][x]; + //printf("%u",img[y][x]); } + //printf("\n"); + //printf("ext(%lu) = %u\n",x,ext[x]); + } } - - /** * * Uses vert_histo vertical histogram to segment letters in a word. @@ -254,30 +260,28 @@ void vert_histo(size_t upper,size_t lower,unsigned int ext[], size_t sizex, size * @param right specify from which indexe to end each word * */ -void letter_seg(size_t upper,size_t lower, size_t left, size_t right, size_t sizex, - size_t sizey, unsigned char img[sizey][sizex]) -{ - unsigned int word_histo[right-left+1]; - vert_histo(upper,lower,word_histo,sizex,sizey,img,left,right); +void letter_seg(size_t upper, + size_t lower, + size_t left, + size_t right, + size_t sizex, + size_t sizey, + unsigned char img[sizey][sizex]) { + unsigned int word_histo[right - left + 1]; + vert_histo(upper, lower, word_histo, sizex, sizey, img, left, right); - size_t x = 0; - size_t y = 0; - while (x + +void hori_histo(unsigned int ext[], + size_t sizex, + size_t sizey, + unsigned char img[sizey][sizex]); +void vert_histo(size_t upper, + size_t lower, + unsigned int ext[], + size_t sizex, + size_t sizey, + unsigned char img[sizey][sizex], + size_t left, + size_t right); +void letter_seg(size_t upper, + size_t lower, + size_t left, + size_t right, + size_t sizex, + size_t sizey, + unsigned char img[sizey][sizex]); +void word_seg(size_t upper, + size_t lower, + size_t sizex, + size_t sizey, + unsigned char img[sizey][sizex]); void line_seg(size_t sizex, size_t sizey, unsigned char img[sizey][sizex]); #endif \ No newline at end of file