当前位置:   article > 正文

TPS 薄板样条插值 python的opencv实现_cv2.createthinplatesplineshapetransformer()

cv2.createthinplatesplineshapetransformer()

原始论文:


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:


  1. import cv2
  2. import numpy as np
  3. import random
  4. # 首先读入img
  5. img = cv2.imread('data/liv.png',cv2.IMREAD_COLOR)
  6. img = cv2.resize(img,(180,32))
  7. # N对基准控制点
  8. N=5
  9. points=[]
  10. dx=int(180/(N-1))
  11. for i in range(2*N):
  12. points.append((dx*i,4))
  13. points.append((dx*i,36))
  14. # 周围拓宽一圈
  15. img = cv2.copyMakeBorder(img,4,4,0,0,cv2.BORDER_REPLICATE)
  16. # 画上绿色的圆圈
  17. # for point in points:
  18. # cv2.circle(img, point, 1, (0, 255, 0), 2)
  19. tps = cv2.createThinPlateSplineShapeTransformer()
  20. sourceshape = np.array(points,np.int32)
  21. sourceshape=sourceshape.reshape(1,-1,2)
  22. matches =[]
  23. for i in range(1,N+1):
  24. matches.append(cv2.DMatch(i,i,0))
  25. # 开始随机变动
  26. newpoints=[]
  27. PADDINGSIZ=10
  28. for i in range(N):
  29. nx=points[i][0]+random.randint(0,PADDINGSIZ)-PADDINGSIZ/2
  30. ny=points[i][1]+random.randint(0,PADDINGSIZ)-PADDINGSIZ/2
  31. newpoints.append((nx,ny))
  32. print(points,newpoints)
  33. targetshape = np.array(newpoints,np.int32)
  34. targetshape=targetshape.reshape(1,-1,2)
  35. tps.estimateTransformation(sourceshape,targetshape ,matches)
  36. img=tps.warpImage(img)
  37. 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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/128782
推荐阅读
相关标签
  

闽ICP备14008679号