5/06/2015

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.



1 comment:

  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