private bool NeedSquareRoot(DistanceTransformMethod distance)
        {
            switch (distance)
            {
            case DistanceTransformMethod.Euclidean:
                return(true);

            case DistanceTransformMethod.Chessboard:
            case DistanceTransformMethod.Manhattan:
            case DistanceTransformMethod.SquaredEuclidean:
                return(false);
            }

            throw new Exception();
        }
Esempio n. 2
0
        public void LoadFromString(TifFileInfo fi, string str)
        {
            string[] vals           = str.Split(new string[] { "\t" }, StringSplitOptions.None);
            int      channel        = int.Parse(vals[0]);
            float    tol            = float.Parse(vals[1]);
            int      thresholdValue = int.Parse(vals[2]);
            int      distanceInd    = int.Parse(vals[3]);

            //fill holes part
            bool fillHoles = false;

            if (vals.Length == 5)
            {
                fillHoles = bool.Parse(vals[4]);
            }
            fi.fillHoles = fillHoles;

            //distance type
            DistanceTransformMethod dist = DistanceTransformMethod.Euclidean;

            switch (distanceInd)
            {
            //"Euclidean", "SquaredEuclidean", "Chessboard", "Manhattan"
            case 0:
                dist = DistanceTransformMethod.Euclidean;
                break;

            case 1:
                dist = DistanceTransformMethod.SquaredEuclidean;
                break;

            case 2:
                dist = DistanceTransformMethod.Chessboard;
                break;

            case 3:
                dist = DistanceTransformMethod.Manhattan;
                break;
            }

            ProcessFilter(fi, channel, tol, thresholdValue, dist);
            fi.isBinary[channel] = true;
        }
        private int calcDistance(int x, int y, int x0, int y0, DistanceTransformMethod distance)
        {
            switch (distance)
            {
            case DistanceTransformMethod.Euclidean:
                return((x - x0) * (x - x0) + (y - y0) * (y - y0));

            case DistanceTransformMethod.Manhattan:
                return(Math.Abs(x - x0) + Math.Abs(y - y0));

            case DistanceTransformMethod.Chessboard:
                return(Math.Max(Math.Abs(x - x0), Math.Abs(y - y0)));

            case DistanceTransformMethod.SquaredEuclidean:
                return((x - x0) * (x - x0) + (y - y0) * (y - y0));
            }

            throw new Exception();
        }
Esempio n. 4
0
        private void createDialog()
        {
            Dialog.FormBorderStyle = FormBorderStyle.FixedDialog;
            Dialog.Text            = "Watershed";
            Dialog.StartPosition   = FormStartPosition.CenterScreen;
            Dialog.WindowState     = FormWindowState.Normal;
            Dialog.MinimizeBox     = false;
            Dialog.MaximizeBox     = false;
            Dialog.BackColor       = IA.FileBrowser.BackGround2Color1;
            Dialog.ForeColor       = IA.FileBrowser.ShriftColor1;

            Dialog.FormClosing += new FormClosingEventHandler(delegate(object o, FormClosingEventArgs a)
            {
                Dialog.Visible = false;
                a.Cancel       = true;
            });

            Dialog.Width  = 240;
            Dialog.Height = 190;

            Dialog.SuspendLayout();

            /*
             * Label ThresholdLab = new Label();
             * ThresholdLab.Text = "Select threshold:";
             * ThresholdLab.Width = 100;
             * ThresholdLab.Location = new System.Drawing.Point(10,15);
             * Dialog.Controls.Add(ThresholdLab);
             *
             * ThresholdCB.Width = 30;
             * ThresholdCB.Location = new System.Drawing.Point(110, 13);
             * Dialog.Controls.Add(ThresholdCB);
             * ThresholdCB.SelectedIndexChanged += new EventHandler(delegate (object o, EventArgs a)
             * {
             *  threshold = ThresholdCB.SelectedIndex + 1;
             * });
             */

            Label ToleranceLab = new Label();

            ToleranceLab.Text     = "Tolerance:";
            ToleranceLab.Width    = 100;
            ToleranceLab.Location = new System.Drawing.Point(10, 15);
            Dialog.Controls.Add(ToleranceLab);

            ToleranceTB.Text     = tolerance.ToString();
            ToleranceTB.Width    = 30;
            ToleranceTB.Location = new System.Drawing.Point(110, 13);
            Dialog.Controls.Add(ToleranceTB);

            Label DistanceLab = new Label();

            DistanceLab.Text     = "Distance Transform:";
            DistanceLab.Width    = 100;
            DistanceLab.Location = new System.Drawing.Point(10, 45);
            Dialog.Controls.Add(DistanceLab);

            ComboBox DistanceCB = new ComboBox();

            DistanceCB.Width = 110;
            DistanceCB.Items.AddRange(new string[] { "Euclidean", "SquaredEuclidean", "Chessboard", "Manhattan" });
            DistanceCB.SelectedIndex         = 0;
            DistanceCB.SelectedIndexChanged += new EventHandler(delegate(object o, EventArgs a)
            {
                switch (DistanceCB.SelectedIndex)
                {
                case 0:
                    distance = DistanceTransformMethod.Euclidean;
                    break;

                case 1:
                    distance = DistanceTransformMethod.SquaredEuclidean;
                    break;

                case 2:
                    distance = DistanceTransformMethod.Chessboard;
                    break;

                case 3:
                    distance = DistanceTransformMethod.Manhattan;
                    break;

                default:
                    distance = DistanceTransformMethod.Euclidean;
                    break;
                }
            });
            DistanceCB.Location = new System.Drawing.Point(110, 43);
            Dialog.Controls.Add(DistanceCB);

            FillHolesCB.Text     = "Fill holes";
            FillHolesCB.Location = new System.Drawing.Point(15, 75);
            Dialog.Controls.Add(FillHolesCB);

            //buttons
            Panel okBox = new Panel();

            okBox.Height = 40;
            okBox.Dock   = DockStyle.Bottom;
            Dialog.Controls.Add(okBox);

            Button okBtn = new Button();

            okBtn.Text      = "Process";
            okBtn.BackColor = System.Drawing.SystemColors.ButtonFace;
            okBtn.ForeColor = System.Drawing.Color.Black;
            okBtn.Location  = new System.Drawing.Point(20, 10);
            okBtn.Anchor    = AnchorStyles.Top | AnchorStyles.Left;
            okBox.Controls.Add(okBtn);

            Button cancelBtn = new Button();

            cancelBtn.Text      = "Cancel";
            cancelBtn.BackColor = System.Drawing.SystemColors.ButtonFace;
            cancelBtn.Location  = new System.Drawing.Point(Dialog.Width - cancelBtn.Width - 60, 10);
            cancelBtn.ForeColor = System.Drawing.Color.Black;
            cancelBtn.Anchor    = AnchorStyles.Top | AnchorStyles.Right;
            okBox.Controls.Add(cancelBtn);

            okBtn.Click += new EventHandler(delegate(object sender, EventArgs e)
            {
                try
                {
                    tolerance = float.Parse(ToleranceTB.Text);
                }
                catch
                {
                    MessageBox.Show("Tolerance must be numeric!");
                    return;
                }
                Dialog.Visible = false;
                //event
                TifFileInfo fi = null;
                try
                {
                    fi = IA.TabPages.TabCollections[IA.TabPages.SelectedIndex].tifFI;
                }
                catch { return; }

                if (fi == null)
                {
                    return;
                }
                if (!fi.available)
                {
                    MessageBox.Show("Image is not ready yet! \nTry again later.");
                    return;
                }
                int C = fi.cValue;
                //background worker
                var bgw = new BackgroundWorker();
                bgw.WorkerReportsProgress = true;
                fi.available = false;
                fi.fillHoles = FillHolesCB.Checked;
                //Add event for projection here
                bgw.DoWork += new DoWorkEventHandler(delegate(Object o, DoWorkEventArgs a)
                {
                    ProcessFilter(fi, C, this.tolerance, 1, this.distance);
                    ((BackgroundWorker)o).ReportProgress(0);
                });

                bgw.ProgressChanged += new ProgressChangedEventHandler(delegate(Object o, ProgressChangedEventArgs a)
                {
                    if (a.ProgressPercentage == 0)
                    {
                        IA.Segmentation.MyFilters.addToHistoryOldInfo(C, fi);

                        fi.newFilterHistory[C].Add(
                            ToString(fi, C, this.tolerance, 1, this.distance));

                        IA.Segmentation.MyFilters.addToHistoryNewInfo(C, fi);

                        IA.MarkAsNotSaved();
                        IA.ReloadImages();
                    }
                    fi.available = true;
                    IA.FileBrowser.StatusLabel.Text = "Ready";
                });
                //Start background worker
                IA.FileBrowser.StatusLabel.Text = "Watershed...";
                //start bgw
                bgw.RunWorkerAsync();
            });

            cancelBtn.Click += new EventHandler(delegate(object sender, EventArgs e)
            {
                Dialog.Visible = false;
            });

            Dialog.KeyPreview = true;
            Dialog.KeyDown   += new KeyEventHandler(delegate(object sender, KeyEventArgs e)
            {
                switch (e.KeyCode)
                {
                case Keys.Escape:
                    Dialog.Visible = false;
                    break;

                case Keys.Enter:
                    okBtn.PerformClick();
                    break;

                default:
                    break;
                }
            });

            Dialog.ResumeLayout();
        }
Esempio n. 5
0
        public string ToString(TifFileInfo fi, int channel, float tol, int thresholdValue, DistanceTransformMethod dist)
        {
            int distanceInd = 0;

            switch (dist)
            {
            //"Euclidean", "SquaredEuclidean", "Chessboard", "Manhattan"
            case DistanceTransformMethod.Euclidean:
                distanceInd = 0;
                break;

            case DistanceTransformMethod.SquaredEuclidean:
                distanceInd = 1;
                break;

            case DistanceTransformMethod.Chessboard:
                distanceInd = 2;
                break;

            case DistanceTransformMethod.Manhattan:
                distanceInd = 3;
                break;
            }

            return("wshed\t" +
                   channel + "\t" +
                   tol + "\t" +
                   thresholdValue + "\t" +
                   distanceInd + "\t" +
                   fi.fillHoles.ToString());
        }
Esempio n. 6
0
        public void ProcessFilter(TifFileInfo fi, int channel, float tolerance, int thresholdValue, DistanceTransformMethod distance)
        {
            //Apply in the history
            if (fi.newFilterHistory == null || fi.newFilterHistory.Length != fi.sizeC)
            {
                fi.isBinary         = new bool[fi.sizeC];
                fi.newFilterHistory = new List <string> [fi.sizeC];
                for (int i = 0; i < fi.sizeC; i++)
                {
                    fi.newFilterHistory[i] = new List <string>();
                    fi.isBinary[i]         = false;
                }
            }
            fi.isBinary[fi.cValue] = true;
            Filters.MyConvolution.CheckIsImagePrepared(fi);

            int[] indexes = new int[fi.imageCount / fi.sizeC];

            for (int i = channel, position = 0; i < fi.imageCount; i += fi.sizeC, position++)
            {
                indexes[position] = i;
            }

            foreach (int frame in indexes)
            {
                UnmanagedImage input = CreateImage(frame, fi, thresholdValue);

                BinaryWatershed bw = new BinaryWatershed(tolerance, distance);
                input = bw.Apply(input);

                ReturnCTImage(frame, fi, input);
            }
        }
        private float minDist2(int[] points, int pPrev, int pDiag, int x, int y, int distSqr, DistanceTransformMethod distance)
        {
            int p0           = points[x]; // the nearest background point for the same x in the previous line
            int nearestPoint = p0;

            if (p0 != -1)
            {
                int x0 = p0 & 0xffff; int y0 = (p0 >> 16) & 0xffff;
                int dist1Sqr = calcDistance(x, y, x0, y0, distance);
                if (dist1Sqr < distSqr)
                {
                    distSqr = dist1Sqr;
                }
            }

            if (pDiag != p0 && pDiag != -1)
            {
                int x1 = pDiag & 0xffff; int y1 = (pDiag >> 16) & 0xffff;
                int dist1Sqr = calcDistance(x, y, x1, y1, distance);
                if (dist1Sqr < distSqr)
                {
                    nearestPoint = pDiag;
                    distSqr      = dist1Sqr;
                }
            }

            if (pPrev != pDiag && pPrev != -1)
            {
                int x1 = pPrev & 0xffff; int y1 = (pPrev >> 16) & 0xffff;
                int dist1Sqr = calcDistance(x, y, x1, y1, distance);
                if (dist1Sqr < distSqr)
                {
                    nearestPoint = pPrev;
                    distSqr      = dist1Sqr;
                }
            }

            points[x] = nearestPoint;
            return((float)distSqr);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="DistanceTransform"/> class.
 /// </summary>
 ///
 public DistanceTransform(DistanceTransformMethod method)
     : this()
 {
     this.distance = method;
 }
Esempio n. 9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BinaryWatershed"/> class.
 /// </summary>
 ///
 /// <param name="distance">The distance method.</param>
 ///
 public BinaryWatershed(DistanceTransformMethod distance)
     : this()
 {
     this.distance = distance;
 }
Esempio n. 10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BinaryWatershed"/> class.
 /// </summary>
 ///
 /// <param name="tolerance">The tolerance. Default is 0.5f.</param>
 /// <param name="distance">The distance method.</param>
 ///
 public BinaryWatershed(float tolerance, DistanceTransformMethod distance)
     : this(tolerance)
 {
     this.distance = distance;
 }