[aedd99]: / opencvinpaint.py

Download this file

139 lines (99 with data), 3.5 kB

  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
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import numpy as np
import cv2
#from skimage.restoration import inpaint
import glob
import os
import shutil
#This code is provided to show hole filling procces, it will not work without SDU frames in relevant directory
def fill_depth_im(img_path, plot = False):
# Set values equal to or above thresh to 0.
# Set values below thresh to maxval.
img = cv2.imread(img_path,0)
print(np.amax(img), np.amin(img))
# return
thresh,maxval= 20,255
th, im_th = cv2.threshold(img, thresh, maxval, cv2.THRESH_BINARY_INV)
print(np.amax(im_th), np.amin(im_th))
mask = im_th
# cv2.imshow('mask', mask)
# cv2.waitKey(0)
dst = cv2.inpaint(img,mask,3, cv2.INPAINT_NS) #paints non-zero pixels, Try adaptive thresh?
if plot == True:
disp = np.concatenate((dst, img), axis=1)
disp = np.concatenate((disp, mask), axis=1)
cv2.imshow('sdu', disp)
cv2.imwrite('./filling_SDU.png', disp)
#cv2.imshow('sdu',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
return dst
'N:/FallDetection/Fall-Data/SDUFall/train/NonFall/ADL1/Depth'
def fill_SDU_NonFall():
root = './Fall-Data/SDUFall'
train_path = root + '/train/NonFall/ADL*/'
#train_path_depth = train_path + 'Depth'
print(train_path)
ADL_dir_list = glob.glob(train_path)
print(len(ADL_dir_list))
for ADL_dir in ADL_dir_list:
frames = glob.glob(ADL_dir + '/Depth/*.png')
print(len(frames))
#print("\n".join(frames)) #Use this to check if sorted
frames = sort_frames(frames, 'SDU')
#print("\n".join(frames)) #Use this to check if sorted
save_path = ADL_dir + '/Filled'
if os.path.isdir(save_path):
assert 'Filled' in save_path
if not 'Filled' in save_path:
print('trying to remove dir which is not Filled')
shutil.rmtree(save_path)
os.mkdir(save_path)
print(save_path)
for frame in frames:
frame_filled = fill_depth_im(frame)
#print(frame)
frame_base = os.path.basename(frame)
#print(frame_base)
save_path_fr = save_path + '/' + frame_base
print(save_path_fr)
save_path_fr.replace('\\','/')
cv2.imwrite(save_path_fr, frame_filled)
#break
break
def fill_SDU_Fall():
root = 'N:/FallDetection/Fall-Data/SDUFall/sorted_by_person/Fall'
test_path_F = root + '/Fall*/'
#train_path_depth = train_path + 'Depth'
print(test_path_F)
Fall_dir_list = glob.glob(test_path_F)
print(len(Fall_dir_list))
for Fall_dir in Fall_dir_list:
frames = glob.glob(Fall_dir + '/Depth/*.png')
print(len(frames))
#print("\n".join(frames)) #Use this to check if sorted
#frames = sort_frames(frames, 'SDU')
#print("\n".join(frames)) #Use this to check if sorted
save_path = Fall_dir + '/Filled'
if os.path.isdir(save_path):
assert 'Filled' in save_path
if not 'Filled' in save_path:
print('trying to remove dir which is not Filled')
shutil.rmtree(save_path)
os.mkdir(save_path)
print(save_path)
for frame in frames:
frame_filled = fill_depth_im(frame)
#print(frame)
frame_base = os.path.basename(frame)
#print(frame_base)
save_path_fr = save_path + '/' + frame_base
print(save_path_fr)
save_path_fr.replace('\\','/')
cv2.imwrite(save_path_fr, frame_filled)
#break
if __name__ == "__main__":
fill_SDU_Fall()
sdu = 'N:/FallDetection/Fall-Data/SDUFall/test/Fall/Fall18/Depth/0080.png'
# sdu = 'N:/FallDetection/Fall-Data/SDUFall/train/NonFall/ADL1/Depth/0020.png'.format(i)
# TST = 'N:/FallDetection/Fall-Data/TST_Kinect_V2/Reorganized/train/NonFall/ADL1/Filedepth_{}.png'.format(i)
#dst = fill_depth_im(sdu, True)