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

你可能感兴趣的:(TPS 薄板样条插值 python的opencv实现)