public ImageDisplayForm() { InitializeComponent(); SySal.ImageProcessorDisplay.Configuration c = (Configuration)SySal.Management.MachineSettings.GetSettings(typeof(Configuration)); if (c == null) { c = new Configuration(); c.PanelWidth = 640; c.PanelHeight = 480; c.PanelTop = 0; c.PanelLeft = 0; } StartPosition = FormStartPosition.Manual; Left = c.PanelLeft; Top = c.PanelTop; Width = c.PanelWidth; Height = c.PanelHeight; SySal.Imaging.ImageInfo info = new SySal.Imaging.ImageInfo(); info.BitsPerPixel = 8; info.PixelFormat = SySal.Imaging.PixelFormatType.GrayScale8; info.Width = (ushort)(Width - pnRight.MinimumSize.Width); info.Height = (ushort)(Height - pnBottom.MinimumSize.Height); pnRight.Width = pnRight.MinimumSize.Width; pnBottom.Height = pnBottom.MinimumSize.Height; HostedFormat = info; System.Drawing.Bitmap z = new Bitmap(8, 8, System.Drawing.Imaging.PixelFormat.Format8bppIndexed); GrayScalePalette = z.Palette; int i; for (i = 0; i < GrayScalePalette.Entries.Length; i++) { GrayScalePalette.Entries[i] = Color.FromArgb(i, i, i); } }
private static SySal.Imaging.ImageInfo InfoFromImage(System.Drawing.Image im) { SySal.Imaging.ImageInfo info = new SySal.Imaging.ImageInfo(); info.Width = (ushort)im.Width; info.Height = (ushort)im.Height; info.PixelFormat = SySal.Imaging.PixelFormatType.GrayScale8; info.BitsPerPixel = 8; return(info); }
/// <summary> /// Builds a DCT-interpolated image. /// </summary> /// <param name="info">format of the image.</param> /// <param name="xwaves">number of X waves.</param> /// <param name="ywaves">number of Y waves.</param> /// <param name="pval">the values of a set of sampled points.</param> public DCTInterpolationImage(SySal.Imaging.ImageInfo info, int xwaves, int ywaves, PointValue[] pval) : base(info, new DCT(info.BitsPerPixel / 8, xwaves, ywaves, info.Width, info.Height, pval)) { XWaves = xwaves; YWaves = ywaves; PointValues = pval; NumericalTools.AdvancedFitting.LeastSquares lsq = new NumericalTools.AdvancedFitting.LeastSquares(); NumericalTools.Minimization.ITargetFunction dct = (NumericalTools.Minimization.ITargetFunction)Pixels; double[][] indep = new double[pval.Length][]; double[] dep = new double[pval.Length]; double[] deperr = new double[pval.Length]; int i; for (i = 0; i < indep.Length; i++) { indep[i] = new double[2] { pval[i].X, pval[i].Y }; dep[i] = pval[i].Value; deperr[i] = 1.0; } double[] p = lsq.Fit(dct, xwaves * ywaves, indep, dep, deperr, 10); ((DCT)dct).ParamValues = p; ((DCT)dct).MakeCosValues(); ((DCT)dct).m_CachedValues = new int[info.Width * info.Height]; System.Threading.Thread[] hcomp_Threads = new System.Threading.Thread[info.Height]; int iiy; for (iiy = 0; iiy < info.Height; iiy++) { hcomp_Threads[iiy] = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(delegate(object oiiiy) { int iiiy = (int)oiiiy; int iix, ix, iy; for (iix = 0; iix < info.Width; iix++) { double dv = 0.0; for (ix = 0; ix < XWaves; ix++) { for (iy = 0; iy < YWaves; iy++) { dv += ((DCT)dct).ParamValues[iy * XWaves + ix] * ((DCT)dct).XCosValues[ix, iix] * ((DCT)dct).YCosValues[iy, iiiy]; } } ((DCT)dct).m_CachedValues[iiiy * info.Width + iix] = (int)Math.Round(dv); } })); hcomp_Threads[iiy].Start(iiy); } for (iiy = 0; iiy < info.Height; iiy++) { hcomp_Threads[iiy].Join(); hcomp_Threads[iiy] = null; } }
public ImagingConfiguration() : base("") { SySal.Imaging.ImageInfo info = new SySal.Imaging.ImageInfo(); info.Width = (ushort)ImageWidth; info.Height = (ushort)ImageHeight; info.PixelFormat = SySal.Imaging.PixelFormatType.GrayScale8; info.BitsPerPixel = 16; SySal.Imaging.DCTInterpolationImage.PointValue [] pval = new SySal.Imaging.DCTInterpolationImage.PointValue[1]; pval[0].X = (ushort)(info.Width / 2); pval[0].Y = (ushort)(info.Height / 2); pval[0].Value = 200; ThresholdImage = new SySal.Imaging.DCTInterpolationImage(info, 1, 1, pval).ToString(); Pixel2Micron.X = -0.3; Pixel2Micron.Y = 0.3; }
internal ImagePixels(SySal.Imaging.ImageInfo info, byte[] b) { m_Info = info; m_Bytes = b; }
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(); } }