Esempio n. 1
0
        public static CImage ImpulseNoiseReduction_Universal(CImage original, int maxSizeD, int maxSizeL, IProgress <int> progressReporter = null)
        {
            Utilities.SetProgress(progressReporter, 0);

            if (original == null)
            {
                throw new ArgumentNullException();
            }

            if (original.Width < 100 || original.Height < 100)
            {
                throw new ArgumentOutOfRangeException("original", "Image needs to be greater than 100 x 100");
            }

            int[] histo = new int[256];
            int   maxLight = 0, minLight = 0;

            CImage workingCopy = new CImage(original.Width, original.Height, original.nBytes, original.Grid); //new CImage(original);

            workingCopy.DeleteBit0();

            GenerateLightHistogram(workingCopy, ref histo, ref maxLight, ref minLight);

            CPnoise PN = new CPnoise(histo, 1000, 4000);

            PN.NoiseFilter(ref workingCopy, histo, minLight, maxLight, maxSizeD, maxSizeL, progressReporter);


            // Clean-up
            int cleanUpLimit = (3 * workingCopy.Width * workingCopy.Height);

            for (int i = 0; i < cleanUpLimit; i++)
            {
                if (workingCopy.Grid[i] == 252 || workingCopy.Grid[i] == 254)
                {
                    workingCopy.Grid[i] = 255;
                }
            }

            Utilities.SetProgress(progressReporter, 100);
            return(workingCopy);
        }
        } //****************************** end ImageToBitmapNew ****************************************

        private void button2_Click(object sender, EventArgs e) // Impulse noise
        {
            ImpulseIm = new CImage(OrigIm.width, OrigIm.height, 24);

            ImpulseIm.Copy(OrigIm);

            int nbyte = ImpulseIm.N_Bits / 8;

            ImpulseIm.DeleteBit0(nbyte, this);

            int maxLight, minLight;

            int[] histo = new int[256];
            for (int i = 0; i < 256; i++)
            {
                histo[i] = 0;
            }

            int  light, index;
            byte R = 1, G = 1, B = 1;

            progressBar1.Step    = 1;
            progressBar1.Value   = 0;
            progressBar1.Maximum = 100;
            progressBar1.Visible = true;
            int jump, nStep = 15;

            if (ImpulseIm.height > 2 * nStep)
            {
                jump = ImpulseIm.height / nStep;
            }
            else
            {
                jump = 2;
            }
            for (int y = 0; y < ImpulseIm.height; y++)
            {
                if (y % jump == jump - 1)
                {
                    progressBar1.PerformStep();
                }
                for (int x = 0; x < ImpulseIm.width; x++) //======================================================
                {
                    index = x + y * ImpulseIm.width;      // Index of the pixel (x, y)
                    if (nbyte == 1)
                    {
                        light = ImpulseIm.Grid[index];
                    }
                    else
                    {
                        R     = (byte)(ImpulseIm.Grid[nbyte * index + 2] & 254);
                        G     = (byte)(ImpulseIm.Grid[nbyte * index + 1] & 254);
                        B     = (byte)(ImpulseIm.Grid[nbyte * index + 0] & 254);
                        light = MaxC(R, G, B);
                    }

                    if (light < 0)
                    {
                        light = 0;
                    }
                    if (light > 255)
                    {
                        light = 255;
                    }
                    histo[light]++;
                } //============================ end for (int x = 0; .. ========================================
            }     //============================== end for (int y = 0; .. ========================================
            for (maxLight = 255; maxLight > 0; maxLight--)
            {
                if (histo[maxLight] != 0)
                {
                    break;
                }
            }
            for (minLight = 0; minLight < 256; minLight++)
            {
                if (histo[minLight] != 0)
                {
                    break;
                }
            }

            CPnoise PN     = new CPnoise(histo, 1000, 4000);
            int     Number = 0;

            progressBar1.Visible = true;

            PN.Sort(ImpulseIm, histo, Number, pictureBox1.Width, pictureBox1.Height, this);

            int maxSizeD = (int)numericUpDown1.Value;
            int maxSizeL = (int)numericUpDown2.Value;

            PN.LightNoise(ref ImpulseIm, minLight, maxLight, maxSizeL, this);
            ImpulseIm.DeleteBit0(nbyte, this);

            PN.DarkNoise(ref ImpulseIm, minLight, maxLight, maxSizeD, this);

            progressBar1.Step = 1;
            int Len = nbyte * origBmp.Width * origBmp.Height;

            nStep = 10;
            jump  = Len / nStep;
            for (int i = 0; i < nbyte * origBmp.Width * origBmp.Height; i++)
            {
                if ((i % jump) == jump - 1)
                {
                    progressBar1.PerformStep();
                }
                if (ImpulseIm.Grid[i] == 252 || ImpulseIm.Grid[i] == 254)
                {
                    ImpulseIm.Grid[i] = 255;
                }
            }

            ImageToBitmapNew(ImpulseIm, BmpPictBox2); // ImpulseIm is always color image but BmpPictBox2 can be indexed
            pictureBox2.Image = BmpPictBox2;

            progressBar1.Visible = false;
            pictureBox2.Visible  = true;
            label3.Visible       = true;
            label3.Text          = "Impulse noise suppressed";
            button3.Visible      = true;
            label7.Text          = "Click 'Segment'";
            label7.Visible       = true;
            IMPULSE = true;
        } //********************************* end impulse noise *************************************