123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- #include "UniformHelper.h"
- /// <summary>
- /// ÏßÐÔ²åÖµ
- /// </summary>
- /// <param name="imgFront"></param>
- /// <param name="imgBack"></param>
- /// <param name="position"></param>
- /// <returns></returns>
- cv::Mat UniformHelper::LinearInterpolation(cv::Mat imgFront, cv::Mat imgBack, float position)
- {
- if(imgFront.size()!=imgBack.size())
- {
- ErrorMsg::SetErrorMsg(LinearInterpolationError,{ "the size of the interpolating images should be equal."});
- return cv::Mat();
- }
- if (imgFront.depth() != imgBack.depth())
- {
- ErrorMsg::SetErrorMsg(LinearInterpolationError,{ "the depth of the interpolating images should be equal." });
- return cv::Mat();
- }
- int imgWidth = imgFront.cols;
- int imgHeight = imgFront.rows;
- cv::MatStep step = imgFront.step;
- int type = imgFront.type();
- cv::Mat dstImg = cv::Mat(cv::Size(imgWidth, imgHeight), type);
- if(type!=CV_8UC1 && type!=CV_16UC1 && type!=CV_8UC3 && type!=CV_8UC4 && type!=CV_32FC1)
- {
- char strMsgBuff[64];
- std::snprintf(strMsgBuff, 64, "unexpected image type ( %i )", type);
- ErrorMsg::SetErrorMsg(LinearInterpolationError,{ strMsgBuff });
- return cv::Mat();
- }
- if (type == CV_8UC1)
- {
- uchar pixelFront;
- uchar pixelBack;
- uchar pixelDst;
- for (int nh = 0; nh < imgHeight; nh++)
- {
- for (int nw = 0; nw < imgWidth; nw++)
- {
- pixelFront = imgFront.at<uchar>(nh, nw);
- pixelBack = imgBack.at<uchar>(nh, nw);
- pixelDst = uchar(pixelFront * (1 - position) + pixelBack * position);
- dstImg.at<uchar>(nh, nw) = pixelDst;
- }
- }
- }
- if (type == CV_16UC1)
- {
- ushort pixelFront;
- ushort pixelBack;
- ushort pixelDst;
- for (int nh = 0; nh < imgHeight; nh++)
- {
- for (int nw = 0; nw < imgWidth; nw++)
- {
- pixelFront = imgFront.at<ushort>(nh, nw);
- pixelBack = imgBack.at<ushort>(nh, nw);
- pixelDst = ushort(pixelFront * (1 - position) + pixelBack * position);
- dstImg.at<ushort>(nh, nw) = pixelDst;
- }
- }
- }
- if (type == CV_8UC3)
- {
- cv::Vec<uchar, 3> pixelFront;
- cv::Vec<uchar, 3> pixelBack;
- cv::Vec<uchar, 3> pixelDst;
- for (int nh = 0; nh < imgHeight; nh++)
- {
- for (int nw = 0; nw < imgWidth; nw++)
- {
- pixelFront = imgFront.at<cv::Vec<uchar, 3>>(nh, nw);
- pixelBack = imgBack.at<cv::Vec<uchar, 3>>(nh, nw);
- for (int ni = 0; ni < 3; ni++)
- {
- pixelDst[ni] = uchar(pixelFront[ni] * (1 - position) + pixelBack[ni] * position);
- }
- dstImg.at<cv::Vec<uchar, 3>>(nh, nw) = pixelDst;
- }
- }
- }
- if (type == CV_8UC4)
- {
- cv::Vec<uchar, 4> pixelFront;
- cv::Vec<uchar, 4> pixelBack;
- cv::Vec<uchar, 4> pixelDst;
- for (int nh = 0; nh < imgHeight; nh++)
- {
- for (int nw = 0; nw < imgWidth; nw++)
- {
- pixelFront = imgFront.at<cv::Vec<uchar, 4>>(nh, nw);
- pixelBack = imgBack.at<cv::Vec<uchar, 4>>(nh, nw);
- for (int ni = 0; ni < 4; ni++)
- {
- pixelDst[ni] = uchar(pixelFront[ni] * (1 - position) + pixelBack[ni] * position);
- }
- dstImg.at<cv::Vec<uchar, 4>>(nh, nw) = pixelDst;
- }
- }
- }
- if (type == CV_32FC1)
- {
- float pixelFront;
- float pixelBack;
- float pixelDst;
- for (int nh = 0; nh < imgHeight; nh++)
- {
- for (int nw = 0; nw < imgWidth; nw++)
- {
- pixelFront = imgFront.at<float>(nh, nw);
- pixelBack = imgBack.at<float>(nh, nw);
- pixelDst = float(pixelFront * (1 - position) + pixelBack * position);
- dstImg.at<float>(nh, nw) = pixelDst;
- }
- }
- }
- return dstImg;
- }
|