forked from ybakshi01/lab20
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab20.ml
More file actions
41 lines (32 loc) · 1.41 KB
/
lab20.ml
File metadata and controls
41 lines (32 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
type image = float list list ;;
(* images are lists of lists of floats between 0. (white) and 1. (black) *)
type size = int * int ;;
open Graphics ;;
(* threshold thershold image -- image where pixels above the threshold
value are black *)
let iterate funct img =
List.map (fun row -> List.map (funct)
row) img ;;
let threshold img threshold =
iterate (fun v -> if v <= threshold then 0. else 1.) img ;;
(* List.map (fun row -> List.map (fun v -> if v <= threshold then 0. else 1.)
row) img *)
(* show the image *)
let depict img =
Graphics.open_graph ""; Graphics.clear_graph ();
let x, y = List.length (List.hd img), List.length img in Graphics.resize_window x y;
let depict_pix v r c = let lvl = int_of_float (255. *. (1. -. v)) in Graphics.set_color (Graphics.rgb lvl lvl lvl);
plot c (y - r) in
List.iteri (fun r row -> List.iteri (fun c pix -> depict_pix pix r c) row) img;
Unix.sleep 2; Graphics.close_graph () ;;
(* dither max image -- dithered image *)
let dither img =
iterate (fun v -> if v > Random.float 1. then 1. else 0.) img ;;
(* List.map(fun row ->
List.map (fun v -> if v > Random.float 1. then 1. else 0.) row) img *)
let mona = Monalisa.image ;;
depict mona ;;
let mona_threshold = threshold mona 0.75 ;;
depict mona_threshold ;;
let mona_dither = dither mona ;;
depict mona_dither ;;