当前位置:   article > 正文

python下使用TPS进行变换_tps transformation

tps transformation

  1. import cv2
  2. import numpy as np
  3. import random
  4. # 首先读入img
  5. img = cv2.imread('0000003.jpg')
  6. label = open('0000003.txt','r',encoding='utf8')
  7. lines = label.readlines()
  8. for line in lines:
  9. points=[]
  10. point_ = line.split(',')[0:-1]
  11. for i in range(len(point_)//2):
  12. points.append((int(point_[2*i]),int(point_[2*i+1])))
  13. x,y,w,h = cv2.boundingRect(np.array(points))
  14. roi_img = img[y:y+h,x:x+w]
  15. cv2.imshow('roi_img',roi_img)
  16. cv2.waitKey()
  17. # 画上绿色的圆圈
  18. for i in range(len(points)):
  19. points[i] = (points[i][0]-x,points[i][1]-y)
  20. for point in points:
  21. cv2.circle(roi_img, point, 1, (0, 255, 0), 2)
  22. cv2.imshow('img',roi_img)
  23. cv2.waitKey()
  24. tps = cv2.createThinPlateSplineShapeTransformer()
  25. sourceshape = np.array(points,np.int32)
  26. sourceshape = sourceshape.reshape(1,-1,2)
  27. matches =[]
  28. N = len(points)
  29. for i in range(0,N):
  30. matches.append(cv2.DMatch(i,i,0))
  31. # 开始变动
  32. newpoints=[]
  33. N = N//2
  34. dx=int(w/(N-1))
  35. for i in range(0,N):
  36. newpoints.append((dx*i,2))
  37. for i in range(N-1,-1,-1):
  38. newpoints.append((dx*i,h-2))
  39. print(points,newpoints)
  40. targetshape = np.array(newpoints,np.int32)
  41. targetshape=targetshape.reshape(1,-1,2)
  42. tps.estimateTransformation(targetshape,sourceshape ,matches)
  43. roi_img_=tps.warpImage(roi_img)
  44. for point in newpoints:
  45. cv2.circle(roi_img_, (int(point[0]),int(point[1])), 1, (0, 0, 255), 2)
  46. cv2.namedWindow('img2',cv2.WINDOW_FREERATIO)
  47. cv2.imshow('img2',roi_img_)
  48. cv2.waitKey()

 

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

闽ICP备14008679号