示例#1
0
        } //********************** end ColorToGray **********************

        public int ColorToGrayMC(CImage inp, Form1 fm1)

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

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

            fm1.progressBar1.Visible = true;
            N_Bits = 8; 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++) // =====================
                {
                    gv = 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)gv;
                } // ========== for (x.  ====================
            }
            fm1.progressBar1.Visible = false;
            return(1);
        } //********************** end ColorToGrayMC **********************
示例#2
0
 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 ColorToGrayMC **********************

        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;

            fm1.progressBar1.Visible = false;
            for (int y = 0; y < height + hWind; y++)
            {
                int yout = y - hWind, ysub = y - 2 * hWind - 1;
                Sum = 0; nS = 0;

                int y1 = 1 + (height + hWind) / 100;
                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 ********************************
示例#4
0
        private int ImageToBitmapNew(CImage Image, Bitmap bmp, int progPart)
        // 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 (Image.MessReturn("ImageToBitmapNew: we don't use this pixel format") < 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];

            progressBar1.Visible = true;
            int nbyteIm = Image.N_Bits / 8;

            for (int y = 0; y < bmp.Height; y++) //=================================================================
            {
                int jump = bmp.Height / progPart;
                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 ****************************************
示例#5
0
        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 **********************************
示例#6
0
        }         //********************** end SigmaSimpleUni **********************************

        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.N_Bits != 24)
            {
                return(-1);
            }

            fm1.progressBar1.Visible = true;
            N_Bits = 8; 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++) // =====================
                {
                    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 **********************
示例#7
0
        private void button1_Click(object sender, EventArgs e) // Open image
        {
            label1.Visible  = false;
            label2.Visible  = false;
            label3.Visible  = false;
            label4.Visible  = false;
            label5.Visible  = false;
            label6.Visible  = false;
            button2.Visible = false;
            button3.Visible = false;
            button4.Visible = false;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

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

            label1.Visible         = true;
            label2.Visible         = true;
            label5.Visible         = true;
            label5.Text            = "Opened image:" + OpenImageFile;
            label6.Visible         = true;
            button2.Visible        = true;
            numericUpDown1.Visible = true;
            numericUpDown2.Visible = true;

            width  = original_Bitmap.Width;
            height = original_Bitmap.Height;

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

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



            nbyteIm = BitmapToImage(original_Bitmap, ref OrigIm);
            int N_Bits = nbyteIm * 8;

            SigmaIm = new CImage(width, height, N_Bits);
            SubIm   = new CImage(width, height, N_Bits);
            DivIm   = new CImage(width, height, N_Bits);
            GrayIm  = new CImage(width, height, 8);
            MeanIm  = new CImage(width, height, 8);
            BinIm   = new CImage(width, height, 8);

            SigmaIm.SigmaSimpleUni(OrigIm, 1, 30);
            if (OrigIm.N_Bits == 24)
            {
                GrayIm.ColorToGray(SigmaIm, this);
            }
            else
            {
                GrayIm.Copy(SigmaIm);
            }

            Threshold1           = -1;
            Threshold            = -1;
            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;
            progressBar1.Visible = false;

            SHADING = false;
        } //****************************** end Open image ***************************
示例#8
0
        } //****************************** 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;
                progressBar1.Value   = 0;
                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 ****************************************