View Source Evision Example - Get Started with Some Basic Functions

# 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
# System.put_env("EVISION_PREFER_PRECOMPILED", "false")

Mix.install([
  {:evision, "~> 0.1.21"},
  {:kino, "~> 0.7"},
  {:req, "~> 0.3"}
])
:ok

define-some-helper-functions

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

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

Encode an Image

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

resize-an-image

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

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 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.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

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.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

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

gray_scale_mat = Cv.imread(lenna_test_image_path, flags: Cv.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)