kushalkolar / MESmerize

Platform for Calcium Imaging analysis. DEPRECATED.
GNU General Public License v3.0
58 stars 14 forks source link

Import ROI ImageJ error #63

Closed pr4deepr closed 2 years ago

pr4deepr commented 2 years ago

Hi

Describe the bug When importing ImageJ ROIs into MESmerize, it does not read ROIs created by different drawing tools (oval and rectangle) in ImageJ. It recognises freehand ROIs.

To Reproduce Go to Viewer -> ROI Manager -> Import ROI Manager (attached)

Expected behavior Ideally, It should import all ROIs regardless of size/type.

Error image

I have fixed it by modifying the function import_from_imagej in managers.py. Not the most elegant code, but it works.

Attached ImageJ ROI file for testing. roi_test.zip

    def import_from_imagej(self, path: str):
        """
        Uses read-roi package created by Hadrien Mary.
        https://pypi.org/project/read-roi/

        :param path: Full path to the ImageJ ROIs zip file
        """
        ij_roi = read_imagej(path)
        for k in ij_roi.keys():
            if ij_roi[k]['type'] in ('oval', 'rectangle'):
                width=ij_roi[k]['width']
                height=ij_roi[k]['height']
                left=ij_roi[k]['left']
                top=ij_roi[k]['top']
                x_center=left+width/2
                y_center=top+height/2
                if ij_roi[k]['type'] == 'oval':
                    shape=Ellipse((x_center,y_center),width,height)
                    vertices_orig=shape.get_path().vertices #get all vertices  
                    vertices = shape.get_patch_transform().transform(vertices_orig)
                    ps=[tuple(x) for x in vertices]
                else:
                    ps=list(itertools.product((left,left+width),(top,top+height)))
                    ps[2],ps[3]=ps[3],ps[2] #swap coordinates so it draws coordinates in sequence, must be a better way to do this
            else:
                xs = ij_roi[k]['x']
                ys = ij_roi[k]['y']
                ps = list(zip(xs, ys))

            roi = ManualROI.from_positions(positions=ps,
                                                curve_plot_item=self.get_plot_item(),
                                                view_box=self.vi.viewer.getView())
            self.roi_list.append(roi)
        self.roi_list.reindex_colormap()

Operating System & specs (CPU, RAM etc.). Please complete the following information:

Details about your Mesmerize install

Cheers Pradeep

kushalkolar commented 2 years ago

This is more of a feature request, see #64

kushalkolar commented 2 years ago

fixed in #68