View Source getting_started

Evision Example - Get Started with Some Basic Functions

Mix.install([
  {:evision, "~> 0.1"},
  {:kino, "~> 0.7"},
  {:req, "~> 0.3"}
], system_env: [
  # optional, defaults to `true`
  # set `EVISION_PREFER_PRECOMPILED` to `false`
  # if you prefer `:evision` to be compiled from source
  # note that to compile from source, you may need at least 1GB RAM
  {"EVISION_PREFER_PRECOMPILED", true},

  # optional, defaults to `true`
  # set `EVISION_ENABLE_CONTRIB` to `false`
  # if you don't need modules from `opencv_contrib`
  {"EVISION_ENABLE_CONTRIB", true},

  # optional, defaults to `false`
  # set `EVISION_ENABLE_CUDA` to `true`
  # if you wish to use CUDA related functions
  # note that `EVISION_ENABLE_CONTRIB` also has to be `true`
  # because cuda related modules come from the `opencv_contrib` repo
  {"EVISION_ENABLE_CUDA", false},

  # required when
  # - `EVISION_ENABLE_CUDA` is `true`
  # - and `EVISION_PREFER_PRECOMPILED` is `true`
  #
  # set `EVISION_CUDA_VERSION` to the version that matches
  # your local CUDA runtime version
  #
  # current available versions are
  # - 118
  # - 121
  {"EVISION_CUDA_VERSION", "118"},

  # require for Windows users when
  # - `EVISION_ENABLE_CUDA` is `true`
  # set `EVISION_CUDA_RUNTIME_DIR` to the directory that contains
  # CUDA runtime libraries
  {"EVISION_CUDA_RUNTIME_DIR", "C:/PATH/TO/CUDA/RUNTIME"}
])
:ok

Define Some Helper Functions

defmodule Helper do
  def download!(url, save_as, overwrite? \\ false) do
    unless File.exists?(save_as) do
      Req.get!(url, http_errors: :raise, output: save_as, cache: not overwrite?)
    end

    :ok
  end
end
{:module, Helper, <<70, 79, 82, 49, 0, 0, 10, ...>>, {:download!, 3}}

Read an Image From File

alias Evision, as: Cv

# Download the test image
lenna_test_image_path = Path.join(__DIR__, "lenna_test_image.png")
Helper.download!(
  "https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png",
  lenna_test_image_path
)

# Read the test image
mat = Cv.imread(lenna_test_image_path)
%Evision.Mat{
  channels: 3,
  dims: 2,
  type: {:u, 8},
  raw_type: 16,
  shape: {512, 512, 3},
  ref: #Reference<0.2845725607.2514092053.18350>
}

Encode an Image

encoded = Cv.imencode(".png", mat)
Kino.Image.new(encoded, :png)

Resize an Image

resized_mat = Cv.resize(mat, {_width = 256, _height = 128})
%Evision.Mat{
  channels: 3,
  dims: 2,
  type: {:u, 8},
  raw_type: 16,
  shape: {128, 256, 3},
  ref: #Reference<0.2845725607.2514092052.18171>
}
encoded = Cv.imencode(".png", resized_mat)
Kino.Image.new(encoded, :png)

Get a Subarea in an Image

Evision.Mat.roi

Cv.imencode(".png", Cv.Mat.roi(mat, {10, 10, 130, 200}))
|> Kino.Image.new(:png)

Encode and Decode Image in Memory

# encode in memory
encoded_in_memory = Cv.imencode(".png", resized_mat)
<<137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 1, 0, 0, 0, 0, 128, 8, 2, 0,
  0, 0, 107, 215, 32, 93, 0, 0, 32, 0, 73, 68, 65, 84, 120, 1, 20, 193, 93, 175, 110, 105, 118,
  ...>>
# decode in memory
decoded_mat = Cv.imdecode(encoded_in_memory, Cv.Constant.cv_IMREAD_ANYCOLOR())
%Evision.Mat{
  channels: 3,
  dims: 2,
  type: {:u, 8},
  raw_type: 16,
  shape: {128, 256, 3},
  ref: #Reference<0.2845725607.2514092052.18187>
}

Read an PNG File With/Without Alpha Channel

# download the test image (has alpha channel)
download_path = Path.join(__DIR__, "test.png")
Helper.download!("https://github.com/cocoa-xu/evision/raw/main/test/testdata/test.png", download_path)
:ok
without_alpha = Cv.imread(download_path)
%Evision.Mat{
  channels: 3,
  dims: 2,
  type: {:u, 8},
  raw_type: 16,
  shape: {2, 3, 3},
  ref: #Reference<0.2845725607.2514092053.18729>
}
Cv.imread(download_path, flags: Cv.Constant.cv_IMREAD_UNCHANGED())
%Evision.Mat{
  channels: 4,
  dims: 2,
  type: {:u, 8},
  raw_type: 24,
  shape: {2, 3, 4},
  ref: #Reference<0.2845725607.2514092053.18730>
}

Read an Image and Convert It to Grayscale in a Single Call

gray_scale_mat = Cv.imread(lenna_test_image_path, flags: Cv.Constant.cv_IMREAD_GRAYSCALE())
%Evision.Mat{
  channels: 1,
  dims: 2,
  type: {:u, 8},
  raw_type: 0,
  shape: {512, 512},
  ref: #Reference<0.2845725607.2514092053.18731>
}
gray_scale = Cv.imencode(".png", gray_scale_mat)
Kino.Image.new(gray_scale, :png)