Exemplo n.º 1
0
        public static System.Drawing.Image ThresholdImage(SySal.Imaging.DCTInterpolationImage dct)
        {
            int    maxsize = Math.Max(dct.Info.Width, dct.Info.Height);
            double f       = Math.Min(320, maxsize) / (double)maxsize;
            int    w       = (int)(f * dct.Info.Width);
            int    w1      = w + 1;
            int    h       = (int)(f * dct.Info.Height);
            int    h1      = h + 1;

            NumericalTools.Plot   pl  = new NumericalTools.Plot();
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(w * 2 + 100, h * 2 + 100);
            double[] x = new double[w1 * h1];
            double[] y = new double[w1 * h1];
            double[] t = new double[w1 * h1];
            int      ix = 0, iy = 0;

            for (iy = 0; iy < h; iy++)
            {
                for (ix = 0; ix < w; ix++)
                {
                    x[iy * w1 + ix] = ix / f;
                    y[iy * w1 + ix] = -iy / f;
                    t[iy * w1 + ix] = (double)((dct.Pixels[(ushort)x[iy * w1 + ix], (ushort)(-y[iy * w1 + ix]), 0] << 8) + dct.Pixels[(ushort)x[iy * w1 + ix], (ushort)(-y[iy * w1 + ix]), 1]);
                }
            }
            double minth = 0.0, maxth = 0.0, avgth = 0.0, rmsth = 0.0;

            NumericalTools.Fitting.FindStatistics(t, ref minth, ref maxth, ref avgth, ref rmsth);
            pl.LabelFont = new System.Drawing.Font("Segoe UI", 12);
            pl.PanelX    = 2.0;
            pl.PanelY    = 0.0;
            pl.VecX      = x;
            pl.VecY      = y;
            pl.VecZ      = t;
            pl.DX        = (float)(dct.Info.Width / w);
            pl.DY        = (float)(dct.Info.Height / h);
            pl.MinX      = 0.0;
            pl.MinX      = dct.Info.Width;
            pl.MinY      = -dct.Info.Height;
            pl.MaxY      = 0.0;
            pl.MinZ      = minth - 10.0;
            pl.MaxZ      = maxth + 10.0;
            pl.XTitle    = "X Pixels";
            pl.YTitle    = "-Y Pixels";
            pl.ZTitle    = "Threshold";
            pl.HueAreaComputedValues(System.Drawing.Graphics.FromImage(bmp), w * 2, h * 2);
            return(bmp);
        }
Exemplo n.º 2
0
 private void btnViewResult_Click(object sender, EventArgs e)
 {
     if (m_Result.Length <= 0)
     {
         return;
     }
     try
     {
         SySal.Imaging.DCTInterpolationImage dct = Scanner.DCTInterpolationImageFromString(m_Result);
         InfoPanel panel = new InfoPanel();
         panel.SetContent("Threshold image", SySalImageFromImage.ThresholdImage(dct));
         panel.TopLevel = true;
         panel.ShowDialog();
     }
     catch (Exception x)
     {
         MessageBox.Show(x.ToString(), "Error creating interpolated image.", MessageBoxButtons.OK, MessageBoxIcon.Error);
         return;
     }
 }
Exemplo n.º 3
0
        void ApplyGPUConfig()
        {
            int i;

            SySal.Imaging.Image eqim = (IC.EmptyImage != null && IC.EmptyImage.Length > 0) ? SySal.Imaging.Base64ImageEncoding.ImageFromBase64(IC.EmptyImage) : null;
            SySal.Imaging.DCTInterpolationImage dctim = (IC.ThresholdImage != null && IC.ThresholdImage.Length > 0) ? DCTInterpolationImageFromString(IC.ThresholdImage) : null;
            for (i = 0; i < iGPU.Length; i++)
            {
                try
                {
                    iGPU[i].IProc.ImageFormat             = iGrab.ImageFormat;
                    iGPU[i].IProc.MaxSegmentsPerScanLine  = IC.MaxSegmentsPerLine;
                    iGPU[i].IProc.MaxClustersPerImage     = IC.MaxClusters;
                    iGPU[i].IProc.OutputFeatures          = SySal.Imaging.ImageProcessingFeatures.Cluster2ndMomenta | SySal.Imaging.ImageProcessingFeatures.BinarizedImage;
                    iGPU[i].IProc.EqGreyLevelTargetMedian = (byte)IC.GreyTargetMedian;
                    iGPU[i].IProc.EmptyImage     = eqim;
                    iGPU[i].IProc.ThresholdImage = dctim;
                }
                catch (Exception x)
                {
                    Log("ApplyGPUConfig", x.ToString());
                }
            }
        }
Exemplo n.º 4
0
        private void btnCompute_Click(object sender, EventArgs e)
        {
            if (m_Running)
            {
                m_Running = false;
                return;
            }
            if (S.MinimumThreshold == S.MaximumThreshold)
            {
                SySal.Imaging.DCTInterpolationImage dct = null;
                try
                {
                    SySal.Imaging.ImageInfo info = m_ImageFormat;
                    info.BitsPerPixel = 16;
                    SySal.Imaging.DCTInterpolationImage.PointValue pval = new SySal.Imaging.DCTInterpolationImage.PointValue();
                    pval.X     = (ushort)(info.Width / 2);
                    pval.Y     = (ushort)(info.Height / 2);
                    pval.Value = (int)S.MaximumThreshold;
                    dct        = new SySal.Imaging.DCTInterpolationImage(info, 1, 1, new SySal.Imaging.DCTInterpolationImage.PointValue [] { pval });
                    m_Result   = txtResult.Text = dct.ToString();
                    MessageBox.Show("Constant threshold image built.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                catch (Exception x)
                {
                    MessageBox.Show("Invalid DCT built.", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            if (m_Files.Length <= 0)
            {
                MessageBox.Show("Please choose sample image files.", "Input missing", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            double[][,] densities = null;
            SySal.Imaging.Image[] thresholdimgs = null;
            int[] cellmeanx = new int[S.XCells];
            int[] cellmeany = new int[S.YCells];
            int[] cellminx  = new int[S.XCells];
            int[] cellmaxx  = new int[S.YCells];
            int[] cellminy  = new int[S.XCells];
            int[] cellmaxy  = new int[S.YCells];
            m_Running = true;
            EnableButtons();
            pbProgress.Minimum = 0.0;
            pbProgress.Maximum = (double)m_Files.Length;
            pbProgress.Value   = 0.0;
            System.Threading.Thread execthread = new System.Threading.Thread(new System.Threading.ThreadStart(delegate()
            {
                thresholdimgs = new SySal.Imaging.Image[S.ThresholdSteps + 1];
                int i, j, ix, iy;
                SySal.Imaging.ImageInfo m_info = m_ImageFormat;
                m_info.BitsPerPixel            = 16;
                int bestgpu   = 0;
                int maximages = iGPU[bestgpu].MaxImages;
                for (i = 1; i < iGPU.Length; i++)
                {
                    if (iGPU[i].MaxImages > maximages)
                    {
                        bestgpu   = i;
                        maximages = iGPU[i].MaxImages;
                    }
                }
                ;
                int xstep = (int)(m_ImageFormat.Width / S.XCells);
                int ystep = (int)(m_ImageFormat.Height / S.YCells);
                for (i = 0; i < S.XCells; i++)
                {
                    cellmeanx[i] = (int)(xstep * (i + 0.5));
                    cellminx[i]  = cellmeanx[i] - (int)S.CellWidth / 2;
                    cellmaxx[i]  = cellmeanx[i] + (int)S.CellWidth / 2;
                }
                for (i = 0; i < S.YCells; i++)
                {
                    cellmeany[i] = (int)(ystep * (i + 0.5));
                    cellminy[i]  = cellmeany[i] - (int)S.CellHeight / 2;
                    cellmaxy[i]  = cellmeany[i] + (int)S.CellHeight / 2;
                }
                densities   = new double[thresholdimgs.Length][, ];
                int[] files = new int[thresholdimgs.Length];
                for (j = 0; j <= S.ThresholdSteps; j++)
                {
                    densities[j]     = new double[S.XCells, S.YCells];
                    thresholdimgs[j] = new SySal.Imaging.Image(m_info, new ConstThresholdImagePixels((short)(S.MinimumThreshold + (j * (S.MaximumThreshold - S.MinimumThreshold)) / S.ThresholdSteps)));
                }
                for (i = 0; i < m_Files.Length; i++)
                {
                    SySal.Imaging.LinearMemoryImage im = null;
                    try
                    {
                        im = iGPU[bestgpu].ImageFromFile(m_Files[i]);
                        for (j = 0; j < thresholdimgs.Length; j++)
                        {
                            iGPU[bestgpu].ThresholdImage = thresholdimgs[j];
                            iGPU[bestgpu].Input          = im;
                            if (iGPU[bestgpu].Warnings == null || iGPU[bestgpu].Warnings.Length == 0)
                            {
                                SySal.Imaging.Cluster[] clusters = iGPU[bestgpu].Clusters[0];
                                foreach (SySal.Imaging.Cluster cls in clusters)
                                {
                                    if (cls.Area >= S.MinClusterSize && cls.Area <= S.MaxClusterSize)
                                    {
                                        for (ix = 0; ix < S.XCells; ix++)
                                        {
                                            if (cls.X >= cellminx[ix] && cls.X <= cellmaxx[ix])
                                            {
                                                for (iy = 0; iy < S.YCells; iy++)
                                                {
                                                    if (cls.Y >= cellminy[iy] && cls.Y <= cellmaxy[iy])
                                                    {
                                                        densities[j][ix, iy]++;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                files[j]++;
                            }
                            if (m_Running == false)
                            {
                                break;
                            }
                        }
                    }
                    catch (Exception)
                    {
                        if (im != null)
                        {
                            ((SySal.Imaging.LinearMemoryImage)im).Dispose();
                        }
                    }
                    this.Invoke(new dSetValue(SetValue), new object[] { (double)(i + 1) });
                    if (m_Running == false)
                    {
                        break;
                    }
                }
                for (j = 0; j < files.Length; j++)
                {
                    if (files[j] > 0)
                    {
                        for (ix = 0; ix < S.XCells; ix++)
                        {
                            for (iy = 0; iy < S.YCells; iy++)
                            {
                                densities[j][ix, iy] /= (files[j] * m_ImageFormat.Width * m_ImageFormat.Height);
                            }
                        }
                    }
                }
            }));
            execthread.Start();
            while (execthread.Join(100) == false)
            {
                Application.DoEvents();
            }
            System.Collections.ArrayList hidens = new System.Collections.ArrayList();
            foreach (double d in densities[densities.Length - 1])
            {
                hidens.Add(d);
            }
            hidens.Sort();
            if (hidens.Count < HiDensValues)
            {
                MessageBox.Show("Too few clusters:\r\nthe threshold was set too high, or the images were not taken in emulsion.\r\nPlease correct and retry.", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                m_Running = false;
                EnableButtons();
                return;
            }
            double target = 0.0;
            int    t;

            for (t = 1; t <= HiDensValues; t++)
            {
                target += (double)hidens[hidens.Count - t];
            }
            target /= HiDensValues;
            System.Collections.ArrayList pvals = new System.Collections.ArrayList();
            {
                int ix, iy;
                for (ix = 0; ix < S.XCells; ix++)
                {
                    for (iy = 0; iy < S.YCells; iy++)
                    {
                        for (t = 1; t < densities.Length && (densities[t - 1][ix, iy] < target || densities[t][ix, iy] > target); t++)
                        {
                            ;
                        }
                        if (t < densities.Length)
                        {
                            SySal.Imaging.DCTInterpolationImage.PointValue pval = new SySal.Imaging.DCTInterpolationImage.PointValue();
                            pval.X     = (ushort)cellmeanx[ix];
                            pval.Y     = (ushort)cellmeanx[iy];
                            pval.Value = (int)((target - densities[t - 1][ix, iy]) / (densities[t][ix, iy] - densities[t - 1][ix, iy]) *
                                               (((ConstThresholdImagePixels)thresholdimgs[t].Pixels).m_Threshold - ((ConstThresholdImagePixels)thresholdimgs[t - 1].Pixels).m_Threshold) +
                                               ((ConstThresholdImagePixels)thresholdimgs[t - 1].Pixels).m_Threshold);
                            pvals.Add(pval);
                        }
                    }
                }
                SySal.Imaging.DCTInterpolationImage dct = null;
                try
                {
                    SySal.Imaging.ImageInfo info = m_ImageFormat;
                    info.BitsPerPixel = 16;
                    dct = new SySal.Imaging.DCTInterpolationImage(info, (int)S.XWaves, (int)S.YWaves,
                                                                  (SySal.Imaging.DCTInterpolationImage.PointValue [])pvals.ToArray(typeof(SySal.Imaging.DCTInterpolationImage.PointValue)));
                    m_Result = txtResult.Text = dct.ToString();
                }
                catch (Exception x)
                {
                    MessageBox.Show("Invalid DCT built.", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    m_Running = false;
                    EnableButtons();
                    return;
                }
            }
            m_Running = false;
            EnableButtons();
            {
                System.IO.StringWriter sw = new System.IO.StringWriter();
                sw.WriteLine("THRESHOLD\tX\tY\tDENSITY");
                int j, ix, iy;
                for (j = 0; j < thresholdimgs.Length; j++)
                {
                    for (ix = 0; ix < S.XCells; ix++)
                    {
                        for (iy = 0; iy < S.YCells; iy++)
                        {
                            sw.WriteLine(((ConstThresholdImagePixels)thresholdimgs[j].Pixels).m_Threshold + "\t" + cellmeanx[ix] + "\t" + cellmeany[iy] + "\t" + densities[j][ix, iy].ToString(System.Globalization.CultureInfo.InvariantCulture));
                        }
                    }
                }
                sw.Flush();
                sw.Close();
                InfoPanel panel = new InfoPanel();
                panel.TopLevel = true;
                panel.SetContent("Density vs. Threshold", sw.ToString());
                panel.ShowDialog();
            }
        }