Skip to content

Commit

Permalink
Merge pull request #29 from KitwareMedical/fix-scaling
Browse files Browse the repository at this point in the history
WIP: Fix scaling
  • Loading branch information
brad-t-moore authored Apr 1, 2022
2 parents c82d81f + 3352b3e commit ab68b1d
Show file tree
Hide file tree
Showing 9 changed files with 475 additions and 22 deletions.
2 changes: 1 addition & 1 deletion itkpocus/itkpocus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
'''

__version__ = '0.1.0'
__version__ = '0.2.0'

21 changes: 11 additions & 10 deletions itkpocus/itkpocus/clarius.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
Preprocessing and device-specific IO for the Clarius HD C7.
'''

def _get_rightmost_ruler_from_mask(mask):
def _get_rightmost_ruler_from_mask(mask, tick_dist):
'''
Finds the ruler as long as it is the rightmost thing on the mask.
Parameters
----------
mask : ndarray
MxN where the ruler and other overlay elements are non-zero values
tick_dist : float
Distance between each tick in the ruler in mm
Returns
-------
Expand All @@ -32,7 +34,6 @@ def _get_rightmost_ruler_from_mask(mask):
row_width=5
consistency_threshold = 10 # max number of pixels a ruler step can differ in
outlier_threshold = 0.1
tick_dist = 10.0 # physical distance between ticks in mm

colsum = np.sum(mask, axis=0)
col_peaks, col_props = find_peaks(colsum, prominence=col_prominence)
Expand Down Expand Up @@ -124,7 +125,7 @@ def _get_overlay_mask(npimg, version=None, threshold=250):
mask = dilation(mask, disk(5))
return mask

def preprocess_image(npimg, version=None):
def preprocess_image(npimg, version=None, tick_dist=10):
'''
Calculate physical spacing of image from identified ruler within image and crops to B-mode only.
Expand All @@ -143,13 +144,13 @@ def preprocess_image(npimg, version=None):
mask = _get_overlay_mask(npimg)
npimg_clean = _inpaint(npimg[:,:,0], mask)
crop = _find_crop(npimg_clean)
spacing, ruler_col, ruler_peaks = _get_rightmost_ruler_from_mask(mask)
spacing, ruler_col, ruler_peaks = _get_rightmost_ruler_from_mask(mask, tick_dist=tick_dist)
img = itk.image_from_array(npimg_clean[crop[0,0]:crop[0,1]+1, crop[1,0]:crop[1,1]+1])
img.SetSpacing([spacing, spacing])

return img, { 'spacing' : [spacing, spacing], 'crop' : crop }

def preprocess_video(npvid, framerate=1, version=None):
def preprocess_video(npvid, framerate=1, version=None, tick_dist=10):
'''
npvid : ndarray
video TxMxNx3
Expand All @@ -161,7 +162,7 @@ def preprocess_video(npvid, framerate=1, version=None):
npmean = np.mean(npvid, axis=0)

mask = _get_overlay_mask(npmean)
spacing, ruler_col, ruler_peaks = _get_rightmost_ruler_from_mask(mask)
spacing, ruler_col, ruler_peaks = _get_rightmost_ruler_from_mask(mask, tick_dist=tick_dist)

npmean_clean = _inpaint(npmean[:,:,0], mask)
crop = _find_crop(npmean_clean)
Expand All @@ -176,7 +177,7 @@ def preprocess_video(npvid, framerate=1, version=None):

return vid, { 'spacing' : [spacing, spacing, framerate], 'crop' : crop }

def load_and_preprocess_image(fp, version=None):
def load_and_preprocess_image(fp, version=None, tick_dist=10):
'''
Loads and preprocesses a Clarius image.
Expand All @@ -194,9 +195,9 @@ def load_and_preprocess_image(fp, version=None):
meta : dict
Meta data (includes spacing and crop)
'''
return preprocess_image(itk.imread(fp))
return preprocess_image(itk.imread(fp), tick_dist=tick_dist)

def load_and_preprocess_video(fp, version=None):
def load_and_preprocess_video(fp, version=None, tick_dist=10):
'''
Loads and preprocesses a Clarius video.
Expand All @@ -214,4 +215,4 @@ def load_and_preprocess_video(fp, version=None):
meta : dict
Meta data (includes spacing and crop)
'''
return preprocess_video(skvideo.io.vread(fp), framerate=get_framerate(skvideo.io.ffprobe(fp)))
return preprocess_video(skvideo.io.vread(fp), framerate=get_framerate(skvideo.io.ffprobe(fp)), tick_dist=tick_dist)
Loading

0 comments on commit ab68b1d

Please sign in to comment.