public override void MouseClick(object sender, MouseEventArgs e)
        {
            int x1 = m_StateManager.GetSpectraPixelNoFromMouseCoordinates(e.Location);

            m_SelectedPoint = m_MasterSpectra.Points.SingleOrDefault(x => x.PixelNo == x1);

            if (m_SelectedPoint != null)
            {
                m_SelectedPointPos = m_SelectedPoint.PixelNo;

                if (Control.ModifierKeys != Keys.Control)
                {
                    // Find the local maximum or minimum
                    List<SpectraPoint> pointsInArea = m_MasterSpectra.Points.Where(x => x.PixelNo >= m_SelectedPoint.PixelNo - SEARCH_AREA_WING && x.PixelNo <= m_SelectedPoint.PixelNo + SEARCH_AREA_WING).ToList();
                    float maxValue = float.MinValue;
                    int maxPixelNo = m_SelectedPoint.PixelNo;
                    float minValue = float.MaxValue;
                    int minPixelNo = m_SelectedPoint.PixelNo;
                    foreach (var spectraPoint in pointsInArea)
                    {
                        if (spectraPoint.RawValue > maxValue)
                        {
                            maxValue = spectraPoint.RawValue;
                            maxPixelNo = spectraPoint.PixelNo;
                        }

                        if (spectraPoint.RawValue < minValue)
                        {
                            minValue = spectraPoint.RawValue;
                            minPixelNo = spectraPoint.PixelNo;
                        }
                    }

                    // Check if local maximum or minimum
                    if (minPixelNo != m_SelectedPoint.PixelNo && minPixelNo > m_SelectedPoint.PixelNo - SEARCH_AREA_WING && minPixelNo < m_SelectedPoint.PixelNo + SEARCH_AREA_WING)
                        m_SelectedPoint = m_MasterSpectra.Points.Single(x => x.PixelNo == minPixelNo);
                    else if (maxPixelNo != m_SelectedPoint.PixelNo && maxPixelNo > m_SelectedPoint.PixelNo - SEARCH_AREA_WING && maxPixelNo < m_SelectedPoint.PixelNo + SEARCH_AREA_WING)
                        m_SelectedPoint = m_MasterSpectra.Points.Single(x => x.PixelNo == maxPixelNo);

                    if (Control.ModifierKeys == Keys.Shift)
                    {
                        // NOTE: Doing a 2-nd order polynomial fit to find the sub-pixel location of the minima
                        var cal = new MinimaFinder();
                        pointsInArea = m_MasterSpectra.Points.Where(x => x.PixelNo >= m_SelectedPoint.PixelNo - SEARCH_AREA_WING && x.PixelNo <= m_SelectedPoint.PixelNo + SEARCH_AREA_WING).ToList();
                        foreach (SpectraPoint point in pointsInArea) cal.AddDataPoint(point.RawSignal, point.PixelNo);
                        cal.Calibrate();
                        m_SelectedPointPos = cal.GetMinimaCloseTo(m_SelectedPoint.PixelNo);

                        if (float.IsNaN(m_SelectedPointPos))
                            m_SelectedPointPos = m_SelectedPoint.PixelNo;
                    }
                    else
                        m_SelectedPointPos = m_SelectedPoint.PixelNo;
                }

                m_StateManager.Redraw();
                m_SpectroscopyController.SelectPixel(m_SelectedPoint.PixelNo);
                EnsureEnterWavelengthForm();
            }
        }
Exemplo n.º 2
0
        public bool LoadCalibration(TangraConfig.PersistedConfiguration configuration)
        {
            float        maxVal    = m_MasterSpectra.Points.Max(x => x.RawValue);
            SpectraPoint peakPoint = m_MasterSpectra.Points.SingleOrDefault(x => Math.Abs(maxVal - x.RawValue) < 0.00001);

            if (peakPoint != null)
            {
                switch (configuration.Order)
                {
                case 1:
                    m_WavelengthCalibration = new LinearWavelengthCalibration(configuration.A, peakPoint.PixelNo, configuration.RMS);
                    return(true);

                case 2:
                    m_WavelengthCalibration = new QuadraticWavelengthCalibration(configuration.A, configuration.B, peakPoint.PixelNo, configuration.RMS);
                    return(true);

                case 3:
                    m_WavelengthCalibration = new CubicWavelengthCalibration(configuration.A, configuration.B, configuration.C, peakPoint.PixelNo, configuration.RMS);
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 3
0
 internal SpectraPoint(SpectraPoint cloneFrom)
 {
     PixelNo               = cloneFrom.PixelNo;
     Wavelength            = cloneFrom.Wavelength;
     RawSignal             = cloneFrom.RawSignal;
     RawSignalPixelCount   = cloneFrom.RawSignalPixelCount;
     RawBackgroundPerPixel = cloneFrom.RawBackgroundPerPixel;
     RawValue              = cloneFrom.RawValue;
     SmoothedValue         = cloneFrom.SmoothedValue;
 }
Exemplo n.º 4
0
        public float ResolveWavelength(int pixelNo)
        {
            if (m_WavelengthCalibration != null)
            {
                SpectraPoint point = m_MasterSpectra.Points.SingleOrDefault(x => x.PixelNo == pixelNo);
                return(m_WavelengthCalibration.ResolveWavelength(point));
            }

            return(float.NaN);
        }
Exemplo n.º 5
0
 public float ResolveWavelength(SpectraPoint point)
 {
     if (point != null)
     {
         return(ResolveWavelength(point.PixelNo));
     }
     else
     {
         return(float.NaN);
     }
 }
Exemplo n.º 6
0
 public float ResolveWavelength(SpectraPoint point)
 {
     if (point != null)
     {
         return(m_ZeroWavelength + (point.PixelNo - m_ZeroPixelNo) * m_AperPixels);
     }
     else
     {
         return(float.NaN);
     }
 }
        public override void MouseClick(object sender, MouseEventArgs e)
        {
            int x1 = m_StateManager.GetSpectraPixelNoFromMouseCoordinates(e.Location);

            m_SelectedPoint = m_MasterSpectra.Points.SingleOrDefault(x => x.PixelNo == x1);

            if (m_SelectedPoint != null)
            {
                m_StateManager.Redraw();
                m_SpectroscopyController.SelectPixel(m_SelectedPoint.PixelNo);
            }
        }
        public override void PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Left)
            {
                m_SelectedPoint = m_MasterSpectra.Points.SingleOrDefault(x => x.PixelNo == m_SelectedPoint.PixelNo - 1);
            }
            else if (e.KeyCode == Keys.Right)
            {
                m_SelectedPoint = m_MasterSpectra.Points.SingleOrDefault(x => x.PixelNo == m_SelectedPoint.PixelNo + 1);
            }

            if (m_SelectedPoint != null)
            {
                m_StateManager.Redraw();
                m_SpectroscopyController.SelectPixel(m_SelectedPoint.PixelNo);
            }
        }
Exemplo n.º 9
0
        public Spectra ReadSpectra(float x0, float y0, int halfWidth, int bgHalfWidth, int bgGap, PixelCombineMethod bgMethod)
        {
            var rv = new Spectra()
            {
                SignalAreaWidth         = 2 * halfWidth,
                BackgroundAreaHalfWidth = bgHalfWidth,
                BackgroundAreaGap       = bgGap,
                MaxPixelValue           = m_Image.Pixelmap.MaxSignalValue
            };

            int xFrom = int.MaxValue;
            int xTo   = int.MinValue;

            // Find the destination pixel range at the destination horizontal
            PointF p1 = m_Mapper.GetDestCoords(x0, y0);

            rv.ZeroOrderPixelNo = (int)Math.Round(p1.X);

            for (float x = p1.X - m_Mapper.MaxDestDiagonal; x < p1.X + m_Mapper.MaxDestDiagonal; x++)
            {
                PointF p = m_Mapper.GetSourceCoords(x, p1.Y);
                if (m_SourceVideoFrame.Contains(p))
                {
                    int xx = (int)x;

                    if (xx < xFrom)
                    {
                        xFrom = xx;
                    }
                    if (xx > xTo)
                    {
                        xTo = xx;
                    }
                }
            }

            m_BgValues     = new float[xTo - xFrom + 1];
            m_BgPixelCount = new uint[xTo - xFrom + 1];
            m_BgValuesList = new List <float> [xTo - xFrom + 1];
            rv.InitialisePixelArray(xTo - xFrom + 1);

            // Get all readings in the range
            for (int x = xFrom; x <= xTo; x++)
            {
                var point = new SpectraPoint();
                point.PixelNo             = x;
                point.RawSignalPixelCount = 0;

                for (int z = -halfWidth; z <= halfWidth; z++)
                {
                    PointF p  = m_Mapper.GetSourceCoords(x, p1.Y + z);
                    int    xx = (int)Math.Round(p.X);
                    int    yy = (int)Math.Round(p.Y);

                    if (m_SourceVideoFrame.Contains(xx, yy))
                    {
                        float sum       = 0;
                        int   numPoints = 0;
                        for (float kx = -0.4f; kx < 0.5f; kx += 0.2f)
                        {
                            for (float ky = -0.4f; ky < 0.5f; ky += 0.2f)
                            {
                                p = m_Mapper.GetSourceCoords(x + kx, p1.Y + ky + z);
                                int xxx = (int)Math.Round(p.X);
                                int yyy = (int)Math.Round(p.Y);
                                if (m_SourceVideoFrame.Contains(xxx, yyy))
                                {
                                    sum += (m_Image.Pixelmap[xxx, yyy] * m_PixelValueCoeff);
                                    numPoints++;
                                }
                            }
                        }
                        float destPixVal = (sum / numPoints);
                        point.RawValue += destPixVal;
                        point.RawSignalPixelCount++;
                        rv.Pixels[x - xFrom, z + bgHalfWidth + bgGap + halfWidth - 1] = destPixVal;
                    }
                }

                point.RawSignal = point.RawValue;
                rv.Points.Add(point);

                #region Reads background
                if (bgMethod == PixelCombineMethod.Average)
                {
                    ReadAverageBackgroundForPixelIndex(halfWidth, bgHalfWidth, bgGap, x, p1.Y, x - xFrom);
                }
                else if (bgMethod == PixelCombineMethod.Median)
                {
                    ReadMedianBackgroundForPixelIndex(halfWidth, bgHalfWidth, bgGap, x, p1.Y, x - xFrom);
                }
                #endregion
            }

            // Apply background
            for (int i = 0; i < rv.Points.Count; i++)
            {
                SpectraPoint point = rv.Points[i];

                if (bgMethod == PixelCombineMethod.Average)
                {
                    point.RawBackgroundPerPixel = GetAverageBackgroundValue(point.PixelNo, xFrom, xTo, bgHalfWidth);
                }
                else if (bgMethod == PixelCombineMethod.Median)
                {
                    point.RawBackgroundPerPixel = GetMedianBackgroundValue(point.PixelNo, xFrom, xTo, bgHalfWidth);
                }

                for (int z = -halfWidth - bgGap - bgHalfWidth + 1; z < -halfWidth - bgGap; z++)
                {
                    rv.Pixels[i, z + bgHalfWidth + bgGap + halfWidth - 1] = point.RawBackgroundPerPixel;
                }

                for (int z = halfWidth + bgGap + 1; z < halfWidth + bgGap + bgHalfWidth + 1; z++)
                {
                    rv.Pixels[i, z + bgHalfWidth + bgGap + halfWidth - 1] = point.RawBackgroundPerPixel;
                }

                point.RawValue -= point.RawBackgroundPerPixel * point.RawSignalPixelCount;

                if (point.RawValue < 0 && !TangraConfig.Settings.Spectroscopy.AllowNegativeValues)
                {
                    point.RawValue = 0;
                }
            }

            rv.MaxSpectraValue = (uint)Math.Ceiling(rv.Points.Where(x => x.PixelNo > rv.ZeroOrderPixelNo + 20).Select(x => x.RawValue).Max());

            return(rv);
        }
Exemplo n.º 10
0
        public MasterSpectra(BinaryReader reader)
        {
            int version = reader.ReadInt32();

            CombinedMeasurements = reader.ReadInt32();
            SignalAreaWidth      = reader.ReadInt32();
            MaxPixelValue        = reader.ReadUInt32();
            MaxSpectraValue      = reader.ReadUInt32();
            ZeroOrderPixelNo     = reader.ReadInt32();

            int pixelsCount = reader.ReadInt32();

            Points = new List <SpectraPoint>();
            for (int i = 0; i < pixelsCount; i++)
            {
                var point = new SpectraPoint(reader);
                Points.Add(point);
            }

            int width  = reader.ReadInt32();
            int height = reader.ReadInt32();

            Pixels = new float[width, height];
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    Pixels[x, y] = reader.ReadSingle();
                }
            }

            MeasurementInfo = new MeasurementInfo(reader);
            if (reader.ReadBoolean())
            {
                Calibration = new SpectraCalibration(reader);
            }

            int rawFramesCount = reader.ReadInt32();

            for (int i = 0; i < rawFramesCount; i++)
            {
                var frameMeasurement = new Spectra();
                RawMeasurements.Add(frameMeasurement);

                frameMeasurement.SignalAreaWidth  = reader.ReadInt32();
                frameMeasurement.MaxPixelValue    = reader.ReadUInt32();
                frameMeasurement.MaxSpectraValue  = reader.ReadUInt32();
                frameMeasurement.ZeroOrderPixelNo = reader.ReadInt32();

                frameMeasurement.ZeroOrderFWHM = float.NaN;
                if (version > 1)
                {
                    frameMeasurement.ZeroOrderFWHM = reader.ReadSingle();
                }

                int frameMeaCount = reader.ReadInt32();

                for (int j = 0; j < frameMeaCount; j++)
                {
                    var point = new SpectraPoint(reader);
                    frameMeasurement.Points.Add(point);
                }
            }

            ProcessingInfo  = new ProcessingInfo(reader);
            ObservationInfo = new ObservationInfo(reader);

            ZeroOrderFWHM = float.NaN;
            if (version > 1)
            {
                ZeroOrderFWHM = reader.ReadSingle();
            }
        }
Exemplo n.º 11
0
        public Spectra ReadSpectra(float x0, float y0, int halfWidth, int bgHalfWidth, int bgGap, PixelCombineMethod bgMethod)
        {
            var rv = new Spectra()
            {
                SignalAreaWidth = 2 * halfWidth,
                BackgroundAreaHalfWidth = bgHalfWidth,
                BackgroundAreaGap = bgGap,
                MaxPixelValue = m_Image.Pixelmap.MaxSignalValue
            };

            int xFrom = int.MaxValue;
            int xTo = int.MinValue;

            // Find the destination pixel range at the destination horizontal
            PointF p1 = m_Mapper.GetDestCoords(x0, y0);
            rv.ZeroOrderPixelNo = (int)Math.Round(p1.X);

            for (float x = p1.X - m_Mapper.MaxDestDiagonal; x < p1.X + m_Mapper.MaxDestDiagonal; x++)
            {
                PointF p = m_Mapper.GetSourceCoords(x, p1.Y);
                if (m_SourceVideoFrame.Contains(p))
                {
                    int xx = (int) x;

                    if (xx < xFrom) xFrom = xx;
                    if (xx > xTo) xTo = xx;
                }
            }

            m_BgValues = new float[xTo - xFrom + 1];
            m_BgPixelCount = new uint[xTo - xFrom + 1];
            m_BgValuesList = new List<float>[xTo - xFrom + 1];
            rv.InitialisePixelArray(xTo - xFrom + 1);

            // Get all readings in the range
            for (int x = xFrom; x <= xTo; x++)
            {
                var point = new SpectraPoint();
                point.PixelNo = x;
                point.RawSignalPixelCount = 0;

                for (int z = -halfWidth; z <= halfWidth; z++)
                {
                    PointF p = m_Mapper.GetSourceCoords(x, p1.Y +z);
                    int xx = (int)Math.Round(p.X);
                    int yy = (int)Math.Round(p.Y);

                    if (m_SourceVideoFrame.Contains(xx, yy))
                    {
                        float sum = 0;
                        int numPoints = 0;
                        for (float kx = -0.4f; kx < 0.5f; kx+=0.2f)
                        for (float ky = -0.4f; ky < 0.5f; ky += 0.2f)
                        {
                            p = m_Mapper.GetSourceCoords(x + kx, p1.Y + ky + z);
                            int xxx = (int)Math.Round(p.X);
                            int yyy = (int)Math.Round(p.Y);
                            if (m_SourceVideoFrame.Contains(xxx, yyy))
                            {
                                sum += (m_Image.Pixelmap[xxx, yyy] * m_PixelValueCoeff);
                                numPoints++;
                            }
                        }
                        float destPixVal = (sum/numPoints);
                        point.RawValue += destPixVal;
                        point.RawSignalPixelCount++;
                        rv.Pixels[x - xFrom, z + bgHalfWidth + bgGap + halfWidth - 1] = destPixVal;
                    }
                }

                point.RawSignal = point.RawValue;
                rv.Points.Add(point);

                #region Reads background
                if (bgMethod == PixelCombineMethod.Average)
                {
                    ReadAverageBackgroundForPixelIndex(halfWidth, bgHalfWidth, bgGap, x, p1.Y, x - xFrom);
                }
                else if (bgMethod == PixelCombineMethod.Median)
                {
                    ReadMedianBackgroundForPixelIndex(halfWidth, bgHalfWidth, bgGap, x, p1.Y, x - xFrom);
                }
                #endregion
            }

            // Apply background
            for (int i = 0; i < rv.Points.Count; i++)
            {
                SpectraPoint point = rv.Points[i];

                if (bgMethod == PixelCombineMethod.Average)
                {
                    point.RawBackgroundPerPixel = GetAverageBackgroundValue(point.PixelNo, xFrom, xTo, bgHalfWidth);
                }
                else if (bgMethod == PixelCombineMethod.Median)
                {
                    point.RawBackgroundPerPixel = GetMedianBackgroundValue(point.PixelNo, xFrom, xTo, bgHalfWidth);
                }

                for (int z = -halfWidth - bgGap - bgHalfWidth + 1; z < -halfWidth - bgGap; z++)
                    rv.Pixels[i, z + bgHalfWidth + bgGap + halfWidth - 1] = point.RawBackgroundPerPixel;

                for (int z = halfWidth + bgGap + 1; z < halfWidth + bgGap + bgHalfWidth + 1; z++)
                    rv.Pixels[i, z + bgHalfWidth + bgGap + halfWidth - 1] = point.RawBackgroundPerPixel;

                point.RawValue -= point.RawBackgroundPerPixel * point.RawSignalPixelCount;

                if (point.RawValue < 0 && !TangraConfig.Settings.Spectroscopy.AllowNegativeValues)
                    point.RawValue = 0;
            }

            rv.MaxSpectraValue = (uint)Math.Ceiling(rv.Points.Where(x => x.PixelNo > rv.ZeroOrderPixelNo + 20).Select(x => x.RawValue).Max());

            return rv;
        }
Exemplo n.º 12
0
 internal SpectraPoint(SpectraPoint cloneFrom)
 {
     PixelNo = cloneFrom.PixelNo;
     Wavelength = cloneFrom.Wavelength;
     RawSignal = cloneFrom.RawSignal;
     RawSignalPixelCount = cloneFrom.RawSignalPixelCount;
     RawBackgroundPerPixel = cloneFrom.RawBackgroundPerPixel;
     RawValue = cloneFrom.RawValue;
     SmoothedValue = cloneFrom.SmoothedValue;
 }
Exemplo n.º 13
0
        public MasterSpectra(BinaryReader reader)
        {
            int version = reader.ReadInt32();

            CombinedMeasurements = reader.ReadInt32();
            SignalAreaWidth = reader.ReadInt32();
            MaxPixelValue = reader.ReadUInt32();
            MaxSpectraValue = reader.ReadUInt32();
            ZeroOrderPixelNo = reader.ReadInt32();

            int pixelsCount = reader.ReadInt32();
            Points = new List<SpectraPoint>();
            for (int i = 0; i < pixelsCount; i++)
            {
                var point = new SpectraPoint(reader);
                Points.Add(point);
            }

            int width = reader.ReadInt32();
            int height = reader.ReadInt32();
            Pixels = new float[width,height];
            for (int x = 0; x < width; x++)
            for (int y = 0; y < height; y++)
            {
                Pixels[x, y] = reader.ReadSingle();
            }

            MeasurementInfo = new MeasurementInfo(reader);
            if (reader.ReadBoolean())
                Calibration = new SpectraCalibration(reader);

            int rawFramesCount = reader.ReadInt32();
            for (int i = 0; i < rawFramesCount; i++)
            {
                var frameMeasurement = new Spectra();
                RawMeasurements.Add(frameMeasurement);

                frameMeasurement.SignalAreaWidth = reader.ReadInt32();
                frameMeasurement.MaxPixelValue = reader.ReadUInt32();
                frameMeasurement.MaxSpectraValue = reader.ReadUInt32();
                frameMeasurement.ZeroOrderPixelNo = reader.ReadInt32();

                frameMeasurement.ZeroOrderFWHM = float.NaN;
                if (version > 1)
                {
                    frameMeasurement.ZeroOrderFWHM = reader.ReadSingle();
                }

                int frameMeaCount = reader.ReadInt32();

                for (int j = 0; j < frameMeaCount; j++)
                {
                    var point = new SpectraPoint(reader);
                    frameMeasurement.Points.Add(point);
                }
            }

            ProcessingInfo = new ProcessingInfo(reader);
            ObservationInfo = new ObservationInfo(reader);

            ZeroOrderFWHM = float.NaN;
            if (version > 1)
            {
                ZeroOrderFWHM = reader.ReadSingle();
            }
        }
Exemplo n.º 14
0
 internal void CalibrationPointSelectionCancelled()
 {
     m_SelectedPoint = null;
     m_SpectroscopyController.DeselectPixel();
     m_StateManager.Redraw();
 }
Exemplo n.º 15
0
        internal void CalibrationPointSelected(float selectedWaveLength, bool attemptCalibration, int polynomialOrder)
        {
            m_SpectroscopyController.SetMarker(m_SelectedPointPos, selectedWaveLength, attemptCalibration, polynomialOrder);

            if (m_SpectroscopyController.IsCalibrated())
            {
                m_SpectroscopyController.SaveCalibratedConfiguration();
                m_StateManager.ChangeState<SpectraViewerStateCalibrated>();
            }
            else
            {
                m_MarkedCalibrationPoints.Add(new SpectraPoint(m_SelectedPoint));
                m_SelectedPoint = null;

                m_StateManager.Redraw();
            }
        }
Exemplo n.º 16
0
        public override void PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (m_SelectedPoint != null)
            {
                if (e.KeyCode == Keys.Left)
                {
                    int newPixNo = m_SelectedPoint.PixelNo - 1;
                    if (newPixNo >= m_MasterSpectra.Points[0].PixelNo)
                        m_SelectedPoint = m_MasterSpectra.Points.Single(x => x.PixelNo == newPixNo);

                    m_SpectroscopyController.SelectPixel(m_SelectedPoint.PixelNo);
                    m_StateManager.Redraw();
                }
                else if (e.KeyCode == Keys.Right)
                {
                    int newPixNo = m_SelectedPoint.PixelNo + 1;
                    if (newPixNo <= m_MasterSpectra.Points[m_MasterSpectra.Points.Count - 1].PixelNo)
                        m_SelectedPoint = m_MasterSpectra.Points.Single(x => x.PixelNo == newPixNo);

                    m_SpectroscopyController.SelectPixel(m_SelectedPoint.PixelNo);
                    m_StateManager.Redraw();
                }
            }
        }