/* CHAR DETECTION PART */ struct vector *lines_to_char(struct matrix *img, struct vector *lines) { struct vector *imgs = vector_make((img->height) * (img->width)); for (size_t k = 0; k < lines->size; k++) { struct coords actual_coords; actual_coords.w1 = 0; actual_coords.w2 = 0; actual_coords.h1 = lines->data[k].h1; actual_coords.h2 = lines->data[k].h2; int recording = 0; for (size_t i = 0; i < img->width; i++) { if (!recording) { if (!column_is_empty(img, i, lines->data[k].h1, lines->data[k].h2)) { recording = 1; actual_coords.w1 = i; actual_coords.w2 = i; } } else { if (!column_is_empty(img, i, lines->data[k].h1, lines->data[k].h2)) { actual_coords.w2++; } else { vector_push_back(imgs, actual_coords); recording = 0; } } } actual_coords.w1 = -1; vector_push_back(imgs, actual_coords); } return imgs; }
int vertical_search(struct matrix *img, struct coords block) { int i = block.w1; for(; i < block.w2 && column_is_empty(img, i, block.h1, block.h2); i++){ } for(; i < block.w2 && !column_is_empty(img, i, block.h1, block.h2); i++){ } if(i == block.w2) return 0; return i; }
//the main functions : struct vector *img_to_blocks(struct matrix *img) { struct coords init; struct vector *output; //all the text is within the square ((w1,h1),(w2,h2)) size_t i = 0; for(; i < img->height - 1 && line_is_empty(img, i, 0, img->width - 1); i++){} init.h1 = i; for(i = img->height - 1; i > 0 && line_is_empty(img, i, 0, img->width - 1); i--){} init.h2 = i; for(i = 0; i < img->width - 1 && column_is_empty(img, i, init.h1, init.h2); i++){} init.w1 = i; for(i = img->width - 1; i > 0 && column_is_empty(img, i, init.h1, init.h2); i--){} init.w2 = i; //we create a matrix with appearant blocks struct matrix *M = malloc(sizeof(struct matrix)); M->data = malloc(sizeof(double) * img->width * img->height); M->width = img->width, M->height = img->height; for(size_t i = 0; i < img->width * img->height; i++) { M->data[i] = img->data[i]; } for(size_t i = 0; i < 5; i++) { filter_noise(M); if(i % 2) filter_contrast(M); } //we launch the block detection in that original block if(init.w1 < init.w2 && init.h1 < init.h2) output = vertical_rec(M, init, 1); else output = NULL; //invalid image. free(M->data); free(M); return output; }
/* Is there just one column, and is it empty? */ Column * workspace_is_one_empty( Workspace *ws ) { GSList *children = ICONTAINER( ws )->children; Column *col; if( g_slist_length( children ) != 1 ) return( NULL ); col = COLUMN( children->data ); if( !column_is_empty( col ) ) return( NULL ); return( col ); }