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

equalize_and_stack_square.py 1.3 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
38
39
40
41
42
43
44
45
  1. import numpy as np
  2. import cv2
  3. def equalize_and_stack_square (images, axis=1):
  4. max_c = max ([ 1 if len(image.shape) == 2 else image.shape[2] for image in images ] )
  5. target_wh = 99999
  6. for i,image in enumerate(images):
  7. if len(image.shape) == 2:
  8. h,w = image.shape
  9. c = 1
  10. else:
  11. h,w,c = image.shape
  12. if h < target_wh:
  13. target_wh = h
  14. if w < target_wh:
  15. target_wh = w
  16. for i,image in enumerate(images):
  17. if len(image.shape) == 2:
  18. h,w = image.shape
  19. c = 1
  20. else:
  21. h,w,c = image.shape
  22. if c < max_c:
  23. if c == 1:
  24. if len(image.shape) == 2:
  25. image = np.expand_dims ( image, -1 )
  26. image = np.concatenate ( (image,)*max_c, -1 )
  27. elif c == 2: #GA
  28. image = np.expand_dims ( image[...,0], -1 )
  29. image = np.concatenate ( (image,)*max_c, -1 )
  30. else:
  31. image = np.concatenate ( (image, np.ones((h,w,max_c - c))), -1 )
  32. if h != target_wh or w != target_wh:
  33. image = cv2.resize ( image, (target_wh, target_wh) )
  34. h,w,c = image.shape
  35. images[i] = image
  36. return np.concatenate ( images, axis = 1 )
Tip!

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

Comments

Loading...