Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update OCR/Pretreatment.ml

  • Loading branch information...
commit c086316a868ed5dff3f5600f894b6360f9760550 1 parent 1e325a6
dezyal dezyal authored
Showing with 18 additions and 103 deletions.
  1. +18 −103 OCR/Pretreatment.ml
121 OCR/Pretreatment.ml
View
@@ -16,10 +16,10 @@ let mtx2su mtx w h pfi =
for c = 0 to h-1 do
for l = 0 to w-1 do
if mtx.(l).(c) = 100 then
- Sdlvideo.put_pixel_color su l c (255, 140, 0)
- else
+ Sdlvideo.put_pixel_color su l c (16, 52, 166)
+ else begin
clr := mtx.(l).(c);
- Sdlvideo.put_pixel_color su l c (!clr, !clr, !clr)
+ Sdlvideo.put_pixel_color su l c (!clr, !clr, !clr) end
done
done;
Sdlvideo.unlock su;
@@ -29,6 +29,7 @@ let mtx2su mtx w h pfi =
Grise une surface et renvoi la matrice de couleurs correspondante.
*)
let su2grey su w h =
+ print_string "Level of grey ...";
let mtx = Array.make_matrix w h 0 in
for c = 0 to h-1 do
for l = 0 to w-1 do
@@ -40,12 +41,14 @@ let su2grey su w h =
mtx.(l).(c) <- lu
done
done;
+ print_string " done\n";
mtx
(*
Applique un filtre médian relâché sur la matrice de couleur d'une surface.
*)
let dust_off mtx w h =
+ print_string "Median filter ...";
let mtx2 = Array.copy mtx and ar = Array.make 9 0 in
for c = 1 to h-2 do
for l = 1 to w-2 do
@@ -57,49 +60,16 @@ let dust_off mtx w h =
Array.fast_sort Tools.cmp ar;
mtx.(l).(c) <- ar.(4)
done
- done
+ done;
+ print_string " done\n";
+ mtx2
-(*
- Binarisation locale de la matrice.
-*)
-(*
-let createMtx mtx c l =
- let new_mtx = Array.make_matrix 15 15 0 in
- let x = c-7 in
- let y = l-7 in
- for i = 0 to 14 do
- for j = 0 to 14 do
- new_mtx.(i).(j) <- mtx.(i+x).(j+y)
- done
- done;
- new_mtx
-*)
-(*
-let bin mtx w h =
- let new_mtx = Array.make_matrix w h 0 in
- for c = 7 to w-8 do
- for l = 7 to h-8 do
- let pix = mtx.(c).(l) in
- let tab = Tools.mtx2arr (createMtx mtx c l) in
- let zb = tab.(0) in
- let zh = tab.((Array.length tab)-1) in
- let seuil = ((float)zb +. (float)zh)/.2. in
- print_int( int_of_float seuil);
- print_newline();
- if (float)pix < seuil then
- new_mtx.(c).(l) <- 0
- else
- new_mtx.(c).(l) <- 255
- done
- done;
- new_mtx
-*)
let pre_bin mtx w h =
let mtx2 = Array.make_matrix w h 255 and px = ref(255) in
for c = 0 to h-1 do
for l = 0 to w-1 do
px := mtx.(l).(c);
- if !px > 180 then
+ if !px > 220 then
mtx2.(l).(c) <- 255
else
mtx2.(l).(c) <- !px
@@ -107,11 +77,12 @@ let pre_bin mtx w h =
done;
mtx2
-let new_binarisation mtx w h =
+let bin mtx w h =
+print_string "Binarisation ...";
let new_mtx = Array.make_matrix w h 255 in
for i = 7 to w-8 do
for j = 7 to h-8 do
- let px = ref(0) and min = ref(256) and max=ref(-1) in
+ let px = ref(0) and min = ref(256) and max= ref(-1) in
for x = -7 to 7 do
for y = -7 to 7 do
px := mtx.(x+i).(y+j);
@@ -124,12 +95,13 @@ let new_mtx = Array.make_matrix w h 255 in
done;
let seuil = ((float)!min +. (float)!max) /. 2. and
pix = mtx.(i).(j) in
- if (float)pix < seuil then
+ if seuil < 220. && (float)pix < seuil then
new_mtx.(i).(j) <- 0
else
new_mtx.(i).(j) <- 255
done
done;
+ print_string " done\n";
new_mtx
(*
@@ -138,6 +110,7 @@ kernel: tableau d'entier
factor: float
*)
let conv_filter_3x3 mtx w h kernel factor =
+ print_string "Convolution filter ...";
let new_mtx = Array.make_matrix w h 100 in
for x = 1 to w-2 do
for y = 1 to h-2 do
@@ -154,63 +127,5 @@ let conv_filter_3x3 mtx w h kernel factor =
new_mtx.(x).(y) <- if px < 0 then 0 else px
done
done;
- new_mtx
-
-(*
- [Filtre de convolution (demo)]
- p1: matrice d'entier
- p2: largeur de la matrice
- p3: hauteur de la matrice
- -> renvoie une matrice d'entier
- *C'est une démo du filtre de convolution, il existe un autre version
- ou il faut renseigner le noyau et le facteur du filtre.
- Ici c'est un filtre de détection des bords.
-*)
-let out_of_bounds mtx width height x y =
- x < 0 || x > width-1 || y < 0 || y > height-1
-
-let conv_filter_demo mtx w h =
- let filter = [|0;-1;0;-1;5;-1;0;-1;0|] and factor = 1.0 in
- let new_mtx = Array.make_matrix w h 100 in
- for x = 0 to w-1 do
- for y = 0 to h-1 do
- let a = if not(out_of_bounds mtx w h (x-1) (y-1)) then
- mtx.(x-1).(y-1) * filter.(0) else 0 in
- let b = if not(out_of_bounds mtx w h (x) (y-1)) then
- mtx.(x).(y-1) * filter.(1) else 0 in
- let c = if not(out_of_bounds mtx w h (x+1) (y-1)) then
- mtx.(x+1).(y-1) * filter.(2) else 0 in
- let d = if not(out_of_bounds mtx w h (x-1) y) then
- mtx.(x-1).(y) * filter.(3) else 0 in
- let e = if not(out_of_bounds mtx w h x y) then
- mtx.(x).(y) * filter.(4) else 0 in
- let f = if not(out_of_bounds mtx w h (x+1) y) then
- mtx.(x+1).(y) * filter.(5) else 0 in
- let g = if not(out_of_bounds mtx w h (x-1) (y+1)) then
- mtx.(x-1).(y+1) * filter.(6) else 0 in
- let h = if not(out_of_bounds mtx w h (x) (y+1)) then
- mtx.(x).(y+1) * filter.(7) else 0 in
- let i = if not(out_of_bounds mtx w h (x+1) (y+1)) then
- mtx.(x+1).(y+1) * filter.(8) else 0 in
- let px = int_of_float((float)(a+b+c+d+e+f+g+h+i)*.factor) in
- new_mtx.(x).(y) <- if px < 0 then 0 else px
- done
- done;
- new_mtx
-
-let cdt mtx p l c = (p = 255) &&
- (mtx.(l-1).(c) = 0 ||
- mtx.(l-1).(c-1) = 0 ||
- mtx.(l).(c-1) = 0 ||
- mtx.(l+1).(c) = 0)
-
-let line_recognition mtx w h =
- let mtx2 = Array.copy mtx in
- for c = 1 to h-2 do
- for l = 1 to w-2 do
- if (cdt mtx2 mtx2.(l).(c) l c) then
- mtx.(l).(c) <- 0
- done
- done
-
-
+ print_string " done\n";
+ new_mtx
Please sign in to comment.
Something went wrong with that request. Please try again.