Skip to content

Commit

Permalink
4.1.0 BioImage.IO models support.
Browse files Browse the repository at this point in the history
BioImage.IO model support as well as TIFF multipage fix.
  • Loading branch information
BiologyTools committed May 8, 2024
1 parent 7fc849c commit 5d0f924
Show file tree
Hide file tree
Showing 6 changed files with 609 additions and 32 deletions.
8 changes: 6 additions & 2 deletions BioImager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
<PackageTags>Microscopy;Biology;</PackageTags>
<PackageLicenseExpression>GPL-3.0-only</PackageLicenseExpression>
<Description>A .NET microscopy imaging application based on Bio library. Supports various microscopes by using imported libraries &amp; GUI automation. Supported libraries include Prior® &amp; Zeiss® &amp; all devices supported by Micromanager 2.0 and python-microscope.</Description>
<Version>4.0.0</Version>
<Version>4.1.0</Version>
<PackageProjectUrl></PackageProjectUrl>
<RepositoryUrl>https://github.com/BiologyTools/BioImager</RepositoryUrl>
<PackageLicenseFile></PackageLicenseFile>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<PackageReleaseNotes>OME type slides fix. Support for 16 bit slides.</PackageReleaseNotes>
<PackageReleaseNotes>BioImage.IO model support both ONNX &amp; PyTorch formats.</PackageReleaseNotes>
<Platforms>AnyCPU;x86;x64</Platforms>
</PropertyGroup>
<ItemGroup>
Expand Down Expand Up @@ -491,6 +491,8 @@
<PackageReference Include="FlaUI.Core" Version="4.0.0" />
<PackageReference Include="FlaUI.UIA3" Version="4.0.0" />
<PackageReference Include="H.InputSimulator" Version="1.4.1" />
<PackageReference Include="MathNet.Numerics" Version="5.0.0" />
<PackageReference Include="Microsoft.ML.OnnxRuntime" Version="1.17.3" />
<PackageReference Include="MouseKeyHook" Version="5.7.1" />
<PackageReference Include="NetVips" Version="2.4.1" />
<PackageReference Include="NetVips.Native.win-x64" Version="8.15.2" />
Expand All @@ -507,6 +509,8 @@
<PackageReference Include="System.Reflection" Version="4.3.0" />
<PackageReference Include="System.Reflection.Emit" Version="4.7.0" />
<PackageReference Include="System.Runtime" Version="4.3.1" />
<PackageReference Include="TorchSharp-cpu" Version="0.102.4" />
<PackageReference Include="YamlDotNet" Version="15.1.2" />
</ItemGroup>
<ItemGroup>
<None Update="banner.jpeg">
Expand Down
12 changes: 6 additions & 6 deletions BioImager.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ Global
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CC49B76C-BE6F-4094-8554-072028B4D391}.Debug|Any CPU.ActiveCfg = Debug|x64
{CC49B76C-BE6F-4094-8554-072028B4D391}.Debug|Any CPU.Build.0 = Debug|x64
{CC49B76C-BE6F-4094-8554-072028B4D391}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CC49B76C-BE6F-4094-8554-072028B4D391}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CC49B76C-BE6F-4094-8554-072028B4D391}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{CC49B76C-BE6F-4094-8554-072028B4D391}.Debug|ARM64.Build.0 = Debug|Any CPU
{CC49B76C-BE6F-4094-8554-072028B4D391}.Debug|x64.ActiveCfg = Debug|x64
Expand All @@ -33,8 +33,8 @@ Global
{CC49B76C-BE6F-4094-8554-072028B4D391}.Release|ARM64.Build.0 = Release|Any CPU
{CC49B76C-BE6F-4094-8554-072028B4D391}.Release|x64.ActiveCfg = Release|Any CPU
{CC49B76C-BE6F-4094-8554-072028B4D391}.Release|x64.Build.0 = Release|Any CPU
{E1764F8A-42FF-42FA-9C20-71773582243A}.Debug|Any CPU.ActiveCfg = Debug|x64
{E1764F8A-42FF-42FA-9C20-71773582243A}.Debug|Any CPU.Build.0 = Debug|x64
{E1764F8A-42FF-42FA-9C20-71773582243A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E1764F8A-42FF-42FA-9C20-71773582243A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E1764F8A-42FF-42FA-9C20-71773582243A}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{E1764F8A-42FF-42FA-9C20-71773582243A}.Debug|ARM64.Build.0 = Debug|Any CPU
{E1764F8A-42FF-42FA-9C20-71773582243A}.Debug|x64.ActiveCfg = Debug|x64
Expand All @@ -45,8 +45,8 @@ Global
{E1764F8A-42FF-42FA-9C20-71773582243A}.Release|ARM64.Build.0 = Release|Any CPU
{E1764F8A-42FF-42FA-9C20-71773582243A}.Release|x64.ActiveCfg = Release|Any CPU
{E1764F8A-42FF-42FA-9C20-71773582243A}.Release|x64.Build.0 = Release|Any CPU
{A78B58AD-F086-4F44-8C99-51D68E225AA7}.Debug|Any CPU.ActiveCfg = Debug|x64
{A78B58AD-F086-4F44-8C99-51D68E225AA7}.Debug|Any CPU.Build.0 = Debug|x64
{A78B58AD-F086-4F44-8C99-51D68E225AA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A78B58AD-F086-4F44-8C99-51D68E225AA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A78B58AD-F086-4F44-8C99-51D68E225AA7}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{A78B58AD-F086-4F44-8C99-51D68E225AA7}.Debug|ARM64.Build.0 = Debug|Any CPU
{A78B58AD-F086-4F44-8C99-51D68E225AA7}.Debug|x64.ActiveCfg = Debug|x64
Expand Down
11 changes: 11 additions & 0 deletions Source/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,17 @@ private static void ItemClicked(object sender, EventArgs e)
string st = File.ReadAllText(Path.GetDirectoryName(ImageJ.ImageJPath) + "/macros/" + ts.Text);
ImageJ.RunOnImage(st, BioConsole.headless, BioConsole.onTab, BioConsole.useBioformats, BioConsole.newTab);
}
else if(ts.Text.EndsWith(".pt") || ts.Text.EndsWith(".onnx"))
{
try
{
ML.ML.Run(ts.Text, ImageView.SelectedImage);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
}
else
{
Function f = (Function)ts.Tag;
Expand Down
60 changes: 36 additions & 24 deletions Source/Bio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4676,15 +4676,6 @@ static bool IsTiffTiled(string imagePath)
}
else return false;
}
/// The function `InitDirectoryResolution` initializes the resolution properties of a `BioImage`
/// object based on the resolution information stored in a TIFF image.
///
/// @param BioImage BioImage is a class representing a bioimage, which contains information
/// about the image resolution, size, and pixel format.
/// @param Tiff The "Tiff" parameter is an object of the Tiff class. It represents a TIFF image
/// file and provides methods and properties to access and manipulate the image data.
/// @param ImageJDesc ImageJDesc is a class that contains information about the image dimensions
/// and resolution in ImageJ format. It has the following properties:
static void InitDirectoryResolution(BioImage b, Tiff image, ImageJDesc jdesc = null)
{
Resolution res = new Resolution();
Expand Down Expand Up @@ -4749,14 +4740,20 @@ static void InitDirectoryResolution(BioImage b, Tiff image, ImageJDesc jdesc = n
res.PhysicalSizeZ = 2.54 / 96;
}
}
if (res.PhysicalSizeX == 0 || res.PhysicalSizeY == 0)
{
res.PhysicalSizeX = 2.54 / 96;
res.PhysicalSizeY = 2.54 / 96;
res.PhysicalSizeZ = 2.54 / 96;
}
res.SizeX = image.GetField(TiffTag.IMAGEWIDTH)[0].ToInt();
res.SizeY = image.GetField(TiffTag.IMAGELENGTH)[0].ToInt();
int bitsPerPixel = image.GetField(TiffTag.BITSPERSAMPLE)[0].ToInt();
int RGBChannelCount = image.GetField(TiffTag.SAMPLESPERPIXEL)[0].ToInt();
res.PixelFormat = GetPixelFormat(RGBChannelCount, bitsPerPixel);
b.Resolutions.Add(res);
}
/// The OpenFile function in C# opens a BioImage file, reads its metadata, and loads the image
/// The OpenFile function opens a BioImage file, reads its metadata, and loads the image
/// data into a BioImage object.
///
/// @param file The file path of the bioimage to be opened.
Expand Down Expand Up @@ -4794,21 +4791,17 @@ public static BioImage OpenFile(string file, int series, bool tab, bool addToIma

Stopwatch st = new Stopwatch();
st.Start();
status = "Opening Image";
progFile = file;
progressValue = 0;
Progress pr = new Progress(file, "Opening Image");
BioImage b = new BioImage(file);
if (tiled && file.EndsWith(".tif") && !file.EndsWith(".ome.tif"))
{
//To open this we need libvips
vips = VipsSupport(b.file);
}
if(tiled)
{
b.type = ImageType.pyramidal;
}
b.series = series;
b.file = file;
if (tiled)
b.Type = ImageType.pyramidal;
string fn = Path.GetFileNameWithoutExtension(file);
string dir = Path.GetDirectoryName(file);
if (File.Exists(fn + ".csv"))
Expand All @@ -4819,11 +4812,10 @@ public static BioImage OpenFile(string file, int series, bool tab, bool addToIma
if (file.EndsWith("tif") || file.EndsWith("tiff") || file.EndsWith("TIF") || file.EndsWith("TIFF"))
{
Tiff image = Tiff.Open(file, "r");
b.tifRead = image;
int SizeX = image.GetField(TiffTag.IMAGEWIDTH)[0].ToInt();
int SizeY = image.GetField(TiffTag.IMAGELENGTH)[0].ToInt();
b.bitsPerPixel = image.GetField(TiffTag.BITSPERSAMPLE)[0].ToInt();
b.littleEndian = !image.IsBigEndian();
b.littleEndian = image.IsBigEndian();
int RGBChannelCount = image.GetField(TiffTag.SAMPLESPERPIXEL)[0].ToInt();
string desc = "";

Expand Down Expand Up @@ -5019,12 +5011,32 @@ public static BioImage OpenFile(string file, int series, bool tab, bool addToIma
Bitmap inf = new Bitmap(file, SizeX, SizeY, b.Resolutions[series].PixelFormat, bytes, new ZCT(0, 0, 0), p, null, b.littleEndian, inter);
b.Buffers.Add(inf);
Statistics.CalcStatistics(inf);
progressValue = (float)p / (float)(series + 1) * pages;
pr.UpdateProgressF((float)p / (float)(series + 1) * pages);
}
}
image.Close();
b.UpdateCoords();
}
else
{
var bmp = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(file);
var data = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);
int bytes = Math.Abs(data.Stride) * bmp.Height;
byte[] rgbValues = new byte[bytes];
// Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(data.Scan0, rgbValues, 0, bytes);
Bitmap pf = new Bitmap(bmp.Width,bmp.Height, (AForge.PixelFormat)data.PixelFormat, rgbValues, new ZCT(), "");
b.littleEndian = BitConverter.IsLittleEndian;
b.Resolutions.Add(new Resolution(pf.SizeX, pf.SizeY, pf.PixelFormat, 96 * (1 / 2.54) / 1000, 96 * (1 / 2.54) / 1000, 96 * (1 / 2.54) / 1000, 0, 0, 0));
b.bitsPerPixel = pf.BitsPerPixel;
b.Buffers.Add(pf);
Statistics.CalcStatistics(b.Buffers.Last());
b.Channels.Add(new Channel(0, b.bitsPerPixel, b.RGBChannelCount));
b.Coords = new int[1, 1, 1];
b.sizeC = 1;
b.sizeT = 1;
b.sizeZ = 1;
}
if (b.StageSizeX == -1)
{
b.imageInfo.Series = 0;
Expand All @@ -5035,7 +5047,7 @@ public static BioImage OpenFile(string file, int series, bool tab, bool addToIma
b.Volume = new VolumeD(new Point3D(b.StageSizeX, b.StageSizeY, b.StageSizeZ), new Point3D(b.PhysicalSizeX * b.SizeX, b.PhysicalSizeY * b.SizeY, b.PhysicalSizeZ * b.SizeZ));

//If file is ome and we have OME support then check for annotation in metadata.
if (ome && OmeSupport)
if (ome)
{
b.Annotations.AddRange(OpenOMEROIs(file, series));
}
Expand All @@ -5051,11 +5063,11 @@ public static BioImage OpenFile(string file, int series, bool tab, bool addToIma
b.StackThreshold(true);
else
b.StackThreshold(false);
Recorder.AddLine("Bio.BioImage.Open(" + '"' + file + '"' + ");");
Recorder.AddLine("BioGTK.BioImage.OpenFile(" + '"' + file + '"' + ");");
if (addToImages)
Images.AddImage(b, tab);
//pr.Close();
//pr.Dispose();
pr.Close();
pr.Dispose();
st.Stop();
b.loadTimeMS = st.ElapsedMilliseconds;
Console.WriteLine("BioImage loaded " + b.ToString());
Expand Down
Loading

0 comments on commit 5d0f924

Please sign in to comment.