let get_bw (r, g, b) threshold = match r with (*Or g or b*)
  | r when r > threshold -> (255, 255, 255) (*We return the white color*)
  | _ -> (0, 0, 0) (*Otherwise, we return the black color*)

let average_dithering matrix (width, height) style =
  (*We convert the image in grey level according to style*)
  let matrix_grey = grey matrix (width, height) style in
  let histo = get_histogram matrix_grey (width, height) 0 and (*0 = histogram of the grey level*)
      aod = ref 0 in
  for i = 0 to 255 do
    aod := !aod + histo.(i)*(i+1);
  done;
  aod := !aod / (width * height);
  for j = 0 to height - 2 do
    for i = 0 to width - 2 do
      matrix.(i).(j) <- get_bw matrix_grey.(i).(j) !aod;
    done
  done;
  matrix