We're Hiring!

Read in MATLAB ROIs from CZI file

Historical discussions about the Bio-Formats library. Please look for and ask new questions at https://forum.image.sc/tags/bio-formats
Please note:
Historical discussions about the Bio-Formats library. Please look for and ask new questions at https://forum.image.sc/tags/bio-formats

If you are having trouble with image files, there is information about reporting bugs in the Bio-Formats documentation. Please send us the data and let us know what version of Bio-Formats you are using. For issues with your code, please provide a link to a public repository, ideally GitHub.

Read in MATLAB ROIs from CZI file

Postby espezi » Tue Aug 28, 2018 11:25 am

Dear community,

we are new to bio-format so the answer to this question may be trivial, but I could not find to date an example of how to import in MATLAB ROIs from a CZI file.

We have several CZI files where ROIs were defined using the ZEN application. The ROIs are visible in ZEN and can also successfully imported and displayed in OMERO. With MATLAB we can read the images fine but we cannot find a way to import the ROIs as a set of points in the coordinate system of the image. Does anyone have an example script showing how this can be done in MATLAB?

We went as far as using the r.getMetadataStore().getROICount command (reading the bfopen.m code) but cannot get the points coordinates.

Any help would be very much appreciated.

Kind regards,

Emiliano
espezi
 
Posts: 4
Joined: Mon Aug 27, 2018 5:45 pm

Re: Read in MATLAB ROIs from CZI file

Postby dgault » Wed Aug 29, 2018 9:45 am

Hi Emiliano,

There is a utility function which can parse through and print out ROI's (https://github.com/openmicroscopy/biofo ... tROIs.java). Although it doesn't return the coordinates you can use the same logic as it does to print them.

First it retrieves the ROICount as you have done and loops through all the ROI's: https://github.com/openmicroscopy/biofo ... s.java#L56

Next it uses getShapeCount to return the number of shapes in a particular ROI: https://github.com/openmicroscopy/biofo ... s.java#L88

You can then get a Union of all these shapes and loop through each shape: https://github.com/openmicroscopy/biofo ... s.java#L96

Lastly you will have to check what type each shape (https://github.com/openmicroscopy/biofo ... .java#L107) and access the relevant points (for example a Point shape has X, Y and text https://github.com/openmicroscopy/biofo ... .java#L128 while an Ellipse will also have RadiusX and RadiusY https://github.com/openmicroscopy/biofo ... .java#L109)

Hopefully you can reuse a lot of this logic in order to access the ROI coordinates needed

David Gault
User avatar
dgault
Team Member
 
Posts: 208
Joined: Fri Aug 14, 2015 2:56 pm

Re: Read in MATLAB ROIs from CZI file

Postby espezi » Wed Aug 29, 2018 11:03 am

Thanks very much for your reply David.

I understand the logic and could try to find a way of calling the java code you pointed out from MATLAB, but I wonder if there is an example of code written in the MATLAB language.

Kind regards,

Emiliano
espezi
 
Posts: 4
Joined: Mon Aug 27, 2018 5:45 pm

Re: Read in MATLAB ROIs from CZI file

Postby dgault » Thu Aug 30, 2018 3:07 pm

Im afraid we don't have a MATLAB example of this specific ROI access, however it would look something very similar to the below code:

Code: Select all

omexml = r.getMetadataStore();
roiCount = omexml.getROICount();

for roi = 1:roiCount
    int shapeCount = omexml.getShapeCount(roi);
    allShapes = omexml.getRoot().getROI(roi).getUnion();

    for shape = 1:shapeCount
        shapeObject = allShapes.getShape(shape);
        shapeType = omexml.getShapeType(roi, shape);

        if strcmp(shapeType,'Ellipse')
            text = omexml.getEllipseText(roi, shape);
            pointX = omexml.getEllipseX(roi, shape);
            pointY = omexml.getEllipseY(roi, shape);
            radiusX = omexml.getEllipseRadiusX(roi, shape);
            radiusY = omexml.getEllipseRadiusY(roi, shape);
        else if strcmp(shapeType,'Line')
            text = omexml.getLineText(roi, shape);
            pointX1 = omexml.getLineX1(roi, shape);
            pointX2 = omexml.getLineX2(roi, shape);
            pointY1 = omexml.getLineY1(roi, shape);
            pointY2= omexml.getLineY2(roi, shape);
        else if strcmp(shapeType,'Point')
            text = omexml.getPointText(roi, shape);
            pointX = omexml.getPointX(roi, shape);
            pointY = omexml.getPointY(roi, shape);
        else if strcmp(shapeType,'Polyline')
            text = omexml.getPolylineText(roi, shape);
            points = omexml.getPolylinePoints(roi, shape);
        else if strcmp(shapeType,'Polygon')
            text = omexml.getPolygonText(roi, shape);
            points = omexml.getPolygonPoints(roi, shape);
        else if strcmp(shapeType,'Rectangle')
            text = omexml.getRectangleText(roi, shape);
            pointX = omexml.getRectangleX(roi, shape);
            pointY = omexml.getRectangleY(roi, shape);
            width = omexml.getRectangleWidth(roi, shape);
            height = omexml.getRectangleHeight(roi, shape);
        else if strcmp(shapeType,'Label')
            text = omexml.getLabelText(roi, shape);
            pointX = omexml.getLabelX(roi, shape);
            pointY = omexml.getLabelY(roi, shape);
        else if strcmp(shapeType,'Mask')
            text = omexml.getMaskText(roi, shape);
            pointX = omexml.getMaskX(roi, shape);
            pointY = omexml.getMaskY(roi, shape);
            width = omexml.getMaskWidth(roi, shape);
            height = omexml.getMaskHeight(roi, shape);
        end
    end
end

User avatar
dgault
Team Member
 
Posts: 208
Joined: Fri Aug 14, 2015 2:56 pm

Re: Read in MATLAB ROIs from CZI file

Postby espezi » Fri Aug 31, 2018 10:42 am

Thanks this is very similar to what I wrote since the last post. I was able to get the points from the Polygon and the shape looks indeed correct when compared to what I see in the ZEN app. But now I will compare the points from your code and mine.

I am interested in overlaying (and eventually masking) the image with the ROI.
How can the spatial coordinates of the image pixels can be extracted (so that I can plot them together with the ROI an ensure the spatial location is correct)?

This post "https://docs.openmicroscopy.org/bio-formats/5.9.0/developers/matlab-dev.html" explains how to get the physical voxel and stack sizes of the data. But I could not find a mention of the origin of the coordinate system.

Many thanks!

Emiliano
espezi
 
Posts: 4
Joined: Mon Aug 27, 2018 5:45 pm

Re: Read in MATLAB ROIs from CZI file

Postby espezi » Tue Sep 04, 2018 7:25 am

Hi there,

I understand that there is no actual frame or reference but the coordinate points are relative to the upper left corner of the image. I have now the code that reads and masks the image with the ROIs. We shall share it when it's polished and full tested. Many thanks.

Best regards,

Emiliano
espezi
 
Posts: 4
Joined: Mon Aug 27, 2018 5:45 pm


Return to User Discussion [Legacy]

Who is online

Users browsing this forum: No registered users and 1 guest