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


The method to make dynamic link library is introduced in here.
https://msdn.microsoft.com/en-us//library/ms235636.aspx

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.
https://www.youtube.com/watch?v=yEqRyQhhto8


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.


OpenCVDLL.h
...

#pragma once
// OpenCVDLL.h

#ifdef OPENCVDLL_EXPORTS
#define OPENCVDLL_API __declspec(dllexport) 
#else
#define OPENCVDLL_API __declspec(dllimport) 
#endif

#include < opencv2\opencv.hpp>

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

namespace opencvdll_mare
{

 class OPENCVDLL_API OpenCVDLL
 {
 public:
  OpenCVDLL(void);
  ~OpenCVDLL(void);
  cv::Mat convert(cv::Mat& inMat);

 };
}


...

OpenCVDLL.cpp
...
#include "OpenCVDLL.h"

namespace opencvdll_mare
{

 OpenCVDLL::OpenCVDLL(void)
 {
 }


 OpenCVDLL::~OpenCVDLL(void)
 {
 }

 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.
loadOpenCVDLL/Source.cpp
...
#include < iostream>
using namespace std;

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

#ifdef _DEBUG            

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


using namespace cv;

void main()
{
 

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

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

 waitKey(0);
}
...


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



Comments

  1. JessieKay31/5/16 02:28

    thanks, but i used http://fix4dll.com/d3dx9_43_dll this fix for my file, and only then created dynamic link library!

    ReplyDelete

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)