Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

morph.py 1.4 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  1. import numpy as np
  2. import cv2
  3. from scipy.spatial import Delaunay
  4. def applyAffineTransform(src, srcTri, dstTri, size) :
  5. warpMat = cv2.getAffineTransform( np.float32(srcTri), np.float32(dstTri) )
  6. return cv2.warpAffine( src, warpMat, (size[0], size[1]), None, flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REFLECT_101 )
  7. def morphTriangle(dst_img, src_img, st, dt) :
  8. (h,w,c) = dst_img.shape
  9. sr = np.array( cv2.boundingRect(np.float32(st)) )
  10. dr = np.array( cv2.boundingRect(np.float32(dt)) )
  11. sRect = st - sr[0:2]
  12. dRect = dt - dr[0:2]
  13. d_mask = np.zeros((dr[3], dr[2], c), dtype = np.float32)
  14. cv2.fillConvexPoly(d_mask, np.int32(dRect), (1.0,)*c, 8, 0);
  15. imgRect = src_img[sr[1]:sr[1] + sr[3], sr[0]:sr[0] + sr[2]]
  16. size = (dr[2], dr[3])
  17. warpImage1 = applyAffineTransform(imgRect, sRect, dRect, size)
  18. if c == 1:
  19. warpImage1 = np.expand_dims( warpImage1, -1 )
  20. dst_img[dr[1]:dr[1]+dr[3], dr[0]:dr[0]+dr[2]] = dst_img[dr[1]:dr[1]+dr[3], dr[0]:dr[0]+dr[2]]*(1-d_mask) + warpImage1 * d_mask
  21. def morph_by_points (image, sp, dp):
  22. if sp.shape != dp.shape:
  23. raise ValueError ('morph_by_points() sp.shape != dp.shape')
  24. (h,w,c) = image.shape
  25. result_image = np.zeros(image.shape, dtype = image.dtype)
  26. for tri in Delaunay(dp).simplices:
  27. morphTriangle(result_image, image, sp[tri], dp[tri])
  28. return result_image
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...