OpenCV之薄板样条插值(ThinPlateSpline)

官方文档:OpenCV: cv::ThinPlateSplineShapeTransformer Class Reference

使用方法:

头文件:#include <opencv2/shape/shape_transformer.hpp>

复制代码
(1)点匹配
一般根据有多少个样本(或者点)来设置,如有M个点,并且下标相同的点是对应点,那么其匹配设置:
复制代码
std::vector<cv::DMatch> matches;
for(int i=0;i < M; ++i)
    matches.append(cv::DMatch(i, i, 0))

(2)计算转换矩阵

复制代码
//源点及目标点(请自行填充)
vector<Point2f> _dstPts,_srcPts;

//获取转换矩阵
cv::Ptr<cv::ThinPlateSplineShapeTransformer> tps
tps = cv::createThinPlateSplineShapeTransformer(0);
tps->estimateTransformation(_dstPts, _srcPts,matches);

(3)获取任意点的转换值

复制代码
//_in_pts--输入值,_out_pts--输出值
vector<Point2f> _in_pts,_out_pts;
_in_pts.push_back(Point2f(0,0));//只举了两个值
_in_pts.push_back(Point2f(1,1));
tps->applyTransformation(_in_pts,_out_pts);

(4)图像变换

图像变换与多点变换相同,注意estimateTransformation只接受输入为(1,m,2)的tuple,不然就会报错,因此如果是图像需要做以下变换:

img = img.reshape(1, -1, 2)

estimatetransformation的说明文档的输入顺序如下:source_points,target_points,matches. 但开发者搞反了输入顺序,如果对图像变换,source 和 target 点应该互换,但对浮动图像选的点做变换又是正确的(崩溃)参考这个opencv的issue:

https://github.com/opencv/opencv/issues/7084

相关推荐
视觉AI20 小时前
一帧就能“训练”的目标跟踪算法:通俗理解 KCF 的训练机制
人工智能·算法·目标跟踪
MediaTea21 小时前
Python 第三方库:PyTorch(动态计算图的深度学习框架)
开发语言·人工智能·pytorch·python·深度学习
Code884821 小时前
观察Springboot AI-Function Tools 执行过程
人工智能·spring boot·后端
kyle-fang21 小时前
pytorch-张量转换
人工智能·pytorch·python
甄心爱学习21 小时前
计算机视觉11-相机模型与多视几何
人工智能·数码相机·计算机视觉
qunshankeji21 小时前
草莓病害智能识别与分类_Cascade-RCNN_HRNetV2p-W18-20e_COCO实现
人工智能·数据挖掘
CloudWeGo21 小时前
用 Eino ADK 构建你的第一个 AI 智能体:从 Excel Agent 实战开始
人工智能·开源·github
Blossom.11821 小时前
AI Agent记忆系统深度实现:从短期记忆到长期人格的演进
人工智能·python·深度学习·算法·决策树·机器学习·copilot
晞微1 天前
实战|SpringBoot+Vue3 医院智能预约挂号系统(含 AI 助手)
人工智能·spring boot·后端