<jupyter_start><jupyter_text>Auxiliary routines<jupyter_code>let create_rgba_image rgba =
  let height, width = Array3.dim1 rgba, Array3.dim2 rgba in
  let pixels = reshape_1 (genarray_of_array3 rgba) (height * width * 4) in
  Cairo.Image.(create_for_data8 ~stride:(width * 4) pixels RGB24 width height)

let plot_rgba_image rgba =
  let b = Buffer.create 256 in
  Cairo.PNG.write_to_stream ~output:(Buffer.add_string b) (create_rgba_image rgba) ;
  Jupyter_notebook.display ~base64:true "image/png" (Buffer.contents b)<jupyter_output><empty_output><jupyter_text>Source image<jupyter_code>let surface = Cairo.PNG.create "datasets/lenna.png" ;;
let width, height = Cairo.Image.get_width surface, Cairo.Image.get_height surface ;;
let src =
  let data = Array3.create Int8_unsigned C_layout height width 4 in
  let ctx = Cairo.create @@ create_rgba_image data in
  Cairo.set_source_surface ctx surface ~x:0.0 ~y:0.0 ;
  Cairo.paint ctx ;
  data
let b = Buffer.create 16 in
Cairo.PNG.write_to_stream surface ~output:(Buffer.add_string b) ;
Jupyter_notebook.display ~base64:true "image/png" (Buffer.contents b) ;
Buffer.length b<jupyter_output><empty_output><jupyter_text>Gaussian filter  1/162/161/16  2/164/162/16  1/162/161/16<jupyter_code>let dst = Array3.create Int8_unsigned C_layout width height 4 in
for i = 1 to height - 2 do
  for j = 1 to width - 2 do
    for k = 0 to 3 do
      let z = (    src.{i-1,j+1,k} + 2 * src.{i,j+1,k} +     src.{i+1,j+1,k}
             + 2 * src.{i-1,j  ,k} + 4 * src.{i,j  ,k} + 2 * src.{i+1,j  ,k}
             +     src.{i-1,j-1,k} + 2 * src.{i,j-1,k} +     src.{i+1,j-1,k}) / 16 in
      dst.{i,j,k} <- min (max z 0) 255
    done
  done
done ;
plot_rgba_image dst<jupyter_output><empty_output><jupyter_text>Horizontal sobel filter  -10+1  -20+2  -10+1<jupyter_code>let dst = Array3.create Int8_unsigned C_layout width height 4 in
for i = 1 to height - 2 do
  for j = 1 to width - 2 do
    for k = 0 to 3 do
      let z = src.{i-1,j+1,k} + 2 * src.{i,j+1,k} + src.{i+1,j+1,k}
            - src.{i-1,j-1,k} - 2 * src.{i,j-1,k} - src.{i+1,j-1,k} in
      dst.{i,j,k} <- min (max z 0) 255
    done
  done
done ;
plot_rgba_image dst<jupyter_output><empty_output><jupyter_text>Vertical sobel filter  -1-2-1  000  +1+2+1<jupyter_code>let dst = Array3.create Int8_unsigned C_layout width height 4 in
for i = 1 to height - 2 do
  for j = 1 to width - 2 do
    for k = 0 to 3 do
      let z = src.{i+1,j-1,k} + 2 * src.{i+1,j,k} + src.{i+1,j+1,k}
            - src.{i-1,j-1,k} - 2 * src.{i-1,j,k} - src.{i-1,j+1,k} in
      dst.{i,j,k} <- min (max z 0) 255
    done
  done
done ;
plot_rgba_image dst<jupyter_output><empty_output><jupyter_text>Laplacian filter  010  1-41  010<jupyter_code>let dst = Array3.create Int8_unsigned C_layout width height 4 in
for i = 1 to height - 2 do
  for j = 1 to width - 2 do
    for k = 0 to 3 do
      let z = src.{i,j+1,k} + src.{i-1,j,k} - 4 * src.{i,j,k} + src.{i+1,j,k} + src.{i,j-1,k} in
      dst.{i,j,k} <- min (max z 0) 255
    done
  done
done ;
plot_rgba_image dst<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14504563233376794, 'fraction_numerical': 0.05899608865710561, 'mean_word_length': 3.1698369565217392, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '130721', 'license': 'MIT', 'n_tokens_mistral': 1454, 'n_tokens_neox': 1327, 'n_words': 324, 'path': 'notebooks/simple_image_filtering.ipynb', 'repo_name': 'akabe/docker-ocaml-jupyter-datascience', 'size': '3068'}