Skip to content

Commit

Permalink
tested the conversion and made an example
Browse files Browse the repository at this point in the history
  • Loading branch information
lext committed Jul 26, 2018
1 parent 57e5d7a commit 50ae013
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
21 changes: 21 additions & 0 deletions Dataset/test_xray_processing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from xray_processor import process_file
import os
import numpy as np

if __name__ == "__main__":
DS_DIR = '/mnt/nas/MOST/Images/XR'
TO_SAVE = 'KL_dataset/'

detections = np.loadtxt('MOST_train.csv', dtype=str)
# Use this file to determine which knee was badly detected
# number indicates the line numer in "the array detections"
bad_detections = set(np.loadtxt('MOST_train_poor_detections.csv',dtype=str).tolist())

for i in range(2):
fname, bbox = detections[i][0], detections[i][1:].astype(int)
# read KL grades for this file somewhere
# ATTENTION: these grades are FAKE and you need to retrieve them depending on the filename
gradeL, gradeR = 5, 5
process_file(i, fname, DS_DIR, TO_SAVE, bbox, gradeL, gradeR)
print(fname, bbox)

19 changes: 8 additions & 11 deletions Dataset/xray_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def read_dicom(filename):
try:
data = dicom.read_file(filename)
img = np.frombuffer(data.PixelData,dtype=np.uint16).copy().astype(np.float64)

if data.PhotometricInterpretation == 'MONOCHROME1':
img = img.max()-img
img = img.reshape((data.Rows,data.Columns))
Expand All @@ -37,7 +36,7 @@ def read_dicom(filename):
except:
return None

def preprocess_xray(img, cut_min=5, cut_max=99, multiplier=255):
def process_xray(img, cut_min=5, cut_max=99, multiplier=255):
"""
This function changes the histogram of the image by doing global contrast normalization
Expand Down Expand Up @@ -70,7 +69,7 @@ def preprocess_xray(img, cut_min=5, cut_max=99, multiplier=255):

return img

def process_file(i, fname, dataset_dir, save_dir, bbox, gradeL, gradeR, pad=300):
def process_file(i, fname, dataset_dir, save_dir, bbox, gradeL, gradeR, sizemm=140, pad=300):
"""
Processes one knee xray and saves left and right images into 16bit png files.
Expand All @@ -90,6 +89,8 @@ def process_file(i, fname, dataset_dir, save_dir, bbox, gradeL, gradeR, pad=300)
KL grade for the left joint.
gradeR : int
KL grade for the right joint.
sizemm : float
Size of the ROI in mm.
pad : int
Padding for the xray image. It is very useful in the case when the knee is too close to the edges.
Expand Down Expand Up @@ -139,10 +140,8 @@ def process_file(i, fname, dataset_dir, save_dir, bbox, gradeL, gradeR, pad=300)
patch = np.round(patch)
patch = patch.astype(np.uint16)

save_dir = os.path.join(dataset_dir, str(gradeL))
os.makedirs(save_dir, exist_ok=True)

name_save = os.path.join(save_dir, f'{i}_{gradeL}_L.png')
os.makedirs(os.path.join(save_dir, str(gradeL)), exist_ok=True)
name_save = os.path.join(save_dir, str(gradeL), f'{i}_{gradeL}_L.png')
cv2.imwrite(name_save, np.fliplr(patch))

if rightok:
Expand All @@ -163,10 +162,8 @@ def process_file(i, fname, dataset_dir, save_dir, bbox, gradeL, gradeR, pad=300)
patch = np.round(patch)
patch = patch.astype(np.uint16)

save_dir = os.path.join(dataset_dir, str(gradeR))
os.makedirs(save_dir, exist_ok=True)

name_save = os.path.join(save_dir, f'{i}_{gradeR}_R.png')
os.makedirs(os.path.join(save_dir, str(gradeR)), exist_ok=True)
name_save = os.path.join(save_dir, str(gradeR), f'{i}_{gradeR}_R.png')
cv2.imwrite(name_save, patch)

return False
Expand Down

0 comments on commit 50ae013

Please sign in to comment.