Permalink
Browse files

update Rotation.ml

  • Loading branch information...
1 parent 3d3357a commit 8bf11533531b9a05a93efaa220b6babd610aac68 dezyal committed Oct 19, 2012
Showing with 61 additions and 6 deletions.
  1. +57 −4 OCR/Rotation.ml
  2. +4 −2 OCR/main.ml
View
@@ -5,12 +5,65 @@
Découpage en 4 fonctions.
*)
+let winner acc w h rho_max=
+ let max = ref(-1) and win_rho = ref(0) and win_theta = ref(0) in
+ for x = 0 to w-1 do
+ for y = 0 to h-1 do
+ if acc.(x).(y) > !max then
+ max := acc.(x).(y);
+ win_rho := y;
+ win_theta := x;
+ done
+ done;
+ print_int !max;
+ let rho = ((float)!win_rho/.(float)w -. 0.5)*.rho_max in
+ let theta = ((float)!win_theta/.(float)h)*.3.14159265 in
+ (rho,theta)
+
+let rhotheta2ab rho theta w h =
+ let a = -.cos(theta)/.sin(theta) in
+ let b = rho/.sin(theta) +. (float)h/.2. -. a*.(float)w/.2. in
+ (a,b)
+
+let hough_skew_detection mtx w h =
+ let rho_max = sqrt((float)(w*w +h*h)) in
+ let theta_w = 360 and rho_h = int_of_float(1.+.rho_max) in
+ let acc = Array.make_matrix theta_w rho_h 0 in
+ for x = 0 to w-1 do
+ for y = 0 to h-h/2 do
+ if (mtx.(x).(y) = 0) then
+ begin
+ let ox = (float)x-.(float)w/.2. and oy = (float)y-.(float)h/.2. in
+ for i=0 to theta_w-1 do
+ let theta = (float)i/.(float)theta_w*.3.14159265 in
+ let rho = ox*.cos(theta) +. oy*.sin(theta) in
+ let i_rho = int_of_float(
+ 0.5 +. (rho/.rho_max +. 0.5)*.(float)rho_h) in
+ acc.(i).(i_rho) <- acc.(i).(i_rho) + 1
+ done
+ end
+ done
+ done;
+ let (rho,theta) = winner acc theta_w rho_h rho_max in
+ let (a,b) = rhotheta2ab rho theta w h in
+ print_float b;
+ (atan(a),acc,theta_w,rho_h)
+
+let display_matrix mtx w h =
+ for x = 0 to w-1 do
+ for y = 0 to h-1 do
+ print_int x;print_string ":";print_int y;print_string " -> ";
+ print_int (mtx.(x).(y));print_newline()
+ done
+ done
+
+(*
(* Remplissage de l'accumulateur *)
let px_vote acc acc_w acc_h x y w h rho_max =
- let ox = x-w/2 and oy = y-h/2 in
+ let ox = (float)x-.(float)w/.2. and oy = (float)y-.(float)h/.2. in
for i=0 to acc_w-1 do
let theta = (float)i/.(float)acc_w*.3.14159265 in
- let rho = (float)ox*.cos(theta) +. (float)oy*.sin(theta) in
+ let rho = ox*.cos(theta) +. oy*.sin(theta) in
let i_rho = int_of_float(
0.5 +. (rho/.rho_max +. 0.5)*.(float)acc_h) in
acc.(i).(i_rho) <- acc.(i).(i_rho) + 1
@@ -48,8 +101,8 @@ let hough_skew_detection mtx w h =
done;
let (rho,theta) = winner acc theta_w rho_h rho_max in
let (a,b) = rhotheta2ab rho theta w h in
- atan(a)
-
+ (atan(a),acc,theta_w,rho_h)
+*)
(* Debug Hough *)
let trace_line mtx w h a =
let new_mtx = Array.copy mtx in
View
@@ -11,10 +11,12 @@ let main () =
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
+ [|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 (angle) = Rotation.hough_skew_detection mtx3 w h in
+ let (angle,acc,ww,hh) = Rotation.hough_skew_detection mtx3 w h in
+ (* Rotation.display_matrix acc ww hh; *)
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;

0 comments on commit 8bf1153

Please sign in to comment.