From 68c684a19f16188fcb48d90ea97f7c30cb55337a Mon Sep 17 00:00:00 2001 From: Andrew Micallef Date: Fri, 15 Feb 2019 11:31:19 +1100 Subject: [PATCH 1/6] allows recognition of traced rois --- ijroi/ijroi.py | 2 +- ijroi/version.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ijroi/ijroi.py b/ijroi/ijroi.py index 9037096..aa2468f 100644 --- a/ijroi/ijroi.py +++ b/ijroi/ijroi.py @@ -72,7 +72,7 @@ def getfloat(): # Discard second Byte: get8() - if roi_type not in [RoiType.FREEHAND, RoiType.POLYGON, RoiType.RECT, RoiType.POINT]: + 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) top = get16() diff --git a/ijroi/version.py b/ijroi/version.py index 717870e..7e602ea 100644 --- a/ijroi/version.py +++ b/ijroi/version.py @@ -1 +1 @@ -__version__ = '0.4.1.dev0' +__version__ = '0.4.1.dev1' From 0ff79e3d1bd20bb303d2014cb70a022dc5fa70b2 Mon Sep 17 00:00:00 2001 From: Andrew Micallef Date: Mon, 18 Feb 2019 10:48:05 +1100 Subject: [PATCH 2/6] updated subpixelresolution check --- ijroi/ijroi.py | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/ijroi/ijroi.py b/ijroi/ijroi.py index aa2468f..c8b0581 100644 --- a/ijroi/ijroi.py +++ b/ijroi/ijroi.py @@ -62,6 +62,9 @@ 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') @@ -71,10 +74,7 @@ def getfloat(): roi_type = get8() # Discard second Byte: get8() - - 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) - + top = get16() left = get16() bottom = get16() @@ -89,8 +89,7 @@ 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() @@ -98,8 +97,23 @@ def getfloat(): position = get32() header2offset = get32() + # End Header data + #=========================================================================== + + #RoiDecoder.java#L177 + 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) @@ -108,7 +122,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) @@ -119,7 +133,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 subPixelResolution == 0: points[:, 1] += left points[:, 0] += top From 1268666ec4d7c65be48f4fc7e9cd6506a4a9f867 Mon Sep 17 00:00:00 2001 From: Andrew Micallef Date: Mon, 18 Feb 2019 14:13:31 +1100 Subject: [PATCH 3/6] Update version.py --- ijroi/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ijroi/version.py b/ijroi/version.py index 7e602ea..717870e 100644 --- a/ijroi/version.py +++ b/ijroi/version.py @@ -1 +1 @@ -__version__ = '0.4.1.dev1' +__version__ = '0.4.1.dev0' From 1e6e944c7e40650711ccd0f26c157fd3c1230354 Mon Sep 17 00:00:00 2001 From: Andrew Micallef Date: Mon, 18 Feb 2019 14:14:09 +1100 Subject: [PATCH 4/6] Update ijroi.py --- ijroi/ijroi.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ijroi/ijroi.py b/ijroi/ijroi.py index c8b0581..b3340a5 100644 --- a/ijroi/ijroi.py +++ b/ijroi/ijroi.py @@ -66,8 +66,6 @@ def getfloat(): #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 @@ -105,13 +103,15 @@ def getfloat(): # Check exceptions + if magic != b'Iout': + raise ValueError('Magic number not found') + 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 subPixelResolution: return np.array( @@ -133,7 +133,7 @@ def getfloat(): points[:, 1] = [getc() for i in range(n_coordinates)] points[:, 0] = [getc() for i in range(n_coordinates)] - if subPixelResolution == 0: + if not subPixelResolution: points[:, 1] += left points[:, 0] += top From 94070a86feac235097fdb2489d31265613fb931c Mon Sep 17 00:00:00 2001 From: Andrew Micallef Date: Mon, 18 Feb 2019 15:12:57 +1100 Subject: [PATCH 5/6] Adds rudimentary support for composite roi parsing --- ijroi/ijroi.py | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/ijroi/ijroi.py b/ijroi/ijroi.py index c8b0581..480634f 100644 --- a/ijroi/ijroi.py +++ b/ijroi/ijroi.py @@ -104,14 +104,47 @@ def getfloat(): 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) - + #Composite ROI + if shape_roi_size > 0: + coords_bytes = file_obj.read() + buffer = np.frombuffer(coords_bytes, dtype='>f4', count=shape_roi_size) + + segments = [] + + # the number of units to read depens on the type of segement... + # this only works with basic line segemnts (ie type 1, type 2 is quadratic + # and type 3 is cubic + + rc = {4:1, 0:3, 1:3, 2:5, 3:7} + + # scrolls through the buffer one line segment at a time, + # line segments are stored as x,y tuples, I reverse the + # segment, in order to remain consitant with the other return types + i = 0 + these_points = [] + while i < buffer.size: + _type = buffer[i] + read_len = rc[_type] + seg = buffer[i+1:i+read_len] + if seg.size == 0: + segments.append(np.r_[these_points]) + these_points = [] + + else: + these_points.append(seg[::-1]) + + i = i+read_len + + return segments + + if roi_type == RoiType.RECT: if subPixelResolution: return np.array( From c2c8f5bd537d12118944f7058e2a9e6394ad727f Mon Sep 17 00:00:00 2001 From: Andrew Micallef Date: Mon, 18 Feb 2019 15:25:41 +1100 Subject: [PATCH 6/6] Update ijroi.py --- ijroi/ijroi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ijroi/ijroi.py b/ijroi/ijroi.py index e172171..e272658 100644 --- a/ijroi/ijroi.py +++ b/ijroi/ijroi.py @@ -113,7 +113,7 @@ def getfloat(): #Composite ROI if shape_roi_size > 0: - coords_bytes = file_obj.read() + coords_bytes = fileobj.read() buffer = np.frombuffer(coords_bytes, dtype='>f4', count=shape_roi_size) segments = []