官方文檔: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