Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. #!/usr/bin/env owl
  2.  
  3. open Owl
  4.  
  5. let fname = "test.ppm"
  6.  
  7. let read_img fname =
  8. (* naive grayscale binary ppm reading. If you know how to, improve it *)
  9. let fp = open_in fname in
  10. let ver = input_line fp in (* version *)
  11.  
  12. if ver <> "P6" then (* expect a .ppm file *)
  13. raise (Invalid_argument ("Unable to read image: " ^ fname))
  14. else
  15. ();
  16.  
  17. (* This will skip comments. *)
  18. let rec ignore_comments_then_get_w_h () =
  19. let maybe_comment = input_line fp in
  20. if maybe_comment.[0] = '#' then
  21. ignore_comments_then_get_w_h ()
  22. else
  23. (* This line has the width and height in it *)
  24. maybe_comment
  25. in
  26.  
  27. (* width, height num colors *)
  28. let w_h_line = ignore_comments_then_get_w_h () in
  29. let num_col_line = input_line fp in
  30. let w, h = Scanf.sscanf w_h_line "%d %d" (fun w h -> w, h) in
  31. let num_col = Scanf.sscanf num_col_line "%d" (fun n -> n) in
  32.  
  33. let img = String.make (w * h * 3) ' ' in
  34. (*
  35. let imf_r = Array.make_matrix w h 0.0 in
  36. let imf_g = Array.make_matrix w h 0.0 in
  37. let imf_b = Array.make_matrix w h 0.0 in
  38. *)
  39.  
  40. let imf_o = Array.make_matrix (w * 3) h 0.0 in
  41.  
  42. (* Note that under 32bit OCaml, this will only work when reading strings up
  43. to ~16 megabytes. *)
  44. really_input fp img 0 (w * h * 3); (* this is indeed the end of file *)
  45.  
  46. close_in fp;
  47.  
  48. (*
  49. for i = 0 to w - 1 do
  50. for j = 0 to h - 1 do
  51. (*
  52. imf_r.(i).(j) <-
  53. (* flip image up-down *)
  54. float_of_int (int_of_char (img.[(h - 1 - j ) * w + (3*i + 0)]));
  55. imf_g.(i).(j) <-
  56. (* flip image up-down *)
  57. float_of_int (int_of_char (img.[(h - 1 - j ) * w + (3*i + 1)]));
  58. imf_b.(i).(j) <-
  59. (* flip image up-down *)
  60. float_of_int (int_of_char (img.[(h - 1 - j ) * w + (3*i + 2)]));
  61. *)
  62. done
  63. done;
  64. *)
  65.  
  66. let ww = 3 * w in
  67. for i = 0 to ww - 1 do
  68. for j = 0 to h - 1 do
  69. imf_o.(i).(j) <- float_of_int (int_of_char (img.[(h - 1 - j ) * ww + i ]));
  70. done
  71. done;
  72.  
  73. (* imf_o, imf_r, imf_g, imf_b, w, h, num_col *)
  74. imf_o, w, h, num_col
  75.  
  76. let img_to_owl fname =
  77. let img, w, h, num_col = read_img fname in
  78. let m = Mat.of_arrays img in
  79. let m = Mat.rotate m 270 in
  80. (* r,g, b: Mat of size h * w *)
  81. let r = Mat.get_slice_simple [[];[0;-1;3]] m in
  82. let g = Mat.get_slice_simple [[];[1;-1;3]] m in
  83. let b = Mat.get_slice_simple [[];[2;-1;3]] m in
  84.  
  85. (* combine 3 channel matrices into one ndarray *)
  86. let r' = Mat.to_ndarray r in
  87. let g' = Mat.to_ndarray g in
  88. let b' = Mat.to_ndarray b in
  89.  
  90. let r' = Arr.reshape r' [|h;w;1|] in
  91. let g' = Arr.reshape g' [|h;w;1|] in
  92. let b' = Arr.reshape b' [|h;w;1|] in
  93.  
  94. let img = Arr.zeros [|h;w;3|] in
  95. Arr.set_slice [R []; R []; I 0] img r';
  96. Arr.set_slice [R []; R []; I 1] img g';
  97. Arr.set_slice [R []; R []; I 2] img b';
  98. r, g, b, img
  99.  
  100. (*
  101. let _ =
  102. let r, _, _, _ = img_to_owl fname in
  103. Plot.image r
  104. *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement