-
Notifications
You must be signed in to change notification settings - Fork 0
/
DicomToRGB
50 lines (43 loc) · 1.67 KB
/
DicomToRGB
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
# import the necessary packages
from imutils import contours
import scipy
from skimage import measure
import numpy as np # numeric library needed
import pandas as pd #for datafrome
import argparse # simple argparser
import imutils
import cv2 # for opencv image recognising tool
import dicom
filename = askopenfilename()
dicom_file = dicom.read_file(filename) ## original dicom File
#### a dicom monochrome-2 file has pixel value between approx -2000 and +2000, opencv doesn't work with it#####
#### in a first step we transform those pixel values in (R,G,B)
### to have gray in RGB, simply give the same values for R,G, and B,
####(0,0,0) will be black, (255,255,255) will be white,
## the threeshold to be automized with a proper quartile function of the pixel distribution
black_threeshold=0###pixel value below 0 will be black,
white_threeshold=1400###pixel value above 1400 will be white
wt=white_threeshold
bt=black_threeshold
def DicomtoRGB(dicomfile,bt,wt):
#load the dicom_file as a numpy pixel array
img=np.array(dicom_file.pixel_array, np.int16)
# enforce boundary conditions
clipped_img = np.clip(img,bt,wt)
# linear transformation
# additive
rgb_add = clipped_img - bt
#multiplicative
rgb_mult = np.multiply(rgb_add,255/(wt-bt))
# round to nearest int
rgb_int = np.around(rgb_mult,0).astype(np.uint8)
# stack thrice on the last axis for R G B
rgb_img = np.stack([rgb_int]*3,axis=-1)
return rgb_img
image=DicomtoRGB(dicom_file,bt=0,wt=1400)
## loading the RGB in a proper opencv format
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
## look at the gray file
cv2.imshow("gray", gray)
cv2.waitKey(0)
cv2.destroyWindow("gray")