Permalink
Browse files

Hough transform done.

  • Loading branch information...
1 parent 1783bc0 commit 946f2befcd537993f6f0758a86fc4640aac92789 @dezyal dezyal committed Oct 20, 2012
Showing with 43 additions and 112 deletions.
  1. +43 −112 OCR/Rotation.ml
View
@@ -1,130 +1,46 @@
-(*
- [Détection d'angle: hough_skew_detection]
- Détection de l'inclinaison à l'aide
- de la transformé de Hough.
- Découpage en 4 fonctions.
+(*
+ [Fonction de detection d'angle: Transformé de Hough]
+ Renvoie l'angle d'inclinaison du texte
+ en radian.
*)
-
-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
+let hough_skew_detection mtx w h =
+ let w_acc = 360 and
+ h_acc = int_of_float (sqrt((float)(w*w+h*h))+.1.) in
+ let acc = Array.make_matrix w_acc h_acc 0 in
for x = 0 to w-1 do
- for y = 0 to h-h/2 do
- if (mtx.(x).(y) = 0) then
+ for y = 0 to h-1 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
+ for theta = 0 to w_acc-1 do
+ let rho = int_of_float(
+ (float)x*.cos((float)theta) +. (float)y*.sin((float)theta)) in
+ if (rho > -1 && rho < h_acc) then
+ acc.(theta).(rho) <- acc.(theta).(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 = (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 = 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
- done
-(* Detection de l'intersection des sinusoides *)
-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
- let xy_val = acc.(x).(y) in
- if xy_val > !max then
- max := xy_val;
+ for x = 0 to w_acc-1 do
+ for y = 0 to h_acc-1 do
+ if (!max < acc.(x).(y)) then
+ begin
+ max := acc.(x).(y);
win_rho := y;
win_theta := x;
- done
- done;
- let rho = ((float)!win_rho/.(float)w -. 0.5)*.rho_max in
- let theta = ((float)!win_theta/.(float)h -. 0.5)*.3.14159265 in
- (rho,theta)
-(* Conversion d'une droite polaire en cartésien *)
-let rhotheta2ab rho theta w h =
- let a = -.cos(theta)/.sin(theta) in
- let b = rho/.sin(theta) +. (float)w/.2. -. a*.(float)w/.2. in
- (a,b)
-(* Fonction finale *)
-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 i = 0 to w-1 do
- for j = 0 to h-1 do
- if (mtx.(i).(j) == 0) then
- px_vote acc theta_w rho_h i j w h rho_max;
+ end
done
- done;
- let (rho,theta) = winner acc theta_w rho_h rho_max in
- let (a,b) = rhotheta2ab rho theta w h in
- (atan(a),acc,theta_w,rho_h)
-*)
-(* Debug Hough *)
-let trace_line mtx w h a =
- let new_mtx = Array.copy mtx in
- for x = 0 to w-1 do
- let y = int_of_float(a*.(float)x +. (float)h/.3.) in
- if (y > -1 && y < h-1) then
- new_mtx.(x).(y) <- 100
done;
- new_mtx
-
+ let theta = !win_theta (*and rho = !win_rho*) in
+ let a = -.cos((float)theta)/.sin((float)theta) in
+ atan(a)
(*
[Fonction Rotate]
Effectue la rotation d'une image à partir
- d'un angle en degré
+ d'un angle en radian
*)
let rotate mtx w h a =
- (*let a = Tools.deg2rad angle in *)
- (*let new_w = int_of_float(
- (float)w*.cos(a) +. (float)h*.sin(a))
- and new_h = int_of_float(
- (float)w*.sin(a) +. (float)h*.cos(a)) in*)
let new_mtx = Array.make_matrix w h 255 in
for x = 0 to w-1 do
for y = 0 to h-1 do
@@ -133,11 +49,26 @@ let rotate mtx w h a =
(float)x*.cos(a) -. (float)y*.sin(a))
and new_y = int_of_float(
(float)(x)*.sin(a) +. (float)y*.cos(a)) in
- (*print_int new_x;print_string ":";print_int new_y;print_newline();*)
- if (new_x > -1 && new_x < w-1 && new_y > -1 && new_y < h-1) then
+ if (new_x > -1 && new_x < w-1 && new_y > -1 && new_y < h-1) then
new_mtx.(new_x).(new_y) <- 0
done
done;
new_mtx
+(* Debug Hough *)
+let trace_line mtx w h a =
+ let new_mtx = Array.copy mtx in
+ for x = 0 to w-1 do
+ let y = int_of_float(a*.(float)x +. (float)h/.3.) in
+ if (y > -1 && y < h-1) then
+ new_mtx.(x).(y) <- 100
+ done;
+ new_mtx
+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

0 comments on commit 946f2be

Please sign in to comment.