Esempio n. 1
0
 public override void Clear()
 {
     Peaks.Clear();
     Direction = 0;
 }
Esempio n. 2
0
        public void AnalyzeSpectrum()
        {
            try
            {
                Peaks.Clear();
                PlotPoints.Clear();
                if (firstAnalyze)
                {
                    SamplePeaks.Clear();
                }

                var    uri    = new Uri(PicPath);
                var    bitmap = new BitmapImage(uri);
                int    stride = bitmap.PixelWidth * 4;
                int    size   = bitmap.PixelHeight * stride;
                byte[] pixels = new byte[size];
                bitmap.CopyPixels(pixels, stride, 0);
                //make an average values
                //1. get sum of each color components
                int[] avgpixels = new int[stride];
                for (int y = 0; y < bitmap.PixelHeight; y++)
                {
                    for (int x = 0; x < bitmap.PixelWidth; x++)
                    {
                        int index = y * stride + 4 * x;
                        avgpixels[4 * x]     += pixels[index];
                        avgpixels[4 * x + 1] += pixels[index + 1];
                        avgpixels[4 * x + 2] += pixels[index + 2];
                    }
                }

                //2. get an average values - smooth
                for (int x = 0; x < bitmap.PixelWidth; x++)
                {
                    int index = bitmap.PixelHeight * stride + 4 * x;
                    avgpixels[4 * x]     /= bitmap.PixelHeight;
                    avgpixels[4 * x + 1] /= bitmap.PixelHeight;
                    avgpixels[4 * x + 2] /= bitmap.PixelHeight;
                }

                //get intensity and make points on plot
                for (int x = 0; x < bitmap.PixelWidth; x++)
                {
                    var spec  = new AnalyzedSpectrum();
                    int index = 4 * x;
                    int red   = avgpixels[index];
                    int green = avgpixels[index + 1];
                    int blue  = avgpixels[index + 2];
                    spec.PeakPixel     = x + 380;                                                                                   //scale to wavelenght
                    spec.PeakIntensity = Math.Round(Math.Sqrt(0.299 * red * red + 0.587 * green * green + 0.114 * blue * blue), 2); // hsp color model
                    Peaks.Add(spec);
                    if (firstAnalyze)
                    {
                        SamplePeaks.Add(spec);
                    }

                    var _point = new DataPoint(spec.PeakPixel, spec.PeakIntensity);
                    PlotPoints.Add(_point);
                }

                firstAnalyze = false;
            }
            catch (Exception e)
            {
                MessageBox.Show("You must make a photo of spectrum first!");
            }
        }