Пример #1
0
      } //********************** end ColorToGray **********************

      public int ColorToGray(CImage inp)

      /* Transforms the colors of the color image "inp" in lightness=(r+g+b)/3
       * and puts these values to this.Grid. --------- */
      {
          int sum, x, y, c;

          if (inp.nBits != 24)
          {
              return(-1);
          }
          nBits = 8; width = inp.width; height = inp.height;
          Grid  = new byte[width * height * 8];
          for (y = 0; y < height; y++)    //=========================
          {
              for (x = 0; x < width; x++) // =====================
              {
                  sum = 0;
                  for (c = 0; c < 3; c++)
                  {
                      sum += inp.Grid[c + 3 * (x + width * y)];
                  }
                  Grid[y * width + x] = (byte)(sum / 3);
              } // ========== for (x.  ====================
          }
          return(1);
      } //********************** end ColorToGray **********************
Пример #2
0
      public int ColorToGray(CImage inp, Form1 fm1)

      /* Transforms the colors of the color image "inp" in lightness=(r+g+b)/3
       * and puts these values to this.Grid. --------- */
      {
          int c, sum, x, y;

          if (inp.nBits != 24)
          {
              return(-1);
          }
          fm1.progressBar1.Value   = 0;
          fm1.progressBar1.Step    = 1;
          fm1.progressBar1.Visible = true;
          nBits = 8; width = inp.width; height = inp.height;
          Grid  = new byte[width * height * 8];
          for (y = 0; y < height; y++) //=========================
          {
              fm1.progressBar1.PerformStep();
              for (x = 0; x < width; x++) // =====================
              {
                  sum = 0;
                  for (c = 0; c < 3; c++)
                  {
                      sum += inp.Grid[c + 3 * (x + width * y)];
                  }
                  Grid[y * width + x] = (byte)(sum / 3);
              } // ========== for (x.  ====================
          }
          fm1.progressBar1.Visible = false;
          return(1);
      } //********************** end ColorToGray **********************
Пример #3
0
 public void Copy(CImage inp)
 {
     width  = inp.width;
     height = inp.height;
     nBits  = inp.nBits;
     for (int i = 0; i < width * height * nBits / 8; i++)
     {
         Grid[i] = inp.Grid[i];
     }
 }
Пример #4
0
        }         //************************************ end BreadthFirst_D ************************************

        public int DarkNoise(ref CImage Image, int minLight, int maxLight, int maxSize, Form1 fm1)
        {
            bool COLOR = (Image.nBits == 24);
            int  ind3 = 0, // index multiplied with 3
                 LabelBig2, Lum, rv = 0;

            if (maxSize == 0)
            {
                return(0);
            }
            fm1.progressBar1.Maximum = 100;
            fm1.progressBar1.Step    = 1;
            fm1.progressBar1.Value   = 0;
            fm1.progressBar1.Visible = false;
            int bri1 = 2;

            fm1.progressBar1.Visible = true;
            for (int light = maxLight - 2; light >= minLight; light--) //=========================================
            {
                if ((light % bri1) == 1)
                {
                    fm1.progressBar1.PerformStep();
                }
                for (int i = 0; i < nPixel[light]; i++) //========================================
                {
                    ind3 = 3 * Index[light][i];
                    if (COLOR)
                    {
                        LabelBig2 = Image.Grid[2 + ind3] & 1;
                        Lum       = MaxC(Image.Grid[2 + ind3], Image.Grid[1 + ind3], Image.Grid[0 + ind3]) & 254;
                    }
                    else
                    {
                        LabelBig2 = Image.Grid[Index[light][i]] & 2;
                        Lum       = Image.Grid[Index[light][i]] & 252;
                    }
                    if (Lum == light && LabelBig2 == 0)
                    {
                        rv = BreadthFirst_D(ref Image, i, light, maxSize);
                        if (rv < 0)
                        {
                            return(-1);
                        }
                    }
                } //============================= end for (int i.. =======================
            }     //=============================== end for (int light.. ========================
            fm1.progressBar1.Visible = false;
            return(rv);
        } //********************************* end DarkNoise *******************************
Пример #5
0
        } //******************************** end Sort *********************************************************

        public int Neighb(CImage Image, int W, int n)
        // Returns the index of the nth neighboor of the pixel W. If the neighboor
        // is outside the grid, then it returns -1.
        {
            int dx, dy, x, y, xn, yn;

            if (n == 4)
            {
                return(-1);  // "n==4" means Neigb==W
            }
            yn  = y = W / Image.width; xn = x = W % Image.width;
            dx  = (n % 3) - 1; dy = n / 3 - 1;
            xn += dx; yn += dy;
            if (xn < 0 || xn >= Image.width || yn < 0 || yn >= Image.height)
            {
                return(-2);
            }
            return(xn + Image.width * yn);
        }
Пример #6
0
        private void button1_Click(object sender, EventArgs e) // Open image
        {
            label3.Visible = false;
            label4.Visible = false;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    origBmp       = new Bitmap(openFileDialog1.FileName);
                    OpenImageFile = openFileDialog1.FileName;
                    Number        = 0;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " +
                                    ex.Message);
                }
            }
            else
            {
                return;
            }

            label5.Text          = "Opened file: " + openFileDialog1.FileName;
            label5.Visible       = true;
            label1.Visible       = true;
            label2.Visible       = true;
            label3.Visible       = true;
            label4.Visible       = true;
            button3.Visible      = true;
            textBox1.Visible     = true;
            textBox2.Visible     = true;
            progressBar1.Maximum = 100;
            progressBar1.Value   = 0;
            progressBar1.Step    = 1;
            progressBar1.Visible = true;

            byte[] Grid;
            if (origBmp.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                Grid = new byte[3 * origBmp.Width * origBmp.Height];
                BitmapToGridOld(origBmp, Grid);
                BMP_Graph = false;
            }
            else if (origBmp.PixelFormat == PixelFormat.Format24bppRgb)
            {
                Grid = new byte[3 * origBmp.Width * origBmp.Height];
                BitmapToGrid(origBmp, Grid);
                BMP_Graph = true;
            }
            else
            {
                MessageBox.Show("Not suitable pixel format=" + origBmp.PixelFormat);
                return;
            }
            label3.Visible = true;

            progressBar1.Visible = false;

            pictureBox1.Image = origBmp;

            Orig = new CImage(origBmp.Width, origBmp.Height, 24, Grid);
            Work = new CImage(origBmp.Width, origBmp.Height, 24, Grid);

            OPEN = true;
        } //***************************** end Open image *************************
Пример #7
0
        public int Sort(CImage Image, int[] histo, int Number, int picBox1Width, int picBox1Height, Form1 fm1)
        {
            int    light, i;
            double ScaleX = (double)picBox1Width / (double)Image.width;
            double ScaleY = (double)picBox1Height / (double)Image.height;
            double Scale; // Scale of the presentation of the image in "pictureBox1"

            if (ScaleX < ScaleY)
            {
                Scale = ScaleX;
            }
            else
            {
                Scale = ScaleY;
            }
            bool COLOR;

            if (Image.nBits == 24)
            {
                COLOR = true;
            }
            else
            {
                COLOR = false;
            }
            double marginX   = (double)(picBox1Width - Scale * Image.width) * 0.5;   // space left of the image
            double marginY   = (double)(picBox1Height - Scale * Image.height) * 0.5; // space above the image
            bool   Condition = false;                                                // Condition for skipping pixel (x, y) if it lies in one of the global rectangles "fm1.v"

            fm1.progressBar1.Value   = 0;
            fm1.progressBar1.Step    = 1;
            fm1.progressBar1.Visible = true;
            fm1.progressBar1.Maximum = 100;
            for (light = 0; light < 256; light++)
            {
                nPixel[light] = 0;
            }
            for (light = 0; light < 256; light++)
            {
                for (int light1 = 0; light1 < histo[light] + 1; light1++)
                {
                    Index[light][light1] = 0;
                }
            }

            int y1 = 1 + Image.height / 100;

            for (int y = 0; y < Image.height; y++) //===============================================================
            {
                if (y % y1 == 1)
                {
                    fm1.progressBar1.PerformStep();
                }
                for (int x = 0; x < Image.width; x++) //============================================================
                {
                    Condition = false;
                    for (int k = 0; k < Number; k += 2)
                    {
                        Condition = Condition || getCond(k, x, y, marginX, marginY, Scale, fm1);
                    }
                    if (Condition)
                    {
                        continue;
                    }
                    i = x + y * Image.width; // Index of the pixel (x, y)
                    if (COLOR)
                    {
                        light = MaxC(Image.Grid[3 * i + 2] & 254, Image.Grid[3 * i + 1] & 254, Image.Grid[3 * i + 0] & 254);
                    }
                    else
                    {
                        light = Image.Grid[i] & 252;
                    }
                    if (light < 0)
                    {
                        light = 0;
                    }
                    if (light > 255)
                    {
                        light = 255;
                    }
                    Index[light][nPixel[light]] = i; // record of the index "i" of a pixel with lightness "light"
                    if (nPixel[light] < histo[light])
                    {
                        nPixel[light]++;
                    }
                } //============================ end for (int x=1; .. ========================================
            }     //============================== end for (int y=1; .. ========================================
            fm1.progressBar1.Visible = false;
            return(1);
        } //******************************** end Sort *********************************************************
Пример #8
0
        } //********************************* end LightNoise ******************************

        private int BreadthFirst_L(ref CImage Image, int i, int light, int maxSize)
        // Looks for pixels with gray values >=light composing with the pixel "Index[light][i]"
        // an 8-connected subset. The size of the subset must be less than "maxSize".
        // Instead of labeling the pixels of the subset, indices of pixels of the subset are saved in Comp.
        // Variable "i" is the index of the starting pixel in Index[light][i];
        // Pixels which are put into queue and into Comp[] are labeled in "Image.Grid(green)" by setting Bit 0 to 1.
        // Pixels wich belong to a too big component and having the gray value equal to "light" are
        // labeled in "Image.Grid(red)" by setting Bit 0 to 1. If such a labeled pixel is found in the while loop
        // then "small" is set to 0. The insruction for breaking the loop is at the end of the loop.
        {
            int  lightNeb, index, LabelQ1, LabelBig2, MaskBri = 252, MaskColor = 254, maxNeib = 8, Neib, nextIndex;
            bool small = true;

            int[] MaxBound = new int[3];
            bool  COLOR    = (Image.nBits == 24);

            index = Index[light][i];
            for (int c = 0; c < 3; c++)
            {
                MaxBound[c] = -255;
            }
            for (int p = 0; p < MaxSize; p++)
            {
                Comp[p] = -1;
            }
            int numbPix = 0;

            Comp[numbPix] = index;
            numbPix++;
            if (COLOR == true)
            {
                Image.Grid[1 + 3 * index] |= 1; // Labeling as in Comp
            }
            else
            {
                Image.Grid[index] |= 1; // Labeling as in Comp
            }
            Q1.input = Q1.output = 0;
            Q1.Put(index);          // putting index into the queue

            while (Q1.Empty() == 0) //=== loop running while queue not empty ========================
            {
                nextIndex = Q1.Get();
                for (int n = 0; n <= maxNeib; n++)      //======== all neighbors of nextIndex ================
                {
                    Neib = Neighb(Image, nextIndex, n); // the index of the nth neighbor of nextIndex
                    if (Neib < 0)
                    {
                        continue; // Neib<0 means outside the image
                    }
                    if (COLOR)
                    {
                        LabelQ1   = Image.Grid[1 + 3 * Neib] & 1;
                        LabelBig2 = Image.Grid[2 + 3 * Neib] & 1;
                        lightNeb  = MaxC(Image.Grid[2 + 3 * Neib], Image.Grid[1 + 3 * Neib],
                                         Image.Grid[0 + 3 * Neib]) & 254;                    // MaskColor;
                    }
                    else
                    {
                        LabelQ1   = Image.Grid[Neib] & 1;
                        LabelBig2 = Image.Grid[Neib] & 2;
                        lightNeb  = Image.Grid[Neib] & MaskBri;
                    }
                    if (lightNeb == light && LabelBig2 > 0)
                    {
                        small = false;
                    }

                    if (lightNeb >= light) //------------------------------------------------------------
                    {
                        if (LabelQ1 > 0)
                        {
                            continue;
                        }
                        Comp[numbPix] = Neib; // putting the element with index Neib into Comp
                        numbPix++;
                        if (COLOR)
                        {
                            Image.Grid[1 + 3 * Neib] |= 1; // Labeling with "1" as in Comp
                        }
                        else
                        {
                            Image.Grid[Neib] |= 1; // Labeling with "1" as in Comp
                        }
                        if (numbPix > maxSize)
                        {
                            small = false;
                            break;
                        }
                        Q1.Put(Neib);
                    }
                    else // lightNeb<light
                    {
                        if (Neib != index) //-----------------------------------------------------
                        {
                            if (COLOR)
                            {
                                if (lightNeb > MaxC(MaxBound[2], MaxBound[1], MaxBound[0]))
                                {
                                    for (int c = 0; c < 3; c++)
                                    {
                                        MaxBound[c] = (Image.Grid[c + 3 * Neib] & MaskColor);
                                    }
                                }
                            }
                            else
                            {
                                if (lightNeb > MaxBound[0])
                                {
                                    MaxBound[0] = lightNeb;
                                }
                            }
                        } //------------------ end if (Neib!=index) ----------------------------
                    }     //-------------------- end if (lightNeb<=light) and else ------------------------
                }         // =================== end for (n=0; .. ====================================
                if (small == false)
                {
                    break;
                }
            } // ===================== end while ==============================================
            int lightComp, // lightness of a pixel whose index is contained in "Comp"
                nChanged = 0; // number of pixels whose lightness was changed

            for (int m = 0; m < numbPix; m++)          //========================================================
            {
                if (small == true && MaxBound[0] >= 0) //it was >-255; ----"-1" means MaxBound was not calculated ---------
                {
                    if (COLOR == true)
                    {
                        for (int c = 0; c < 3; c++)
                        {
                            Image.Grid[c + 3 * Comp[m]] = (byte)MaxBound[c];
                        }
                    }
                    else
                    {
                        Image.Grid[Comp[m]] = (byte)MaxBound[0];
                        nChanged++;
                    }
                }
                else
                {
                    if (COLOR == true)
                    {
                        lightComp = MaxC(Image.Grid[2 + 3 * Comp[m]], Image.Grid[1 + 3 * Comp[m]],
                                         Image.Grid[0 + 3 * Comp[m]]) & MaskColor;
                    }
                    else
                    {
                        lightComp = Image.Grid[Comp[m]] & MaskBri;
                    }

                    if (lightComp == light) //------------------------------------------------------
                    {
                        if (COLOR == true)
                        {
                            Image.Grid[2 + 3 * Comp[m]] |= 1;
                        }
                        else
                        {
                            Image.Grid[Comp[m]] |= 2;
                        }
                    }
                    else
                    {
                        if (COLOR == true)
                        {
                            Image.Grid[1 + 3 * Comp[m]] &= (byte)MaskColor; // deleting label 1
                            Image.Grid[2 + 3 * Comp[m]] &= (byte)MaskColor; // deleting label 2
                        }
                        else
                        {
                            Image.Grid[Comp[m]] &= (byte)MaskBri; // deleting the labels
                        }
                    } //----------------------- end if (lightComp==light) and else ---------------
                } //------------------------- end if (small && MaxBound[0]>0) and else ----------
            }     //=========================== end for (int m=0 .. ================================
            return(nChanged); // numbPix;
        }         //************************************ end BreadthFirst_L *****************************
Пример #9
0
        private int BreadthFirst_D(ref CImage Image, int i, int light, int maxSize)

        /* Looks for pixels with lightness <=light composing with the pixel "Index[light][i]"
         * an 8-connected subset. The size of the subset must be less than "maxSize".
         * Instead of labeling the pixels of the subset, indices of pixels of the subset are saved in Comp.
         * Variable "index" is the index of the starting pixel in Index[light][i];
         * Pixels which are put into queue and into Comp[] are labeled in "Image.Grid(green)" by setting Bit 0 to 1.
         * Pixels which belong to a too big component and having the gray value equal to "light" are
         * labeled in "Image.Grid(red)" by setting Bit 0 to 1. If such a labeled pixel is found in the while loop
         * then "small" is set to 0. The instruction for breaking the loop is at the end of the loop. --*/
        {
            int lightNeb,                           // lightness of the neighbor
                index, LabelQ1, LabelBig2, maxNeib, // maxNeib is the maximum number of neighbors of a pixel
                Neib,                               // the index of a neighbor
                nextIndex,                          // index of the next pixel in the queue
                numbPix;                            // number of pixel indices in "Comp"
            bool small;
            bool COLOR = (Image.nBits == 24);

            index = Index[light][i];
            int[] MinBound = new int[3]; // color of a pixel with minimum lightness among pixels near the subset
            for (int c = 0; c < 3; c++)
            {
                MinBound[c] = 300;
            }
            for (int p = 0; p < MaxSize; p++)
            {
                Comp[p] = -1;                         // MaxSize is element of class CPnoise
            }
            numbPix       = 0;
            maxNeib       = 8; // maximum number of neighbors
            small         = true;
            Comp[numbPix] = index;
            numbPix++;
            if (COLOR)
            {
                Image.Grid[1 + 3 * index] |= 1; // Labeling as in Comp (LabelQ1)
            }
            else
            {
                Image.Grid[index] |= 1; // Labeling as in Comp
            }
            Q1.input = Q1.output = 0;
            Q1.Put(index);          // putting index into the queue
            while (Q1.Empty() == 0) //=  loop running while queue not empty =======================
            {
                nextIndex = Q1.Get();
                for (int n = 0; n <= maxNeib; n++)      // == all neighbors of nextIndex =====================
                {
                    Neib = Neighb(Image, nextIndex, n); // the index of the nth neighbor of nextIndex
                    if (Neib < 0)
                    {
                        continue; // Neib<0 means outside the image
                    }
                    if (COLOR)
                    {
                        LabelQ1   = Image.Grid[1 + 3 * Neib] & 1;
                        LabelBig2 = Image.Grid[2 + 3 * Neib] & 1;
                        lightNeb  = MaxC(Image.Grid[2 + 3 * Neib], Image.Grid[1 + 3 * Neib],
                                         Image.Grid[0 + 3 * Neib]) & 254;                    // MaskColor;
                    }
                    else
                    {
                        LabelQ1   = Image.Grid[Neib] & 1;
                        LabelBig2 = Image.Grid[Neib] & 2;
                        lightNeb  = Image.Grid[Neib] & 252; // MaskGV;
                    }
                    if (lightNeb == light && LabelBig2 > 0)
                    {
                        small = false;
                    }
                    if (lightNeb <= light) //------------------------------------------------------------
                    {
                        if (LabelQ1 > 0)
                        {
                            continue;
                        }
                        Comp[numbPix] = Neib; // putting the element with index Neib into Comp
                        numbPix++;
                        if (COLOR)
                        {
                            Image.Grid[1 + 3 * Neib] |= 1; // Labeling with "1" as in Comp
                        }
                        else
                        {
                            Image.Grid[Neib] |= 1; // Labeling with "1" as in Comp
                        }
                        if (numbPix > maxSize)
                        {
                            small = false;
                            break;
                        }
                        Q1.Put(Neib);
                    }
                    else // lightNeb < light
                    {
                        if (Neib != index) //-----------------------------------------------------
                        {
                            if (COLOR)
                            {
                                if (lightNeb < MaxC(MinBound[2], MinBound[1], MinBound[0]))
                                {
                                    for (int c = 0; c < 3; c++)
                                    {
                                        MinBound[c] = Image.Grid[c + 3 * Neib];
                                    }
                                }
                            }
                            else
                            if (lightNeb < MinBound[0])
                            {
                                MinBound[0] = lightNeb;
                            }
                        } //------------------ end if (Neib != index) ----------------------------
                    }     //-------------------- end if (lightNeb<=light) and else ----------------------
                }         // ===================== end for (n=0; .. ======================================
                if (small == false)
                {
                    break;
                }
            } // ===================== end while =================================================

            // Deleting
            int lightComp;                      // lightness of a pixel whose index is contained in "Comp"

            for (int m = 0; m < numbPix; m++)   //======================================================
            {
                if (small && MinBound[0] < 300) //--"300" means MinBound was not calculated ---
                {
                    if (COLOR)
                    {
                        for (int c = 0; c < 3; c++)
                        {
                            Image.Grid[c + 3 * Comp[m]] = (byte)MinBound[c];
                        }
                    }
                    else
                    {
                        Image.Grid[Comp[m]] = (byte)MinBound[0];
                    }
                }
                else
                {
                    if (COLOR)
                    {
                        lightComp = MaxC(Image.Grid[2 + 3 * Comp[m]], Image.Grid[1 + 3 * Comp[m]],
                                         Image.Grid[0 + 3 * Comp[m]]) & 254;
                    }
                    else
                    {
                        lightComp = Image.Grid[Comp[m]] & 252; // MaskGV;
                    }
                    if (lightComp == light)                    //----------------------------------------------------------------
                    {
                        if (COLOR)
                        {
                            Image.Grid[2 + 3 * Comp[m]] |= 1; // setting label 2
                        }
                        else
                        {
                            Image.Grid[Comp[m]] |= 2;
                        }
                    }
                    else // lightComp!=light
                    {
                        if (COLOR)
                        {
                            Image.Grid[1 + 3 * Comp[m]] &= (byte)254; // deleting label 1
                            Image.Grid[2 + 3 * Comp[m]] &= (byte)254; // deleting label 2
                        }
                        else
                        {
                            Image.Grid[Comp[m]] &= 252; // (byte)MaskGV; // deleting the labels
                        }
                    } //------------------------------ end if (bric == light) and else ------------------
                } //-------------------------------- end if (small != false) and else -----------------
            }     //================================== end for (int m=0 .. ================================
            return(numbPix);
        }         //************************************ end BreadthFirst_D ************************************