Skip to content

Commit

Permalink
Handle traced rois (#9)
Browse files Browse the repository at this point in the history
* allows recognition of traced rois

* updated subpixelresolution check
  • Loading branch information
AndrewMicallef authored and tdsmith committed Feb 21, 2019
1 parent 787fca2 commit 611a220
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions ijroi/ijroi.py
Expand Up @@ -62,19 +62,20 @@ def getfloat():
v = np.int32(get32())
return v.view(np.float32)

#===========================================================================
#Read Header data

magic = fileobj.read(4)
if magic != b'Iout':
raise ValueError('Magic number not found')

version = get16()

# It seems that the roi type field occupies 2 Bytes, but only one is used
roi_type = get8()
# Discard second Byte:
get8()

if roi_type not in [RoiType.FREEHAND, RoiType.POLYGON, RoiType.RECT, RoiType.POINT]:
raise NotImplementedError('roireader: ROI type %s not supported' % roi_type)


top = get16()
left = get16()
bottom = get16()
Expand All @@ -89,17 +90,29 @@ def getfloat():
stroke_color = get32()
fill_color = get32()
subtype = get16()
if subtype != 0:
raise NotImplementedError('roireader: ROI subtype %s not supported (!= 0)' % subtype)

options = get16()
arrow_style = get8()
arrow_head_size = get8()
rect_arc_size = get16()
position = get32()
header2offset = get32()

# End Header data
#===========================================================================

#RoiDecoder.java checks the version when setting sub-pixel resolution, therefore so do we
subPixelResolution = ((options&SUB_PIXEL_RESOLUTION)!=0) and (version>=222)

# Check exceptions
if roi_type not in [RoiType.FREEHAND, RoiType.TRACED, RoiType.POLYGON, RoiType.RECT, RoiType.POINT]:
raise NotImplementedError('roireader: ROI type %s not supported' % roi_type)

if subtype != 0:
raise NotImplementedError('roireader: ROI subtype %s not supported (!= 0)' % subtype)

if roi_type == RoiType.RECT:
if options & SUB_PIXEL_RESOLUTION:
if subPixelResolution:
return np.array(
[[y1, x1], [y1, x1+x2], [y1+y2, x1+x2], [y1+y2, x1]],
dtype=np.float32)
Expand All @@ -108,7 +121,7 @@ def getfloat():
[[top, left], [top, right], [bottom, right], [bottom, left]],
dtype=np.int16)

if options & SUB_PIXEL_RESOLUTION:
if subPixelResolution:
getc = getfloat
points = np.empty((n_coordinates, 2), dtype=np.float32)
fileobj.seek(4*n_coordinates, 1)
Expand All @@ -119,7 +132,7 @@ def getfloat():
points[:, 1] = [getc() for i in range(n_coordinates)]
points[:, 0] = [getc() for i in range(n_coordinates)]

if options & SUB_PIXEL_RESOLUTION == 0:
if not subPixelResolution:
points[:, 1] += left
points[:, 0] += top

Expand Down

0 comments on commit 611a220

Please sign in to comment.