libimageproc.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // libimageproc.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. #include "stdafx.h"
  4. #include "libimageproc.h"
  5. #include "highgui.h"
  6. #include "cv.h"
  7. #include "resource.h"
  8. #include "atlcore.h"
  9. static HINSTANCE g_module = NULL;
  10. // 这是导出函数的一个示例。
  11. LIBIMAGEPROC_API bool imageprocess(char*BackImgPath,char*FrontImgPath,char*ResultImgPath)
  12. {
  13. IplImage *pBK = cvCreateImage(cvSize(324,202),IPL_DEPTH_8U,3);
  14. HBITMAP hBitmap = (HBITMAP)LoadImageA(g_module,
  15. MAKEINTRESOURCEA(IDB_BK),
  16. IMAGE_BITMAP,
  17. 0,
  18. 0,
  19. LR_CREATEDIBSECTION|LR_DEFAULTSIZE);
  20. if (hBitmap)
  21. {
  22. LONG cb = 324 * 202 * 3;
  23. GetBitmapBits(hBitmap, cb, pBK->imageData);
  24. DeleteObject(hBitmap);
  25. }
  26. IplImage *pfront = cvLoadImage(FrontImgPath,1);
  27. if (pBK == NULL || pfront == NULL)
  28. return false;
  29. IplImage *pImg = cvCreateImage(cvSize(pfront->width,pfront->height),IPL_DEPTH_8U,1);
  30. int j,i;
  31. CvMat bk;
  32. cvGetSubRect(pBK,&bk,cvRect(200,30,pfront->width,pfront->height));
  33. cvCvtColor(pfront,pImg,CV_RGB2GRAY); //rgb转换为灰度图
  34. for(j = 0;j< pImg->height ; j++)
  35. {
  36. uchar* ptr = (uchar*)(pImg->imageData+j*pImg->widthStep);
  37. uchar* ptr1 = (uchar*)(pfront->imageData+j*pfront->widthStep);
  38. uchar* ptr2 = (uchar*)(bk.data.ptr+j*bk.step);
  39. for(i = 0;i < pImg->width ; i++)
  40. {
  41. if(ptr[i] < 220) //像素值小于220的保留下来 并显示到背景图上 即白色部分被去除
  42. {
  43. ptr2[i*3] = ptr1[i*3];
  44. ptr2[i*3 + 1] = ptr1[i*3 + 1];
  45. ptr2[i*3 + 2] = ptr1[i*3 + 2];
  46. }
  47. }
  48. }
  49. cvSaveImage(ResultImgPath,pBK);
  50. cvReleaseImage( &pBK);
  51. cvReleaseImage( &pfront);
  52. cvReleaseImage( &pImg);
  53. return true;
  54. }
  55. BOOL APIENTRY DllMain( HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  56. {
  57. switch (ul_reason_for_call)
  58. {
  59. case DLL_PROCESS_ATTACH:
  60. g_module = hModule;
  61. DisableThreadLibraryCalls(hModule);
  62. break;
  63. case DLL_THREAD_ATTACH:
  64. case DLL_THREAD_DETACH:
  65. case DLL_PROCESS_DETACH:
  66. break;
  67. }
  68. return TRUE;
  69. }