Creating dynamic link library including OpenCV(on MS Visual studio)

The method to make dynamic link library is introduced in here.

This introduction is similar with this.
But if you refer both, you will be more helpful.

And see the this youtube video.
This video is also very useful.

firstly, make dll project in VS tool. like this->

And add new class.

And set opencv include and lib path setting.

Now coding your opencv application class.
see the sample code
In here, please note OPENCVDLL_API keyword.
OPENCVDLL_API is replaced dllexport or dllimport.
and It surely have to declare in front of exporting class name.


#pragma once
// OpenCVDLL.h

#define OPENCVDLL_API __declspec(dllexport) 
#define OPENCVDLL_API __declspec(dllimport) 

#include < opencv2\opencv.hpp>

#ifdef _DEBUG            
#pragma comment(lib, "opencv_core249d.lib")    
#pragma comment(lib, "opencv_imgproc249d.lib")   //MAT processing    
#pragma comment(lib, "opencv_core249.lib")    
#pragma comment(lib, "opencv_imgproc249.lib")   

namespace opencvdll_mare

  cv::Mat convert(cv::Mat& inMat);



#include "OpenCVDLL.h"

namespace opencvdll_mare



 cv::Mat OpenCVDLL::convert(cv::Mat& inMat)
  cv::Mat cMat;
  cv::Sobel(inMat, cMat, inMat.depth(), 1, 0);
  return cMat;


You will get .dll, .lib and .h file after compile the OpenCVDLL project.

".dll, .lib" is build in Release or Debug folder. ".h" is just OpenCVDLL.h .

Now, let's use this dll in normal project.
Firstly, make console project and copy files of .dll, .lib, .h into new project folder.

We have to do lib setting. OpenCV setting is same with normal method.
Our dll setting is same with OpenCV.
But we don't need to set include folder path, because we copy header file in local project.

And coding your application.
Sample is like this.
#include < iostream>
using namespace std;

#include < opencv2\opencv.hpp>
#include "OpenCVDLL.h"

#ifdef _DEBUG            

#pragma comment(lib, "opencv_highgui249d.lib")    
#pragma comment(lib, "opencv_highgui249.lib")    

using namespace cv;

void main()

 Mat test = imread("AA.jpg");

 opencvdll_mare::OpenCVDLL cOpenCVDLL_test;
 Mat outmat = cOpenCVDLL_test.convert(test);
 imshow("dll test", outmat);


After all, build and check the result.
My result is here.


  1. JessieKay31/5/16 02:28

    thanks, but i used this fix for my file, and only then created dynamic link library!


Post a Comment

Popular posts from this blog

(OpenCV Study) Background subtractor MOG, MOG2, GMG example source code (BackgroundSubtractorMOG, BackgroundSubtractorMOG2, BackgroundSubtractorGMG)

OpenCV Stitching example (Stitcher class, Panorama)

AMP dose run on my pc?, AMP(Accelerated Massive Parallelism)

Example source code of extract HOG feature from images, save descriptor values to xml file, using opencv (using HOGDescriptor )

Optical Flow sample source code using OpenCV

Real-time N camera stitching Class.

OpenCV meanShiftFiltering example source code ( cpu: pyrMeanShiftFiltering, gpu:meanShiftFiltering, gpu:meanShiftSegmentation )

8 point algorithm (Matlab source code) / The method to get the Fundamental Matrix and the Essential matrix

Image warping (using opencv findHomography, warpPerspective)

Video Stabilization example source code, (using cvFindHomography, cvWarpPerspective functions in openCV)