UniformHelper.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "UniformHelper.h"
  2. /// <summary>
  3. /// ÏßÐÔ²åÖµ
  4. /// </summary>
  5. /// <param name="imgFront"></param>
  6. /// <param name="imgBack"></param>
  7. /// <param name="position"></param>
  8. /// <returns></returns>
  9. cv::Mat UniformHelper::LinearInterpolation(cv::Mat imgFront, cv::Mat imgBack, float position)
  10. {
  11. if(imgFront.size()!=imgBack.size())
  12. {
  13. ErrorMsg::SetErrorMsg(LinearInterpolationError,{ "the size of the interpolating images should be equal."});
  14. return cv::Mat();
  15. }
  16. if (imgFront.depth() != imgBack.depth())
  17. {
  18. ErrorMsg::SetErrorMsg(LinearInterpolationError,{ "the depth of the interpolating images should be equal." });
  19. return cv::Mat();
  20. }
  21. int imgWidth = imgFront.cols;
  22. int imgHeight = imgFront.rows;
  23. cv::MatStep step = imgFront.step;
  24. int type = imgFront.type();
  25. cv::Mat dstImg = cv::Mat(cv::Size(imgWidth, imgHeight), type);
  26. if(type!=CV_8UC1 && type!=CV_16UC1 && type!=CV_8UC3 && type!=CV_8UC4 && type!=CV_32FC1)
  27. {
  28. char strMsgBuff[64];
  29. std::snprintf(strMsgBuff, 64, "unexpected image type ( %i )", type);
  30. ErrorMsg::SetErrorMsg(LinearInterpolationError,{ strMsgBuff });
  31. return cv::Mat();
  32. }
  33. if (type == CV_8UC1)
  34. {
  35. uchar pixelFront;
  36. uchar pixelBack;
  37. uchar pixelDst;
  38. for (int nh = 0; nh < imgHeight; nh++)
  39. {
  40. for (int nw = 0; nw < imgWidth; nw++)
  41. {
  42. pixelFront = imgFront.at<uchar>(nh, nw);
  43. pixelBack = imgBack.at<uchar>(nh, nw);
  44. pixelDst = uchar(pixelFront * (1 - position) + pixelBack * position);
  45. dstImg.at<uchar>(nh, nw) = pixelDst;
  46. }
  47. }
  48. }
  49. if (type == CV_16UC1)
  50. {
  51. ushort pixelFront;
  52. ushort pixelBack;
  53. ushort pixelDst;
  54. for (int nh = 0; nh < imgHeight; nh++)
  55. {
  56. for (int nw = 0; nw < imgWidth; nw++)
  57. {
  58. pixelFront = imgFront.at<ushort>(nh, nw);
  59. pixelBack = imgBack.at<ushort>(nh, nw);
  60. pixelDst = ushort(pixelFront * (1 - position) + pixelBack * position);
  61. dstImg.at<ushort>(nh, nw) = pixelDst;
  62. }
  63. }
  64. }
  65. if (type == CV_8UC3)
  66. {
  67. cv::Vec<uchar, 3> pixelFront;
  68. cv::Vec<uchar, 3> pixelBack;
  69. cv::Vec<uchar, 3> pixelDst;
  70. for (int nh = 0; nh < imgHeight; nh++)
  71. {
  72. for (int nw = 0; nw < imgWidth; nw++)
  73. {
  74. pixelFront = imgFront.at<cv::Vec<uchar, 3>>(nh, nw);
  75. pixelBack = imgBack.at<cv::Vec<uchar, 3>>(nh, nw);
  76. for (int ni = 0; ni < 3; ni++)
  77. {
  78. pixelDst[ni] = uchar(pixelFront[ni] * (1 - position) + pixelBack[ni] * position);
  79. }
  80. dstImg.at<cv::Vec<uchar, 3>>(nh, nw) = pixelDst;
  81. }
  82. }
  83. }
  84. if (type == CV_8UC4)
  85. {
  86. cv::Vec<uchar, 4> pixelFront;
  87. cv::Vec<uchar, 4> pixelBack;
  88. cv::Vec<uchar, 4> pixelDst;
  89. for (int nh = 0; nh < imgHeight; nh++)
  90. {
  91. for (int nw = 0; nw < imgWidth; nw++)
  92. {
  93. pixelFront = imgFront.at<cv::Vec<uchar, 4>>(nh, nw);
  94. pixelBack = imgBack.at<cv::Vec<uchar, 4>>(nh, nw);
  95. for (int ni = 0; ni < 4; ni++)
  96. {
  97. pixelDst[ni] = uchar(pixelFront[ni] * (1 - position) + pixelBack[ni] * position);
  98. }
  99. dstImg.at<cv::Vec<uchar, 4>>(nh, nw) = pixelDst;
  100. }
  101. }
  102. }
  103. if (type == CV_32FC1)
  104. {
  105. float pixelFront;
  106. float pixelBack;
  107. float pixelDst;
  108. for (int nh = 0; nh < imgHeight; nh++)
  109. {
  110. for (int nw = 0; nw < imgWidth; nw++)
  111. {
  112. pixelFront = imgFront.at<float>(nh, nw);
  113. pixelBack = imgBack.at<float>(nh, nw);
  114. pixelDst = float(pixelFront * (1 - position) + pixelBack * position);
  115. dstImg.at<float>(nh, nw) = pixelDst;
  116. }
  117. }
  118. }
  119. return dstImg;
  120. }