# load meta data frame from file
data = pd.read_pickle((data_path + 'data.pkl'))
for i in range(len(data)):
print('sample',k+1,'processing file ',i+1,' of ',len(data))
# read + rotate original image
img = cv2.imread((source_path + 'frame_%s.TIFF'%i),0)
img = skimage.transform.rotate(img,data.angle.median(),resize=False)
img_cham_A = cv2.imread((source_path + '00_WT/frame_%s_WT.TIFF'%i),0)
img_cham_B = cv2.imread((source_path + '01_MT/frame_%s_MT.TIFF'%i),0)
img_cham_A = 255 - img_cham_A
img_cham_B = 255 - img_cham_B
# rotate chamber images 90°
img_cham_A = np.array([list(i) for i in zip(*img_cham_A)], dtype=np.int16)
img_cham_B = np.array([list(i) for i in zip(*img_cham_B)], dtype=np.int16)
# cut reference space (+ invert)
refspace = img[data.line3[i]:data.line3[i]+100, 10:len(img[0])-10]
refspace90 = np.array([list(i) for i in zip(*refspace)], dtype=np.int16)
# calculate median intensity of background
background = np.zeros(len(refspace90))
for j in range(len(refspace90)):
background[j] = np.median(refspace90[j])
background = np.array(background,dtype=np.int16)
cham_A_new = np.zeros(img_cham_A.shape, dtype=np.int16)
cham_B_new = np.zeros(img_cham_B.shape, dtype=np.int16)
for l in range(len(background)):
cham_A_new[l] = img_cham_A[l] - background[l]
cham_B_new[l] = img_cham_B[l] - background[l]
# rotate and invert new images back to 0°
cham_A_new = 255 - np.array([list(i) for i in zip(*cham_A_new)], \
dtype=np.int16).clip(0, 255)
cham_B_new = 255 - np.array([list(i) for i in zip(*cham_B_new)], \
dtype=np.int16).clip(0, 255)
cv2.imwrite((target_path[k] + '00_WT/frame_%s_WT.TIFF'%i),cham_A_new)
cv2.imwrite((target_path[k] + '01_MT/frame_%s_MT.TIFF'%i),cham_B_new)