-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransformationImageProcessing.py
52 lines (39 loc) · 2.14 KB
/
TransformationImageProcessing.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
# Author: Keshava Prasad Gubbi
# For any questions: Contact [email protected]
# Script to perform General Image processing on General 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.
# TODO: Rewrite the file also as a tiff file with same metadata.
import os
import nrrd
import cv2 as cv
outpath = r'C:/Users/keshavgubbi/Desktop/nifti/standard_output/'
inpath = r'C:/Users/keshavgubbi/Desktop/nifti/standard/'
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']
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
def image_to_nrrd(image, header):
return nrrd.write(os.path.join(outpath, f"{name}.nrrd"), image, header=header)
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
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 = read_nrrd_file(file)
# print(header)
name, ext = file.split('.', 1)
processed_image = image_to_nrrd(nrrd_image_array, Header)
print(f'####End of processing {file}!##################')