Giter VIP home page Giter VIP logo

cimg_ex's Introduction

cimg_ex

Light-weight image processing module in Elixir with CImg. This module aims to create auxiliary routines for Deep Learning.

Note: It still has a few image processing functions currentrly.

Design detail

The entity of the image handled by CImg is on the NIF side. On Elixir, the reference to the image generated by NIF is stored in the %CImg{} structure. You cannot read out the pixels of the image and process it directly, instead you can use the image processing functions provided in CImg module.

The image will be assigned to Erlang Resource by NIF, so the image will automatically be subject to garbage collection when it is no longer in use.

This is a very important point. Some of the functions in CImg module mutably rewrite the original image. I recommend you to make a duplicate of the image before performing the image processing.

Platform

It has been confirmed to work in the following OS environment.

  • Windows MSYS2/MinGW64
  • WSL2/Ubuntu 20.04, 18.04
  • Linux Mint 20 "Ulyana"
  • Nerves rpi

Requirements

The following libraries are required to display images on the PC screen.

  • GDI32 on Windows
  • X11 on Linux

Installation

Add following dependency to your mix.exs.

def deps do
  [
    {:cimg, "~> 0.1.8"}
  ]
end

and install dependencies:

$ mix deps.get
$ mix deps.compile

Demo

There is a simple program in demo directory. You can do it by following the steps below.

$ cd demo
$ mix deps.get
$ mix run -e "CImgDemo.demo1"

Close the appaired window, and stop the demo program.

Another demo:

$ mix run -e "CImgDemo.demo2"
$ mix run -e "CImgDemo.demo3"

Example: Deep Labelling for Semantic Image Segmentation

The below code is an excerpt of the inference part from the DeepLab app implemented with TflInterp. CImg preprocesses the input image and provides a DeepLab model. The DeepLab result tensor is then converted by CImg into a colormaedp image.

defmodule TflDemo.DeepLab3 do
  alias TflDemo.DeepLab3.Prediction

  def apply_deeplab3(img_file) do
    img = CImg.load(img_file)

    segments = Prediction.apply(img)
  end
end


defmodule TflDemo.DeepLab3.Prediction do
  use TflInterp, model: "priv/lite-model_deeplabv3_1_metadata_2.tflite"
  
  @deeplab3_shape {257, 257}

  def apply(img) do
    # preprocess
    bin =
      CImg.dup(img)
      |> CImg.resize(@deeplab3_shape)
      |> CImg.to_binary(range: {-1.0, 1.0})

    # prediction
    outputs =
      __MODULE__
      |> TflInterp.set_input_tensor(0, bin)
      |> TflInterp.invoke()
      |> TflInterp.get_output_tensor(0)
      |> Nx.from_binary({:f, 32}) |> Nx.reshape({257, 257, :auto})
      
    # postprocess
    outputs
    |> Nx.argmax(axis: 2)
    |> Nx.as_type({:u, 8})
    |> Nx.to_binary()
    |> CImg.from_binary(257, 257, 1, 1, "<u1")
    |> CImg.color_mapping(:lines)    # convert into a colormaped image
  end
end

License

cimg_ex is licensed under the Apache License Version 2.0.

-- license overview of included 3rd party libraries --

  • The "CImg" Library is licensed under the CeCILL-C/CeCILL.
  • The "stb" - single-file public domain libraries for C/C++ - is public domain or MIT licensed.

cimg_ex's People

Contributors

shoz-f avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.