} //************************** end ColorToGray ***************************

        public int ColorToGray(CImage inp)

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

            if (inp.N_Bits != 24)
            {
                return(-1);
            }
            N_Bits = 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 **********************
 public void Copy(CImage inp)
 {
     width  = inp.width;
     height = inp.height;
     N_Bits = inp.N_Bits;
     for (int i = 0; i < width * height * N_Bits / 8; i++)
     {
         Grid[i] = inp.Grid[i];
     }
 }
예제 #3
0
        }         //********************************* end DarkNoise *******************************

        public int LightNoise(ref CImage Image, int minLi, int maxLi, int maxLight, Form1 fm1)
        {
            bool COLOR = (Image.N_Bits == 24);
            int  ind3 = 0, // index multiplied with 3
                 Label2, Lum, rv = 0;

            if (maxLight == 0)
            {
                return(0);
            }

            fm1.progressBar1.Minimum = 0;
            fm1.progressBar1.Maximum = 100;
            fm1.progressBar1.Step    = 1;
            fm1.progressBar1.Value   = 0;
            fm1.progressBar1.Visible = true;

            int light1 = 1 + (255 - minLi + 1) / 100;

            if (light1 == 0)
            {
                light1 = 1;
            }
            for (int light = minLi; light <= 255; light++) //=========================================
            {
                int index;
                if (light % light1 == 1)
                {
                    fm1.progressBar1.PerformStep();
                }
                for (int i = 0; i < nPixel[light]; i++) //========================================
                {
                    ind3  = 3 * Index[light][i];
                    index = Index[light][i];
                    if (COLOR)
                    {
                        Label2 = Image.Grid[2 + 3 * Index[light][i]] & 1;
                        Lum    = ((Image.Grid[0 + ind3] & 254) + (Image.Grid[1 + ind3] & 254) + (Image.Grid[2 + ind3])) / 3;
                    }
                    else
                    {
                        Label2 = Image.Grid[Index[light][i]] & 2;
                        Lum    = Image.Grid[Index[light][i]];
                    }
                    if (Lum == light && Label2 == 0)
                    {
                        rv = BreadthFirst_L(ref Image, i, light, maxLi);
                    }
                } //============================= end for (int i.. =======================
            }     //=============================== end for (int light.. ========================
            fm1.progressBar1.Visible = false;
            return(rv);
        } //********************************* end LightNoise ******************************
        public int SigmaSimpleUni(CImage Inp, int hWind, int Toleranz)
        // Simple sigma filter for both gray value and color images.
        {
            int[] gvMin = new int[3], gvMax = new int[3], nPixel = new int[3], Sum = new int[3];
            int   c;

            N_Bits = Inp.N_Bits;
            int nbyte = N_Bits / 8;

            for (int y = 0; y < height; y++) // ==================================================
            {
                int gv, y1, yStart = Math.Max(y - hWind, 0), yEnd = Math.Min(y + hWind, height - 1);
                for (int x = 0; x < width; x++) //===============================================
                {
                    int x1, xStart = Math.Max(x - hWind, 0), xEnd = Math.Min(x + hWind, width - 1);
                    for (c = 0; c < nbyte; c++)
                    {
                        Sum[c]   = 0; nPixel[c] = 0;
                        gvMin[c] = Math.Max(0, Inp.Grid[c + nbyte * (x + width * y)] - Toleranz);
                        gvMax[c] = Math.Min(255, Inp.Grid[c + nbyte * (x + width * y)] + Toleranz);
                    }
                    for (y1 = yStart; y1 <= yEnd; y1++)
                    {
                        for (x1 = xStart; x1 <= xEnd; x1++)
                        {
                            for (c = 0; c < nbyte; c++)
                            {
                                gv = Inp.Grid[c + nbyte * (x1 + y1 * width)];
                                if (gv >= gvMin[c] && gv <= gvMax[c])
                                {
                                    Sum[c] += gv;
                                    nPixel[c]++;
                                }
                            }
                        }
                    }
                    for (c = 0; c < nbyte; c++)
                    {
                        if (nPixel[c] > 0)
                        {
                            Grid[c + nbyte * (x + width * y)] = (byte)((Sum[c] + nPixel[c] / 2) / nPixel[c]);
                        }
                        else
                        {
                            Grid[c + nbyte * (x + width * y)] = Inp.Grid[c + nbyte * (x + width * y)];
                        }
                    }
                } //================== end for (int x... =================================
            }     //==================== end for (int y... ===================================
            return(1);
        }         //********************** end SigmaSimpleUni **********************************
        } //********************** end ColorToGray **********************

        public int FastAverageM(CImage Inp, int hWind, Form1 fm1)
        // Filters the gray value image "Inp" and returns the result as *this."
        {
            if (Inp.N_Bits != 8)
            {
                MessageBox.Show("FastAverageM cannot process an image with " + Inp.N_Bits + " bits per pixel");
                return(-1);
            }
            N_Bits = 8; width = Inp.width; height = Inp.height;
            Grid   = new byte[width * height];
            int[] ColSum; int[] nC;
            ColSum = new int[width];
            nC     = new int[width];
            for (int i = 0; i < width; i++)
            {
                ColSum[i] = nC[i] = 0;
            }

            int nS = 0, Sum = 0;

            for (int y = 0; y < height + hWind; y++)
            {
                int yout = y - hWind, ysub = y - 2 * hWind - 1;
                Sum = 0; nS = 0;
                for (int x = 0; x < width + hWind; x++)
                {
                    int xout = x - hWind, xsub = x - 2 * hWind - 1; // 1. and 2. addition
                    if (y < height && x < width)
                    {
                        ColSum[x] += Inp.Grid[x + width * y]; nC[x]++;
                    }                                                                           // 3. and 4. addition
                    if (ysub >= 0 && x < width)
                    {
                        ColSum[x] -= Inp.Grid[x + width * ysub]; nC[x]--;
                    }
                    if (yout >= 0 && x < width)
                    {
                        Sum += ColSum[x]; nS += nC[x];
                    }
                    if (yout >= 0 && xsub >= 0)
                    {
                        Sum -= ColSum[xsub]; nS -= nC[xsub];
                    }
                    if (xout >= 0 && yout >= 0)
                    {
                        Grid[xout + width * yout] = (byte)((Sum + nS / 2) / nS);
                    }
                }
            }
            return(1);
        } //***************************** end FastAverageM ************************************************
        } //***************************** end CorrectShading **********************************************

        private int ImageToBitmapNew(CImage Image, Bitmap bmp)
        // Any image and color bitmap.
        {
            Rectangle  rect    = new Rectangle(0, 0, bmp.Width, bmp.Height);
            BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);

            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
            {
                if (MessReturn("ImageToBitmapNew: we don't use this pixel format=" + bmp.PixelFormat) < 0)
                {
                    return(-1);
                }
            }
            IntPtr ptr    = bmpData.Scan0;
            int    size   = bmp.Width * bmp.Height;
            int    length = Math.Abs(bmpData.Stride) * bmp.Height;

            byte[] rgbValues = new byte[length];

            int nbyteIm = Image.N_Bits / 8;

            for (int y = 0; y < bmp.Height; y++) //=================================================================
            {
                int jump = bmp.Height / 100;
                if (y % jump == jump - 1)
                {
                    progressBar1.PerformStep();
                }

                for (int x = 0; x < bmp.Width; x++)
                {
                    Color color = Color.FromArgb(0, 0, 0);;
                    if (nbyteIm == 3)
                    {
                        color = Color.FromArgb(Image.Grid[2 + 3 * (x + Image.width * y)],
                                               Image.Grid[1 + 3 * (x + Image.width * y)], Image.Grid[0 + 3 * (x + Image.width * y)]);
                    }
                    else
                    {
                        color = Color.FromArgb(Image.Grid[x + Image.width * y],
                                               Image.Grid[x + Image.width * y], Image.Grid[x + Image.width * y]);
                    }
                    rgbValues[3 * x + Math.Abs(bmpData.Stride) * y + 0] = color.B;
                    rgbValues[3 * x + Math.Abs(bmpData.Stride) * y + 1] = color.G;
                    rgbValues[3 * x + Math.Abs(bmpData.Stride) * y + 2] = color.R;
                } //==================================== end for (int x ... =============================
            }     //===================================== end for (int y ... ===============================
            System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, length);
            bmp.UnlockBits(bmpData);
            return(1);
        } //****************************** end ImageToBitmapNew ****************************************
예제 #7
0
        }         //********************************* end DarkNoise *******************************

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

            fm1.progressBar1.Minimum = 0;
            fm1.progressBar1.Maximum = 100;
            fm1.progressBar1.Step    = 1;
            fm1.progressBar1.Visible = true;
            if (maxSize == 0)
            {
                fm1.progressBar1.Step = 50;
                fm1.progressBar1.PerformStep();
                return(1);
            }

            int jump = Image.width * Image.height / 50;

            for (int light = minLight; light <= maxLight; light++) //=========================================
            {
                int index;
                for (int i = 0; i <= nPixel[light]; i++) //========================================
                {
                    ind3  = 3 * Index[light][i];
                    index = Index[light][i];
                    if ((index % jump) == jump - 1)
                    {
                        fm1.progressBar1.PerformStep();
                    }

                    if (COLOR)
                    {
                        Label2 = Image.Grid[2 + 3 * Index[light][i]] & 1;
                        Lum    = MaxC(Image.Grid[2 + ind3] & 254, Image.Grid[1 + ind3] & 254, Image.Grid[0 + ind3]);
                    }
                    else
                    {
                        Label2 = Image.Grid[Index[light][i]] & 2;
                        Lum    = Image.Grid[Index[light][i]];
                    }
                    if (Lum == light && Label2 == 0)
                    {
                        rv = BreadthFirst_L(ref Image, i, light, maxSize);
                    }
                } //============================= end for (int i.. =======================
            }     //=============================== end for (int light.. ========================
            return(rv);
        }         //********************************* end LightNoise ******************************
예제 #8
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);
        }
예제 #9
0
        }         //************************************ end BreadthFirst_D ************************************

        public int DarkNoise(ref CImage Image, int minLi, int maxLi, int maxDark, Form1 fm1)
        {
            bool COLOR = (Image.N_Bits == 24);
            int  ind3 = 0, // index multilied with 3
                 Label2, Lum, rv = 0;

            if (maxDark == 0)
            {
                return(0);
            }
            fm1.progressBar1.Visible = true;
            int light1 = (maxLi - minLi + 1) / 100;

            if (light1 == 0)
            {
                light1 = 2;
            }
            for (int light = maxLi - 2; light >= minLi; light--) //=========================================
            {
                //if (light % light1 == 1) fm1.progressBar1.PerformStep();
                for (int i = 0; i < nPixel[light]; i++) //========================================
                {
                    ind3 = 3 * Index[light][i];
                    if (COLOR)
                    {
                        Label2 = Image.Grid[2 + ind3] & 1;
                        Lum    = ((Image.Grid[0 + ind3] & 254) + (Image.Grid[1 + ind3] & 254) + (Image.Grid[2 + ind3] & 254)) / 3;
                    }
                    else
                    {
                        Label2 = Image.Grid[Index[light][i]] & 2;
                        Lum    = Image.Grid[Index[light][i]] & 252;
                    }
                    if (Lum == light && Label2 == 0)
                    {
                        rv = BreadthFirst_D(ref Image, i, light, maxDark);
                    }
                } //============================= end for (int i.. =======================
            }     //=============================== end for (int light.. ========================
            return(rv);
        }         //********************************* end DarkNoise *******************************
        }         //********************** end SigmaSimpleUni **********************************

        public int ColorToGrayMC(CImage inp, Form1 fm1)

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

            if (inp.N_Bits != 24)
            {
                return(-1);
            }

            fm1.progressBar1.Value   = 0;
            fm1.progressBar1.Step    = 1;
            fm1.progressBar1.Visible = true;
            int width = inp.width, height = inp.height;

            Grid = new byte[width * height * 8];
            int y1 = 1 + height / 100;

            for (y = 0; y < height; y++) //========================================
            {
                if (y % y1 == 1)
                {
                    fm1.progressBar1.PerformStep();
                }
                for (x = 0; x < width; x++) // ====================================
                {
                    Light = MaxC(inp.Grid[2 + 3 * (x + width * y)],
                                 inp.Grid[1 + 3 * (x + width * y)],
                                 inp.Grid[0 + 3 * (x + width * y)]);
                    Grid[y * width + x] = (byte)Light;
                } // ==================== for (x.  ================================
            }     // ====================== for (x.  ==================================
            fm1.progressBar1.Visible = false;
            return(1);
        } //************************** end ColorToGray ***************************
예제 #11
0
        } //******************************* end getCond **********************************

        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.N_Bits == 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;

            int y1 = 1 + Image.height / 100;

            for (int y = 1; y < Image.height; y++)
            {
                if (y % y1 == 1)
                {
                    fm1.progressBar1.PerformStep();
                }
                for (int x = 1; 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 = ((Image.Grid[3 * i] & 254) + (Image.Grid[3 * i + 1] & 254) + (Image.Grid[3 * i + 2] & 254)) / 3;
                    }
                    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; .. ========================================
            }
            return(1);
        } //******************************** end Sort *********************************************************
예제 #12
0
        } //********************************* end LightNoise ******************************

        private int BreadthFirst_L(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 "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 belonging 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  lightNeib, index, Label1, Label2, maxLight = 252, MaskColor = 254, maxNeib = 8, Neib, nextIndex;
            bool small = true;

            int[] MaxBound = new int[3];
            bool  COLOR    = (Image.N_Bits == 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)
                    {
                        Label1    = Image.Grid[1 + 3 * Neib] & 1;
                        Label2    = Image.Grid[2 + 3 * Neib] & 1;
                        lightNeib = Lumi(Image.Grid[0 + 3 * Neib], Image.Grid[1 + 3 * Neib], Image.Grid[2 + 3 * Neib]) & MaskColor;
                    }
                    else
                    {
                        Label1    = Image.Grid[Neib] & 1;
                        Label2    = Image.Grid[Neib] & 2;
                        lightNeib = Image.Grid[Neib] & maxLight;
                    }
                    if (lightNeib == light && Label2 > 0)
                    {
                        small = false;
                    }

                    if (lightNeib >= light) //------------------------------------------------------------
                    {
                        if (Label1 > 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 // lightNeib<light
                    {
                        if (Neib != index) //-----------------------------------------------------
                        {
                            if (COLOR)
                            {
                                if (lightNeib > (MaxBound[0] + MaxBound[1] + MaxBound[2]) / 3)
                                {
                                    for (int c = 0; c < 3; c++)
                                    {
                                        MaxBound[c] = (Image.Grid[c + 3 * Neib] & MaskColor);
                                    }
                                }
                            }
                            else
                            {
                                if (lightNeib > MaxBound[0])
                                {
                                    MaxBound[0] = lightNeib;
                                }
                            }
                        } //------------------ end if (Neib!=index) ----------------------------
                    }     //-------------------- end if (lightNeib<=light) and else ------------------------
                }         // =================== end for (n=0; .. ====================================
                if (small == false)
                {
                    break;
                }
            } // ===================== end while ==============================================
            int lightC, // 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)
                    {
                        lightC = Lumi(Image.Grid[0 + 3 * Comp[m]], Image.Grid[1 + 3 * Comp[m]],
                                      Image.Grid[2 + 3 * Comp[m]]) & MaskColor;
                    }
                    else
                    {
                        lightC = Image.Grid[Comp[m]] & maxLight;
                    }

                    if (lightC == 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)maxLight; // deleting the labels
                        }
                    } //----------------------- end if (lightC==light) and else ---------------
                } //------------------------- end if (small && MaxBound[0]>0) and else ----------
            }     //=========================== end for (int m=0 .. ================================
            return(nChanged); // numbPix;
        }         //************************************ end BreadthFirst_L *****************************
예제 #13
0
        private int BreadthFirst_D(ref CImage Image, int i, int light, int maxDark)

        /* 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 "maxDark".
         * 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 lightNeib,                      // lightness of the neighbor
                index, Label1, Label2, 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;                         // equals "true"
            bool COLOR = (Image.N_Bits == 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 < maxDark; p++)
            {
                Comp[p] = -1;
            }
            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 (Label1)
            }
            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)
                    {
                        Label1    = Image.Grid[1 + 3 * Neib] & 1;
                        Label2    = Image.Grid[2 + 3 * Neib] & 1;
                        lightNeib = Lumi(Image.Grid[0 + 3 * Neib], Image.Grid[1 + 3 * Neib],
                                         Image.Grid[2 + 3 * Neib]) & 254;                    // MaskColor;
                    }
                    else
                    {
                        Label1    = Image.Grid[Neib] & 1;
                        Label2    = Image.Grid[Neib] & 2;
                        lightNeib = Image.Grid[Neib] & 252; // MaskGV;
                    }
                    if (lightNeib == light && Label2 > 0)
                    {
                        small = false;
                    }

                    if (lightNeib <= light) //------------------------------------------------------------
                    {
                        if (Label1 > 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 == maxDark)
                        {
                            small = false;
                            break;
                        }
                        Q1.Put(Neib);
                    }
                    else // lightNeib<light
                    {
                        if (Neib != index) //---------------------------------------------------
                        {
                            if (COLOR)
                            {
                                if (lightNeib < (MinBound[0] + MinBound[1] + MinBound[2]) / 3)
                                {
                                    for (int c = 0; c < 3; c++)
                                    {
                                        MinBound[c] = Image.Grid[c + 3 * Neib];
                                    }
                                }
                            }
                            else
                            if (lightNeib < MinBound[0])
                            {
                                MinBound[0] = lightNeib;
                            }
                        } //------------------ end if (Neib!=index) --------------------------
                    }     //-------------------- end if (lightNeib<=light) and else ------------------------
                }         // =================== end for (n=0; .. ====================================
                if (small == false)
                {
                    break;
                }
            } // ===================== end while ==============================================
            int lightC; // lightness of a pixel whose index is contained in "Comp"

            for (int m = 0; m < numbPix; m++)            //======================================================
            {
                if (small != false && 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)
                    {
                        lightC = Lumi(Image.Grid[3 * Comp[m]], Image.Grid[1 + 3 * Comp[m]],
                                      Image.Grid[2 + 3 * Comp[m]]) & 254;
                    }
                    else
                    {
                        lightC = Image.Grid[Comp[m]] & 252; // MaskGV;
                    }
                    if (lightC == light)                    //----------------------------------------------------------------
                    {
                        if (COLOR)
                        {
                            Image.Grid[2 + 3 * Comp[m]] |= 1;
                        }
                        else
                        {
                            Image.Grid[Comp[m]] |= 2;
                        }
                    }
                    else // lightC!=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 (lightc == light) and else ------------------
                } //-------------------------------- end if (small != false) and else -----------------
            }     //================================== end for (int m=0 .. ================================
            return(numbPix);
        }         //************************************ end BreadthFirst_D ************************************
예제 #14
0
        private bool TestComp(int[] Comp, int numbPix, int width)
        {
            int ind, minX = 10000, maxX = 0, minY = 10000, maxY = 0, x, y, xs, ys;

            for (int p = 0; p < numbPix; p++)
            {
                ind = Comp[p];
                ys  = ind / width; // standard coordinates
                xs  = ind - ys * width;
                if (xs < minX)
                {
                    minX = xs;
                }
                if (xs > maxX)
                {
                    maxX = xs;
                }
                if (ys < minY)
                {
                    minY = ys;
                }
                if (ys > maxY)
                {
                    maxY = ys;
                }
            }
            int    widthC = 2 * (maxX - minX + 1) + 5, heightC = 2 * (maxY - minY + 1) + 5;
            CImage Combin = new CImage(widthC, heightC, 8);

            for (int i = 0; i < widthC * heightC; i++)
            {
                Combin.Grid[i] = 0;
            }

            for (int p = 0; p < numbPix; p++) // Labeling pixels of 'Comp' in 'Combin'
            {
                ind = Comp[p];
                y   = (ind / width - minY) * 2 + 3;
                x   = (ind - (ind / width) * width - minX) * 2 + 3;
                Combin.Grid[x + widthC * y] = 1;
            }

            for (y = 2; y < heightC - 2; y++) //===============================================
            {
                if ((y & 1) == 0)
                {
                    for (x = 3; x < widthC - 3; x += 2)
                    {
                        if (Combin.Grid[x + widthC * (y - 1)] != Combin.Grid[x + widthC * (y + 1)])
                        {
                            Combin.Grid[x + widthC * y] = 1; // Labeling a horizontal crack
                        }
                    }
                }
                else
                {
                    for (x = 2; x < widthC - 2; x += 2)
                    {
                        if (Combin.Grid[x - 1 + widthC * y] != Combin.Grid[x + 1 + widthC * y])
                        {
                            Combin.Grid[x + widthC * y] = 1; // Labeling a vertical crack
                        }
                    }
                }
            } //================================= end for (y = 2 ... ==========================

            double Perim = 0.0, Corner = 1.414;
            bool rv, found = false;
            int xStart, xEnd;
            for (y = 2; y < heightC - 2; y++) //=================================================
            {
                if ((y & 1) == 0)             //-----------
                {
                    xStart = 3;
                    xEnd   = widthC - 3;
                }
                else
                {
                    xStart = 2;
                    xEnd   = widthC - 2;
                } //-------- end if (( ... ----

                for (x = xStart; x < xEnd; x += 2) //=========== testing cracks =================
                {
                    if (Combin.Grid[x + widthC * y] != 1)
                    {
                        continue;
                    }
                    found = false;
                    for (int neib = 0; neib < 4; neib++) //===== testing four neighbour cracks ====
                    {
                        switch (neib)                    //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
                        {
                        case 0:
                            if (Combin.Grid[x - 1 + widthC * (y - 1)] == 1)
                            {
                                Combin.Grid[x + widthC * y]           = 2; //Labeled with 2 a used
                                Combin.Grid[x - 1 + widthC * (y - 1)] = 2; //Labeled with 2 a used
                                Perim += Corner;
                                found  = true;
                            }
                            break;

                        case 1:
                            if (Combin.Grid[x - 1 + widthC * (y + 1)] == 1)
                            {
                                Combin.Grid[x + widthC * y]           = 2; //Labeled with 2 a used
                                Combin.Grid[x - 1 + widthC * (y + 1)] = 2; //Labeled with 2 a used
                                Perim += Corner;
                                found  = true;
                            }
                            break;

                        case 2:
                            if (Combin.Grid[x + 1 + widthC * (y - 1)] == 1)
                            {
                                Combin.Grid[x + widthC * y]           = 2; //Labeled with 2 a used
                                Combin.Grid[x + 1 + widthC * (y - 1)] = 2; //Labeled with 2 a used
                                Perim += Corner;
                                found  = true;
                            }
                            break;

                        case 3:
                            if (Combin.Grid[x + 1 + widthC * (y + 1)] == 1)
                            {
                                Combin.Grid[x + widthC * y]           = 2; //Labeled with 2 a used
                                Combin.Grid[x + 1 + widthC * (y + 1)] = 2; //Labeled with 2 a used
                                Perim += Corner;
                                found  = true;
                            }
                            break;
                        } //:::::::::::::::::::::::::::::::: end switch :::::::::::::::::::::::::
                        if (found)
                        {
                            break;
                        }
                    } //================================== end for (int neib... =================
                    if (!found)
                    {
                        Combin.Grid[x + widthC * y] = 2; //Labeled with 2 a used
                        Perim += 1.0;
                        //break;
                    }
                } //==================================== end for (x... ==========================
            }     //====================================== end for (y = 2 ... =======================
            rv = (numbPix < 20 && Perim * Perim / (double)numbPix < 18.0) || 2.0 * numbPix / Perim > 2.5;
            return(rv);
        } //**************************************** end TestComp *******************************
        private void button1_Click_1(object sender, EventArgs e) // Open image
        {
            pictureBox1.Visible = false;
            pictureBox2.Visible = false;
            pictureBox3.Visible = true;
            groupBox1.Visible   = false;

            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                label12.Visible = false;
                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;
            }

            label12.Text    = "Opened image: " + OpenImageFile;
            label12.Visible = true;
            label11.Visible = true;
            label6.Visible  = true;

            button2.Visible        = false;
            button3.Visible        = false;
            button4.Visible        = false;
            numericUpDown1.Visible = false;
            numericUpDown2.Visible = false;
            numericUpDown4.Visible = false;
            numericUpDown5.Visible = false;
            label1.Visible         = false;
            label2.Visible         = false;
            label4.Visible         = false;
            label5.Visible         = false;
            label7.Visible         = false;
            label8.Visible         = false;
            label9.Visible         = false;
            label10.Visible        = false;
            pictureBox2.Visible    = false;
            pictureBox3.Visible    = false;

            groupBox1.Visible = true;

            progressBar1.Maximum = 100;
            progressBar1.Value   = 0;
            progressBar1.Step    = 1;
            progressBar1.Visible = true;

            if (origBmp.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                nbyteBmp = 1;
            }
            else
            if (origBmp.PixelFormat == PixelFormat.Format24bppRgb)
            {
                nbyteBmp = 3;
            }
            else
            {
                MessageBox.Show("Pixel format=" + origBmp.PixelFormat + " not used in this project.");
                return;
            }

            nbyteIm = BitmapToImage(origBmp, ref OrigIm);

            pictureBox1.Visible = true;
            pictureBox1.Image   = origBmp;

            width  = origBmp.Width;
            height = origBmp.Height;

            int N_Bits = nbyteIm * 8;

            SigmaIm = new CImage(width, height, N_Bits);
            SigmaIm.SigmaSimpleUni(OrigIm, 2, 50);
            GrayIm = new CImage(width, height, 8);
            if (nbyteIm == 3)
            {
                GrayIm.ColorToGray(SigmaIm);
            }
            else
            {
                GrayIm.Copy(SigmaIm);
            }
            MeanIm    = new CImage(width, height, 8);
            ShadIm    = new CImage(width, height, N_Bits);
            ImpulseIm = new CImage(width, height, N_Bits);
            BinIm     = new CImage(width, height, 8);

            ScaleX     = (double)pictureBox1.Width / (double)width;
            ScaleY     = (double)pictureBox1.Height / (double)height;
            Scale1     = Math.Min(ScaleX, ScaleY);
            marginX    = (pictureBox1.Width - (int)(Scale1 * width)) / 2;
            marginY    = (pictureBox1.Height - (int)(Scale1 * height)) / 2;
            ShadingBmp = new Bitmap(origBmp.Width, origBmp.Height, PixelFormat.Format24bppRgb);

            pictureBox2.Visible  = false;
            pictureBox3.Visible  = false;
            radioButton1.Checked = false;
            radioButton2.Checked = false;
            radioButton3.Checked = false;
            radioButton4.Checked = false;
            progressBar1.Visible = false;

            KIND = -1;
            OPEN = true;
        } //********************************** end Open image *************************************
        } //********************************** end Open image *************************************

        public int BitmapToImage(Bitmap bmp, ref CImage Image)
        // Converts any bitmap to a color or to a grayscale image.
        {
            int   nbyteIm = 1, rv = 0, x, y;
            Color color;

            if (nbyteBmp == 1) // nbyteBmp is member of "Form1" according to the PixelFormat of "bmp"
            {
                x     = 10;
                y     = 2;
                color = bmp.GetPixel(x, y);
                if (color.R != color.G)
                {
                    nbyteIm = 3;
                }
                Image = new CImage(bmp.Width, bmp.Height, nbyteIm * 8);

                progressBar1.Visible = true;
                for (y = 0; y < bmp.Height; y++) //========================================================
                {
                    int jump = bmp.Height / 100;
                    if (y % jump == jump - 1)
                    {
                        progressBar1.PerformStep();
                    }

                    for (x = 0; x < bmp.Width; x++) //======================================================
                    {
                        color = bmp.GetPixel(x, y);
                        if (nbyteIm == 3)
                        {
                            Image.Grid[3 * (x + bmp.Width * y) + 0] = color.B;
                            Image.Grid[3 * (x + bmp.Width * y) + 1] = color.G;
                            Image.Grid[3 * (x + bmp.Width * y) + 2] = color.R;
                        }
                        else // nbyteIm == 1:
                        {
                            Image.Grid[x + bmp.Width * y] = color.R;
                        }
                    } //================================== end for (x ... ===================================
                }     //==================================== end for (y ... =====================================
                rv = nbyteIm;
            }
            else // nbyteBmp == 3 and nbyteIm == 3:
            {
                Rectangle  rect      = new Rectangle(0, 0, bmp.Width, bmp.Height);
                BitmapData bmpData   = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
                IntPtr     ptr       = bmpData.Scan0;
                int        Str       = bmpData.Stride;
                int        bytes     = Math.Abs(bmpData.Stride) * bmp.Height;
                byte[]     rgbValues = new byte[bytes];
                System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

                nbyteIm = 3;
                Image   = new CImage(bmp.Width, bmp.Height, nbyteIm * 8);
                for (y = 0; y < bmp.Height; y++) //=============================================
                {
                    int jump = bmp.Height / 100;
                    if (y % jump == jump - 1)
                    {
                        progressBar1.PerformStep();
                    }
                    for (x = 0; x < bmp.Width; x++)
                    {
                        for (int c = 0; c < nbyteIm; c++)
                        {
                            Image.Grid[c + nbyteIm * (x + bmp.Width * y)] =
                                rgbValues[c + nbyteBmp * x + Math.Abs(bmpData.Stride) * y];
                        }
                    }
                } //========================= end for (y = 0; ... ==============================
                rv = nbyteIm;
                bmp.UnlockBits(bmpData);
            }
            return(rv);
        } //****************************** end BitmapToImage ****************************************