-
Notifications
You must be signed in to change notification settings - Fork 2
/
HUtoRGB.py
85 lines (65 loc) · 2.99 KB
/
HUtoRGB.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""
Performs CT scan attenuation rescale on a folder of images.
Primarily aimed at converting 16bit tiff images from the Talisman Test Suite to RGB jpgs.
"""
import cv2
import os
import numpy as np
def map_HU_to_RGB(value, minHU, maxHU):
''' Rescales Hounsfield unit to the range [0,255] using linear transformation.
Inputs:
value: Hounsfield unit (integer)
minHU: Hounsfield unit value to map to 0 (integer)
maxHU: Hounsfield unit value to map to 255 (integer)
Output: Integer in range [0,255]
'''
if value < minHU:
newValue = 0
elif value > maxHU:
newValue = 255
else:
spanHU = maxHU - minHU
valueScaled = float(value - minHU) / float(spanHU)
newValue = int((valueScaled * 255))
return newValue
v_map_HU_to_RGB = np.vectorize(map_HU_to_RGB) # allow map_HU_to_RGB to be used pointwise on numpy array
def convert_image(inputFilePath, outputFilePath, minR, maxR, minG, maxG, minB, maxB):
''' Converts 16bit grayscale tiff image to 8bit rgb jpg.
Input:
inputFilePath: path to image to be converted
outputFilePath: location to save converted image, with new filename
min*: Hounsfield unit value to map to 0 (integer), * refers to color channel (RGB)
max*: Hounsfield unit value to map to 255 (integer), * refers to color channel (RGB)
'''
HUImage = cv2.imread(inputFilePath, -1)
HUImage = HUImage.astype(np.int16)
red = v_map_HU_to_RGB(HUImage, minR, maxR)
green = v_map_HU_to_RGB(HUImage, minG, maxG)
blue = v_map_HU_to_RGB(HUImage, minB, maxB)
RGBImage = np.stack((blue, green, red), axis=-1) # opencv expects blue first
# print(RGBImage.shape)
# print(RGBImage.dtype)
# print(type(RGBImage))
cv2.imwrite(outputFilePath, RGBImage, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
#minR, maxR, minG, maxG, minB, maxB = -1400, -950, -1400, -200, -160, 240
minR, maxR, minG, maxG, minB, maxB = -1400, -853, -852, -306, -305, 240
#minR, maxR, minG, maxG, minB, maxB = -1000, -600, -601, -200, -201, 200
#minR, maxR, minG, maxG, minB, maxB = -1000, 200, -1000, 200, -1000, 200
#inputFilePath = '../ILD_medgift/ILD_DB_talismanTestSuite/emphysema_patch1000_patient-1_7.tif'
#outputFilePath = './even-split_-1000to200.jpg'
#convert_image(inputFilePath, outputFilePath, minR, maxR, minG, maxG, minB, maxB)
inputDir = './ILD_DB_talismanTestSuite/'
outputDir = './converted2/'
if not os.path.exists(outputDir):
os.makedirs(outputDir)
fileNames = os.listdir(inputDir)
print("R: [", minR, ", ", maxR, "] HU -> [0, 255]", sep='')
print("G: [", minG, ", ", maxG, "] HU -> [0, 255]", sep='')
print("B: [", minB, ", ", maxB, "] HU -> [0, 255]", sep='')
print("Converting images to RGB...")
for fileName in fileNames:
if fileName.endswith(".tif"):
inputFilePath = inputDir + fileName
outputFilePath = outputDir + os.path.splitext(fileName)[0] + ".jpg"
convert_image(inputFilePath, outputFilePath, minR, maxR, minG, maxG, minB, maxB)
print("Done!")