libimageproc.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. LIBIMAGEPROC_API bool imageprocess(char* BackImgPath, char* FrontImgPath, char* ResultImgPath)
  9. {
  10. IplImage* pBK = cvLoadImage(BackImgPath, 1);
  11. IplImage* pfront = cvLoadImage(FrontImgPath, 1);
  12. if (pBK == NULL || pfront == NULL)
  13. return false;
  14. IplImage* pImg = cvCreateImage(cvSize(pfront->width, pfront->height), IPL_DEPTH_8U, 1);
  15. int j, i;
  16. CvMat bk;
  17. cvGetSubRect(pBK, &bk, cvRect(200, 30, pfront->width, pfront->height));
  18. cvCvtColor(pfront, pImg, CV_RGB2GRAY); //rgb转换为灰度图
  19. for (j = 0; j < pImg->height; j++)
  20. {
  21. uchar* ptr = (uchar*)(pImg->imageData + j * pImg->widthStep);
  22. uchar* ptr1 = (uchar*)(pfront->imageData + j * pfront->widthStep);
  23. uchar* ptr2 = (uchar*)(bk.data.ptr + j * bk.step);
  24. for (i = 0; i < pImg->width; i++)
  25. {
  26. if (ptr[i] < 220) //像素值小于220的保留下来 并显示到背景图上 即白色部分被去除
  27. {
  28. ptr2[i * 3] = ptr1[i * 3];
  29. ptr2[i * 3 + 1] = ptr1[i * 3 + 1];
  30. ptr2[i * 3 + 2] = ptr1[i * 3 + 2];
  31. }
  32. }
  33. }
  34. cvSaveImage(ResultImgPath, pBK);
  35. cvReleaseImage(&pBK);
  36. cvReleaseImage(&pfront);
  37. cvReleaseImage(&pImg);
  38. return true;
  39. }