View Source stitching

Evision Example - Stitching Multiple Photos into A Panorama Photo

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

Download Test Images

# change to the file's directory
# or somewhere you have write permission
File.cd!(__DIR__)

# https://github.com/opencv/opencv_extra/tree/4.x/testdata/stitching
Helper.download!(
  "https://raw.githubusercontent.com/opencv/opencv_extra/master/testdata/stitching/a1.png",
  "a1.png"
)

Helper.download!(
  "https://raw.githubusercontent.com/opencv/opencv_extra/master/testdata/stitching/a2.png",
  "a2.png"
)

Helper.download!(
  "https://raw.githubusercontent.com/opencv/opencv_extra/master/testdata/stitching/a3.png",
  "a3.png"
)
:ok

Stitching

alias Evision, as: Cv

a1 = Cv.imread("./a1.png")
a2 = Cv.imread("./a2.png")
a3 = Cv.imread("./a3.png")
sticher = Cv.Stitcher.create()
{status_code, result} = Cv.Stitcher.stitch(sticher, [a1, a2, a3])
0 = status_code
# status_code should be 0 (OK),
# for other status_code, please refer to https://github.com/opencv/opencv/blob/4.5.4/modules/stitching/include/opencv2/stitching.hpp#L152

Cv.imencode(".png", result)
|> Kino.Image.new(:png)