Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

filtre et binarisation a tester

  • Loading branch information...
commit f978b07e525d2e0c37f496312583fab98b869498 1 parent bdddb37
Chuck authored
Showing with 49 additions and 25 deletions.
  1. +32 −9 OCR/Binarisation.ml
  2. +17 −16 OCR/main.ml
View
41 OCR/Binarisation.ml
@@ -1,10 +1,6 @@
-(* Filtre de Wienner *)
+(* filtre de wiener et binarisation *)
-
-
-(* Binarisation de Niblack *)
-
-(* creation d'une matrice NxM, à partir d'une matrice source, centre en x y *)
+(* creation d'une matrice 25x25, à partir d'une matrice source, centre en x y *)
let makeMatrix source x y =
let mtx = Array.make_matrix 25 25 0 in
for i = 0 to 24 do
@@ -14,7 +10,7 @@ let makeMatrix source x y =
done;
mtx
-(* Calcul de la moyenne des pixels d'une matrice NxM *)
+(* Calcul de la moyenne des pixels d'une matrice 25x25 *)
let moyenne mtx =
let m = ref 0 in
for i = 0 to Array.length(mtx)-1 do
@@ -25,7 +21,7 @@ let moyenne mtx =
m := !m/((Array.length(mtx))*(Array.length(mtx.(0))));
!m
-(* Calcul de l'ecart-type des pixels d'une matrice NxM *)
+(* Calcul de l'ecart-type des pixels d'une matrice 25x25 *)
let pow = (fun a -> a*.a)
let ecart mtx =
@@ -40,7 +36,11 @@ let ecart mtx =
b := pow ((!b/.(((float_of_int (Array.length(mtx))))*.((float_of_int (Array.length(mtx.(0))))))));
int_of_float (sqrt (!a -. !b))
-(* Calcul du seuil d'une matrice NxM avec k = -0.2 *)
+(* Variance *)
+let variance mtx =
+ pow (float_of_int (ecart mtx))
+
+(* Calcul du seuil d'une matrice 25x25 avec k = -0.2 *)
let s m sigma = m +. (-0.2) *. sigma
let seuil mtx =
@@ -48,6 +48,29 @@ let seuil mtx =
let si = float_of_int (ecart mtx) in
let se = s m si in
se
+
+(* moyenne des variances*)
+let mapMatrix f mtx =
+ for i = 12 to Array.length(mtx)-12 do
+ for j = 12 to Array.length(mtx.(0))-12 do
+ let mtxAux = makeMatrix mtx i j in
+ mtx.(i).(j) <- int_of_float(f mtxAux)
+ done
+ done;
+ mtx
+
+let mVariance mtx =
+ moyenne (mapMatrix variance mtx)
+
+(* filtre de wiener *)
+let wiener mtx =
+ for i = 12 to Array.length(mtx)-12 do
+ for j = 12 to Array.length(mtx.(0))-12 do
+ let mtxAux = makeMatrix mtx i j in
+ mtx.(i).(j) <- int_of_float(float_of_int(moyenne mtxAux) +. ((pow (variance mtxAux)) -. float_of_int(mVariance mtxAux))*.(float_of_int(mtx.(i).(j)) -. float_of_int(moyenne mtxAux))/.(pow (variance mtxAux)))
+ done
+ done;
+ mtx
(* binarisation de Niblack *)
let niblack mtx =
View
33 OCR/main.ml
@@ -6,23 +6,24 @@ let main () =
failwith "Aucun fichier à charger";
OSdl.init ();
let su = Sdlloader.load_image Sys.argv.(1) in
- let si = Sdlvideo.surface_info su and
- pfi = Sdlvideo.surface_format su in
- let w = si.Sdlvideo.w and h = si.Sdlvideo.h in
- let mtx = Pretreatment.su2grey su w h in
- let mtx2 = Pretreatment.conv_filter_3x3 mtx w h
- [|2;5;2;5;20;5;2;5;2|] (1./.48.) in
-(* [|0;1;0;1;-4;1;0;1;0|] (1.) in*)
- (* let mtx3 = Pretreatment.new_binarisation
- (Pretreatment.pre_bin mtx2 w h) w h in
-*) let mtx3 = Binarisation.niblack mtx2 in
- let angle = Rotation.hough_skew_detection mtx3 w h in
- let mtx3 = Rotation.rotate mtx3 w h (-.angle) in
+ let si = Sdlvideo.surface_info su and
+ pfi = Sdlvideo.surface_format su in
+ let w = si.Sdlvideo.w and h = si.Sdlvideo.h in
+ let mtx = Pretreatment.su2grey su w h in
+ let mtx2 = Pretreatment.conv_filter_3x3 mtx w h
+ [|2;5;2;5;20;5;2;5;2|] (1./.48.) in
+ let mtx2 = Binarisation.wiener mtx2 in(*
+ [|0;1;0;1;-4;1;0;1;0|] (1.) in
+let mtx3 = Pretreatment.new_binarisation
+ (Pretreatment.pre_bin mtx2 w h) w h in*)
+ let mtx3 = Binarisation.niblack mtx2 in
+ let angle = Rotation.hough_skew_detection mtx3 w h in
+ let mtx3 = Rotation.rotate mtx3 w h (-.angle) in
(* let mtx_debug = Rotation.trace_line mtx3 w h (angle) in
- *) print_string " degree:";print_float (Tools.rad2deg angle);
- OSdl.display (Pretreatment.mtx2su mtx3 w h pfi) w h;
- OSdl.wait ();
+ *) print_string " degree:";print_float (Tools.rad2deg angle);
+ OSdl.display (Pretreatment.mtx2su mtx3 w h pfi) w h;
+ OSdl.wait ();
(*OSdl.display (Pretreatment.mtx2su mtx_debug w h pfi) w h;*)
- exit 0
+ exit 0
let _ = main ();
Please sign in to comment.
Something went wrong with that request. Please try again.