private void button8_Click(object sender, EventArgs e) // Detect edges
        {
            if (OPEN == false)
            {
                MessageBox.Show("Please open an image");
                return;
            }
            progressBar1.Visible = true;
            progressBar1.Value   = 0;

            SigmaIm.SigmaSimpleUni(OrigIm, 1, 30, this);

            if (OrigIm.N_Bits == 24)
            {
                ExtremIm.ExtremVarColor(SigmaIm, 2, this);
            }
            else
            {
                ExtremIm.ExtremVar(SigmaIm, 2, this);
            }

            Threshold = (int)numericUpDown1.Value;

            CombIm.LabelCellsSign(Threshold, ExtremIm, this);
            CombIm.CleanCombNew(21, this);

            EdgeIm.CracksToPixel(CombIm);

            EdgeBmp = new Bitmap(OrigIm.width, OrigIm.height, PixelFormat.Format24bppRgb);
            EdgeIm.ImageToBitmapOld(BmpPictBox2, this);

            pictureBox2.Image      = BmpPictBox2;
            label4.Text            = "Detected edges";
            label4.Visible         = true;
            label5.Text            = "Click 'Polygons'";
            label5.Visible         = true;
            label8.Visible         = true;
            button2.Visible        = true;
            numericUpDown8.Visible = true;
            progressBar1.Visible   = false;
            EDGES = true;
        } //*********************************** end Detect edges ************************************
        } //**************************** end Open image *************************************

        private void button2_Click(object sender, EventArgs e) //  Edge detection
        {
            if (OPEN == false)
            {
                MessageBox.Show("Please open an image");
                return;
            }
            if (!radioButton1.Checked && !radioButton2.Checked)
            {
                MessageBox.Show("Please click one of the radio buttons on the left side");
                return;
            }

            progressBar1.Visible = true;
            progressBar1.Value   = 0;

            progressBar1.Visible = true;
            SigmaIm.SigmaSimpleUni(OrigIm, 1, 30, this);
            ExtremIm.ExtremLightUni(SigmaIm, 3, this);

            Threshold = (int)numericUpDown1.Value;
            int NX = OrigIm.width;

            CombIm.LabelCellsSign(Threshold, ExtremIm, this);

            CombIm.CleanCombNew(81, this);

            int rv = CombIm.CheckComb(this);

            if (rv < 0)
            {
                Application.Exit();
            }

            EdgeIm.CracksToPixel(CombIm);

            // The image "CombIm" gets the pixel values as lightnes (MaxC) of pixels of "ExtremIm"
            for (int y = 0; y < OrigIm.height; y++)
            {
                for (int x = 0; x < OrigIm.width; x++)
                {
                    CombIm.Grid[2 * x + 1 + (2 * NX + 1) * (2 * y + 1)] =
                        CombIm.MaxC(ExtremIm.Grid[3 * (x + NX * y) + 2], ExtremIm.Grid[3 * (x + NX * y) + 1], ExtremIm.Grid[3 * (x + NX * y) + 0]);
                }
            }

            EdgeIm.nLoop     = nLoop;
            EdgeIm.denomProg = denomProg;
            EdgeIm.ImageToBitmapOld(BmpPictBox2, this);
            pictureBox2.Refresh();

            progressBar1.Visible = false;

            label6.Visible         = true;
            label6.Text            = "Detected edges";
            button3.Visible        = true;
            label2.Visible         = true;
            numericUpDown2.Visible = true;
            label7.Text            = "Click 'Pollygons'";
            label7.Visible         = true;
            if (PanelWithGrid != null)
            {
                PanelWithGrid.Dispose();
            }

            EDGES = true;
        } //*************************** end edge detection ******************************
        private void button3_Click(object sender, EventArgs e) // Segment
        {
            if (!OPEN)
            {
                MessageBox.Show("Please open an image");
                return;
            }

            if (!IMPULSE)
            {
                MessageBox.Show("Please click 'Impulse noise'");
                return;
            }

            SigmaIm = new CImage(OrigIm.width, OrigIm.height, nbyteIm * 8);

            progressBar1.Value = 0;
            SigmaIm.SigmaSimpleUni(ImpulseIm, 1, 30, this);

            ExtremIm = new CImage(OrigIm.width, OrigIm.height, nbyteIm * 8);
            if (nbyteBmp == 3)
            {
                ExtremIm.ExtremVarColor(SigmaIm, 2, this);
            }
            else
            {
                ExtremIm.ExtremLightUni(SigmaIm, 2, this);
            }

            int rv, x, y;

            Palet = new int[256];                               // This is a palette containing an RGB int color for each of 256 indices
            Pal   = new CImage(OrigIm.width, OrigIm.height, 8); // This is an indexed image
            if (ExtremIm.N_Bits == 24)
            {
                rv = Pal.MakePalette(ExtremIm, Palet, this);
            }

            SegmentIm = new CImage(SigmaIm.width, SigmaIm.height, 24);
            Bitmap bmp = new Bitmap(SigmaIm.width, SigmaIm.height, PixelFormat.Format24bppRgb);
            Color  color;

            int PalColor, jump, value;

            if (SigmaIm.height > 300)
            {
                jump = SigmaIm.height / (100 / 6);
            }
            else
            {
                jump = 2;
            }
            progressBar1.Visible = true;
            for (y = 0; y < SigmaIm.height; y++)
            {
                if (y % jump == jump - 1)
                {
                    progressBar1.PerformStep();
                }
                for (x = 0; x < SigmaIm.width; x++)
                {
                    value    = Pal.Grid[x + SigmaIm.width * y];
                    PalColor = Palet[value];
                    color    = Color.FromArgb((PalColor) & 255, (PalColor >> 8) & 255, (PalColor >> 16) & 255);
                    SegmentIm.Grid[2 + 3 * (x + SigmaIm.width * y)] = (byte)(PalColor & 255);
                    SegmentIm.Grid[1 + 3 * (x + SigmaIm.width * y)] = (byte)((PalColor >> 8) & 255);
                    SegmentIm.Grid[0 + 3 * (x + SigmaIm.width * y)] = (byte)((PalColor >> 16) & 255);
                }
            }

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

            label3.Text            = "Segmented image, table 'Palet'";
            label3.Visible         = true;
            pictureBox2.Visible    = true;
            progressBar1.Value     = 0;
            button4.Visible        = true;
            label4.Visible         = true;
            numericUpDown3.Visible = true;
            label7.Text            = "Click 'Detect edges'";
            label7.Visible         = true;
            SEGMENTED = true;
        } //********************************* end Segment ******************************************