BioLib  3.7.0
A GUI-less version of Bio .NET library for editing & annotating various microscopy image formats.
Loading...
Searching...
No Matches
BioLib.ROI.Mask Class Reference

Represents a Mask layer. More...

Inheritance diagram for BioLib.ROI.Mask:

Public Member Functions

bool IsSelected (int x, int y)
 
float GetValue (int x, int y)
 
void SetValue (int x, int y, float val)
 
 Mask (float[] fts, int width, int height, double physX, double physY, double x, double y)
 
 Mask (byte[] fts, int width, int height, double physX, double physY, double x, double y)
 
Gdk.Pixbuf GetColored (AForge.Color col, byte alpha, bool forceUpdate=false)
 
byte[] GetColoredBytes (AForge.Color col)
 
byte[] GetBytes ()
 
void Dispose ()
 

Static Public Member Functions

static byte[] int int int int startY OutputAs8BitImage (float[] fullMask, int imageWidth, int imageHeight, float threshold=0.0f)
 
static float float max GetMinAndMax (float[] floatArray)
 
static float[] int int int int startY CropFullImageMask (float[] fullMask, int imageWidth, int imageHeight, float threshold=0.0f)
 

Public Attributes

float min = 0
 

Static Public Attributes

static byte[] grayImage
 Crops a mask based on non-zero values and converts it to an 8-bit grayscale image.
 
static byte[] int cropWidth
 
static byte[] int int cropHeight
 
static byte[] int int int startX
 
static float min
 Gets the minimum and maximum values from a float array.
 
static float[] croppedMask
 Crops the smallest rectangular region containing all "white" pixels (values above a threshold) from a full image mask.
 
static float[] int cropWidth
 
static float[] int int cropHeight
 
static float[] int int int startX
 

Properties

double X [get, set]
 
double Y [get, set]
 
int Width [get, set]
 
int Height [get, set]
 
double PhysicalSizeX [get, set]
 
double PhysicalSizeY [get, set]
 
Gdk.Pixbuf Pixbuf [get]
 

Detailed Description

Represents a Mask layer.

Constructor & Destructor Documentation

◆ Mask() [1/2]

BioLib.ROI.Mask.Mask ( float[] fts,
int width,
int height,
double physX,
double physY,
double x,
double y )
255 {
256 this.width = width;
257 this.height = height;
258 X = x; Y = y;
259 PhysicalSizeX = physX;
260 PhysicalSizeY = physY;
261 mask = fts;
262 byte[] bt = GetBytesCropped();
263 mask = new float[bt.Length];
264 for (int i = 0; i < bt.Length; i++)
265 {
266 mask[i] = (float)bt[i];
267 }
268 }

◆ Mask() [2/2]

BioLib.ROI.Mask.Mask ( byte[] fts,
int width,
int height,
double physX,
double physY,
double x,
double y )
270 {
271 this.width = width;
272 this.height = height;
273 this.X = x / physX;
274 this.Y = y / physY;
275 PhysicalSizeX = physX;
276 PhysicalSizeY = physY;
277 mask = new float[fts.Length];
278 for (int i = 0; i < fts.Length; i++)
279 {
280 mask[i] = (float)fts[i];
281 }
282 }

Member Function Documentation

◆ CropFullImageMask()

static float[] int int int int startY BioLib.ROI.Mask.CropFullImageMask ( float[] fullMask,
int imageWidth,
int imageHeight,
float threshold = 0::0f )
static
524 {
525 if (fullMask == null || fullMask.Length != imageWidth * imageHeight)
526 throw new ArgumentException("Invalid mask dimensions or null mask.");
527
528 // Initialize bounding box values
529 int minX = imageWidth, minY = imageHeight, maxX = -1, maxY = -1;
530
531 // Find the bounding box of "white" pixels
532 for (int y = 0; y < imageHeight; y++)
533 {
534 for (int x = 0; x < imageWidth; x++)
535 {
536 int index = y * imageWidth + x;
537 if (fullMask[index] > threshold) // Pixel exceeds threshold, considered "white"
538 {
539 minX = Math.Min(minX, x);
540 minY = Math.Min(minY, y);
541 maxX = Math.Max(maxX, x);
542 maxY = Math.Max(maxY, y);
543 }
544 }
545 }
546
547 // Handle cases where no white pixels are found
548 if (minX > maxX || minY > maxY)
549 return (Array.Empty<float>(), 0, 0, 0, 0); // Return an empty mask
550
551 // Calculate the cropped region dimensions
552 int cropWidth = maxX - minX + 1;
553 int cropHeight = maxY - minY + 1;
554
555 // Extract the cropped region
556 float[] croppedMask = new float[cropWidth * cropHeight];
557 for (int y = 0; y < cropHeight; y++)
558 {
559 for (int x = 0; x < cropWidth; x++)
560 {
561 int sourceIndex = (minY + y) * imageWidth + (minX + x);
562 int targetIndex = y * cropWidth + x;
563
564 croppedMask[targetIndex] = fullMask[sourceIndex];
565 }
566 }
567
568 return (croppedMask, cropWidth, cropHeight, minX, minY);
569 }
static float[] croppedMask
Crops the smallest rectangular region containing all "white" pixels (values above a threshold) from a...
Definition ROI.cs:522

◆ Dispose()

void BioLib.ROI.Mask.Dispose ( )
572 {
573 if (pixbuf != null)
574 pixbuf.Dispose();
575 if (colored != null)
576 colored.Dispose();
577 mask = null;
578 }

◆ GetBytes()

byte[] BioLib.ROI.Mask.GetBytes ( )
494 {
495 byte[] rgbaBytes = new byte[width * height];
496 for (int y = 0; y < Height; y++)
497 {
498 for (int x = 0; x < Width; x++)
499 {
500 int index = y * Width + x; // 1D index for the mask
501 // Set RGBA channels
502 rgbaBytes[index] = (byte)mask[index];
503 }
504 }
505 return rgbaBytes;
506 }

◆ GetColored()

Gdk.Pixbuf BioLib.ROI.Mask.GetColored ( AForge.Color col,
byte alpha,
bool forceUpdate = false )
353 {
354 if (updateColored || forceUpdate)
355 {
356 UpdateColored(col, alpha);
357 return colored;
358 }
359 else
360 return colored;
361 }

◆ GetColoredBytes()

byte[] BioLib.ROI.Mask.GetColoredBytes ( AForge.Color col)
364 {
365 byte[] pixelData = new byte[width * height];
366 for (int y = 0; y < height; y++)
367 {
368 for (int x = 0; x < width; x++)
369 {
370 int ind = y * width + x;
371
372 if (ind < mask.Length)
373 {
374 float value = mask[ind];
375 if (value > 0)
376 {
377 pixelData[ind] = (byte)value;
378 }
379 }
380 }
381 }
382 return pixelData;
383 }

◆ GetMinAndMax()

static float float max BioLib.ROI.Mask.GetMinAndMax ( float[] floatArray)
static
430 {
431 if (floatArray == null || floatArray.Length == 0)
432 throw new ArgumentException("Input array must not be null or empty.");
433
434 float min = floatArray[0];
435 float max = floatArray[0];
436
437 foreach (float value in floatArray)
438 {
439 if (value < min)
440 {
441 min = value;
442 }
443 if (value > max)
444 {
445 max = value;
446 }
447 }
448
449 return (min, max);
450 }

◆ GetValue()

float BioLib.ROI.Mask.GetValue ( int x,
int y )
239 {
240 int ind = y * width + x;
241 if (ind > mask.Length)
242 throw new ArgumentException("Point " + x + "," + y + " is outside the mask.");
243 return mask[ind];
244 }

◆ IsSelected()

bool BioLib.ROI.Mask.IsSelected ( int x,
int y )
228 {
229 int ind = y * width + x;
230 if (ind >= mask.Length)
231 return false;
232 if (mask[ind] > min)
233 {
234 return true;
235 }
236 return false;
237 }

◆ OutputAs8BitImage()

static byte[] int int int int startY BioLib.ROI.Mask.OutputAs8BitImage ( float[] fullMask,
int imageWidth,
int imageHeight,
float threshold = 0::0f )
static
400 {
401 if (fullMask == null || fullMask.Length != imageWidth * imageHeight)
402 throw new ArgumentException("Invalid mask dimensions or null mask.");
403
404 // Crop the mask using the threshold
405 var cropResult = CropFullImageMask(fullMask, imageWidth, imageHeight, threshold);
406 float[] croppedMask = cropResult.croppedMask;
407
408 // Handle empty crop
409 if (croppedMask == null || croppedMask.Length == 0)
410 return (Array.Empty<byte>(), 0, 0, 0, 0);
411
412 int cropWidth = cropResult.cropWidth;
413 int cropHeight = cropResult.cropHeight;
414
415 // Normalize and convert to 8-bit grayscale
416 byte[] grayImage = new byte[cropWidth * cropHeight];
417 for (int i = 0; i < croppedMask.Length; i++)
418 {
419 grayImage[i] = (byte)(Math.Clamp(croppedMask[i], 0.0f, 1.0f) * 255);
420 }
421
422 return (grayImage, cropWidth, cropHeight, cropResult.startX, cropResult.startY);
423 }
static byte[] grayImage
Crops a mask based on non-zero values and converts it to an 8-bit grayscale image.
Definition ROI.cs:398

◆ SetValue()

void BioLib.ROI.Mask.SetValue ( int x,
int y,
float val )
246 {
247 int ind = y * width + x;
248 if (ind > mask.Length)
249 throw new ArgumentException("Point " + x + "," + y + " is outside the mask.");
250 mask[ind] = val;
251 updatePixbuf = true;
252 updateColored = true;
253 }

Member Data Documentation

◆ croppedMask

float [] BioLib.ROI.Mask.croppedMask
static

Crops the smallest rectangular region containing all "white" pixels (values above a threshold) from a full image mask.

Parameters
fullMaskThe input float array representing the entire image mask.
imageWidthThe width of the full image.
imageHeightThe height of the full image.
thresholdThe threshold to consider a pixel as "white".
Returns
A tuple containing:
  • Cropped float array mask.
  • Crop width.
  • Crop height.
  • Starting X and Y coordinates of the crop in the original image.

◆ grayImage

byte [] BioLib.ROI.Mask.grayImage
static

Crops a mask based on non-zero values and converts it to an 8-bit grayscale image.

Parameters
fullMaskThe input float array representing the full image mask.
imageWidthThe width of the full image.
imageHeightThe height of the full image.
thresholdThe threshold to consider a pixel as part of the crop.
Returns
A tuple containing:
  • 8-bit grayscale byte array.
  • Width and height of the cropped region.
  • Starting X and Y coordinates of the crop in the original image.

◆ min

float BioLib.ROI.Mask.min
static

Gets the minimum and maximum values from a float array.

Parameters
floatArrayThe input float array.
Returns
A tuple containing the minimum and maximum values.

Property Documentation

◆ Height

int BioLib.ROI.Mask.Height
getset
211{ get { return height; } set { height = value; } }

◆ PhysicalSizeX

double BioLib.ROI.Mask.PhysicalSizeX
getset
212{ get; set; }

◆ PhysicalSizeY

double BioLib.ROI.Mask.PhysicalSizeY
getset
213{ get; set; }

◆ Pixbuf

Gdk.Pixbuf BioLib.ROI.Mask.Pixbuf
get
284 {
285 get
286 {
287 if (updatePixbuf)
288 {
289 if (pixbuf != null)
290 pixbuf.Dispose();
291 byte[] pixelData = new byte[width * height * 4];
292 for (int y = 0; y < height; y++)
293 {
294 for (int x = 0; x < width; x++)
295 {
296 int ind = y * width + x;
297 if (mask[ind] > 0)
298 {
299 pixelData[4 * ind] = (byte)(mask[ind] / 255);// Blue
300 pixelData[4 * ind + 1] = (byte)(mask[ind] / 255);// Green
301 pixelData[4 * ind + 2] = (byte)(mask[ind] / 255);// Red
302 pixelData[4 * ind + 3] = 125;// Alpha
303 }
304 else
305 pixelData[4 * ind + 3] = 0;
306 }
307 }
308 pixbuf = new Gdk.Pixbuf(pixelData, true, 8, width, height, width * 4);
309 updatePixbuf = false;
310 return pixbuf;
311 }
312 else
313 return pixbuf;
314 }
315 }

◆ Width

int BioLib.ROI.Mask.Width
getset
210{ get { return width; } set { width = value; } }

◆ X

double BioLib.ROI.Mask.X
getset
208{ get; set; }

◆ Y

double BioLib.ROI.Mask.Y
getset
209{ get; set; }

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