image_utils.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import numpy as np
  2. import torch
  3. import torch.nn as nn
  4. import torch.nn.functional as F
  5. import cv2
  6. import os
  7. import imageio
  8. def dilate(bin_img, ksize=5):
  9. # bin_img
  10. pad = (ksize-1)//2
  11. bin_img = F.pad(bin_img, pad=[pad,pad,pad,pad], mode='reflect')
  12. out = F.max_pool2d(bin_img, kernel_size=ksize, stride=1, padding=0)
  13. return out
  14. def erode(bin_img, ksize=5):
  15. out = 1 - dilate(1-bin_img, ksize)
  16. return out
  17. def to8b(x):
  18. return (255*np.clip(x, 0, 1)).astype(np.uint8)
  19. def mse2psnr(x):
  20. return -10. * torch.log(x) / torch.log(torch.Tensor([10.]))
  21. def img2mse(x, y):
  22. return torch.mean((x - y) ** 2)
  23. def video2images(video_name, out_dir):
  24. cap = cv2.VideoCapture(video_name)
  25. frame_num = 0
  26. while(True):
  27. _, frame = cap.read()
  28. if frame is None:
  29. break
  30. out_frame_name = os.path.join(out_dir, str(frame_num) + '.jpg')
  31. cv2.imwrite(out_frame_name, frame)
  32. frame_num += + 1
  33. cap.release()
  34. def load_image_as_uint8_tensor(fname):
  35. """
  36. img: (H, W, 3) floatTensor
  37. """
  38. img = torch.as_tensor(imageio.imread(fname))
  39. return img
  40. if __name__ =='__main__':
  41. video2images("test_data/May_val/AD-NeRF.mp4", "test_data/May_val/AD-NeRF")
  42. video2images("test_data/May_val/GeneFace.mp4", "test_data/May_val/GeneFace")
  43. video2images("test_data/May_val/GT.mp4", "test_data/May_val/GT")