opencvd 0.0.4

OpenCV Wrapper for dlang

To use this package, put the following dependency into your project's dependencies section:



Unofficial OpenCV binding for D programming language

This is an initial attempt to create an opencv binding for dlang. The C interface was borrowed from gocv, and the implementation has been highly influenced by it.


I don't describe myself as the most brillant d programmer around. Pull requests are welcome for growing and enhancing the binding. I am willing to add more contributors to the project. Let's make it a complete binding to opencv.

Important note

Please always use updated git repo (~master). The library on the dub repo only exists for increasing the visibility of the library.


Opencvd requires the following packages to build:

  • OpenCV ~>4.0 ( must be built with contrib repo)
  • cmake (version of 3.10.2 is installed in my system)

Tested Systems

  • Ubuntu 18.04.2 LTS 64 bit - ldc2-1.8.0 - Opencv 4.0.0 built from source
  • Windows 10 64 bit - ldc2-1.14.0-windows-x64 - OpenCV-master (4.10.0 AFAIK) - Visual Studio 2017 community Ed.
  • Raspberry Pi 3 - Raspbian Stretch Opencv 4.1.0 built from source with some pain!: (
  • OSX Sierra 10.12.5

Notable features

  • cv::Mat and other types like cv::Ptrcv::ml::SVM are wrapped using opaque pointers.
  • opencv c++ syntax has been tried to imitate as much as the d language allows.
  • Uses d arrays when it is possible like: uses Point to wrap std::vectorstd::vector<cv::Point > Please take a look at examples folder to understand how it looks like and available functionality

Current limitations:

  • There may be unwrapped opencv features.
  • No documentation yet.
  • Most of the functionality has not been tested yet.
  • No unittests yet.

Current roadmap of the project

  • wrap more functionality of opencv.
  • make more examples runnable from or

How to build

Ubuntu - Raspbian

First, you have to compile C/C++ interface files:

cd opencvd/c && mkdir build
cd build
cmake ..
make // or cmake --build .

Then use dub to build the library.

In your app's dub.json, you may need to set linker flags like:

"dependencies": {
        "opencvd": "~>0.0.4"
"lflags": ["-L/home/user/.dub/packages/opencvd", "-lopencvcapi", "-lopencvcapi_contrib"]

and add following to dub.json of your test app:

"libs": [
    "opencv", // this is handled by pkgconfig. You may set it as "opencv4" depending on the name of your pkgconfig file.

Your build experience may vary. On Raspbian, you may want to use RP Official Camera, so you have to register the camera device to be /dev/video0:

sudo nano /etc/modules

and add this line to the end of the file and reboot:


Windows 10 64 bit:

  • Build OpenCV from source following this guide:
  • Open x64 Native Tools Command Prompt for VS 2017 or 2015. (I will assume you use 2017). If it is not on path already, add your ldc compiler's bin folder to path. And create an env-var to point your opencv build:
set PATH=%PATH%;C:\your-compilers-bin-folder\
set OpenCV_DIR=C:\your-opencv-root-folder

your-opencv-root-folder must contain a file named OpenCVConfig.cmake.

  • cd into opencvd/c/, create a build folder, and run cmake:
cd opencvd/c
mkdir build
cd build
cmake .. -G "Visual Studio 15 2017 Win64"

This will create Visual Studio solution files in opencvd/c/build.

  • Open the solution with VS2017.
  • Go to: Configuration Properties -> C/C++ -> Code Generation -> Runtime Library
  • Change it from /MDd to /MT for both opencvcapi and opencvcapi_contrib (This is only working solution I've found so far). It looks like we cannot debug on windows yet.
  • Build opencvcapi and opencvcapi_contrib in Visual Studio, or go back to the command prompt and type:
cmake --build .
  • And finally in the cmd prompt:
cd opencvd

Now you have *.lib files in opencvd folder.

  • Copy thoose lib files to your test app's root next to dub.json.
  • Add following to your dub.json of your test app:
"dependencies": {
        "opencvd": "~>0.0.4"
"libs": [

While compiling your test app, you must always run dub or ldc2 commands in x64 Native Tools Command Prompt for VS 2017. And note that we have built opencvd against shared libs of opencv4. So, Compiled executables will need opencv dlls in the PATH.


  • Build opencv using one of the guides found on internet such as:
  • Before compiling any code or running your test app, set required env-vars like:
export PKG_CONFIG_PATH=/Users/user/opencv4-dev/installation/OpenCV-master/lib/pkgconfig/
export DYLD_LIBRARY_PATH=/Users/user/opencv4-dev/installation/OpenCV-master/lib/
  • Build opencvcapi and opencvcapi_contrib using cmake and make commands following the ubuntu guide.

Copy libopencvcapi_contrib.a and libopencvcapi.a to the root of your example app. This is an example dub.json for test app:

	"description": "A minimal D application.",
    "dependencies": {
        "opencvd": "~>0.0.4"
	"authors": [
		"Ferhat Kurtulmuş"
	"copyright": "Copyright © 2019, Ferhat Kurtulmuş",
	"license": "Boost",
	"name": "testapp",
    "lflags": ["-L/Users/user/opencv4-dev/installation/OpenCV-master/lib/", "-L/Users/user/Desktop/testapp/"],
    "libs": [

Some notes about C interface (C++ functions with C externs)

Gocv does not wrap some important functionality of opencv. Opencvd will cover some of those wrapping them in c++ sources with appropriate naming such as core -> corehelper, imgproc -> imgprochelper. Thus, differences from gocv can be tracked easily. This should be a temporary solution untill a clear roadmap of opencvd project is determined by its community.

Some examples to show how it looks like:

import std.stdio;

import opencvd;

void main()
    Mat img = imread("test.png", 0);
    Mat subIm1 = img[0..$, 200..300]; // no copy, just new Mat header to windowed data
    auto roi = Rect(0, 0, 100, 200 );
    Mat subIm2 = img(roi); // no copy, just new Mat header to windowed data
    img[200..$-50, 50..200] = Scalar.all(255);
    ubyte[] my_ubyte_array = img.array!ubyte; // access flat array of Mat as ubyte
    // my_ubyte_array.writeln;
    double[] my_double_array = img.array!double; // as double
    // my_double_array.writeln;
    ubyte val =!ubyte(50, 30);
    Color color =, 62); // or img[20, 62];
    // img[20, 20] = Color(25, 26, 27); // assign like this if it is a 3 channel mat
    img[20, 20] = ubyte(255); // assign like this if it is a single-channel mat
    namedWindow("res", 0);
    Mat imres = Mat();
    compare(img, Scalar(200, 0, 0, 0), imres, CMP_LT);
    imshow("res", imres);
    blur(img, img, Size(3, 3));
    foreach(int i; 100..200)
        foreach(int j; 100..200)
            img.set!ubyte(i, j, 125);

    auto cnts = findContours(img, RETR_LIST, CHAIN_APPROX_SIMPLE);
    // or :
    Point[][] contours;
    Scalar[] hierarchy;
    auto c_h = findContoursWithHier(img, RETR_CCOMP, CHAIN_APPROX_SIMPLE);
    contours = c_h[0];
    hierarchy = c_h[1];
    namedWindow("hello", 0);
    imshow("hello", img);

    Mat m = Mat();
    auto mt = Mat(20, 20, CV_8U);
    mt[2, 3] = Color(5,6,7,255);
    mt[2, 3].writeln;
    ubyte[] data = [1, 2, 3,
                    4, 5, 6,
                    10,2, 3,
                    1, 1, 1
    Mat mymat = Mat(4, 3, CV_8U, data.ptr);
    mymat = mymat * 2;
    mymat = mymat + 3;
    ubyte[] mtdata = mymat.array!ubyte;

Some screenshots

alt text alt text alt text

  • Ferhat Kurtulmuş
0.0.4 2019-May-27
0.0.3 2019-May-05
0.0.2 2019-Apr-14
0.0.1 2019-Apr-09
Show all 4 versions
Download Stats:
  • 0 downloads today

  • 3 downloads this week

  • 13 downloads this month

  • 26 downloads total

Short URL: