Exemplo n.º 1
0
        public static System.Drawing.Image ToImage(SySal.Imaging.Image im)
        {
            if (im.Info.BitsPerPixel != 8 && im.Info.BitsPerPixel != 16)
            {
                throw new Exception("Only 8 and 16 bit images are supported.");
            }
            bool is16bit = (im.Info.BitsPerPixel == 16);

            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(im.Info.Width, im.Info.Height, is16bit ? System.Drawing.Imaging.PixelFormat.Format16bppGrayScale : System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            ushort x, y;

            if (is16bit)
            {
                for (y = 0; y < im.Info.Height; y++)
                {
                    for (x = 0; x < im.Info.Width; x++)
                    {
                        bmp.SetPixel(x, y, System.Drawing.Color.FromArgb((im.Pixels[x, y, (ushort)0] << 8) + im.Pixels[x, y, (ushort)1]));
                    }
                }
            }
            else
            {
                for (y = 0; y < im.Info.Height; y++)
                {
                    for (x = 0; x < im.Info.Width; x++)
                    {
                        bmp.SetPixel(x, y, System.Drawing.Color.FromArgb(im.Pixels[x, y, (ushort)0], im.Pixels[x, y, (ushort)0], im.Pixels[x, y, (ushort)0]));
                    }
                }
            }
            return(bmp);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Encodes an image in Base64 format.
        /// </summary>
        /// <param name="im">the image to be encoded.</param>
        /// <returns>the string with the encoded image.</returns>
        public static string ImageToBase64(SySal.Imaging.Image im)
        {
            if (im.Info.PixelFormat != PixelFormatType.GrayScale8)
            {
                throw new Exception("Unsupported format. The only supported format is " + PixelFormatType.GrayScale8 + ".");
            }
            int bitsperpixel = (im.Info.BitsPerPixel / 8);

            byte[] b = new byte[bitsperpixel * im.Info.Width * im.Info.Height];
            int    i;

            for (i = 0; i < b.Length; i++)
            {
                b[i] = im.Pixels[(uint)i];
            }
            System.IO.MemoryStream           ms = new System.IO.MemoryStream();
            System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress);
            gz.Write(b, 0, b.Length);
            gz.Flush();
            gz.Close();
            return(Base64String + im.Info.Width + "," + im.Info.Height + "," + im.Info.BitsPerPixel + "\r\n" + System.Convert.ToBase64String(ms.ToArray(), Base64FormattingOptions.InsertLineBreaks));
        }
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();
            }
        }