hadim / read-roi

Read ROI files .zip or .roi generated with ImageJ.
BSD 3-Clause "New" or "Revised" License
51 stars 22 forks source link

Polygon and freehand ROIs (partly) read in incorrectly #7

Closed jlause closed 4 years ago

jlause commented 5 years ago

Hey, I have been using your function to read cell outlines marked in ImageJ into my python pipeline without problems for a year now. Now I moved to considerably larger images (10.000 x 10.000 pixels, before 1.000 x 1.000 pixels). I marked a couple of ROIs, saved as zip, and used the read_roi_zip() function to read. I got a weird pattern of errors:

-Some polygon ROIs are read in correctly. -For some polygon ROIs, all x coordinates have a large negative offset (ca -60.000), while the y is correct -With the freehand ROIs, I observed all four cases: correct ROIs, large negative offsets in x while y is ok, the reverse, and both x and y with large negative offsets.

I also found out that the following function from another github user works correctly on the same ROI file: https://github.com/tdsmith/ijroi

I attach my ROI file, a screenshot of the correct ROIs in Image J and the incorrectly read-in ROIs plotted in python.

image First five coordinates of the read in ROIs (ID refers to legend in above figure). Note that for all plots, I inverted the y coordinate to have the ROIs oriented as they are on the ImageJ screenshot below.

0 x [2104, 2302, 2573, 2729, 2833] 
  y [6647, 6605, 6428, 6251, 6167]
1 x [4709, 4063, 3875, 3844, 3646] 
  y [5073, 4751, 4615, 4417, 4313]
2 x [5605, 6032, 6345, 6522, 6741] 
  y [5521, 5834, 6032, 6188, 6428]
3 x [5188, 5199, 5167, 5240, 5334] 
  y [4928, 4844, 4761, 4709, 4709]
4 x [4844, 4678, 4553, 4553, 4542] 
  y [9126, 8897, 8720, 8616, 8397]
5 x [-57316, -57128, -57087, -57149, -57285] 
  y [4230, 4323, 4177, 4042, 4021]
6 x [-59066, -59108, -59108, -59108, -59108] 
  y [3334, 3365, 3386, 3396, 3406]
7 x [1583, 1541, 1531, 1521, 1510] 
  y [3907, 3907, 3907, 3907, 3907]
8 x [2062, 2104, 2115, 2115, 2135] 
  y [-57649, -57670, -57681, -57691, -57733]
9 x [-57431, -57472, -57493, -57504, -57514] 
  y [-57076, -57087, -57128, -57139, -57149]

image image image

Original ROIs in imageJ:

image

My code for plotting/reading in:

import original_read_roi
import matplotlib.pyplot as plt
import numpy as np
a = original_read_roi.read_roi_zip('hajdu_20180205_0_RBPMS_referenceROIs_day2_nozoom.zip')

for i,k in enumerate(list(a.keys())):
    plt.plot(np.array(a[k]['x']),-(np.array(a[k]['y'])),label=str(i))
    print(i,'x',a[k]['x'][:5],'\n  y',a[k]['y'][:5])

plt.title('all rois')
plt.legend(loc=6)
plt.show()

plt.figure(figsize=(10,10))

for i,k in enumerate(list(a.keys())):

    if a[k]['type']=='polygon':
        plt.plot(a[k]['x'],-(np.array(a[k]['y'])))
plt.title('polygon rois, uncorrected')
plt.show()
#
plt.figure(figsize=(10,10))

for i,k in enumerate(list(a.keys())):

    if a[k]['type']=='polygon':
        if a[k]['name']=='00005-04172-08334':
            plt.plot(np.array(a[k]['x'])+65307,-(np.array(a[k]['y'])),label='corrected outlier (added 65307 to y coordinate)')
        else:
            plt.plot(a[k]['x'],-(np.array(a[k]['y'])))
plt.title('polygon rois, corrected')
plt.legend()
plt.show()

for i,k in enumerate(list(a.keys())):

    if a[k]['type']=='freehand':

        plt.plot(np.array(a[k]['x']),-(np.array(a[k]['y'])),label=str(i))
        plt.title('only freehand rois')
plt.legend()
plt.show()

The ROI file used to obtain the above plots: hajdu_20180205_0_RBPMS_referenceROIs_day2_nozoom.zip

I hope this helps to improve your code! Cheers, Jan.

jtmahoney commented 5 years ago

I am having a similar issue with both versions 1.4.2 and 1.5.0. Here is an image of bounding boxes plotted on the original image (the y-axis is inverted in comparison to ImageJ). As you can see, when the roi value goes above ~4500 read_roi (both zipped and unzipped) returns wildly large numbers.... suspiciously close to 65000...

capture

To me; it appears that some sort of bit-wise function in read_roi is getting its bits confused and converting from 12bit to 16bit depths (4096 vs 65536). I never learned binary (zzzzzzzz) so I dont have a solution for this other than adding 65536 to that super negative number (like -59043).

Hope this helps yall find a fix.

-JT

hadim commented 5 years ago

Hello both and thank you for the report. I don't have time to look into that in the short term. If you have an idea what is going on please feel free to submit a PR. Thank you!

OSchoppe commented 5 years ago

Here is how to get rid of the unexpected large negative offsets:

This is caused by a part of the code for which I did not understand the intention (in _read_roi.py around line 140):

if top > 6000:
    top -= 2**16
if left > 6000:
    left -= 2**16

Removing those lines solved the issue for me; I did not notice any other issues that would be introduced by removing those lines.

Hope this helps!

hadim commented 5 years ago

Thank you @OSchoppe. I'll have a look at any PRs someone could submit with this fix.

YubinXie commented 5 years ago

I have similar issues as attached. @OSchoppe I did not find the code part you mentioned in 1.5.2. Have it been fixed? I still have this issue though.

9DFE76BC-6A62-481C-B00D-162D24162F89

hadim commented 4 years ago

Fixed!