It reads the OME-XML file and converts the ROIs to a list of ROI objects.
10838 {
10839 List<ROI> Annotations = new List<ROI>();
10840
10841 ServiceFactory factory = new ServiceFactory();
10842 OMEXMLService service = (OMEXMLService)factory.getInstance(typeof(OMEXMLService));
10843 loci.formats.ome.OMEXMLMetadata meta = service.createOMEXMLMetadata();
10844
10845 ImageReader imageReader = new ImageReader();
10846 imageReader.setMetadataStore(meta);
10847 imageReader.setOriginalMetadataPopulated(true);
10848 imageReader.setMetadataFiltered(false);
10849
10850 file = file.Replace("\\", "/");
10851 imageReader.setId(file);
10852 int imageCount = imageReader.getImageCount();
10853 int seriesCount = imageReader.getSeriesCount();
10854 double physicalSizeX = 0;
10855 double physicalSizeY = 0;
10856 double physicalSizeZ = 0;
10857 double stageSizeX = 0;
10858 double stageSizeY = 0;
10859 double stageSizeZ = 0;
10860 int SizeX = imageReader.getSizeX();
10861 int SizeY = imageReader.getSizeY();
10862 int SizeZ = imageReader.getSizeY();
10863 try
10864 {
10865 bool hasPhysical = false;
10866 if (meta.getPixelsPhysicalSizeX(series) != null)
10867 {
10868 physicalSizeX = meta.getPixelsPhysicalSizeX(series).value().doubleValue();
10869 hasPhysical = true;
10870 }
10871 if (meta.getPixelsPhysicalSizeY(series) != null)
10872 {
10873 physicalSizeY = meta.getPixelsPhysicalSizeY(series).value().doubleValue();
10874 }
10875 if (meta.getPixelsPhysicalSizeZ(series) != null)
10876 {
10877 physicalSizeZ = meta.getPixelsPhysicalSizeZ(series).value().doubleValue();
10878 }
10879 else
10880 {
10881 physicalSizeZ = 1;
10882 }
10883 if (meta.getStageLabelX(series) != null)
10884 stageSizeX = meta.getStageLabelX(series).value().doubleValue();
10885 if (meta.getStageLabelY(series) != null)
10886 stageSizeY = meta.getStageLabelY(series).value().doubleValue();
10887 if (meta.getStageLabelZ(series) != null)
10888 stageSizeZ = meta.getStageLabelZ(series).value().doubleValue();
10889 else
10890 stageSizeZ = 1;
10891 }
10892 catch (Exception e)
10893 {
10894 stageSizeX = 0;
10895 stageSizeY = 0;
10896 stageSizeZ = 1;
10897 }
10898 VolumeD volume = new VolumeD(new Point3D(stageSizeX, stageSizeY, stageSizeZ), new Point3D(physicalSizeX * SizeX, physicalSizeY * SizeY, physicalSizeZ * SizeZ));
10899 int rc = meta.getROICount();
10900 for (int im = 0; im < rc; im++)
10901 {
10902 string roiID = meta.getROIID(im);
10903 string roiName = meta.getROIName(im);
10904 ZCT co =
new ZCT(0, 0, 0);
10905 int scount = 1;
10906 try
10907 {
10908 scount = meta.getShapeCount(im);
10909 }
10910 catch (Exception e)
10911 {
10912 Console.WriteLine(e.Message.ToString());
10913 }
10914 for (int sc = 0; sc < scount; sc++)
10915 {
10916 co =
new ZCT(0, 0, 0);
10917 string type;
10918 try
10919 {
10920 type = meta.getShapeType(im, sc);
10921 }
10922 catch
10923 {
10924 type = null;
10925 }
10926 if (string.IsNullOrWhiteSpace(type))
10927 {
10928 try
10929 {
10930 byte[] maskProbe = meta.getMaskBinData(im, sc);
10931 if (maskProbe != null && maskProbe.Length > 0)
10932 type = "Mask";
10933 }
10934 catch
10935 {
10936
10937 }
10938 }
10939 if (string.IsNullOrWhiteSpace(type))
10940 continue;
10942 an.roiID = roiID;
10943 an.roiName = roiName;
10944 an.shapeIndex = sc;
10945 if (type == "Point")
10946 {
10948 an.id = meta.getPointID(im, sc);
10949 double dx = meta.getPointX(im, sc).doubleValue();
10950 double dy = meta.getPointY(im, sc).doubleValue();
10951 an.
AddPoint(ToStageSpace(
new PointD(dx, dy), physicalSizeX, physicalSizeY, volume.Location.X, volume.Location.Y));
10952 an.coord =
new ZCT();
10953 ome.xml.model.primitives.NonNegativeInteger nz = meta.getPointTheZ(im, sc);
10954 if (nz != null)
10955 an.coord.Z = nz.getNumberValue().intValue();
10956 ome.xml.model.primitives.NonNegativeInteger nc = meta.getPointTheC(im, sc);
10957 if (nc != null)
10958 an.coord.C = nc.getNumberValue().intValue();
10959 ome.xml.model.primitives.NonNegativeInteger nt = meta.getPointTheT(im, sc);
10960 if (nt != null)
10961 an.coord.T = nt.getNumberValue().intValue();
10962 an.Text = meta.getPointText(im, sc);
10963 ome.units.quantity.Length fl = meta.getPointFontSize(im, sc);
10964 if (fl != null)
10965 an.fontSize = fl.value().intValue();
10966 an.family = meta.getPointFontFamily(im, sc).name();
10967 ome.xml.model.primitives.Color col = meta.getPointStrokeColor(im, sc);
10968 if (col != null)
10969 an.strokeColor =
Color.FromArgb(col.getAlpha(), col.getRed(), col.getGreen(), col.getBlue());
10970 ome.units.quantity.Length fw = meta.getPointStrokeWidth(im, sc);
10971 if (fw != null)
10972 an.strokeWidth = (float)fw.value().floatValue();
10973 ome.xml.model.primitives.Color colf = meta.getPointStrokeColor(im, sc);
10974 if (colf != null)
10975 an.fillColor =
Color.FromArgb(colf.getAlpha(), colf.getRed(), colf.getGreen(), colf.getBlue());
10976 }
10977 else
10978 if (type == "Line")
10979 {
10981 an.id = meta.getLineID(im, sc);
10982 double px1 = meta.getLineX1(im, sc).doubleValue();
10983 double py1 = meta.getLineY1(im, sc).doubleValue();
10984 double px2 = meta.getLineX2(im, sc).doubleValue();
10985 double py2 = meta.getLineY2(im, sc).doubleValue();
10986 an.
AddPoint(ToStageSpace(
new PointD(px1, py1), physicalSizeX, physicalSizeY, volume.Location.X, volume.Location.Y));
10987 an.
AddPoint(ToStageSpace(
new PointD(px2, py2), physicalSizeX, physicalSizeY, volume.Location.X, volume.Location.Y));
10988 ome.xml.model.primitives.NonNegativeInteger nz = meta.getLineTheZ(im, sc);
10989 if (nz != null)
10990 co.Z = nz.getNumberValue().intValue();
10991 ome.xml.model.primitives.NonNegativeInteger nc = meta.getLineTheC(im, sc);
10992 if (nc != null)
10993 co.C = nc.getNumberValue().intValue();
10994 ome.xml.model.primitives.NonNegativeInteger nt = meta.getLineTheT(im, sc);
10995 if (nt != null)
10996 co.T = nt.getNumberValue().intValue();
10997 an.coord =
new ZCT(co.Z, co.C, co.T);
10998 an.Text = meta.getLineText(im, sc);
10999 ome.units.quantity.Length fl = meta.getLineFontSize(im, sc);
11000 if (fl != null)
11001 an.fontSize = fl.value().intValue();
11002 an.family = meta.getPointFontFamily(im, sc).name();
11003 ome.xml.model.primitives.Color col = meta.getLineStrokeColor(im, sc);
11004 if (col != null)
11005 an.strokeColor =
Color.FromArgb(col.getAlpha(), col.getRed(), col.getGreen(), col.getBlue());
11006 ome.units.quantity.Length fw = meta.getLineStrokeWidth(im, sc);
11007 if (fw != null)
11008 an.strokeWidth = (float)fw.value().floatValue();
11009 ome.xml.model.primitives.Color colf = meta.getLineFillColor(im, sc);
11010 if (colf != null)
11011 an.fillColor =
Color.FromArgb(colf.getAlpha(), colf.getRed(), colf.getGreen(), colf.getBlue());
11012 }
11013 else
11014 if (type == "Rectangle")
11015 {
11016 an.type =
ROI.
Type.Rectangle;
11017 an.id = meta.getRectangleID(im, sc);
11018 double px = meta.getRectangleX(im, sc).doubleValue();
11019 double py = meta.getRectangleY(im, sc).doubleValue();
11020 double pw = meta.getRectangleWidth(im, sc).doubleValue();
11021 double ph = meta.getRectangleHeight(im, sc).doubleValue();
11022 an.BoundingBox = ToStageSpace(
new RectangleD(px, py, pw, ph), physicalSizeX, physicalSizeY, volume.Location.X, volume.Location.Y);
11023 ome.xml.model.primitives.NonNegativeInteger nz = meta.getRectangleTheZ(im, sc);
11024 if (nz != null)
11025 co.Z = nz.getNumberValue().intValue();
11026 ome.xml.model.primitives.NonNegativeInteger nc = meta.getRectangleTheC(im, sc);
11027 if (nc != null)
11028 co.C = nc.getNumberValue().intValue();
11029 ome.xml.model.primitives.NonNegativeInteger nt = meta.getRectangleTheT(im, sc);
11030 if (nt != null)
11031 co.T = nt.getNumberValue().intValue();
11032 an.coord =
new ZCT(co.Z, co.C, co.T);
11033
11034 an.Text = meta.getRectangleText(im, sc);
11035 ome.units.quantity.Length fl = meta.getRectangleFontSize(im, sc);
11036 if (fl != null)
11037 an.fontSize = fl.value().intValue();
11038 an.family = meta.getPointFontFamily(im, sc).name();
11039 ome.xml.model.primitives.Color col = meta.getRectangleStrokeColor(im, sc);
11040 if (col != null)
11041 an.strokeColor =
Color.FromArgb(col.getAlpha(), col.getRed(), col.getGreen(), col.getBlue());
11042 ome.units.quantity.Length fw = meta.getRectangleStrokeWidth(im, sc);
11043 if (fw != null)
11044 an.strokeWidth = (float)fw.value().floatValue();
11045 ome.xml.model.primitives.Color colf = meta.getRectangleFillColor(im, sc);
11046 if (colf != null)
11047 an.fillColor =
Color.FromArgb(colf.getAlpha(), colf.getRed(), colf.getGreen(), colf.getBlue());
11048 ome.xml.model.enums.FillRule fr = meta.getRectangleFillRule(im, sc);
11049 }
11050 else
11051 if (type == "Ellipse")
11052 {
11054 an.id = meta.getEllipseID(im, sc);
11055 double px = meta.getEllipseX(im, sc).doubleValue();
11056 double py = meta.getEllipseY(im, sc).doubleValue();
11057 double ew = meta.getEllipseRadiusX(im, sc).doubleValue();
11058 double eh = meta.getEllipseRadiusY(im, sc).doubleValue();
11059
11060 double w = ew * 2;
11061 double h = eh * 2;
11062 double x = px - ew;
11063 double y = py - eh;
11064 an.BoundingBox = ToStageSpace(
new RectangleD(px, py, w, h), physicalSizeX, physicalSizeY, volume.Location.X, volume.Location.Y);
11065 ome.xml.model.primitives.NonNegativeInteger nz = meta.getEllipseTheZ(im, sc);
11066 if (nz != null)
11067 co.Z = nz.getNumberValue().intValue();
11068 ome.xml.model.primitives.NonNegativeInteger nc = meta.getEllipseTheC(im, sc);
11069 if (nc != null)
11070 co.C = nc.getNumberValue().intValue();
11071 ome.xml.model.primitives.NonNegativeInteger nt = meta.getEllipseTheT(im, sc);
11072 if (nt != null)
11073 co.T = nt.getNumberValue().intValue();
11074 an.coord =
new ZCT(co.Z, co.C, co.T);
11075 an.Text = meta.getEllipseText(im, sc);
11076 ome.units.quantity.Length fl = meta.getEllipseFontSize(im, sc);
11077 if (fl != null)
11078 an.fontSize = fl.value().intValue();
11079 an.family = meta.getPointFontFamily(im, sc).name();
11080 ome.xml.model.primitives.Color col = meta.getEllipseStrokeColor(im, sc);
11081 if (col != null)
11082 an.strokeColor =
Color.FromArgb(col.getAlpha(), col.getRed(), col.getGreen(), col.getBlue());
11083 ome.units.quantity.Length fw = meta.getEllipseStrokeWidth(im, sc);
11084 if (fw != null)
11085 an.strokeWidth = (float)fw.value().floatValue();
11086 ome.xml.model.primitives.Color colf = meta.getEllipseFillColor(im, sc);
11087 if (colf != null)
11088 an.fillColor =
Color.FromArgb(colf.getAlpha(), colf.getRed(), colf.getGreen(), colf.getBlue());
11089 }
11090 else
11091 if (type == "Polygon")
11092 {
11094 an.id = meta.getPolygonID(im, sc);
11095 an.closed = true;
11096 string pxs = meta.getPolygonPoints(im, sc);
11098 pts = ToStageSpace(pts, physicalSizeX, physicalSizeY, volume.Location.X, volume.Location.Y);
11099 if (pts.Length > 100)
11100 {
11102 }
11104 ome.xml.model.primitives.NonNegativeInteger nz = meta.getPolygonTheZ(im, sc);
11105 if (nz != null)
11106 co.Z = nz.getNumberValue().intValue();
11107 ome.xml.model.primitives.NonNegativeInteger nc = meta.getPolygonTheC(im, sc);
11108 if (nc != null)
11109 co.C = nc.getNumberValue().intValue();
11110 ome.xml.model.primitives.NonNegativeInteger nt = meta.getPolygonTheT(im, sc);
11111 if (nt != null)
11112 co.T = nt.getNumberValue().intValue();
11113 an.coord =
new ZCT(co.Z, co.C, co.T);
11114 an.Text = meta.getPolygonText(im, sc);
11115 ome.units.quantity.Length fl = meta.getPolygonFontSize(im, sc);
11116 if (fl != null)
11117 an.fontSize = fl.value().intValue();
11118 an.family = meta.getPointFontFamily(im, sc).name();
11119 ome.xml.model.primitives.Color col = meta.getPolygonStrokeColor(im, sc);
11120 if (col != null)
11121 an.strokeColor =
Color.FromArgb(col.getAlpha(), col.getRed(), col.getGreen(), col.getBlue());
11122 ome.units.quantity.Length fw = meta.getPolygonStrokeWidth(im, sc);
11123 if (fw != null)
11124 an.strokeWidth = (float)fw.value().floatValue();
11125 ome.xml.model.primitives.Color colf = meta.getPolygonFillColor(im, sc);
11126 if (colf != null)
11127 an.fillColor =
Color.FromArgb(colf.getAlpha(), colf.getRed(), colf.getGreen(), colf.getBlue());
11128 }
11129 else
11130 if (type == "Polyline")
11131 {
11133 an.id = meta.getPolylineID(im, sc);
11134 string pxs = meta.getPolylinePoints(im, sc);
11136 for (int pi = 0; pi < pts.Length; pi++)
11137 {
11138 pts[pi] = ToStageSpace(pts[pi], physicalSizeX, physicalSizeY, volume.Location.X, volume.Location.Y);
11139 }
11141 ome.xml.model.primitives.NonNegativeInteger nz = meta.getPolylineTheZ(im, sc);
11142 if (nz != null)
11143 co.Z = nz.getNumberValue().intValue();
11144 ome.xml.model.primitives.NonNegativeInteger nc = meta.getPolylineTheC(im, sc);
11145 if (nc != null)
11146 co.C = nc.getNumberValue().intValue();
11147 ome.xml.model.primitives.NonNegativeInteger nt = meta.getPolylineTheT(im, sc);
11148 if (nt != null)
11149 co.T = nt.getNumberValue().intValue();
11150 an.coord =
new ZCT(co.Z, co.C, co.T);
11151 an.Text = meta.getPolylineText(im, sc);
11152 ome.units.quantity.Length fl = meta.getPolylineFontSize(im, sc);
11153 if (fl != null)
11154 an.fontSize = fl.value().intValue();
11155 an.family = meta.getPointFontFamily(im, sc).name();
11156 ome.xml.model.primitives.Color col = meta.getPolylineStrokeColor(im, sc);
11157 if (col != null)
11158 an.strokeColor =
Color.FromArgb(col.getAlpha(), col.getRed(), col.getGreen(), col.getBlue());
11159 ome.units.quantity.Length fw = meta.getPolylineStrokeWidth(im, sc);
11160 if (fw != null)
11161 an.strokeWidth = (float)fw.value().floatValue();
11162 ome.xml.model.primitives.Color colf = meta.getPolylineFillColor(im, sc);
11163 if (colf != null)
11164 an.fillColor =
Color.FromArgb(colf.getAlpha(), colf.getRed(), colf.getGreen(), colf.getBlue());
11165 }
11166 else
11167 if (type == "Label")
11168 {
11170 an.id = meta.getLabelID(im, sc);
11171
11172 ome.xml.model.primitives.NonNegativeInteger nz = meta.getLabelTheZ(im, sc);
11173 if (nz != null)
11174 co.Z = nz.getNumberValue().intValue();
11175 ome.xml.model.primitives.NonNegativeInteger nc = meta.getLabelTheC(im, sc);
11176 if (nc != null)
11177 co.C = nc.getNumberValue().intValue();
11178 ome.xml.model.primitives.NonNegativeInteger nt = meta.getLabelTheT(im, sc);
11179 if (nt != null)
11180 co.T = nt.getNumberValue().intValue();
11181 an.coord =
new ZCT(co.Z, co.C, co.T);
11182
11183 ome.units.quantity.Length fl = meta.getLabelFontSize(im, sc);
11184 if (fl != null)
11185 an.fontSize = fl.value().intValue();
11186 an.family = meta.getPointFontFamily(im, sc).name();
11187 ome.xml.model.primitives.Color col = meta.getLabelStrokeColor(im, sc);
11188 if (col != null)
11189 an.strokeColor =
Color.FromArgb(col.getAlpha(), col.getRed(), col.getGreen(), col.getBlue());
11190 ome.units.quantity.Length fw = meta.getLabelStrokeWidth(im, sc);
11191 if (fw != null)
11192 an.strokeWidth = (float)fw.value().floatValue();
11193 ome.xml.model.primitives.Color colf = meta.getLabelFillColor(im, sc);
11194 if (colf != null)
11195 an.fillColor =
Color.FromArgb(colf.getAlpha(), colf.getRed(), colf.getGreen(), colf.getBlue());
11196 PointD p =
new PointD(meta.getLabelX(im, sc).doubleValue(), meta.getLabelY(im, sc).doubleValue());
11197 an.
AddPoint(ToStageSpace(p, physicalSizeX, physicalSizeY, volume.Location.X, volume.Location.Y));
11198 an.Text = meta.getLabelText(im, sc);
11199 }
11200 else
11201 if (type == "Mask")
11202 {
11203 byte[] bts = meta.getMaskBinData(im, sc);
11204 bool end = meta.getMaskBinDataBigEndian(im, sc).booleanValue();
11205 double h = meta.getMaskHeight(im, sc).doubleValue();
11206 double w = meta.getMaskWidth(im, sc).doubleValue();
11207 double x = meta.getMaskX(im, sc).doubleValue();
11208 double y = meta.getMaskY(im, sc).doubleValue();
11209 var maskDimensions = GetMaskDimensionsForOmeMask(null, bts, w, h);
11210 int maskWidth = maskDimensions.Width;
11211 int maskHeight = maskDimensions.Height;
11212 if (maskWidth <= 0 || maskHeight <= 0)
11213 {
11214 continue;
11215 }
11216 double maskPhysX = w > 0 ? w / maskWidth : physicalSizeX;
11217 double maskPhysY = h > 0 ? h / maskHeight : physicalSizeY;
11218 PointD stageLoc = ToStageSpace(
new PointD(x, y), physicalSizeX, physicalSizeY, volume.Location.X, volume.Location.Y);
11220 co,
11221 bts,
11222 maskWidth,
11223 maskHeight,
11224 stageLoc,
11225 maskPhysX,
11226 maskPhysY,
11227 preserveDimensions: true);
11228 an.Text = meta.getMaskText(im, sc);
11229 an.id = meta.getMaskID(im, sc);
11231 ome.xml.model.primitives.NonNegativeInteger nz = meta.getMaskTheZ(im, sc);
11232 if (nz != null)
11233 co.Z = nz.getNumberValue().intValue();
11234 ome.xml.model.primitives.NonNegativeInteger nc = meta.getMaskTheC(im, sc);
11235 if (nc != null)
11236 co.C = nc.getNumberValue().intValue();
11237 ome.xml.model.primitives.NonNegativeInteger nt = meta.getMaskTheT(im, sc);
11238 if (nt != null)
11239 co.T = nt.getNumberValue().intValue();
11240 an.coord =
new ZCT(co.Z, co.C, co.T);
11241
11242 ome.units.quantity.Length fl = meta.getMaskFontSize(im, sc);
11243 if (fl != null)
11244 an.fontSize = fl.value().intValue();
11245 ome.xml.model.enums.FontFamily ff = meta.getMaskFontFamily(im, sc);
11246 if (ff != null)
11247 an.family = ff.name();
11248 ome.xml.model.primitives.Color col = meta.getMaskStrokeColor(im, sc);
11249 if (col != null)
11250 an.strokeColor =
Color.FromArgb(col.getAlpha(), col.getRed(), col.getGreen(), col.getBlue());
11251 ome.units.quantity.Length fw = meta.getMaskStrokeWidth(im, sc);
11252 if (fw != null)
11253 an.strokeWidth = (float)fw.value().floatValue();
11254 ome.xml.model.primitives.Color colf = meta.getMaskFillColor(im, sc);
11255 if (colf != null)
11256 an.fillColor =
Color.FromArgb(colf.getAlpha(), colf.getRed(), colf.getGreen(), colf.getBlue());
11257 }
11258 Annotations.Add(an);
11259 }
11260 }
11261
11262 imageReader.close();
11263 Recorder.
Record($
"BioImage.OpenOMEROIs(\"{file.Replace("\\
", "\\\\
").Replace("\
"",
"\\\"")}\
", {series});");
11264 return Annotations;
11265 }