OCR project

This commit is contained in:
brice.boisson
2022-02-08 19:16:25 +01:00
commit eaa4da6ade
3475 changed files with 126899 additions and 0 deletions

View File

@@ -0,0 +1,159 @@
#include "extractchar.h"
#include <string.h>
#include "../display.h"
/*
Return the number of word in the current image
*/
int countwc(SDL_Surface *img)
{
int w = img->w;
Uint32 pixel = 0;
Uint8 r, g, b;
int count = 0;
for (int i = 0; i < w; i++)
{
pixel = getpixel(img, i, 1);
SDL_GetRGB(pixel, img->format, &r, &g, &b);
if ((r == 1 && g == 100 && b == 100))
{
count++;
}
}
return count;
}
/*
Return an array of the position of words in the current image
*/
void ReturnPoswc(SDL_Surface *img, int *tab)
{
int w = img->w;
Uint32 pixel = 0;
Uint8 r, g, b;
int count = 0;
for (int i = 0; i < w; i++)
{
pixel = getpixel(img, i, 1);
SDL_GetRGB(pixel, img->format, &r, &g, &b);
if ((r == 1 && g == 100 && b == 100))
{
tab[count] = i;
count++;
}
}
}
/*
Create a new surface and extract the word for charactere segmentation
*/
void extractword(SDL_Surface *img, int x, int y, Neural_Network *network,
char *str)
{
SDL_Surface *new = CreateWhiteSurface(y - x + 1, img->h, img);
Uint32 pixel = 0;
for (int i = x + 1; i < y; i++)
{
for (int j = 0; j < img->h; j++)
{
pixel = getpixel(img, i, j);
putpixel(new, i - x, j, pixel);
}
}
SDL_SaveBMP(new, "final12.bmp");
__extractchar(new, network, str);
SDL_FreeSurface(new);
}
/*
Create a new surface and extract the caracters for neural network
*/
void __extractword(SDL_Surface *img, Neural_Network *network, char *str)
{
SDL_Surface *loadedImage = resize(img, 1218, 41);
cutword(loadedImage);
int count = countwc(loadedImage);
int *tab = malloc(sizeof(int) * count);
ReturnPoswc(loadedImage, tab);
for (int i = 0; i < count - 1; i += 1)
{
extractword(loadedImage, tab[i], tab[i + 1], network, str);
char a[] = " ";
strcat(str, a);
}
SDL_FreeSurface(loadedImage);
free(tab);
}
int fullofwhite(SDL_Surface *img)
{
Uint32 pixel = 0;
Uint8 r, g, b;
for (int i = 0; i < img->h; i++)
{
for (int j = 0; j < img->w; j++)
{
pixel = getpixel(img, j, i);
SDL_GetRGB(pixel, img->format, &r, &g, &b);
if (r == 0 && g == 0 && b == 0)
{
return 1;
}
}
}
return 0;
}
void extractchar(SDL_Surface *img, int x, int y, Neural_Network *network,
char *str)
{
SDL_Surface *new = CreateWhiteSurface(y - x + 1, img->h, img);
Uint32 pixel = 0;
for (int i = x + 1; i < y; i++)
{
for (int j = 0; j < img->h; j++)
{
pixel = getpixel(img, i, j);
putpixel(new, i - x, j, pixel);
}
}
if (fullofwhite(new))
{
double *letter = segmentationtomatrix(new, 20);
ForwardPass(letter, network);
free(letter);
char a[2];
a[0] = indiceToChar(network->output);
a[1] = '\0';
strcat(str, a);
}
}
void __extractchar(SDL_Surface *img, Neural_Network *network, char *str)
{
SDL_Surface *loadedImage = copy_image(img);
CutColumn(loadedImage, 0, img->h);
int count = countwc(loadedImage);
int *tab = malloc(sizeof(int) * count);
ReturnPoswc(loadedImage, tab);
for (int i = 0; i < count - 1; i += 1)
{
extractchar(loadedImage, tab[i], tab[i + 1], network, str);
}
SDL_FreeSurface(loadedImage);
free(tab);
}

View File

@@ -0,0 +1,18 @@
#ifndef _EXTRACTIONCHAR_H
#define _EXTRACTIONCHAR_H
#include "../../NeuralNetwork/structure.h"
#include "../../NeuralNetwork/toolsnetworks.h"
#include "../../NeuralNetwork/traitement.h"
#include "../Segmentation/segmentation.h"
void extractword(SDL_Surface *img, int x, int y, Neural_Network *network,
char *str);
int countwc(SDL_Surface *img);
void ReturnPoswc(SDL_Surface *img, int *tab);
void __extractword(SDL_Surface *img, Neural_Network *network, char *str);
void __extractchar(SDL_Surface *img, Neural_Network *network, char *str);
void extractchar(SDL_Surface *img, int x, int y, Neural_Network *network,
char *str);
#endif

View File

@@ -0,0 +1,142 @@
#include "extraction.h"
#include <string.h>
#include "../Segmentation/segmentation.h"
#include "../display.h"
#include "extractchar.h"
/*
Count the number of line and paragraph in the current image
*/
int countlinepar(SDL_Surface *img)
{
int h = img->h;
Uint32 pixel = 0;
Uint8 r, g, b;
int count = 0;
for (int i = 0; i < h; i++)
{
pixel = getpixel(img, 1, i);
SDL_GetRGB(pixel, img->format, &r, &g, &b);
if ((r == 0 && g == 0 && b == 255)
|| (r == 255 && g == 150 && b == 255))
{
count++;
}
}
return count;
}
/*
Fill an array with the position of line and paragraph
*/
void ReturnPosPar(SDL_Surface *img, int *tab)
{
int h = img->h;
Uint32 pixel = 0;
Uint8 r, g, b;
int count = 0;
for (int i = 0; i < h; i++)
{
pixel = getpixel(img, 1, i);
SDL_GetRGB(pixel, img->format, &r, &g, &b);
if ((r == 0 && g == 0 && b == 255)
|| (r == 255 && g == 150 && b == 255))
{
tab[count] = i;
count++;
}
}
}
/*
Create a new surface and paragraph for line segmentation
*/
void extractpar(SDL_Surface *img, int x, int y, Neural_Network *network,
char *str)
{
SDL_Surface *new = CreateWhiteSurface(img->w, y - x + 1, img);
Uint32 pixel = 0;
for (int i = 0; i < img->w; i++)
{
for (int j = x + 1; j < y; j++)
{
pixel = getpixel(img, i, j);
putpixel(new, i, j - x, pixel);
}
}
__extractline(new, network, str);
SDL_FreeSurface(new);
}
void __extractpar(SDL_Surface *img, Neural_Network *network, char *str)
{
SDL_Surface *imagev = copy_image(img);
SDL_Surface *imagerlsa = copy_image(img);
SDL_Surface *copy_image1 = copy_image(img);
blockDetection_vertical(imagev);
drawBlocks(imagerlsa, imagev);
drawBlocksLines(copy_image1, imagerlsa);
int count = countlinepar(copy_image1);
int *tab = malloc(sizeof(int) * count);
ReturnPosPar(copy_image1, tab);
for (int i = 0; i < count - 1; i += 2)
{
extractpar(copy_image1, tab[i], tab[i + 1], network, str);
char a[] = "\n\n\n";
strcat(str, a);
}
SDL_FreeSurface(imagerlsa);
SDL_FreeSurface(imagev);
SDL_FreeSurface(copy_image1);
free(tab);
}
/*
Create a new surface and extract the line for word segmentation
*/
void extractline(SDL_Surface *img, int x, int y, Neural_Network *network,
char *str)
{
SDL_Surface *new = CreateWhiteSurface(img->w, y - x + 1, img);
Uint32 pixel = 0;
for (int i = 0; i < img->w; i++)
{
for (int j = x + 1; j < y; j++)
{
pixel = getpixel(img, i, j);
putpixel(new, i, j - x, pixel);
}
}
__extractword(new, network, str);
SDL_FreeSurface(new);
}
void __extractline(SDL_Surface *img, Neural_Network *network, char *str)
{
SDL_Surface *copy = copy_image(img);
CutLines(copy, 0);
int count = countlinepar(copy);
int *tab = malloc(sizeof(int) * count);
ReturnPosPar(copy, tab);
for (int i = 0; i < count - 1; i += 2)
{
extractline(copy, tab[i], tab[i + 1], network, str);
char a[] = "\n";
strcat(str, a);
}
SDL_FreeSurface(copy);
free(tab);
}

View File

@@ -0,0 +1,16 @@
#ifndef _EXTRACTION_H
#define _EXTRACTION_H
#include "../../NeuralNetwork/structure.h"
#include "../Segmentation/segmentation.h"
void extractpar(SDL_Surface *img, int x, int y, Neural_Network *network,
char *str);
int countlinepar(SDL_Surface *img);
void ReturnPosPar(SDL_Surface *img, int *tab);
void __extractpar(SDL_Surface *img, Neural_Network *network, char *str);
void __extractline(SDL_Surface *img, Neural_Network *network, char *str);
void extractline(SDL_Surface *img, int x, int y, Neural_Network *network,
char *str);
#endif