BioLib  3.9.1
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 )
270 {
271 this.width = width;
272 this.height = height;
273 X = x; Y = y;
274 PhysicalSizeX = physX;
275 PhysicalSizeY = physY;
276 mask = fts;
277 byte[] bt = GetBytesCropped();
278 mask = new float[bt.Length];
279 for (int i = 0; i < bt.Length; i++)
280 {
281 mask[i] = (float)bt[i];
282 }
283 }

◆ Mask() [2/2]

BioLib.ROI.Mask.Mask ( byte[] fts,
int width,
int height,
double physX,
double physY,
double x,
double y )
285 {
286 this.width = width;
287 this.height = height;
288 this.X = x / physX;
289 this.Y = y / physY;
290 PhysicalSizeX = physX;
291 PhysicalSizeY = physY;
292 mask = new float[fts.Length];
293 for (int i = 0; i < fts.Length; i++)
294 {
295 mask[i] = (float)fts[i];
296 }
297 }

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
539 {
540 if (fullMask == null || fullMask.Length != imageWidth * imageHeight)
541 throw new ArgumentException("Invalid mask dimensions or null mask.");
542
543 // Initialize bounding box values
544 int minX = imageWidth, minY = imageHeight, maxX = -1, maxY = -1;
545
546 // Find the bounding box of "white" pixels
547 for (int y = 0; y < imageHeight; y++)
548 {
549 for (int x = 0; x < imageWidth; x++)
550 {
551 int index = y * imageWidth + x;
552 if (fullMask[index] > threshold) // Pixel exceeds threshold, considered "white"
553 {
554 minX = Math.Min(minX, x);
555 minY = Math.Min(minY, y);
556 maxX = Math.Max(maxX, x);
557 maxY = Math.Max(maxY, y);
558 }
559 }
560 }
561
562 // Handle cases where no white pixels are found
563 if (minX > maxX || minY > maxY)
564 return (Array.Empty<float>(), 0, 0, 0, 0); // Return an empty mask
565
566 // Calculate the cropped region dimensions
567 int cropWidth = maxX - minX + 1;
568 int cropHeight = maxY - minY + 1;
569
570 // Extract the cropped region
571 float[] croppedMask = new float[cropWidth * cropHeight];
572 for (int y = 0; y < cropHeight; y++)
573 {
574 for (int x = 0; x < cropWidth; x++)
575 {
576 int sourceIndex = (minY + y) * imageWidth + (minX + x);
577 int targetIndex = y * cropWidth + x;
578
579 croppedMask[targetIndex] = fullMask[sourceIndex];
580 }
581 }
582
583 return (croppedMask, cropWidth, cropHeight, minX, minY);
584 }
static float[] croppedMask
Crops the smallest rectangular region containing all "white" pixels (values above a threshold) from a...
Definition ROI.cs:537

◆ Dispose()

void BioLib.ROI.Mask.Dispose ( )
587 {
588 if (pixbuf != null)
589 pixbuf.Dispose();
590 if (colored != null)
591 colored.Dispose();
592 mask = null;
593 }

◆ GetBytes()

byte[] BioLib.ROI.Mask.GetBytes ( )
509 {
510 byte[] rgbaBytes = new byte[width * height];
511 for (int y = 0; y < Height; y++)
512 {
513 for (int x = 0; x < Width; x++)
514 {
515 int index = y * Width + x; // 1D index for the mask
516 // Set RGBA channels
517 rgbaBytes[index] = (byte)mask[index];
518 }
519 }
520 return rgbaBytes;
521 }

◆ GetColored()

Gdk.Pixbuf BioLib.ROI.Mask.GetColored ( AForge.Color col,
byte alpha,
bool forceUpdate = false )
368 {
369 if (updateColored || forceUpdate)
370 {
371 UpdateColored(col, alpha);
372 return colored;
373 }
374 else
375 return colored;
376 }

◆ GetColoredBytes()

byte[] BioLib.ROI.Mask.GetColoredBytes ( AForge.Color col)
379 {
380 byte[] pixelData = new byte[width * height];
381 for (int y = 0; y < height; y++)
382 {
383 for (int x = 0; x < width; x++)
384 {
385 int ind = y * width + x;
386
387 if (ind < mask.Length)
388 {
389 float value = mask[ind];
390 if (value > 0)
391 {
392 pixelData[ind] = (byte)value;
393 }
394 }
395 }
396 }
397 return pixelData;
398 }

◆ GetMinAndMax()

static float float max BioLib.ROI.Mask.GetMinAndMax ( float[] floatArray)
static
445 {
446 if (floatArray == null || floatArray.Length == 0)
447 throw new ArgumentException("Input array must not be null or empty.");
448
449 float min = floatArray[0];
450 float max = floatArray[0];
451
452 foreach (float value in floatArray)
453 {
454 if (value < min)
455 {
456 min = value;
457 }
458 if (value > max)
459 {
460 max = value;
461 }
462 }
463
464 return (min, max);
465 }

◆ GetValue()

float BioLib.ROI.Mask.GetValue ( int x,
int y )
254 {
255 int ind = y * width + x;
256 if (ind > mask.Length)
257 throw new ArgumentException("Point " + x + "," + y + " is outside the mask.");
258 return mask[ind];
259 }

◆ IsSelected()

bool BioLib.ROI.Mask.IsSelected ( int x,
int y )
243 {
244 int ind = y * width + x;
245 if (ind >= mask.Length)
246 return false;
247 if (mask[ind] > min)
248 {
249 return true;
250 }
251 return false;
252 }

◆ OutputAs8BitImage()

static byte[] int int int int startY BioLib.ROI.Mask.OutputAs8BitImage ( float[] fullMask,
int imageWidth,
int imageHeight,
float threshold = 0::0f )
static
415 {
416 if (fullMask == null || fullMask.Length != imageWidth * imageHeight)
417 throw new ArgumentException("Invalid mask dimensions or null mask.");
418
419 // Crop the mask using the threshold
420 var cropResult = CropFullImageMask(fullMask, imageWidth, imageHeight, threshold);
421 float[] croppedMask = cropResult.croppedMask;
422
423 // Handle empty crop
424 if (croppedMask == null || croppedMask.Length == 0)
425 return (Array.Empty<byte>(), 0, 0, 0, 0);
426
427 int cropWidth = cropResult.cropWidth;
428 int cropHeight = cropResult.cropHeight;
429
430 // Normalize and convert to 8-bit grayscale
431 byte[] grayImage = new byte[cropWidth * cropHeight];
432 for (int i = 0; i < croppedMask.Length; i++)
433 {
434 grayImage[i] = (byte)(Math.Clamp(croppedMask[i], 0.0f, 1.0f) * 255);
435 }
436
437 return (grayImage, cropWidth, cropHeight, cropResult.startX, cropResult.startY);
438 }
static byte[] grayImage
Crops a mask based on non-zero values and converts it to an 8-bit grayscale image.
Definition ROI.cs:413

◆ SetValue()

void BioLib.ROI.Mask.SetValue ( int x,
int y,
float val )
261 {
262 int ind = y * width + x;
263 if (ind > mask.Length)
264 throw new ArgumentException("Point " + x + "," + y + " is outside the mask.");
265 mask[ind] = val;
266 updatePixbuf = true;
267 updateColored = true;
268 }

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
226{ get { return height; } set { height = value; } }

◆ PhysicalSizeX

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

◆ PhysicalSizeY

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

◆ Pixbuf

Gdk.Pixbuf BioLib.ROI.Mask.Pixbuf
get
299 {
300 get
301 {
302 if (updatePixbuf)
303 {
304 if (pixbuf != null)
305 pixbuf.Dispose();
306 byte[] pixelData = new byte[width * height * 4];
307 for (int y = 0; y < height; y++)
308 {
309 for (int x = 0; x < width; x++)
310 {
311 int ind = y * width + x;
312 if (mask[ind] > 0)
313 {
314 pixelData[4 * ind] = (byte)(mask[ind] / 255);// Blue
315 pixelData[4 * ind + 1] = (byte)(mask[ind] / 255);// Green
316 pixelData[4 * ind + 2] = (byte)(mask[ind] / 255);// Red
317 pixelData[4 * ind + 3] = 125;// Alpha
318 }
319 else
320 pixelData[4 * ind + 3] = 0;
321 }
322 }
323 pixbuf = new Gdk.Pixbuf(pixelData, true, 8, width, height, width * 4);
324 updatePixbuf = false;
325 return pixbuf;
326 }
327 else
328 return pixbuf;
329 }
330 }

◆ Width

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

◆ X

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

◆ Y

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

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