制作动态链接库给opencv程序使用(使用QtCreator)

新建一个c++库项目

 

pro文件

#-------------------------------------------------
#
# Project created by QtCreator 2014-12-29T16:10:53
#
#-------------------------------------------------

QT       -= core gui

TARGET = dlldemo
TEMPLATE = lib
CONFIG += staticlib

SOURCES += dlldemo.cpp

HEADERS += dlldemo.h


INCLUDEPATH += D:/sdk/opencv/build/include

LIBS += -LD:/sdk/opencv/build/x86/vc12/lib \
    -lopencv_calib3d249d \
    -lopencv_contrib249d \
    -lopencv_core249d \
    -lopencv_features2d249d \
    -lopencv_flann249d \
    -lopencv_gpu249d \
    -lopencv_highgui249d \
    -lopencv_imgproc249d \
    -lopencv_legacy249d \
    -lopencv_ml249d \
    -lopencv_nonfree249d \
    -lopencv_objdetect249d \
    -lopencv_ocl249d \
    -lopencv_photo249d \
    -lopencv_stitching249d \
    -lopencv_superres249d \
    -lopencv_ts249d \
    -lopencv_video249d \
    -lopencv_videostab249d

unix {
    target.path = /usr/lib
    INSTALLS += target
}

dlldemo.h

#ifndef DLLDEMO_H
#define DLLDEMO_H
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
using namespace std;
using namespace cv;
class Dlldemo
{

public:
    Dlldemo();
    int addtwo(int a,int b);
    Mat togray(Mat img);
    void draw_text(cv::Mat &canvas, const char *str, int x, int y, float scale = 1.0);
};

#endif // DLLDEMO_H

dlldemo.cpp

#include "dlldemo.h"


Dlldemo::Dlldemo()
{
}

int Dlldemo::addtwo(int a, int b)
{
    return a+b;
}

Mat Dlldemo::togray(Mat img)
{
    cv::Mat greyMat;
    cv::cvtColor(img, greyMat, CV_BGR2GRAY);
    draw_text (greyMat,"#by dll function", 5, 30,0.8);
    return greyMat;
}

void Dlldemo::draw_text(Mat &canvas, const char *str, int x, int y, float scale)
{

    int w = 2;
    for(int dy = -w; dy <= w; ++dy) {
        for (int dx = -w; dx <= w; ++dx) {
            cv::putText(canvas, str, cv::Point(x+dx,y+dy), cv::FONT_HERSHEY_SIMPLEX, scale, cv::Scalar(0,0,0), 2, CV_AA);
        }
    }

    cv::putText(canvas, str, cv::Point(x,y), cv::FONT_HERSHEY_SIMPLEX, scale, cv::Scalar(255,255,255), 2, CV_AA);
}

build--->生成

测试

新建一个c++工程

拷贝dlldemo.h 和dlldemo.lib

修改pro文件

LIBS += -LD:/project/testdlldemo \
    -ldlldemo


INCLUDEPATH += D:/sdk/opencv/build/include

LIBS += -LD:/sdk/opencv/build/x86/vc12/lib \
    -lopencv_calib3d249d \
    -lopencv_contrib249d \
    -lopencv_core249d \
    -lopencv_features2d249d \
    -lopencv_flann249d \
    -lopencv_gpu249d \
    -lopencv_highgui249d \
    -lopencv_imgproc249d \
    -lopencv_legacy249d \
    -lopencv_ml249d \
    -lopencv_nonfree249d \
    -lopencv_objdetect249d \
    -lopencv_ocl249d \
    -lopencv_photo249d \
    -lopencv_stitching249d \
    -lopencv_superres249d \
    -lopencv_ts249d \
    -lopencv_video249d \
    -lopencv_videostab249d

测试代码main.cpp

#include <iostream>
#include "dlldemo.h"
using namespace std;

int main()
{
    cout << "Hello World!" << endl;
    int a=10;
    int b=20;
    Dlldemo dll;
    printf("sum of a,b is :%d",dll.addtwo (a,b));

    cv::Mat canvas;
    canvas = cv::imread("d://nike.jpg", -1);
    Mat result= dll.togray (canvas);
    imshow ("dll result",result);
    waitKey ();

    return 0;
}

 

成功

 

源码下载

http://pan.baidu.com/s/1kT7QBbx

 

posted @ 2014-12-29 16:52  小菜鸟_yang  阅读(760)  评论(0编辑  收藏  举报