一、前言
本文实验在VS2019将C++源码封装为dll、lib ,并做调用
二、欲封装代码
封装代码头文件和cpp文件如下:
//util.h
#ifndef _Util_H_
#define _Util_H_
#include <iostream>
#include<opencv2\opencv.hpp>
#include <stdio.h>
#include <list>
using namespace std;
using namespace cv;
struct Move
{
bool res;
int x;
int y;
};
Mat resizeImg(const Mat& src , float s);
Move matchOneImg( const Rect& ro , const Mat& tem , const float& thr , const Mat& re_ );
void A_on_Mouse(int event, int x, int y, int flags, void* param);
Rect getRoi(const Mat& img);
class TempMatch
{
public:
float scale; //缩放倍数
float simi_thr; //相关系数阈值
Rect roi; //模板区域(x,y.w.h)
Mat templa; //模板区域图像
TempMatch(char* p, float sca, float si = 0.75);
Move matchImg(char* p);
};
#endif
下面是相应cpp文件
#include "util.h"
#include <algorithm>
#define WIN_NAME "draw_rect"
Rect select, select_br;
bool select_flag = false;
Mat im, show_im;
Mat resizeImg(const Mat& src, float s) {
Mat dst;
resize(src, dst, Size(src.cols / s, src.rows / s)) ;
return dst;
}
void A_on_Mouse(int event, int x, int y, int flags, void* param)//实现画矩形框
{
Point p1, p2;
if (event == EVENT_LBUTTONDOWN)
{
select.x = x;
select.y = y;
select_flag = true;
}
else if (select_flag && event == EVENT_MOUSEMOVE)
{
im.copyTo(show_im);
select_br.x = x;
select_br.y = y;
p1 = Point(select.x, select.y);
p2 = Point(x, y);
rectangle(show_im, p1, p2, Scalar(255, 255, 255), 4);
char text[50];
sprintf_s(text, "(%d,%d)", abs(p2.x-p1.x) ,abs(p2.y-p1.y) );
putText(show_im, text, Point(p2.x, p2.y + 50), FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 4);
imshow(WIN_NAME, show_im);
}
else if (select_flag && event == EVENT_LBUTTONUP)
{
select_flag = false;
}
}
Rect getRoi(const Mat& img) {
im = img.clone();
show_im = img.clone();
select.x = select.y = 0;
select_br.x = select_br.y = 0;
namedWindow(WIN_NAME, 0);
imshow(WIN_NAME, show_im);
while (1)
{
waitKey(1);
setMouseCallback(WIN_NAME, A_on_Mouse, 0);
if ((!select_flag) && (select_br.x != 0)) {
//cout << select_br.x;
break;
}
}
destroyAllWindows();
Rect roi(select.x, select.y, select_br.x - select.x, select_br.y - select.y);
return roi;
}
Move matchOneImg( const Rect& roi, const Mat& templa, const float& thr ,const float& scale ,const Mat& img ) {
bool res = true;
int x = 0;
int y = 0;
Mat