// libimageproc.cpp : 定义 DLL 应用程序的导出函数。 // #include "stdafx.h" #include "libimageproc.h" #include "highgui.h" #include "cv.h" #include "resource.h" #include "atlcore.h" static HINSTANCE g_module = NULL; // 这是导出函数的一个示例。 LIBIMAGEPROC_API bool imageprocess(char*BackImgPath,char*FrontImgPath,char*ResultImgPath) { IplImage *pBK = cvCreateImage(cvSize(324,202),IPL_DEPTH_8U,3); HBITMAP hBitmap = (HBITMAP)LoadImageA(g_module, MAKEINTRESOURCEA(IDB_BK), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION|LR_DEFAULTSIZE); if (hBitmap) { LONG cb = 324 * 202 * 3; GetBitmapBits(hBitmap, cb, pBK->imageData); DeleteObject(hBitmap); } IplImage *pfront = cvLoadImage(FrontImgPath,1); if (pBK == NULL || pfront == NULL) return false; IplImage *pImg = cvCreateImage(cvSize(pfront->width,pfront->height),IPL_DEPTH_8U,1); int j,i; CvMat bk; cvGetSubRect(pBK,&bk,cvRect(200,30,pfront->width,pfront->height)); cvCvtColor(pfront,pImg,CV_RGB2GRAY); //rgb转换为灰度图 for(j = 0;j< pImg->height ; j++) { uchar* ptr = (uchar*)(pImg->imageData+j*pImg->widthStep); uchar* ptr1 = (uchar*)(pfront->imageData+j*pfront->widthStep); uchar* ptr2 = (uchar*)(bk.data.ptr+j*bk.step); for(i = 0;i < pImg->width ; i++) { if(ptr[i] < 220) //像素值小于220的保留下来 并显示到背景图上 即白色部分被去除 { ptr2[i*3] = ptr1[i*3]; ptr2[i*3 + 1] = ptr1[i*3 + 1]; ptr2[i*3 + 2] = ptr1[i*3 + 2]; } } } cvSaveImage(ResultImgPath,pBK); cvReleaseImage( &pBK); cvReleaseImage( &pfront); cvReleaseImage( &pImg); return true; } BOOL APIENTRY DllMain( HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: g_module = hModule; DisableThreadLibraryCalls(hModule); break; case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; }