-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinesTransformationImageProcessing.py
69 lines (53 loc) · 2.85 KB
/
LinesTransformationImageProcessing.py
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
# Author: Keshava Prasad Gubbi
# For any questions: Contact [email protected]
# Script to perform Lines- transformation Image processing on both nrrd and tiff files
# after downloadng from cluster
# DONE: Check which file is it and then pass it onto respective read function that processes it.
# DONE: Basic functions ---> convert to 8 bit, enhance contrast, save file into respective format.
# DONE: Preserve and rewrite the processed nrrd files with rest of the metadata same as the original file.
# DONE: Rewrite the file also as a tiff file with same metadata.
import os
import nrrd
import cv2 as cv
import tifffile as tiff
import time
inpath = r'C:/Users/keshavgubbi/Desktop/LinesReg/210607_shhGFP_HuClyntagRFP/manual/transformations_individual'
outpath = 'C:/Users/keshavgubbi/Desktop/nifti/'
def read_nrrd_file(f):
nrrd_image, head = nrrd.read(os.path.join(inpath, f), index_order='F')
width, height, depth = nrrd_image.shape
print(f' The image {file} has dimensions :({width}, {height}) with {depth} '
f'slices')
datatype = head['type']
voxel_x, voxel_y = head['space directions'][0][0], head['space directions'][1][1]
voxel_size_list = [voxel_x, voxel_y]
# print(voxel_size_list)
print(f'The Image has dtype: {datatype}. Converting into a int8 (8 bit) image!')
ce_image = contrast_enhancement(nrrd_image)
return ce_image.astype('uint8'), head, voxel_size_list
def image_to_nrrd(image, header):
return nrrd.write(os.path.join(outpath, f"{name}.nrrd"), image, header=header)
def image_to_tiff(image):
print(f'Creating file {name}.tif')
return tiff.imwrite(os.path.join(outpath, f"{name}.tif"), image,
metadata={'spacing': ['1./VoxelSizeList[0]', '1./VoxelSizeList[0]', '1'], 'unit': 'um',
'axes ': 'ZYX', 'imagej': 'True'})
def contrast_enhancement(f):
alpha = 3.0 # Contrast control (1.0-3.0) but 3 is required for my purposes here
beta = 1 # Brightness control (0-100). Not to be added beyond 5, to not hamper the signal with salt and pepper
# noise.
contrast_enhanced_image = cv.convertScaleAbs(f, alpha=alpha, beta=beta)
return contrast_enhanced_image
start = time.time()
for file in os.listdir(inpath):
if file.endswith('.nrrd'):
print(f'Working with Image: {file}')
# Read the nrrd file and convert it into a 8-bit numpy array after contrast enhancement
nrrd_image_array, Header, VoxelSizeList = read_nrrd_file(file)
# print(Header)
name, ext = file.split('.', 1)
processed_nrrd_image = image_to_nrrd(nrrd_image_array, Header)
processed_tiff_image = image_to_tiff(nrrd_image_array.transpose(2, 1, 0))
print(f'####End of processing {file}!##################')
end = time.time()
print(end - start, 'secs')