Advertisement
Guest User

Untitled

a guest
Feb 12th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import pandas as pd
  4. from scipy.ndimage.morphology import binary_dilation
  5.  
  6. #from https://stackoverflow.com/questions/41190852/most-efficient-way-to-forward-fill-nan-values-in-numpy-array
  7. def pandas_fill(arr):
  8. df = pd.DataFrame(arr)
  9. df.fillna(method='ffill', axis=0, inplace=True)
  10. out = df.as_matrix()
  11. return out
  12.  
  13. import os
  14. directory = 'C:\\Users\\Dj_Mi\\Desktop\\Python\\Test Pictures\\1.12 TGI Exp-6.2-5d F0 SH ST50 AM'
  15.  
  16. for filename in os.listdir(directory):
  17. if filename.endswith(".jpg"):
  18. f = open(filename)
  19. im = plt.imread(filename)
  20. mask = np.any(np.concatenate((im[:, :, 0:1] < 50, im[:, :, 1:2] < 45), axis=2), axis=2)
  21. mask = binary_dilation(mask, iterations=10)
  22.  
  23. im2 = im.copy().astype(np.float)
  24. im2[mask, :] = np.nan
  25.  
  26. for i in range(3):
  27. im2[:, :, i] = pandas_fill(im2[:, :, i])
  28. im2 = im2.astype(np.integer)
  29.  
  30. f, ax = plt.subplots(1, figsize=(100, 10))
  31. ax.imshow(im2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement