BioLib  3.6.2
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.

Definition at line 493 of file Bio.cs.

Constructor & Destructor Documentation

◆ Mask() [1/2]

BioLib.ROI.Mask.Mask ( float[] fts,
int width,
int height,
double physX,
double physY,
double x,
double y )

Definition at line 545 of file Bio.cs.

546 {
547 this.width = width;
548 this.height = height;
549 X = x; Y = y;
550 PhysicalSizeX = physX;
551 PhysicalSizeY = physY;
552 mask = fts;
553 byte[] bt = GetBytesCropped();
554 mask = new float[bt.Length];
555 for (int i = 0; i < bt.Length; i++)
556 {
557 mask[i] = (float)bt[i];
558 }
559 }

◆ Mask() [2/2]

BioLib.ROI.Mask.Mask ( byte[] fts,
int width,
int height,
double physX,
double physY,
double x,
double y )

Definition at line 560 of file Bio.cs.

561 {
562 this.width = width;
563 this.height = height;
564 this.X = x / physX;
565 this.Y = y / physY;
566 PhysicalSizeX = physX;
567 PhysicalSizeY = physY;
568 mask = new float[fts.Length];
569 for (int i = 0; i < fts.Length; i++)
570 {
571 mask[i] = (float)fts[i];
572 }
573 }

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

Definition at line 813 of file Bio.cs.

815 {
816 if (fullMask == null || fullMask.Length != imageWidth * imageHeight)
817 throw new ArgumentException("Invalid mask dimensions or null mask.");
818
819 // Initialize bounding box values
820 int minX = imageWidth, minY = imageHeight, maxX = -1, maxY = -1;
821
822 // Find the bounding box of "white" pixels
823 for (int y = 0; y < imageHeight; y++)
824 {
825 for (int x = 0; x < imageWidth; x++)
826 {
827 int index = y * imageWidth + x;
828 if (fullMask[index] > threshold) // Pixel exceeds threshold, considered "white"
829 {
830 minX = Math.Min(minX, x);
831 minY = Math.Min(minY, y);
832 maxX = Math.Max(maxX, x);
833 maxY = Math.Max(maxY, y);
834 }
835 }
836 }
837
838 // Handle cases where no white pixels are found
839 if (minX > maxX || minY > maxY)
840 return (Array.Empty<float>(), 0, 0, 0, 0); // Return an empty mask
841
842 // Calculate the cropped region dimensions
843 int cropWidth = maxX - minX + 1;
844 int cropHeight = maxY - minY + 1;
845
846 // Extract the cropped region
847 float[] croppedMask = new float[cropWidth * cropHeight];
848 for (int y = 0; y < cropHeight; y++)
849 {
850 for (int x = 0; x < cropWidth; x++)
851 {
852 int sourceIndex = (minY + y) * imageWidth + (minX + x);
853 int targetIndex = y * cropWidth + x;
854
855 croppedMask[targetIndex] = fullMask[sourceIndex];
856 }
857 }
858
859 return (croppedMask, cropWidth, cropHeight, minX, minY);
860 }
static float[] croppedMask
Crops the smallest rectangular region containing all "white" pixels (values above a threshold) from a...
Definition Bio.cs:813

◆ Dispose()

void BioLib.ROI.Mask.Dispose ( )

Definition at line 862 of file Bio.cs.

863 {
864 if (pixbuf != null)
865 pixbuf.Dispose();
866 if (colored != null)
867 colored.Dispose();
868 mask = null;
869 }

◆ GetBytes()

byte[] BioLib.ROI.Mask.GetBytes ( )

Definition at line 784 of file Bio.cs.

785 {
786 byte[] rgbaBytes = new byte[width * height];
787 for (int y = 0; y < Height; y++)
788 {
789 for (int x = 0; x < Width; x++)
790 {
791 int index = y * Width + x; // 1D index for the mask
792 // Set RGBA channels
793 rgbaBytes[index] = (byte)mask[index];
794 }
795 }
796 return rgbaBytes;
797 }

◆ GetColored()

Gdk.Pixbuf BioLib.ROI.Mask.GetColored ( AForge.Color col,
byte alpha,
bool forceUpdate = false )

Definition at line 643 of file Bio.cs.

644 {
645 if (updateColored || forceUpdate)
646 {
647 UpdateColored(col, alpha);
648 return colored;
649 }
650 else
651 return colored;
652 }

◆ GetColoredBytes()

byte[] BioLib.ROI.Mask.GetColoredBytes ( AForge.Color col)

Definition at line 654 of file Bio.cs.

655 {
656 byte[] pixelData = new byte[width * height];
657 for (int y = 0; y < height; y++)
658 {
659 for (int x = 0; x < width; x++)
660 {
661 int ind = y * width + x;
662
663 if (ind < mask.Length)
664 {
665 float value = mask[ind];
666 if (value > 0)
667 {
668 pixelData[ind] = (byte)value;
669 }
670 }
671 }
672 }
673 return pixelData;
674 }

◆ GetMinAndMax()

static float float max BioLib.ROI.Mask.GetMinAndMax ( float[] floatArray)
static

Definition at line 720 of file Bio.cs.

721 {
722 if (floatArray == null || floatArray.Length == 0)
723 throw new ArgumentException("Input array must not be null or empty.");
724
725 float min = floatArray[0];
726 float max = floatArray[0];
727
728 foreach (float value in floatArray)
729 {
730 if (value < min)
731 {
732 min = value;
733 }
734 if (value > max)
735 {
736 max = value;
737 }
738 }
739
740 return (min, max);
741 }

◆ GetValue()

float BioLib.ROI.Mask.GetValue ( int x,
int y )

Definition at line 529 of file Bio.cs.

530 {
531 int ind = y * width + x;
532 if (ind > mask.Length)
533 throw new ArgumentException("Point " + x + "," + y + " is outside the mask.");
534 return mask[ind];
535 }

◆ IsSelected()

bool BioLib.ROI.Mask.IsSelected ( int x,
int y )

Definition at line 518 of file Bio.cs.

519 {
520 int ind = y * width + x;
521 if (ind >= mask.Length)
522 return false;
523 if (mask[ind] > min)
524 {
525 return true;
526 }
527 return false;
528 }

◆ OutputAs8BitImage()

static byte[] int int int int startY BioLib.ROI.Mask.OutputAs8BitImage ( float[] fullMask,
int imageWidth,
int imageHeight,
float threshold = 0::0f )
static

Definition at line 689 of file Bio.cs.

691 {
692 if (fullMask == null || fullMask.Length != imageWidth * imageHeight)
693 throw new ArgumentException("Invalid mask dimensions or null mask.");
694
695 // Crop the mask using the threshold
696 var cropResult = CropFullImageMask(fullMask, imageWidth, imageHeight, threshold);
697 float[] croppedMask = cropResult.croppedMask;
698
699 // Handle empty crop
700 if (croppedMask == null || croppedMask.Length == 0)
701 return (Array.Empty<byte>(), 0, 0, 0, 0);
702
703 int cropWidth = cropResult.cropWidth;
704 int cropHeight = cropResult.cropHeight;
705
706 // Normalize and convert to 8-bit grayscale
707 byte[] grayImage = new byte[cropWidth * cropHeight];
708 for (int i = 0; i < croppedMask.Length; i++)
709 {
710 grayImage[i] = (byte)(Math.Clamp(croppedMask[i], 0.0f, 1.0f) * 255);
711 }
712
713 return (grayImage, cropWidth, cropHeight, cropResult.startX, cropResult.startY);
714 }
static byte[] grayImage
Crops a mask based on non-zero values and converts it to an 8-bit grayscale image.
Definition Bio.cs:689

◆ SetValue()

void BioLib.ROI.Mask.SetValue ( int x,
int y,
float val )

Definition at line 536 of file Bio.cs.

537 {
538 int ind = y * width + x;
539 if (ind > mask.Length)
540 throw new ArgumentException("Point " + x + "," + y + " is outside the mask.");
541 mask[ind] = val;
542 updatePixbuf = true;
543 updateColored = true;
544 }

Member Data Documentation

◆ cropHeight [1/2]

byte [] int int BioLib.ROI.Mask.cropHeight
static

Definition at line 689 of file Bio.cs.

◆ cropHeight [2/2]

float [] int int BioLib.ROI.Mask.cropHeight
static

Definition at line 813 of file Bio.cs.

◆ 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.

Definition at line 813 of file Bio.cs.

◆ cropWidth [1/2]

byte [] int BioLib.ROI.Mask.cropWidth
static

Definition at line 689 of file Bio.cs.

◆ cropWidth [2/2]

float [] int BioLib.ROI.Mask.cropWidth
static

Definition at line 813 of file Bio.cs.

◆ 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.

Definition at line 689 of file Bio.cs.

◆ min [1/2]

float BioLib.ROI.Mask.min = 0

Definition at line 495 of file Bio.cs.

◆ min [2/2]

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.

Definition at line 720 of file Bio.cs.

◆ startX [1/2]

byte [] int int int BioLib.ROI.Mask.startX
static

Definition at line 689 of file Bio.cs.

◆ startX [2/2]

float [] int int int BioLib.ROI.Mask.startX
static

Definition at line 813 of file Bio.cs.

Property Documentation

◆ Height

int BioLib.ROI.Mask.Height
getset

Definition at line 502 of file Bio.cs.

502{ get { return height; } set { height = value; } }

◆ PhysicalSizeX

double BioLib.ROI.Mask.PhysicalSizeX
getset

Definition at line 503 of file Bio.cs.

503{ get; set; }

◆ PhysicalSizeY

double BioLib.ROI.Mask.PhysicalSizeY
getset

Definition at line 504 of file Bio.cs.

504{ get; set; }

◆ Pixbuf

Gdk.Pixbuf BioLib.ROI.Mask.Pixbuf
get

Definition at line 574 of file Bio.cs.

575 {
576 get
577 {
578 if (updatePixbuf)
579 {
580 if (pixbuf != null)
581 pixbuf.Dispose();
582 byte[] pixelData = new byte[width * height * 4];
583 for (int y = 0; y < height; y++)
584 {
585 for (int x = 0; x < width; x++)
586 {
587 int ind = y * width + x;
588 if (mask[ind] > 0)
589 {
590 pixelData[4 * ind] = (byte)(mask[ind] / 255);// Blue
591 pixelData[4 * ind + 1] = (byte)(mask[ind] / 255);// Green
592 pixelData[4 * ind + 2] = (byte)(mask[ind] / 255);// Red
593 pixelData[4 * ind + 3] = 125;// Alpha
594 }
595 else
596 pixelData[4 * ind + 3] = 0;
597 }
598 }
599 pixbuf = new Gdk.Pixbuf(pixelData, true, 8, width, height, width * 4);
600 updatePixbuf = false;
601 return pixbuf;
602 }
603 else
604 return pixbuf;
605 }
606 }

◆ Width

int BioLib.ROI.Mask.Width
getset

Definition at line 501 of file Bio.cs.

501{ get { return width; } set { width = value; } }

◆ X

double BioLib.ROI.Mask.X
getset

Definition at line 499 of file Bio.cs.

499{ get; set; }

◆ Y

double BioLib.ROI.Mask.Y
getset

Definition at line 500 of file Bio.cs.

500{ get; set; }

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