当前位置:   article > 正文

基于MLS的3CCD变形模拟和校正(附C++程序)_移动最小二乘 c++

移动最小二乘 c++

前些天刚在博客介绍过基于移动最小二乘(MLS)的图像变形,这两天恰恰就接到了一个有关图像变形的任务。说来也简单,就是3CCD是用黏合剂粘到3个棱镜面上,由于棱镜平面切割质量或者黏合剂的分布情况可能导致3个CCD接受的图像产生不同程度的变形,从而造成图像边缘上出现色散效果(类似“抖音”文字效果),要求对其进行模拟并提出校正方案。下面首先分析其可能出现的形变。

一、可能出现的形变分析

  1. 若棱镜的切割与装配等都准确无误,仅仅是贴合的时候各传感器摆得不够正,则可能出现的是各通道图像之间的相对旋转;
  2. 若由于棱镜切割精度不够、装配不准或是中间介质(黏合剂)呈楔形分布等原因导致成像面与出射光线的光轴不垂直,那么可能出现的是各通道图像间的投影变换;
  3. 若切割不平整或是黏合剂的分布不均匀导致光线偏转角度不一致,则可能出现图像扭曲的情况。

考虑存在局部性扭曲的最极端情况,此时可将图像平面分为若干子块,每个子块的变形近似于刚性变换,即仅存在旋转与缩放。此时可用移动最小二乘变形进行模拟和校正。事实上,基于移动最小二乘的变形模拟也是目前各种变形技术中结果最真实自然的变形方法。

基于最小二乘变形的校正的出发点是最小二乘变形的相对性,总体思想是以某个色彩通道为基准,认为另两个通道的无变形标准图像可以通过对有变形的实际图像做反向的最小二乘变形获得。校正的整体流程是首先找到某色彩通道图像与基准通道图像中若干匹配的点对,以该色彩通道中的对应点为源控制点,以基准通道中的对应点为目标控制点,然后基于移动最小二乘法计算该通道图像中每个像素点变形后的坐标(浮点型),进而得到变形后每个像素点坐标(整型)对应的源点(在校正前图像中的对应点)坐标(并由此得到每个像素点的偏移量),再由插值算法得到基于移动最小二乘的变形结果作为校正结果。

三、算法的具体实施与模拟结果

为使各区域都能获得良好的校正效果,控制点应该尽可能均匀地分布于图像的各个区域。这可以通过将图像分块并在每个子块中挑选最多一个控制点的方式来实现。匹配的控制点对采用特征点检测与匹配算法获得。若采用棋盘格图像作为校正时的输入图像,由于此时各特征点在整图上缺乏独特性,因而匹配应该从一开始就限制在小于一个格子的小区域内实施以减少误匹配。若采用具有复杂纹理的图像作为输入图像,则可以先进行全局的匹配,再根据变形的局部性对错误匹配进行过滤。

由于对焦过于模糊或是场景过于简单会导致特征点检测和匹配存在失败的风险,而程序可以通过信息反馈要求使用者重新采集图像,因此这里只考虑采用棋盘格图案或者具有复杂纹理场景对焦质量尚可的图样进行校正的情形。为了降低算法复杂度使用户获得更好的操作体验,算法采用了对图像进行降采样进行校正,然后对降采样图像的偏移量进行缩放和插值的方式来获得图像中每个像素点的偏移量。

模拟变形效果的时候是将图像划分成n1×n1的网格,每个网格最多取一个特征点控制变形,模拟校正的时候是先筛选出一些匹配较好的特征点对作为变形前后的控制点,然后将图像划分为n2×n2网格,每个网格里最多取一个控制点控制校正。

废话不多说,直接上图(代码在最后,博客中贴出的是一个比较粗糙的版本,改进了模拟效果和校正精度的进阶版程序作为商业机密自然是不会在这里贴出的)。

输入图像(尺寸690*460):

模拟变形结果(控制点网格4*4,控制点偏移量-5~5像素):

校正结果(控制点网格划分为10*10,采样点网格大小为30*30) :

用于校正的控制点越多、采样网格划分越精细,校正效果越好,当然,相应的计算量也越大。用更大尺寸的图像做测试,控制点偏移量不变(以像素计)的话,变形没那么明显,校正效果也好得多(这里只是给个示意,就没必要多上图了),当然,同样的,计算量也越大。演示程序使用的是SURF算子提取特征点,速度较慢,可以更换为更高效的特征点提取算子,并使用更简单的描述子去做匹配。事实上,这种应用场景下也没必要考虑多尺度的问题,所以没有必要使用多尺度的检测方法和描述子。由于这里只是做个演示,这些改进工作就不展现出来了。

程序(工作环境为VS2017+OpenCV4.5.0,程序为基于点和基于线的变形预留了接口,为仿射变换、相似变换、刚性变换预留了接口,不过校正时像素插值部分只实现了双线性插值算法,感兴趣的读者可以自己进行扩充):

test.h

  1. #pragma once
  2. #include<iostream>
  3. #include<string>
  4. #include<vector>
  5. #include <math.h>
  6. #include "opencv2/highgui/highgui.hpp"
  7. #include "opencv2/core/core.hpp"
  8. #include "opencv2/features2d.hpp"
  9. #include "opencv2/xfeatures2d/nonfree.hpp"
  10. #include "opencv2/opencv.hpp"
  11. using namespace std;
  12. using namespace cv;
  13. enum ePointLine
  14. {
  15. POINT,
  16. LINE
  17. };
  18. enum eTransform
  19. {
  20. AFFINE,
  21. SIMILAR,
  22. RIGID
  23. };
  24. struct stA
  25. {
  26. float a11;
  27. float a12;
  28. float a21;
  29. float a22;
  30. };
  31. class CMyPoint2f : public Point2f
  32. {
  33. public:
  34. CMyPoint2f() {};
  35. ~CMyPoint2f() {};
  36. CMyPoint2f(const Point2f& pt);
  37. CMyPoint2f(float x, float y);
  38. CMyPoint2f operator+(const CMyPoint2f& pt);
  39. CMyPoint2f operator-(const CMyPoint2f& pt);
  40. };
  41. template<class T>
  42. CMyPoint2f operator*(T fator, const CMyPoint2f& pt)
  43. {
  44. CMyPoint2f newPt;
  45. newPt.x = fator * pt.x;
  46. newPt.y = fator * pt.y;
  47. return newPt;
  48. }
  49. template<class T>
  50. CMyPoint2f operator*(const CMyPoint2f& pt, T fator)
  51. {
  52. return operator*(fator, pt);
  53. }
  54. struct stMLSD
  55. {
  56. int iInterval = 30;
  57. ePointLine ePL = POINT;
  58. eTransform eTrans = RIGID;
  59. vector<CMyPoint2f> vecCtrlPt;
  60. vector<CMyPoint2f> vecGridPt;
  61. vector<vector<float>> vec2Weight;
  62. vector<CMyPoint2f> vecPstar;
  63. vector<vector<stA>> vec2A;
  64. vector<float> vecNormOfPstar;
  65. };
  66. void RandomImageDeformation(Mat& img, Mat&outImg, stMLSD& mlsd, int iXGridNum, int iYGridNum, float fMaxShift = 3.0);
  67. void CtrlPtSelect(Mat & refImg, Mat & inImg, int iXGridNum, int iYGridNum, vector<CMyPoint2f>& vecCtrlPt, vector<CMyPoint2f>& vecDesCtrlPt, int radius = 5);
  68. void GetGridPts(int iWidth, int iHeight, int iInterval, vector<CMyPoint2f>& vecGridPt);
  69. void MLSD2DWarp(Mat& img, Mat& outImg, stMLSD& mlsd, vector<CMyPoint2f>& vecDesCtrlPt);
  70. void MLSD2DTransform(stMLSD& mlsd, vector<CMyPoint2f> vecDesCtrlPt, vector<CMyPoint2f>& vecDesGridPt);
  71. void PointsTransformRigid(stMLSD& mlsd, vector<CMyPoint2f> vecDesCtrlPt, vector<CMyPoint2f>& vecDesGridPt);
  72. void BilinearInterp(vector<vector<CMyPoint2f>>& vec2GridPt, vector<vector<CMyPoint2f>>& vec2dXY, vector<vector<CMyPoint2f>>& vec2NewdXY);
  73. void PixelInterp(Mat& img, Mat& outImg, vector<vector<CMyPoint2f>>& vec2NewdXY);
  74. void MLSD2DpointsPrecompute(stMLSD& mlsd, float fOrder = 2.0);
  75. void PrecomputeWeights(vector<CMyPoint2f>& vecCtrlPt, vector<CMyPoint2f>& vecGridPt, vector<vector<float>>& vec2Weight, float fOrder = 2.0);
  76. void PrecomputeWCentroids(vector<CMyPoint2f>& vecCtrlPt, vector<vector<float>>& vec2Weight, vector<CMyPoint2f>& vecPstar);
  77. void PrecomputePhat(vector<CMyPoint2f>& vecCtrlPt, vector<CMyPoint2f>& vecPstar, vector<vector<CMyPoint2f>>& vecPhat);
  78. void PrecomputeA(vector<vector<CMyPoint2f>>& vecPhat, stMLSD& mlsd);
  79. void PrecomputeRigid(stMLSD& mlsd);

test.cpp 

  1. #include "pch.h"
  2. #include "test.h"
  3. #include <opencv2\imgproc\types_c.h>
  4. #include "opencv2/imgproc/imgproc.hpp"
  5. #include <math.h>
  6. using namespace cv::xfeatures2d;
  7. int main()
  8. {
  9. Mat img = imread("LYF.jpg");
  10. //resize(img, img, Size(img.cols / 3, img.rows / 3));
  11. vector<Mat> vecChannels(3);
  12. split(img, vecChannels);
  13. Mat R = vecChannels[0];
  14. Mat G = vecChannels[1];
  15. Mat B = vecChannels[2];
  16. //模拟3CCD扭曲错位
  17. stMLSD mlsdG, mlsdB;
  18. Mat outR, outG, outB;
  19. float fMaxShift = 5.0;
  20. RandomImageDeformation(G, outG, mlsdG, 4, 4, fMaxShift);
  21. RandomImageDeformation(B, outB, mlsdB, 4, 4, fMaxShift);
  22. G = outG;
  23. B = outB;
  24. Mat outImg;
  25. vecChannels[0] = R;
  26. vecChannels[1] = G;
  27. vecChannels[2] = B;
  28. merge(vecChannels, outImg);
  29. stMLSD mlsdGR, mlsdBR;
  30. vector<CMyPoint2f> vecDesCtrlPtGR, vecDesCtrlPtBR;
  31. CtrlPtSelect(R, G, 10, 10, mlsdGR.vecCtrlPt, vecDesCtrlPtGR);
  32. CtrlPtSelect(R, B, 10, 10, mlsdBR.vecCtrlPt, vecDesCtrlPtBR);
  33. //计算采样格点坐标
  34. GetGridPts(img.cols, img.rows, mlsdGR.iInterval, mlsdGR.vecGridPt);
  35. mlsdBR.vecGridPt = mlsdGR.vecGridPt;
  36. //预先计算不变量
  37. MLSD2DpointsPrecompute(mlsdGR);
  38. MLSD2DpointsPrecompute(mlsdBR);
  39. //执行变形
  40. MLSD2DWarp(G, outG, mlsdGR, vecDesCtrlPtGR);
  41. MLSD2DWarp(B, outB, mlsdBR, vecDesCtrlPtBR);
  42. vecChannels[1] = outG;
  43. vecChannels[2] = outB;
  44. merge(vecChannels, outImg);
  45. return 0;
  46. }
  47. void RandomImageDeformation(Mat & img, Mat & outImg, stMLSD& mlsd, int iXGridNum, int iYGridNum, float fMaxShift)
  48. {
  49. auto surf = SURF::create();
  50. vector<KeyPoint> vecKeyPt;
  51. surf->detect(img, vecKeyPt);
  52. //选取若干控制点,并生成变形后对应的坐标
  53. int iKeyPtNum = vecKeyPt.size();
  54. vector<CMyPoint2f> vecCtrlPt;
  55. vector<CMyPoint2f> vecDesCtrlPt;
  56. int iGridWidth = ceil(img.cols / float(iXGridNum));
  57. int iGridHeight = ceil(img.rows / float(iYGridNum));
  58. vector<vector<CMyPoint2f>> vec2KeyPt(iXGridNum*iYGridNum, vector<CMyPoint2f>());
  59. srand(time(0));
  60. for (int i = 0; i < vecKeyPt.size(); i++)
  61. {
  62. int x = vecKeyPt[i].pt.x / iGridWidth;
  63. int y = vecKeyPt[i].pt.y / iGridHeight;
  64. vec2KeyPt[y*iXGridNum + x].push_back(vecKeyPt[i].pt);
  65. }
  66. for (int i = 0; i < iYGridNum; i++)
  67. {
  68. for (int j = 0; j < iXGridNum; j++)
  69. {
  70. int idx = i * iXGridNum + j;
  71. if (vec2KeyPt[idx].size() > 0)
  72. {
  73. int idx2 = rand() % vec2KeyPt[idx].size();
  74. CMyPoint2f ctrlPt = vec2KeyPt[idx][idx2];
  75. mlsd.vecCtrlPt.push_back(ctrlPt);
  76. vecDesCtrlPt.push_back(ctrlPt + fMaxShift * CMyPoint2f((rand() % 100 - 50) / 50.0, (rand() % 100 - 50) / 50.0));
  77. }
  78. }
  79. }
  80. //计算采样格点坐标
  81. GetGridPts(img.cols, img.rows, mlsd.iInterval, mlsd.vecGridPt);
  82. //预先计算不变量
  83. float fOrder = 2.0;
  84. MLSD2DpointsPrecompute(mlsd, fOrder);
  85. //执行变形
  86. MLSD2DWarp(img, outImg, mlsd, vecDesCtrlPt);
  87. }
  88. void CtrlPtSelect(Mat & refImg, Mat & inImg, int iXGridNum, int iYGridNum, vector<CMyPoint2f>& vecCtrlPt, vector<CMyPoint2f>& vecDesCtrlPt, int radius)
  89. {
  90. auto surf = SURF::create();
  91. vector<KeyPoint> vecPt1, vecPt2;
  92. Mat descrip1, descrip2;
  93. surf->detectAndCompute(refImg, cv::Mat(), vecPt1, descrip1); //特征描述子生成
  94. surf->detectAndCompute(inImg, cv::Mat(), vecPt2, descrip2);
  95. vector<DMatch> matches;
  96. if (descrip1.type() != CV_32F || descrip2.type() != CV_32F)
  97. {
  98. descrip1.convertTo(descrip1, CV_32F);
  99. descrip2.convertTo(descrip2, CV_32F);
  100. }
  101. auto matcher = DescriptorMatcher::create(DescriptorMatcher::MatcherType::FLANNBASED);
  102. matcher->match(descrip1, descrip2, matches); //初始匹配
  103. std::vector< cv::DMatch > finalMatches;
  104. for (int i = 0; i < matches.size(); i++) //依据设定的错位幅度限制对初始匹配进行过滤
  105. {
  106. int queryIdx = matches[i].queryIdx;
  107. int trainIdx = matches[i].trainIdx;
  108. CMyPoint2f pt1, pt2;
  109. pt1 = vecPt1[queryIdx].pt;
  110. pt2 = vecPt2[trainIdx].pt;
  111. if (abs(pt1.x - pt2.x) < radius && abs(pt1.y - pt2.y) < radius)
  112. {
  113. finalMatches.push_back(matches[i]);
  114. }
  115. }
  116. //cv::Mat imageOutput;
  117. //cv::drawMatches(refImg, vecPt1, inImg, vecPt2, finalMatches, imageOutput);
  118. int iGridWidth = ceil(refImg.cols / float(iXGridNum));
  119. int iGridHeight = ceil(refImg.rows / float(iYGridNum));
  120. vector<vector<CMyPoint2f>> vec2CtrlPt(iXGridNum*iYGridNum, vector<CMyPoint2f>());
  121. vector<vector<CMyPoint2f>> vec2DesCtrlPt(iXGridNum*iYGridNum, vector<CMyPoint2f>());
  122. srand(time(0));
  123. int num = 0;
  124. for (int i = 0; i < finalMatches.size(); i++)
  125. {
  126. CMyPoint2f ctrlPt = vecPt2[finalMatches[i].trainIdx].pt;
  127. CMyPoint2f desCtrlPt = vecPt1[finalMatches[i].queryIdx].pt;
  128. int x = ctrlPt.x / iGridWidth;
  129. int y = ctrlPt.y / iGridHeight;
  130. vec2CtrlPt[y*iXGridNum + x].push_back(ctrlPt);
  131. vec2DesCtrlPt[y*iXGridNum + x].push_back(desCtrlPt);
  132. }
  133. for (int i = 0; i < iYGridNum; i++)
  134. {
  135. for (int j = 0; j < iXGridNum; j++)
  136. {
  137. int idx = i * iXGridNum + j;
  138. if (vec2CtrlPt[idx].size() > 0)
  139. {
  140. int idx2 = rand() % vec2CtrlPt[idx].size();
  141. vecCtrlPt.push_back(vec2CtrlPt[idx][idx2]);
  142. vecDesCtrlPt.push_back(vec2DesCtrlPt[idx][idx2]);
  143. }
  144. }
  145. }
  146. }
  147. void GetGridPts(int iWidth, int iHeight, int iInterval, vector<CMyPoint2f>& vecGridPt)
  148. {
  149. int iXnum = (iWidth - 1) / iInterval + 1;
  150. int iYnum = (iHeight - 1) / iInterval + 1;
  151. vecGridPt.resize(iXnum*iYnum);
  152. for (int j = 0; j < iYnum; j++)
  153. {
  154. for (int i = 0; i < iXnum; i++)
  155. {
  156. vecGridPt[j*iXnum + i] = CMyPoint2f(i*iInterval, j*iInterval);
  157. }
  158. }
  159. }
  160. void MLSD2DWarp(Mat & img, Mat& outImg, stMLSD & mlsd, vector<CMyPoint2f>& vecDesCtrlPt)
  161. {
  162. int iChannels = img.channels();
  163. int iWidth = img.cols;
  164. int iHeight = img.rows;
  165. //求变形后的格点坐标
  166. vector<CMyPoint2f> vecDesGridPt;
  167. MLSD2DTransform(mlsd, vecDesCtrlPt, vecDesGridPt);
  168. int iGridPtNum = mlsd.vecGridPt.size();
  169. vector<CMyPoint2f> dXY(iGridPtNum); //原始格点坐标与变形后坐标的差值
  170. for (int i = 0; i < iGridPtNum; i++)
  171. {
  172. dXY[i].x = mlsd.vecGridPt[i].x - vecDesGridPt[i].x;
  173. dXY[i].y = mlsd.vecGridPt[i].y - vecDesGridPt[i].y;
  174. }
  175. int iInterval = mlsd.iInterval;
  176. int iSampHeight = (iHeight - 1) / iInterval + 1;
  177. int iSampWidth = (iWidth - 1) / iInterval + 1;
  178. vector<vector<CMyPoint2f>> vec2GridPt(iSampHeight, vector<CMyPoint2f>(iSampWidth));
  179. vector<vector<CMyPoint2f>> vec2dXY(iSampHeight, vector<CMyPoint2f>(iSampWidth));
  180. for (int i = 0; i < iSampHeight; i++)
  181. {
  182. for (int j = 0; j < iSampWidth; j++)
  183. {
  184. vec2GridPt[i][j] = CMyPoint2f(j*iInterval, i*iInterval);
  185. }
  186. }
  187. for (int i = 0; i < iSampHeight; i++)
  188. {
  189. for (int j = 0; j < iSampWidth; j++)
  190. {
  191. vec2dXY[i][j] = dXY[i*iSampWidth + j];
  192. }
  193. }
  194. //对每个像素点的偏移量进行双线性插值,由此得到其对应的源点坐标
  195. vector<vector<CMyPoint2f>> vec2NewdXY(iHeight, vector<CMyPoint2f>(iWidth));
  196. BilinearInterp(vec2GridPt, vec2dXY, vec2NewdXY);
  197. //像素插值
  198. PixelInterp(img, outImg, vec2NewdXY);
  199. }
  200. void MLSD2DTransform(stMLSD& mlsd, vector<CMyPoint2f> vecDesCtrlPt, vector<CMyPoint2f>& vecDesGridPt)
  201. {
  202. switch (mlsd.ePL)
  203. {
  204. case POINT:
  205. switch (mlsd.eTrans)
  206. {
  207. case AFFINE:
  208. //TODO
  209. break;
  210. case SIMILAR:
  211. //TODO
  212. break;
  213. case RIGID:
  214. PointsTransformRigid(mlsd, vecDesCtrlPt, vecDesGridPt);
  215. break;
  216. default:
  217. break;
  218. }
  219. break;
  220. case LINE:
  221. switch (mlsd.eTrans)
  222. {
  223. case AFFINE:
  224. //TODO
  225. break;
  226. case SIMILAR:
  227. //TODO
  228. break;
  229. case RIGID:
  230. //TODO
  231. break;
  232. default:
  233. break;
  234. }
  235. break;
  236. default:
  237. break;
  238. }
  239. }
  240. void PointsTransformRigid(stMLSD& mlsd, vector<CMyPoint2f> vecDesCtrlPt, vector<CMyPoint2f>& vecDesGridPt)
  241. {
  242. int iCtrlPtNum = mlsd.vecCtrlPt.size();
  243. int iGridPtNum = mlsd.vecGridPt.size();
  244. vector<CMyPoint2f> vecQstar(iGridPtNum);
  245. PrecomputeWCentroids(vecDesCtrlPt, mlsd.vec2Weight, vecQstar);
  246. vector<CMyPoint2f> vecFr(iGridPtNum);
  247. vecDesGridPt.resize(iGridPtNum);
  248. for (int i = 0; i < iCtrlPtNum; i++)
  249. {
  250. for (int j = 0; j < iGridPtNum; j++)
  251. {
  252. float fXoffsetDCP2QS = vecDesCtrlPt[i].x - vecQstar[j].x;
  253. float fYoffsetDCP2QS = vecDesCtrlPt[i].y - vecQstar[j].y;
  254. vecFr[j].x += fXoffsetDCP2QS * mlsd.vec2A[i][j].a11 + fYoffsetDCP2QS * mlsd.vec2A[i][j].a21;
  255. vecFr[j].y += fXoffsetDCP2QS * mlsd.vec2A[i][j].a12 + fYoffsetDCP2QS * mlsd.vec2A[i][j].a22;
  256. }
  257. }
  258. vector<float> vecNormFr(iGridPtNum);
  259. for (int i = 0; i < iGridPtNum; i++)
  260. {
  261. vecNormFr[i] = sqrt(pow(vecFr[i].x, 2) + pow(vecFr[i].y, 2));
  262. }
  263. float delta = pow(10, -9);
  264. for (int i = 0; i < iGridPtNum; i++)
  265. {
  266. float fNormFactor = mlsd.vecNormOfPstar[i] / (vecNormFr[i] + delta);
  267. vecDesGridPt[i].x = fNormFactor * vecFr[i].x + vecQstar[i].x;
  268. vecDesGridPt[i].y = fNormFactor * vecFr[i].y + vecQstar[i].y;
  269. }
  270. }
  271. void BilinearInterp(vector<vector<CMyPoint2f>>& vec2GridPt, vector<vector<CMyPoint2f>>& vec2dXY, vector<vector<CMyPoint2f>>& vec2NewdXY)
  272. {
  273. //对采样格点边界所包围的点进行插值,程序允许非均匀采样间隔
  274. for (int i = 0; i < vec2GridPt.size() - 1; i++)
  275. {
  276. for (int j = 0; j < vec2GridPt[0].size() - 1; j++)
  277. {
  278. int x1 = vec2GridPt[i][j].x;
  279. int x2 = vec2GridPt[i][j + 1].x;
  280. int y1 = vec2GridPt[i][j].y;
  281. int y2 = vec2GridPt[i + 1][j].y;
  282. CMyPoint2f v11 = vec2dXY[i][j];
  283. CMyPoint2f v12 = vec2dXY[i][j + 1];
  284. CMyPoint2f v21 = vec2dXY[i + 1][j];
  285. CMyPoint2f v22 = vec2dXY[i + 1][j + 1];
  286. float dx = x2 - x1;
  287. float dy = y2 - y1;
  288. for (int m = 0; m <= dy; m++)
  289. {
  290. for (int n = 0; n <= dx; n++)
  291. {
  292. float w11 = (1 - m / dy)*(1 - n / dx);
  293. float w12 = (1 - m / dy)*(n / dx);
  294. float w21 = (m / dy)*(1 - n / dx);
  295. float w22 = (m*n) / (dx*dy);
  296. vec2NewdXY[y1 + m][x1 + n] = w11 * v11 + w12 * v12 + w21 * v21 + w22 * v22;
  297. }
  298. }
  299. }
  300. }
  301. //采样网格右方的采样点插值
  302. int iXend = vec2GridPt[0][vec2GridPt[0].size() - 1].x;
  303. int iYend = vec2GridPt[vec2GridPt.size() - 1][vec2GridPt[0].size() - 1].y;
  304. for (int i = int(vec2GridPt[0][0].y); i < iYend; i++)
  305. {
  306. for (int j = iXend + 1; j < vec2NewdXY[0].size(); j++)
  307. {
  308. vec2NewdXY[i][j] = 2 * vec2NewdXY[i][j - 1] - vec2NewdXY[i][j - 2];
  309. }
  310. }
  311. //采样网格下方的采样点插值
  312. for (int i = iYend + 1; i < vec2NewdXY.size(); i++)
  313. {
  314. for (int j = int(vec2GridPt[0][0].x); j < iXend; j++)
  315. {
  316. vec2NewdXY[i][j] = 2 * vec2NewdXY[i - 1][j] - vec2NewdXY[i - 2][j];
  317. }
  318. }
  319. //采样网格右下方的采样点插值
  320. for (int i = iYend; i < vec2NewdXY.size(); i++)
  321. {
  322. for (int j = iXend + 1; j < vec2NewdXY[0].size(); j++)
  323. {
  324. vec2NewdXY[i][j] = vec2NewdXY[i - 1][j] + vec2NewdXY[i][j - 1] - vec2NewdXY[i - 1][j - 1];
  325. }
  326. }
  327. }
  328. void PixelInterp(Mat & img, Mat & outImg, vector<vector<CMyPoint2f>>& vec2NewdXY)
  329. {
  330. //默认采用双线性插值和零填充
  331. int iChannels = img.channels();
  332. int iWidth = img.cols;
  333. int iHeight = img.rows;
  334. outImg = img.clone();
  335. outImg.setTo(0);
  336. if (iChannels == 1)
  337. {
  338. for (int i = 0; i < iHeight; i++)
  339. {
  340. for (int j = 0; j < iWidth; j++)
  341. {
  342. float fX = vec2NewdXY[i][j].x + j;
  343. float fY = vec2NewdXY[i][j].y + i;
  344. if (fX >= 0 && fX < iWidth && fY >= 0 && fY < iHeight)
  345. {
  346. int iLx = int(fX);
  347. int iLy = int(fY);
  348. int iHx = min(iLx + 1, iWidth - 1);
  349. int iHy = min(iLy + 1, iHeight - 1);
  350. float dx = fX - iLx;
  351. float dy = fY - iLy;
  352. float w11 = (1 - dx)*(1 - dy);
  353. float w12 = dx * (1 - dy);
  354. float w21 = (1 - dx)*dy;
  355. float w22 = dx * dy;
  356. float v11 = img.at<uchar>(iLy, iLx);
  357. float v12 = img.at<uchar>(iLy, iHx);
  358. float v21 = img.at<uchar>(iHy, iLx);
  359. float v22 = img.at<uchar>(iHy, iHx);
  360. outImg.at<uchar>(i, j) = (uchar)(w11 * v11 + w12 * v12 + w21 * v21 + w22 * v22);
  361. }
  362. }
  363. }
  364. }
  365. else if (iChannels == 3)
  366. {
  367. for (int i = 0; i < iHeight; i++)
  368. {
  369. for (int j = 0; j < iWidth; j++)
  370. {
  371. float fX = vec2NewdXY[i][j].x + j;
  372. float fY = vec2NewdXY[i][j].y + i;
  373. if (fX >= 0 && fX < iWidth && fY >= 0 && fY < iHeight)
  374. {
  375. int iLx = int(fX);
  376. int iLy = int(fY);
  377. int iHx = min(iLx + 1, iWidth - 1);
  378. int iHy = min(iLy + 1, iHeight - 1);
  379. float dx = fX - iLx;
  380. float dy = fY - iLy;
  381. float w11 = (1 - dx)*(1 - dy);
  382. float w12 = dx * (1 - dy);
  383. float w21 = (1 - dx)*dy;
  384. float w22 = dx * dy;
  385. Vec3b v11 = img.at<Vec3b>(iLy, iLx);
  386. Vec3b v12 = img.at<Vec3b>(iLy, iHx);
  387. Vec3b v21 = img.at<Vec3b>(iHy, iLx);
  388. Vec3b v22 = img.at<Vec3b>(iHy, iHx);
  389. outImg.at<Vec3b>(i, j) = (Vec3b)(w11 * v11 + w12 * v12 + w21 * v21 + w22 * v22);
  390. }
  391. }
  392. }
  393. }
  394. }
  395. void MLSD2DpointsPrecompute(stMLSD & mlsd, float fOrder)
  396. {
  397. //计算每个控制点对于每个格点的权重
  398. PrecomputeWeights(mlsd.vecCtrlPt, mlsd.vecGridPt, mlsd.vec2Weight, fOrder);
  399. //预先计算每个格点对应的加权中心控制点P*
  400. PrecomputeWCentroids(mlsd.vecCtrlPt, mlsd.vec2Weight, mlsd.vecPstar);
  401. switch (mlsd.eTrans)
  402. {
  403. case AFFINE:
  404. //PrecomputeAffine(mlsd); //待实现
  405. break;
  406. case SIMILAR:
  407. //PrecomputeSimilar(mlsd); //待实现
  408. break;
  409. case RIGID:
  410. PrecomputeRigid(mlsd);
  411. break;
  412. default:
  413. break;
  414. }
  415. }
  416. void PrecomputeWeights(vector<CMyPoint2f>& vecCtrlPt, vector<CMyPoint2f>& vecGridPt, vector<vector<float>>& vec2Weight, float fOrder)
  417. {
  418. //预分配空间
  419. vec2Weight.resize(vecCtrlPt.size());
  420. for (int i = 0; i < vecCtrlPt.size(); i++)
  421. {
  422. vec2Weight[i].resize(vecGridPt.size());
  423. CMyPoint2f ctrlPt = vecCtrlPt[i];
  424. for (int j = 0; j < vecGridPt.size(); j++)
  425. {
  426. float fSqureDis = pow(ctrlPt.x - vecGridPt[j].x, 2) + pow(ctrlPt.y - vecGridPt[j].y, 2);
  427. float delta = pow(10, -8);
  428. if (fOrder == 2.0)
  429. {
  430. vec2Weight[i][j] = 1 / (fSqureDis + delta);
  431. }
  432. else
  433. vec2Weight[i][j] = 1 / (pow(fSqureDis, fOrder / 2) + delta);
  434. }
  435. }
  436. }
  437. void PrecomputeWCentroids(vector<CMyPoint2f>& vecCtrlPt, vector<vector<float>>& vec2Weight, vector<CMyPoint2f>& vecPstar)
  438. {
  439. int iCtrlPtNum = vecCtrlPt.size();
  440. int iGridPtNum = vec2Weight[0].size();
  441. vecPstar.resize(iGridPtNum);
  442. for (int i = 0; i < iGridPtNum; i++)
  443. {
  444. float fSumX = 0, fSumY = 0;
  445. float fSumWeight = 0;
  446. for (int j = 0; j < iCtrlPtNum; j++)
  447. {
  448. fSumWeight += vec2Weight[j][i];
  449. fSumX += vec2Weight[j][i] * vecCtrlPt[j].x;
  450. fSumY += vec2Weight[j][i] * vecCtrlPt[j].y;
  451. }
  452. vecPstar[i] = CMyPoint2f(fSumX / fSumWeight, fSumY / fSumWeight);
  453. }
  454. }
  455. void PrecomputePhat(vector<CMyPoint2f>& vecCtrlPt, vector<CMyPoint2f>& vecPstar, vector<vector<CMyPoint2f>>& vecPhat)
  456. {
  457. int iCtrlPtNum = vecCtrlPt.size();
  458. int iGridPtNum = vecPstar.size();
  459. vecPhat.resize(iCtrlPtNum);
  460. for (int i = 0; i < iCtrlPtNum; i++)
  461. {
  462. vecPhat[i].resize(iGridPtNum);
  463. for (int j = 0; j < iGridPtNum; j++)
  464. {
  465. vecPhat[i][j] = CMyPoint2f(vecCtrlPt[i].x - vecPstar[j].x, vecCtrlPt[i].y - vecPstar[j].y);
  466. }
  467. }
  468. }
  469. void PrecomputeA(vector<vector<CMyPoint2f>>& vecPhat, stMLSD& mlsd)
  470. {
  471. int iCtrlPtNum = mlsd.vecCtrlPt.size();
  472. int iGridPtNum = mlsd.vecGridPt.size();
  473. //vector<float> vecXoffsetGrid2WC(iGridPtNum);
  474. //vector<float> vecYoffsetGrid2WC(iGridPtNum);
  475. mlsd.vec2A.resize(iCtrlPtNum);
  476. mlsd.vecNormOfPstar.resize(iGridPtNum);
  477. for (int i = 0; i < iCtrlPtNum; i++)
  478. {
  479. mlsd.vec2A[i].resize(iGridPtNum);
  480. for (int j = 0; j < iGridPtNum; j++)
  481. {
  482. float fXoffsetGrid2WC = mlsd.vecGridPt[j].x - mlsd.vecPstar[j].x;
  483. float fYoffsetGrid2WC = mlsd.vecGridPt[j].y - mlsd.vecPstar[j].y;
  484. float fXoffsetCtrlPt2WC = vecPhat[i][j].x;
  485. float fYoffsetCtrlPt2WC = vecPhat[i][j].y;
  486. if (i == 0)
  487. {
  488. mlsd.vecNormOfPstar[j] = sqrt(pow(fXoffsetGrid2WC, 2) + pow(fYoffsetGrid2WC, 2));
  489. }
  490. float fWeight = mlsd.vec2Weight[i][j];
  491. mlsd.vec2A[i][j].a11 = fWeight * (fXoffsetCtrlPt2WC * fXoffsetGrid2WC + fYoffsetCtrlPt2WC * fYoffsetGrid2WC);
  492. mlsd.vec2A[i][j].a22 = mlsd.vec2A[i][j].a11;
  493. mlsd.vec2A[i][j].a12 = fWeight * (fXoffsetCtrlPt2WC * fYoffsetGrid2WC - fYoffsetCtrlPt2WC * fXoffsetGrid2WC);
  494. mlsd.vec2A[i][j].a21 = -mlsd.vec2A[i][j].a12;
  495. }
  496. }
  497. }
  498. void PrecomputeRigid(stMLSD& mlsd)
  499. {
  500. vector<vector<CMyPoint2f>> vecPhat;
  501. PrecomputePhat(mlsd.vecCtrlPt, mlsd.vecPstar, vecPhat);
  502. PrecomputeA(vecPhat, mlsd);
  503. }
  504. CMyPoint2f::CMyPoint2f(const Point2f& pt)
  505. {
  506. this->x = pt.x;
  507. this->y = pt.y;
  508. }
  509. CMyPoint2f::CMyPoint2f(float x, float y)
  510. {
  511. this->x = x;
  512. this->y = y;
  513. }
  514. CMyPoint2f CMyPoint2f::operator+(const CMyPoint2f& pt)
  515. {
  516. CMyPoint2f newPt;
  517. newPt.x = this->x + pt.x;
  518. newPt.y = this->y + pt.y;
  519. return newPt;
  520. }
  521. CMyPoint2f CMyPoint2f::operator-(const CMyPoint2f & pt)
  522. {
  523. CMyPoint2f newPt;
  524. newPt.x = this->x - pt.x;
  525. newPt.y = this->y - pt.y;
  526. return newPt;
  527. }

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/356338
推荐阅读
相关标签
  

闽ICP备14008679号