《Image Warping Using Few Anchor Points and Radial Function》论文实现

《Image Warping Using Few Anchor Points and Radial Functions》achive based OpenCv 、Eigen and Qt

  • (vector blod)
  • paper
    [paper Download](Image Warping Using Few Anchor Points and Radial Functions "paper Download")
  • Tools
    • Eigen
    • OpenCV
  • Concepts
    • radial basic function transformation(RBFT)
    • affine transformation
    • radial transformation
    • radial basic function
    • Euclidean
    • thin-plate spline
    • Gaussians function
  • Important Equation
    • T(xi)=yi for i=1,2,3,……N.——** Equation(1) **
    • T(x)=A(x)+R(x)——** Equation(2) **
    • A(x)=Mx+b ,2D Affine transformation,M is 2 x 2 real Matrix—— ** Equation(3) **
    • R(x)=(Rx(x),Ry(x)),Rx and Ry are both radial functions of the form radial function
      g:is aunivariate function,termed the radial basis function
      ||·||:Euclidean norm
      ai & bi: is determined by Anchor Points——** Equation(4) **
    • R(xi)=yi-A(xi),i=1,2,3,……N.——** Equation(5) **
    • ——** Equation(6) **
    • Gaussian function:——** Equation(7) **
    • thin-plate spline:——** Equation(8) **
  • Thinking
    1. load Image
      load Image
    2. get Anchor points by User Interactive
      get anchor points
    3. Analysis source Point and target Point
    4. Controlling the affine component A(x) accoring to the number of anchor points,by Equation(3)
    5. determining the radial component R(x),by Equation(4),Equation(5)
    6. Apply T(x) to every pixel in this image
  • Question and Answer(Q&A)
    • Q1:How to save Image?
    • A1:Translate cv::Mat to Eigen::MatrixXd(3,width*height).
    • Q2:How to save "M" Matrix in Equation(3)?
    • A2:Using Eigen::MartrixXd(2,3).
    • Q3:How to Calcualte "R" Matrix in Equation(2)?
    • A3:Reference Equation(4) and Equation(5).
    • Q4:How to calculate g(x) in Equation(4)?
    • A4:thin-plate spline or Gaussian function.
    • Q5:How to save coordinate before transform?
    • A5:Using MatrixXd(2,1).
    • Q6:How to save coordinate after transform?
    • A6:Using MatrixXd(2,1).
    • Q7:How to save source points and target points?
    • A7:Using Eigen::MatrixXd(2,N)
    • Q8:How to save corespondence between source coordinate and target coordinate?
    • A8: Qt::QList<Line*> lineList;Line is line for start point to end point for anchor pairs.
  • Achieve
    • WarpingRBF.h
 
 
  1. //WarpingRBF.h
  2. #ifndef WARPINGRBF_H
  3. #define WARPINGRBF_H
  4. #include<opencv2/opencv.hpp>
  5. #include<QList>
  6. #include<Eigen/Eigen>
  7. #include"line.h"
  8. class WarpingRBF
  9. {
  10. public:
  11. enum GFuncType{
  12. Gaussian_Radio_Function,
  13. Thin_Plate_spline,//r^2log(r)
  14. Gaussian_Radial
  15. };
  16. public:
  17. WarpingRBF();
  18. WarpingRBF(const QList<Line*> & lineList,
  19. const int&N);
  20. public:
  21. void SolveRBF();
  22. void SetImage(const cv::Mat &sourceImage);
  23. void SetGFunc(const GFuncType &gfunctype);
  24. protected:
  25. void InitData();
  26. void InitSourceAndTargetMaritrix(const QList<Line*> &list,
  27. Eigen::MatrixXd &Source_M,
  28. Eigen::MatrixXd &Target_M);
  29. void InitImageMatrix(const cv::Mat &sourceImage,
  30. const int&height,
  31. const int &width,
  32. Eigen::MatrixXd &Image_M);
  33. void CalculateAffineMat(const Eigen::MatrixXd &Source_M,
  34. const Eigen::MatrixXd &Target_M,
  35. const int &N,
  36. cv::Mat &Affine_Mat);
  37. void AffineTranslate(const cv::Mat &srcImage,
  38. const cv::Mat &Affine_Mat,
  39. Eigen::MatrixXd &AfterAffine_M);
  40. void RadioTranslate(const Eigen::MatrixXd &Image_M,
  41. const Eigen::MatrixXd &Source_M,
  42. const Eigen::MatrixXd &Target_M,
  43. const GFuncType &gfunctype,
  44. const cv::Mat &Affine_Mat,
  45. const int &N,
  46. const int &height,
  47. const int &width,
  48. Eigen::MatrixXd &AfterRadio_M);
  49. void RBFTranslate(const Eigen::MatrixXd AfterAffine_M,
  50. const Eigen::MatrixXd AfterRadio_M,
  51. Eigen::MatrixXd Image_M);
  52. void ApplyChangeOnImage(cv::Mat &dstImage,const Eigen::MatrixXd Image_M);
  53. double GetEulaNorm(const Eigen::MatrixXd X_M);//(2*1)--(x,y)^T
  54. private:
  55. cv::Mat srcImage,dstImage,Affine_Mat;
  56. int height,width;
  57. Eigen::MatrixXd Image_M,Source_M,Target_M,AfterAffine_M,AfterRadio_M;
  58. GFuncType gfunctype;
  59. int N;//number of Anchor Points
  60. QList<Line*> lineList;
  61. };
  • Line.h
 
 
  1. #ifndef LINE_H
  2. #define LINE_H
  3. #include<QPoint>
  4. class Line
  5. {
  6. public:
  7. Line();
  8. Line(QPoint start,QPoint end);
  9. void SetStart(QPoint start);
  10. void SetEnd(QPoint End);
  11. QPoint end,start;
  12. private:
  13. };
  14. #endif // LINE_H




转载于:https://www.cnblogs.com/LingjieLi-vera/p/5941563.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是自行实现RANSAC算法和image warping算法的代码: ```python import cv2 import numpy as np # 图片路径 img_paths = ['img1.jpg', 'img2.jpg', 'img3.jpg'] # 读取图片 imgs = [cv2.imread(path) for path in img_paths] # 定义SIFT特征点检测器 sift = cv2.xfeatures2d.SIFT_create() # 定义匹配器 bf = cv2.BFMatcher(cv2.NORM_L2, crossCheck=True) # 匹配点对数量阈值 MIN_MATCH_COUNT = 10 # RANSAC算法参数 RANSAC_THRESH = 5 RANSAC_MAX_ITER = 2000 RANSAC_INLIERS_RATIO = 0.5 # 图像拼接参数 OFFSET = 500 # 循环处理每张非基准图像 for i in range(1, len(imgs)): # 特征点检测和匹配 kp1, des1 = sift.detectAndCompute(imgs[i-1], None) kp2, des2 = sift.detectAndCompute(imgs[i], None) matches = bf.match(des1, des2) # 筛选匹配点对 if len(matches) < MIN_MATCH_COUNT: continue # 将匹配点对转换为数组形式 pts1 = np.float32([kp1[m.queryIdx].pt for m in matches]).reshape(-1, 2) pts2 = np.float32([kp2[m.trainIdx].pt for m in matches]).reshape(-1, 2) # RANSAC算法 best_M = None best_inliers = -1 for j in range(RANSAC_MAX_ITER): # 随机采样得到4个点 indices = np.random.randint(0, len(matches), 4) src_pts = pts1[indices] dst_pts = pts2[indices] # 求解变换矩阵 M = cv2.getPerspectiveTransform(src_pts, dst_pts) # 计算变换后的点 dst_pts_transformed = cv2.perspectiveTransform(pts1.reshape(-1, 1, 2), M).reshape(-1, 2) # 计算误差 errors = np.linalg.norm(pts2 - dst_pts_transformed, axis=1) # 统计内点数 inliers = np.sum(errors < RANSAC_THRESH) # 更新最优解 if inliers > best_inliers: best_M = M best_inliers = inliers # 判断是否达到终止条件 if inliers / len(matches) > RANSAC_INLIERS_RATIO: break # 图像配准和image warping h1, w1 = imgs[i-1].shape[:2] h2, w2 = imgs[i].shape[:2] corners1 = np.float32([[0, 0], [0, h1], [w1, h1], [w1, 0]]).reshape(-1, 1, 2) corners2 = np.float32([[0, 0], [0, h2], [w2, h2], [w2, 0]]).reshape(-1, 1, 2) corners2_transformed = cv2.perspectiveTransform(corners2, best_M) corners = np.concatenate((corners1, corners2_transformed), axis=0) [xmin, ymin] = np.int32(corners.min(axis=0).ravel() - OFFSET) [xmax, ymax] = np.int32(corners.max(axis=0).ravel() + OFFSET) t = [-xmin, -ymin] Ht = np.array([[1, 0, t[0]], [0, 1, t[1]], [0, 0, 1]]) img1_transformed = cv2.warpPerspective(imgs[i-1], Ht.dot(best_M), (xmax-xmin, ymax-ymin)) img2_transformed = cv2.warpPerspective(imgs[i], Ht, (xmax-xmin, ymax-ymin)) # 图像拼接 img1_transformed[t[1]:h1+t[1], t[0]:w1+t[0]] = imgs[i-1] img2_transformed[t[1]:h2+t[1], t[0]:w2+t[0]] = imgs[i] base_img = np.zeros((ymax-ymin, xmax-xmin, 3), np.uint8) base_img[t[1]:h1+t[1], t[0]:w1+t[0]] = imgs[i-1] for j in range(t[1], h2+t[1]): for k in range(t[0], w2+t[0]): if np.sum(img2_transformed[j, k]) > 0: base_img[j, k] = img2_transformed[j, k] # 保存拼接后的图像 cv2.imwrite('result.jpg', base_img) ``` 需要注意的是,上述代码实现中的RANSAC算法和image warping算法是根据论文和算法描述实现的,可能与OpenCV库函数的实现略有不同。其中,RANSAC算法使用的是随机采样一致性算法(Random Sample Consensus, RANSAC),而image warping算法使用的是透视变换(Perspective Transformation)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值