Created
October 7, 2017 21:00
-
-
Save BigEpsilon/d8bb95f4442ed325aaaa17b0d69ad2fe to your computer and use it in GitHub Desktop.
Calling OpenCV from Nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
{.link: "/usr/local/lib/libopencv_core.so".} #pass arguments to the linker | |
{.link: "/usr/local/lib/libopencv_highgui.so".} | |
{.link: "/usr/local/lib/libopencv_imgproc.so".} | |
const # headers to include | |
std_vector = "<vector>" | |
cv_core = "<opencv2/core/core.hpp>" | |
cv_highgui = "<opencv2/highgui/highgui.hpp>" | |
cv_imgproc = "<opencv2/imgproc/imgproc.hpp>" | |
type | |
# declare required classes, no need to declare every thing like in rust or D | |
Mat {.final, header: cv_core, importc: "cv::Mat" .} = object | |
rows: cint # No need to import property and method, import only what you use | |
cols: cint | |
Size {.final, header: cv_core, importc: "cv::Size" .} = object | |
InputArray {.final, header: cv_core, importc: "cv::InputArray" .} = object | |
OutputArray {.final, header: cv_core, importc: "cv::OutputArray" .} = object | |
Vector {.final, header: std_vector, importcpp: "std::vector".} [T] = object | |
#constructors | |
proc constructInputArray(m: var Mat): InputArray {. header:cv_core, importcpp: "cv::InputArray(@)", constructor.} | |
proc constructOutputArray(m: var Mat): OutputArray {. header:cv_core, importcpp: "cv::OutputArray(@)", constructor.} | |
proc constructvector*[T](): Vector[T] {.importcpp: "std::vector<'*0>(@)", header: std_vector.} | |
#implicit conversion between types | |
converter toInputArray(m: var Mat) : InputArray {. noinit.} = result=constructInputArray(m) | |
converter toOutputArray(m: var Mat) : OutputArray {. noinit.} = result=constructOutputArray(m) | |
# used methods and functions | |
proc empty(this: Mat): bool {. header:cv_core, importcpp: "empty" .} | |
proc imread(filename: cstring, flag:int): Mat {. header:cv_highgui, importc: "cv::imread" .} | |
proc imwrite(filename: cstring, img: InputArray, params: Vector[cint] = constructvector[cint]()): bool {. header:cv_highgui, importc: "cv::imwrite" .} | |
proc resize(src: InputArray, dst: OutputArray, dsize: Size, fx:cdouble = 0.0, fy: cdouble = 0.0, interpolation: cint = 1) {. header:cv_imgproc, importc: "resize" .} | |
proc `$`(dim: (cint, cint)): string = "(" & $dim[0] & ", " & $dim[1] & ")" #meta-programming capabilities | |
proc main() = | |
for f in walkFiles("myDir/*.png"): | |
var src = imread(f, 1) | |
if not src.empty(): | |
var dst: Mat | |
resize(src, dst, Size(), 0.5, 0.5) | |
discard imwrite(f & ".resized.png", dst) # returns bool, you have to explicitly discard the result | |
echo( f, ": ", (src.rows, src.cols), " -> ", (dst.rows, dst.cols)) | |
else: | |
echo("oups") | |
when isMainModule: | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment