Mix.install([{:evision,"~> 0.2"},{:kino,"~> 0.7"},{:req,"~> 0.5"}],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"}])
# import Bitwise so that we can use `|||` (bitwise or)importBitwise# binarization{_,bw}=Evision.threshold(gray,50,255,Evision.Constant.cv_THRESH_BINARY()|||Evision.Constant.cv_THRESH_OTSU())bw
# Find all the contours in the thresholded image{contours,_}=Evision.findContours(bw,Evision.Constant.cv_RETR_LIST(),Evision.Constant.cv_CHAIN_APPROX_NONE())IO.puts("Find #{Enum.count(contours)} contour(s)")
Find7contour(s)
:ok
Ignore Contours That Are Too Small or Too Large
minimal_area=100maximal_area=100_000contours=Enum.reject(contours,fnc-># Calculate the area of each contourarea=Evision.contourArea(c)# Ignore contours that are too small or too large# (return true to reject)area<minimal_areaorarea>maximal_areaend)IO.puts("#{Enum.count(contours)} contour(s) remains")
6contour(s)remains
:ok
Draw All Contours
# color in {Blue, Green, Red}, range from 0-255edge_color={0,0,255}# draw all contours by setting `index` to `-1`index=-1# Load image in colorsrc=Evision.imread("pca_test.jpg")# draw all contours on the color imageEvision.drawContours(src,contours,index,edge_color,thickness:2)