vs下opencv的人脸检测以及qt下dll的封装调用(二、dll的创建)

一、vs下新建main.cpp
// dllmain.cpp : 定义 DLL 应用程序的入口点。  
#include   
BOOL APIENTRY DllMain(HMODULE hModule,
	DWORD  ul_reason_for_call,
	LPVOID lpReserved
	)
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}
二、新建detectdll.cpp

#include "opencv/cv.hpp"  
#include "opencv2/objdetect/objdetect.hpp"  
#include "opencv2/highgui/highgui.hpp"  
#include "opencv2/imgproc/imgproc.hpp"  
#include 
#include   
#include   
#include 
#include 
using namespace std;
using namespace cv;
IplImage* cutImage(IplImage* src, CvRect rect) {
	cvSetImageROI(src, rect);
	IplImage* dst = cvCreateImage(cvSize(rect.width, rect.height),

		src->depth,
		src->nChannels);

	cvCopy(src, dst, 0);
	cvResetImageROI(src);
	return dst;
}

IplImage* detect1(Mat& img, CascadeClassifier& cascade, double scale)
{
	int i = 0;
	double t = 0;
	vector faces;
	Mat gray, smallImg(cvRound(img.rows / scale), cvRound(img.cols / scale), CV_8UC1);

	cvtColor(img, gray, CV_BGR2GRAY);
	resize(gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR);
	equalizeHist(smallImg, smallImg);

	t = (double)cvGetTickCount();
	cascade.detectMultiScale(smallImg, faces,
		1.3, 2, CV_HAAR_SCALE_IMAGE,
		Size(80, 80));
	t = (double)cvGetTickCount() - t;
	printf("detection time = %g ms\n", t / ((double)cvGetTickFrequency()*1000.));
	for (vector::const_iterator r = faces.begin(); r != faces.end(); r++, i++)
	{
		IplImage* temp = cutImage(&(IplImage(img)), cvRect(r->x, r->y, r->width, r->height));
		return temp;
	}

	return NULL;
}

IplImage* det(Mat srcImg, char* path, String cascadeName){

	IplImage *dst1 = 0;			//目标图像指针
	IplImage *dst2 = 0;			//目标图像指针
	CvSize dst_cvsize;			//目标图像尺寸
	CascadeClassifier cascade;
	if (!cascade.load(cascadeName))
	{
		cout << "cascade not load" << endl;
	}


	IplImage* faceImage1;
	clock_t start2, finish2;
	double totaltime2;
	start2 = clock();
	faceImage1 = detect1(srcImg, cascade, 1);
	finish2 = clock();
	totaltime2 = (double)(finish2 - start2) / CLOCKS_PER_SEC;
	std::cout << "detectface time is :" << totaltime2 << "sec" << std::endl;


	if (faceImage1 == NULL) {

		cout << "no image" << endl;
	}


	dst_cvsize.width = 224;		//目标图像的宽为源图象宽的scale倍
	dst_cvsize.height = 224;		//目标图像的高为源图象高的scale倍

	dst1 = cvCreateImage(dst_cvsize, faceImage1->depth, faceImage1->nChannels);	//构造目标图象
	//   cvResize(src, dst, CV_INTER_LINEAR);	//缩放源图像到目标图像
	cvResize(faceImage1, dst1, CV_INTER_CUBIC);		//缩放源图像到目标图像

	cvSaveImage(path, dst1, 0);



	//    cvSaveImage("d:\\face.jpg", faceImage1, 0);  


	cv::Mat im1 = cv::cvarrToMat(dst1);


	cvReleaseImage(&faceImage1);
	return dst1;

}
三、新建头文件detectdll.h

#include "opencv/cv.hpp"  
#include "opencv2/objdetect/objdetect.hpp"  
#include "opencv2/highgui/highgui.hpp"  
#include "opencv2/imgproc/imgproc.hpp"  
#include 
#include   
#include   
#include 
#include 
using namespace std;
using namespace cv;

IplImage* cutImage(IplImage* src, CvRect rect);
IplImage* detect1(Mat& img, CascadeClassifier& cascade, double scale);
IplImage* det(Mat srcImg, char* path, String cascadeName);
四、新建source.def

把函数写在这里,然后空格@+数字,如果某个函数报错,试着改个名字(我的detect函数一直抱错,换成detect1就好了)

LIBRARY "detectdll"  
EXPORTS  
     cutImage @1  
     detect1 @2  
     det @3  
五、重新生成就好拉



你可能感兴趣的:(c++,dll)