BioGTK  5.4.1
A .NET library & program for annotating, editing various microscopy imaging formats using Bioformats supported images. Including whole slide, pyramidal, and series.
Loading...
Searching...
No Matches
BioGTK.ImageJ Class Reference

Static Public Member Functions

static ImagePlus GetImagePlus (BioImage bm)
 
static bool isRGB (ImagePlus image)
 
static int[] getRGBPixelsFromSlice (ImageStack stack, int sliceIndex)
 
static BioImage GetBioImage (ImagePlus pl, VolumeD vol, double PhysicalX, double PhysicalY, double PhysicalZ)
 Converts ImagePlus to BioImage.
 

Detailed Description

Definition at line 18 of file ImageJ.cs.

Member Function Documentation

◆ GetBioImage()

static BioImage BioGTK.ImageJ.GetBioImage ( ImagePlus pl,
VolumeD vol,
double PhysicalX,
double PhysicalY,
double PhysicalZ )
static

Converts ImagePlus to BioImage.

Parameters
pl
maxz
maxc
maxt
location
Returns
The BioImage represented by the ImagePlus object.

Definition at line 182 of file ImageJ.cs.

183 {
184 BioImage bm = new BioImage(pl.getTitle());
185 ImageStack st = pl.getImageStack();
186 int b = pl.getBitDepth();
187 int slices = pl.getNSlices();
188 int chs = pl.getNChannels();
189 int frs = pl.getNFrames();
190 int rgb = (int)(pl.getBytesPerPixel() * 8);
191 bool isrgb = isRGB(pl);
192 bm.UpdateCoords(slices,chs,frs,BioImage.Order.TCZ);
193 for (int t = 0; t < frs; t++)
194 {
195 for (int c = 0; c < chs; c++)
196 {
197 for (int z = 0; z < slices; z++)
198 {
199 AForge.Bitmap bmp;
200 if (!isrgb)
201 {
202 if (rgb > 8)
203 {
204 bmp = new AForge.Bitmap(st.getWidth(), st.getHeight(), AForge.PixelFormat.Format16bppGrayScale);
205 bm.Buffers.Add(bmp);
206 }
207 else
208 {
209 bmp = new AForge.Bitmap(st.getWidth(), st.getHeight(), AForge.PixelFormat.Format8bppIndexed);
210 bm.Buffers.Add(bmp);
211 }
212 }
213 else
214 {
215 bmp = new AForge.Bitmap(st.getWidth(), st.getHeight(), AForge.PixelFormat.Format24bppRgb);
216 bm.Buffers.Add(bmp);
217 }
218 int ind = bm.GetFrameIndex(z, c, t);
219 if (!isrgb)
220 {
221 for (int y = 0; y < pl.getHeight(); y++)
222 {
223 for (int x = 0; x < pl.getWidth(); x++)
224 {
225 double d = st.getVoxel(x, y, ind);
226 if (bm.Buffers[0].PixelFormat == PixelFormat.Format16bppGrayScale)
227 bmp.SetValue(x, y, (ushort)d);
228 else if (bm.Buffers[0].PixelFormat == PixelFormat.Format8bppIndexed)
229 bmp.SetValue(x, y, (byte)d);
230 }
231 }
232 }
233 else
234 {
235 int[] rgbPixels = getRGBPixelsFromSlice(st, ind);
236 // Get the ImageProcessor for the specified slice
237 ImageProcessor ip = st.getProcessor(ind + 1); // ImageStack uses 1-based indexing
238 // Cast to ColorProcessor to access RGB data
239 ColorProcessor colorProcessor = (ColorProcessor)ip;
240 for (int y = 0; y < pl.getHeight(); y++)
241 {
242 for (int x = 0; x < pl.getWidth(); x++)
243 {
244 int v = colorProcessor.getPixel(x, y);
245 int rv = (v >> 16) & 0xFF; // Red
246 int gv = (v >> 8) & 0xFF; // Green
247 int bv = v & 0xFF; // Blue
248 bmp.SetValue(x, y, 0, (byte)rv);
249 bmp.SetValue(x, y, 1, (byte)gv);
250 bmp.SetValue(x, y, 2, (byte)bv);
251 }
252 }
253
254 }
255 }
256 }
257 }
258 for(int t = 0;t < chs; t++)
259 {
260 if(isrgb)
261 bm.Channels.Add(new AForge.Channel(t, rgb, 3));
262 else
263 bm.Channels.Add(new AForge.Channel(t, rgb, 1));
264 if (t == 0)
265 {
266 bm.rgbChannels[0] = 0;
267 }
268 else
269 if (t == 1)
270 {
271 bm.rgbChannels[1] = 1;
272 }
273 else
274 if (t == 2)
275 {
276 bm.rgbChannels[2] = 2;
277 }
278 }
279 bm.Resolutions.Add(new Resolution(pl.getWidth(), pl.getHeight(), bm.Buffers[0].PixelFormat, PhysicalX, PhysicalY, PhysicalZ, vol.Location.X, vol.Location.Y, vol.Location.X));
280 bm.Volume = vol;
281 bm.littleEndian = BitConverter.IsLittleEndian;
282 bm.seriesCount = 1;
283 bm.bitsPerPixel = b;
284 BioImage.AutoThreshold(bm, true);
285 if (rgb > 8)
286 bm.StackThreshold(true);
287 else
288 bm.StackThreshold(false);
289
290 if (bm.RGBChannelCount == 4)
291 {
292 bm.Channels.Last().SamplesPerPixel = 4;
293 }
294 return bm;
295 }

◆ GetImagePlus()

static ImagePlus BioGTK.ImageJ.GetImagePlus ( BioImage bm)
static

Definition at line 46 of file ImageJ.cs.

47 {
48 ImageStack ims = new ImageStack(bm.SizeX, bm.SizeY);
49 if (bm.Buffers[0].PixelFormat == AForge.PixelFormat.Format16bppGrayScale)
50 {
51 // Populate the stack with 16-bit images
52 for (int t = 0; t < bm.SizeT; t++)
53 {
54 for (int c = 0; c < bm.SizeC; c++)
55 {
56 for (int z = 0; z < bm.SizeZ; z++)
57 {
58 short[] pixels = ConvertByteArrayToShortArray(bm.Buffers[bm.GetFrameIndex(z, c, t)].Bytes, true);
59 ImageProcessor ip = new ShortProcessor(bm.SizeX, bm.SizeY, pixels, java.awt.image.ColorModel.getRGBdefault());
60 ims.addSlice("Z" + z + "C" + c + "T" + t, ip);
61 }
62 }
63 }
64 }
65 else if (bm.Buffers[0].PixelFormat == AForge.PixelFormat.Format8bppIndexed)
66 {
67 // Create a grayscale IndexColorModel
68 byte[] grayscale = new byte[256];
69 for (int i = 0; i < 256; i++)
70 {
71 grayscale[i] = (byte)i;
72 }
73 IndexColorModel colorModel = new IndexColorModel(8, 256, grayscale, grayscale, grayscale);
74 for (int t = 0; t < bm.SizeT; t++)
75 {
76 for (int c = 0; c < bm.SizeC; c++)
77 {
78 for (int z = 0; z < bm.SizeZ; z++)
79 {
80 ImageProcessor ip = new ByteProcessor(bm.SizeX, bm.SizeY, bm.Buffers[bm.GetFrameIndex(z, c, t)].Bytes, colorModel);
81 ims.addSlice("Z" + z + "C" + c + "T" + t, ip);
82 }
83 }
84 }
85 }
86 else if (bm.Buffers[0].PixelFormat == AForge.PixelFormat.Format24bppRgb)
87 {
88 for (int t = 0; t < bm.SizeT; t++)
89 {
90 for (int c = 0; c < bm.SizeC; c++)
91 {
92 for (int z = 0; z < bm.SizeZ; z++)
93 {
94 byte[] bts = bm.Buffers[bm.GetFrameIndex(z, c, t)].Bytes;
95 // Convert the byte array to an int array for the RGB processor
96 int[] rgbPixels = new int[bm.SizeX * bm.SizeY];
97 for (int i = 0; i < rgbPixels.Length; i++)
98 {
99 int r = bts[i * 3] & 0xFF; // Red
100 int g = bts[i * 3 + 1] & 0xFF; // Green
101 int b = bts[i * 3 + 2] & 0xFF; // Blue
102 rgbPixels[i] = (r << 16) | (g << 8) | b; // Combine into RGB int
103 }
104 // Create a ColorProcessor
105 ImageProcessor ip = new ColorProcessor(bm.SizeX, bm.SizeY, rgbPixels);
106 ims.addSlice("Z" + z + "C" + c + "T" + t, ip);
107 }
108 }
109 }
110 }
111 else if(bm.Buffers[0].PixelFormat == AForge.PixelFormat.Format32bppArgb || bm.Buffers[0].PixelFormat == AForge.PixelFormat.Format32bppRgb)
112 {
113 for (int t = 0; t < bm.SizeT; t++)
114 {
115 for (int c = 0; c < bm.SizeC; c++)
116 {
117 for (int z = 0; z < bm.SizeZ; z++)
118 {
119 byte[] bts = bm.Buffers[bm.GetFrameIndex(z, c, t)].Bytes;
120 // Convert the byte array to an int array for the ColorProcessor
121 int[] rgbaPixels = new int[bm.SizeX * bm.SizeY];
122 for (int i = 0; i < rgbaPixels.Length; i++)
123 {
124 int r = bts[i * 4] & 0xFF; // Red
125 int g = bts[i * 4 + 1] & 0xFF; // Green
126 int b = bts[i * 4 + 2] & 0xFF; // Blue
127 int a = bts[i * 4 + 3] & 0xFF; // Alpha
128 rgbaPixels[i] = (a << 24) | (r << 16) | (g << 8) | b; // Combine into ARGB int
129 }
130 // Create a ColorProcessor with the ARGB int array
131 ImageProcessor ip = new ColorProcessor(bm.SizeX, bm.SizeY, rgbaPixels);
132 ims.addSlice("Z" + z + "C" + c + "T" + t, ip);
133 }
134 }
135 }
136 }
137 else if (bm.Buffers[0].PixelFormat == AForge.PixelFormat.Format48bppRgb)
138 {
139 bm.To16Bit();
140 // Populate the stack with 16-bit images
141 for (int t = 0; t < bm.SizeT; t++)
142 {
143 for (int c = 0; c < bm.SizeC; c++)
144 {
145 for (int z = 0; z < bm.SizeZ; z++)
146 {
147 short[] pixels = ConvertByteArrayToShortArray(bm.Buffers[bm.GetFrameIndex(z, c, t)].Bytes, true);
148 ImageProcessor ip = new ShortProcessor(bm.SizeX, bm.SizeY, pixels, java.awt.image.ColorModel.getRGBdefault());
149 ims.addSlice("Z" + z + "C" + c + "T" + t, ip);
150 }
151 }
152 }
153 }
154 ImagePlus imp = new ImagePlus(bm.ID, ims);
155 imp.setDimensions(bm.SizeC, bm.SizeZ, bm.SizeT);
156 return imp;
157 }

◆ getRGBPixelsFromSlice()

static int[] BioGTK.ImageJ.getRGBPixelsFromSlice ( ImageStack stack,
int sliceIndex )
static

Definition at line 164 of file ImageJ.cs.

165 {
166 // Get the ImageProcessor for the specified slice
167 ImageProcessor ip = stack.getProcessor(sliceIndex + 1); // ImageStack uses 1-based indexing
168 // Cast to ColorProcessor to access RGB data
169 ColorProcessor colorProcessor = (ColorProcessor)ip;
170 // Get the RGB pixel data
171 return (int[])colorProcessor.getPixels();
172 }

◆ isRGB()

static bool BioGTK.ImageJ.isRGB ( ImagePlus image)
static

Definition at line 159 of file ImageJ.cs.

160 {
161 return image.getType() == ImagePlus.COLOR_RGB;
162 }

The documentation for this class was generated from the following file: