Esempio n. 1
0
        private RgbImage stackImages3(Rectangle rect, out int count)
        {
            count = 0;
            uint[] pixels = null;
            int    width = 0, height = 0;

            foreach (var item in currentStack)
            {
                using (var rawImage = libraw.load_raw(item.FilePath)) {
                    var _pixels = rawImage.GetRawPixels();
                    if (pixels == null)
                    {
                        width  = rawImage.Width;
                        height = rawImage.Height;
                        pixels = new uint[width * height];
                    }
                    for (int i = 0; i < pixels.Length; i++)
                    {
                        pixels[i] += _pixels[i];
                    }
                    count++;
                    GC.Collect();
                    Application.DoEvents();
                }
            }
            ushort[] __pixels = new ushort[width * height];
            for (int i = 0; i < pixels.Length; i++)
            {
                __pixels[i] = (ushort)((32 * pixels[i]) / count);
            }

            using (var raw = new RawImage(width, height, 0, __pixels)) {
                return(raw.ExtractRgbImage(new Rectangle(5, 5, width - 10, height - 10)));
            }
        }
        private void doExperiment_Click(object sender, EventArgs e)
        {
            /*using( var buffer = new RawBuffer32() ) {
             *  foreach( var item in currentStack ) {
             *      if( !item.IsExcluded ) {
             *          using( var rawImage = libraw.load_raw( item.FilePath ) ) {
             *              buffer.Add2( rawImage );
             *          }
             *      }
             *  }
             *
             *  pictureBox.Image = buffer.GetResult().RenderBitmapHalfRes( currentCurve, 0 );
             * }*/

            Rectangle rect = currentZoomRect;

            int pcount = rect.Width * rect.Height;

            int margin  = 11;
            int window  = 2 * margin + 1;
            var result  = new ushort[pcount * window];
            var sumSqr  = new uint[pcount * window];
            int count   = 0;
            int channel = 0;

            foreach (var item in currentStack)
            {
                if (!item.IsExcluded)
                {
                    using (var rawImage = libraw.load_raw(item.FilePath)) {
                        //rawImage.ApplyFlat( currentSession.Flat, 1 );
                        var pixels = rawImage.GetRawPixels(rect);
                        channel = rawImage.Channel(rect.X, rect.Y);
                        if (count < window)
                        {
                            for (int i = 0; i < pcount; i++)
                            {
                                int i0 = window * i;
                                result[i0 + count] = pixels[i];
                                if (count + 1 == window)
                                {
                                    Array.Sort(result, i0, window);
                                    var p = result[i0 + margin];
                                    sumSqr[i] = (uint)(p * p);
                                }
                            }
                        }
                        else
                        {
                            for (int i = 0; i < pcount; i++)
                            {
                                int i0     = window * i;
                                int center = i0 + margin;
                                var p      = pixels[i];

                                if (p > result[center - 1] && p < result[center + 1])
                                {
                                    result[center] += p;
                                    sumSqr[i]      += (uint)(p * p);
                                }
                                else
                                {
                                    int insertionPoint = i0;
                                    for (int j = i0; j < i0 + window; j++)
                                    {
                                        if (j == center)
                                        {
                                            continue;
                                        }
                                        if (p <= result[j])
                                        {
                                            if (j < center)
                                            {
                                                insertionPoint = j;
                                            }
                                            break;
                                        }
                                        insertionPoint = j;
                                    }

                                    if (insertionPoint < center)
                                    {
                                        var _p = result[center - 1];
                                        result[center] += _p;
                                        sumSqr[i]      += (uint)(_p * _p);
                                        for (int j = center - 1; j > insertionPoint; j--)
                                        {
                                            result[j] = result[j - 1];
                                        }
                                    }
                                    else
                                    {
                                        var _p = result[center + 1];
                                        result[center] += _p;
                                        sumSqr[i]      += (uint)(_p * _p);
                                        for (int j = center + 1; j < insertionPoint; j++)
                                        {
                                            result[j] = result[j + 1];
                                        }
                                    }
                                    result[insertionPoint] = p;
                                }
                            }
                        }
                        count++;
                    }
                }
            }

            double k          = 3.0;
            int    n          = 4;
            int    sumCount   = count - window + 1;
            var    newpixels  = new ushort[pcount];
            var    newpixels2 = new ushort[pcount];
            int    skipped    = 0;

            for (int i = 0; i < pcount; i++)
            {
                int  i0     = window * i;
                int  center = i0 + margin;
                int  sum    = result[center];
                uint sqr    = sumSqr[i];
                int  _count = sumCount;
                while (true)
                {
                    double mean     = (double)sum / _count;
                    double sigma    = Math.Sqrt((double)sqr / _count - mean * mean);
                    double delta    = Math.Max(1.0, k * sigma);
                    int    min      = (int)Math.Round(mean - delta);
                    int    max      = (int)Math.Round(mean + delta);
                    bool   finished = true;
                    for (int j = i0; j < i0 + window; j++)
                    {
                        if (j != center)
                        {
                            var p = result[j];
                            if (p != 0 && p >= min && p <= max)
                            {
                                sum      += p;
                                sqr      += (uint)(p * p);
                                result[j] = 0;
                                _count++;
                                finished = false;
                            }
                        }
                    }
                    if (finished || count == _count)
                    {
                        int sum2   = 0;
                        int count2 = 0;
                        for (int j = i0; j < i0 + window; j++)
                        {
                            if (j != center)
                            {
                                var p = result[j];
                                if (p != 0)
                                {
                                    sum2 += p;
                                    skipped++;
                                    count2++;
                                }
                            }
                        }
                        if (sum2 > 0)
                        {
                            sum2 -= 128 * count2;
                            sum2 *= n;
                            sum2 /= count2;
                        }
                        newpixels2[i] = (ushort)(sum2 + 128);
                        break;
                    }
                }
                sum         -= 128 * _count;
                sum         *= n;
                sum         /= _count;
                newpixels[i] = (ushort)(sum + 128);
            }

            MessageBox.Show(string.Format("Skipped {0}", (double)skipped / (pcount * count)));

            var raw = new RawImage(rect.Width, rect.Height, channel, newpixels);

            stackedZoom      = raw.ExtractRgbImage(new Rectangle(3, 3, rect.Width - 6, rect.Height - 6));
            stackedZoomCount = n;
            recalcCurve();
            showZoomImage();
        }
Esempio n. 3
0
        private RgbImage stackImages2(Rectangle rect, out int count)
        {
            //var start = DateTime.Now;
            var currentOffset = currentStack[currentStackIndex].Offset;

            rect.Offset(-currentOffset.X, -currentOffset.Y);

            count = 0;
            var p = new Int64[4][];

            foreach (var item in currentStack)
            {
                if (!item.IsExcluded)
                {
                    using (var rawImage = libraw.load_raw(item.FilePath)) {
                        var _rect = rect;
                        _rect.Offset(item.Offset);
                        //rawImage.ApplyDark( currentSession.Dark );
                        if (currentSession.Flat != null)
                        {
                            //rawImage.ApplyFlat( currentSession.Flat, 32 );
                        }
                        int channel = rawImage.Channel(_rect.X, _rect.Y);
                        var _pixels = rawImage.GetRawPixelsApplyFlat(_rect, currentSession.Flat, 64);
                        if (p[channel] == null)
                        {
                            var pixels = p[channel] = new Int64[rect.Width * rect.Height];
                            for (int i = 0; i < pixels.Length; i++)
                            {
                                pixels[i] = _pixels[i];
                                //pixels[i] -= 128;
                            }
                        }
                        else
                        {
                            var pixels = p[channel];
                            for (int i = 0; i < pixels.Length; i++)
                            {
                                pixels[i] += _pixels[i];
                                //pixels[i] -= 128;
                            }
                        }
                        count++;
                        GC.Collect();
                        Application.DoEvents();
                    }
                }
            }
            GC.Collect(0);
            //Application.DoEvents();

            var result = new RgbImage(rect.Width - 4, rect.Height - 4);

            ushort[] upixels = new ushort[rect.Width * rect.Height];
            for (int ch = 0; ch < 4; ch++)
            {
                if (p[ch] != null)
                {
                    var pixels = p[ch];
                    Array.Clear(upixels, 0, upixels.Length);
                    if (currentSession.Flat != null)
                    {
                        for (int i = 0; i < pixels.Length; i++)
                        {
                            upixels[i] = (ushort)(pixels[i] / 640 + 128);
                        }
                    }
                    else
                    {
                        for (int i = 0; i < pixels.Length; i++)
                        {
                            upixels[i] = (ushort)(pixels[i]);
                        }
                    }
                    using (var raw = new RawImage(rect.Width, rect.Height, ch, upixels))
                        using (var rgbImage = raw.ExtractRgbImage(new Rectangle(2, 2, rect.Width - 4, rect.Height - 4))) {
                            result.Add(rgbImage);
                        }
                }
            }
            //MessageBox.Show( ( DateTime.Now - start ).TotalMilliseconds.ToString( "0.00" ) );
            return(result);
        }