TPS 薄板样条插值 python的opencv实现

原始论文:


Donato, G., & Belongie, S. J. (2003). Approximation methods for thin plate spline mappings and principal warps. Department of Computer Science and Engineering, University of California, San Diego.

效果:


原图:

变化后:

Code:


import cv2
import numpy as np
import random
# 首先读入img
img = cv2.imread('data/liv.png',cv2.IMREAD_COLOR)
img = cv2.resize(img,(180,32))
# N对基准控制点
N=5
points=[]
dx=int(180/(N-1))
for i in range(2*N):
    points.append((dx*i,4))
    points.append((dx*i,36))
# 周围拓宽一圈
img = cv2.copyMakeBorder(img,4,4,0,0,cv2.BORDER_REPLICATE)
# 画上绿色的圆圈
# for point in points:
# 	cv2.circle(img, point, 1, (0, 255, 0), 2)
tps = cv2.createThinPlateSplineShapeTransformer()

sourceshape = np.array(points,np.int32)
sourceshape=sourceshape.reshape(1,-1,2)
matches =[]
for i in range(1,N+1):
    matches.append(cv2.DMatch(i,i,0))

# 开始随机变动
newpoints=[]
PADDINGSIZ=10
for i in range(N):
    nx=points[i][0]+random.randint(0,PADDINGSIZ)-PADDINGSIZ/2
    ny=points[i][1]+random.randint(0,PADDINGSIZ)-PADDINGSIZ/2
    newpoints.append((nx,ny))
print(points,newpoints)
targetshape = np.array(newpoints,np.int32)
targetshape=targetshape.reshape(1,-1,2)
tps.estimateTransformation(sourceshape,targetshape ,matches)
img=tps.warpImage(img)
cv2.imwrite('tmp.png',img)

Debug参考:


https://xbuba.com/questions/41536344

https://docs.opencv.org/3.4/df/dfe/classcv_1_1ShapeTransformer.html

https://qiita.com/SousukeShimoyama/items/2bf8defb2d057bb8b742#tps%E3%81%AE%E3%82%A4%E3%83%B3%E3%82%B9%E3%82%BF%E3%83%B3%E3%82%B9%E3%82%92%E7%94%9F%E6%88%90

  • 4
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值