Пример #1
0
        private void ParallelInvoke(Bitmap inputBitmap, int[,] mask)
        {
            bool   CornerMask              = CheckIfCornerMask(mask); // sprawdzamy czy maska jest typu :wykrywanie krawędzi"
            Bitmap temorary                = new Bitmap(inputBitmap.Width, inputBitmap.Height);
            CustomBitmapProcessing data    = new CustomBitmapProcessing(new Bitmap(inputBitmap));
            CustomBitmapProcessing dataOut = new CustomBitmapProcessing(inputBitmap);

            data.LockBits();
            dataOut.LockBits();
            mainForm.Invoke((MethodInvoker) delegate {
                mainForm.toolStripProgressBar1.Maximum = (data.Height) * 2;
            });
            Parallel.Invoke(
                () => ApplyMaskThreaded(data, dataOut, mask, 0, 0, data.Height / 2, data.Width / 2, CornerMask),
                () => ApplyMaskThreaded(data, dataOut, mask, 0, data.Width / 2, data.Height / 2, data.Width, CornerMask),
                () => ApplyMaskThreaded(data, dataOut, mask, data.Height / 2, 0, data.Height, data.Width / 2, CornerMask),
                () => ApplyMaskThreaded(data, dataOut, mask, data.Height / 2, data.Width / 2, data.Height, data.Width, CornerMask));
            data.UnlockBits();
            dataOut.UnlockBits();
            mainForm.Invoke((MethodInvoker) delegate
            {
                mainForm.toolStripProgressBar1.Maximum = 100;
                mainForm.toolStripProgressBar1.Value   = 0;
                mainForm.Picture = inputBitmap;
            });
        }
        public void GreenSearch(Bitmap inputBitmap)
        {
            CustomBitmapProcessing data = new CustomBitmapProcessing(inputBitmap);

            data.LockBits();
            for (int i = 0; i < data.Height; i++)
            {
                for (int j = 0; j < data.Width; j++)
                {
                    int pixel = data.GetPixel(i, j);
                    int r     = data.GetPixel(i, j) & 0xFF;
                    int g     = (data.GetPixel(i, j) >> 8) & 0xFF;
                    int b     = (data.GetPixel(i, j) >> 16) & 0xFF;
                    if ((g > b && g > r && g > 60) || (((g + r) / 2) > b && ((g + r) / 2) > (r - 3) && g > 60))
                    {
                        data.SetPixel(i, j, 0);
                    }
                    else
                    {
                        data.SetPixel(i, j, 0xFFFFFF);
                    }
                }
            }
            data.UnlockBits();
            mainForm.Picture = new Bitmap(inputBitmap);
        }
        private void stretch(Bitmap inputBitmap, int minValue, int maxValue)
        {
            CustomBitmapProcessing dataOut = new CustomBitmapProcessing(inputBitmap);

            dataOut.LockBits();
            byte[] LUT   = new byte[256];
            byte   vMaxR = (byte)maxValue;
            byte   vMinR = (byte)minValue;
            byte   iMax  = 255;

            for (int i = 0; i < 256; i++)
            {
                if (i > maxValue)
                {
                    LUT[i] = 255;
                }
                else if (i < minValue)
                {
                    LUT[i] = 0;
                }
                else
                {
                    LUT[i] = (byte)(iMax / (maxValue - minValue) * (i - minValue));
                }
            }
            Parallel.Invoke(
                () =>
            {
                Parallel.For(0, dataOut.Pixels.LongLength / 4, i =>
                {
                    dataOut.Pixels[i] = LUT[dataOut.Pixels[i]];       // w operacji LUT nie obodzi nas które wartości zmieniamy - więc tak jest szybciej
                });
            },
                () =>
            {
                Parallel.For(dataOut.Pixels.LongLength / 4, dataOut.Pixels.LongLength / 2, i =>
                {
                    dataOut.Pixels[i] = LUT[dataOut.Pixels[i]];     // w operacji LUT nie obodzi nas które wartości zmieniamy - więc tak jest szybciej
                });
            },
                () =>
            {
                Parallel.For(dataOut.Pixels.LongLength / 2, ((dataOut.Pixels.LongLength / 4) * 3), i =>
                {
                    dataOut.Pixels[i] = LUT[dataOut.Pixels[i]];         // w operacji LUT nie obodzi nas które wartości zmieniamy - więc tak jest szybciej
                });
            },
                () =>
            {
                Parallel.For(((dataOut.Pixels.LongLength / 4) * 3), dataOut.Pixels.LongLength, i =>
                {
                    dataOut.Pixels[i] = LUT[dataOut.Pixels[i]];     // w operacji LUT nie obodzi nas które wartości zmieniamy - więc tak jest szybciej
                });
            });
            dataOut.UnlockBits();
            mainForm.Invoke((MethodInvoker) delegate {
                //mainForm.pictureBox1.Image = inputBitmap;
                mainForm.Picture = new Bitmap(inputBitmap);
            });
        }
Пример #4
0
        private void ApplyMedianMaskThreaded(CustomBitmapProcessing data, CustomBitmapProcessing dataOut, int startI, int startJ, int endI, int endJ, int maskSize)
        {
            int MaskPositionHelper = (maskSize - 1) / 2;

            size.X = data.Height;
            size.Y = data.Width;
            for (int i = startI; i < endI; i++)
            {
                //Parallel.For(startJ, endJ, j =>
                for (int j = startJ; j < endJ; j++)
                {
                    List <int> r = new List <int>();
                    List <int> g = new List <int>();
                    List <int> b = new List <int>();
                    for (int ii = 0; ii < maskSize; ii++)
                    {
                        for (int jj = 0; jj < maskSize; jj++)
                        {
                            int rgb = data.GetPixel(Val(i, ii - MaskPositionHelper), Val2(j, jj - MaskPositionHelper));
                            r.Add(rgb & 0xFF);
                            g.Add((rgb >> 8) & 0xFF);
                            b.Add((rgb >> 16) & 0xFF);
                        }
                    }
                    r.Sort();
                    g.Sort();
                    b.Sort();
                    int rgbOut = RgbVal(r[r.Count / 2]) + (RgbVal(g[r.Count / 2]) << 8) + (RgbVal(b[r.Count / 2]) << 16);
                    dataOut.SetPixel(i, j, rgbOut);
                }//);
                mainForm.Invoke((MethodInvoker) delegate {
                    mainForm.toolStripProgressBar1.Value++;
                });
            }
        }
Пример #5
0
        private Bitmap MorfologyFilter(Bitmap inputBitmap, Morfology type)
        {
            CustomBitmapProcessing dataOut  = new CustomBitmapProcessing(inputBitmap);
            CustomBitmapProcessing dataRead = new CustomBitmapProcessing(new Bitmap(inputBitmap));

            dataOut.LockBits();
            dataRead.LockBits();
            p.X = dataOut.Height;
            p.Y = dataOut.Width;
            int color;

            if (type == Morfology.Dylation)
            {
                color = 0;
            }
            else
            {
                color = 0xFFFFFF;
            }

            for (int i = 0; i < p.X; i++)
            {
                for (int j = 0; j < p.Y; j++)
                {
                    if (dataRead.GetPixel(mod1(i - 1), j) == color || dataRead.GetPixel(i, j) == color || dataRead.GetPixel(i, mod2(j - 1)) == color || dataRead.GetPixel(i, mod2(j + 1)) == color || dataRead.GetPixel(mod1(i + 1), j) == color)
                    {
                        dataOut.SetPixel(i, j, color);
                    }
                }
            }
            dataOut.UnlockBits();
            dataRead.UnlockBits();
            mainForm.Picture = new Bitmap(inputBitmap);
            return(new Bitmap(inputBitmap));
        }
Пример #6
0
        public void imageThinning(Bitmap inputBitmap, int iterations)
        {
            CustomBitmapProcessing dataRead = new CustomBitmapProcessing(new Bitmap(inputBitmap));
            CustomBitmapProcessing dataOut  = new CustomBitmapProcessing(inputBitmap);

            dataRead.LockBits();
            dataOut.LockBits();
            p.X = dataRead.Height;
            p.Y = dataRead.Width;
            for (int h = 0; h < dataRead.Height - 0; ++h)
            {
                for (int w = 0; w < dataRead.Width - 0; ++w)
                {
                    if ((dataRead.GetPixel(mod1(h - 1), mod2(w - 1)) & 0xFF) == 0 &&
                        (dataRead.GetPixel(mod1(h - 1), mod2(w)) & 0xFF) == 0 &&
                        (dataRead.GetPixel(mod1(h - 1), mod2(w + 1)) & 0xFF) == 0 &&
                        (dataRead.GetPixel(mod1(h), mod2(w - 1)) & 0xFF) == 0 &&
                        (dataRead.GetPixel(mod1(h), mod2(w)) & 0xFF) == 0 &&
                        (dataRead.GetPixel(mod1(h), mod2(w + 1)) & 0xFF) == 0 &&
                        (dataRead.GetPixel(mod1(h + 1), mod2(w - 1)) & 0xFF) == 0 &&
                        (dataRead.GetPixel(mod1(h + 1), mod2(w)) & 0xFF) == 0 &&
                        (dataRead.GetPixel(mod1(h + 1), mod2(w + 1)) & 0xFF) == 0)
                    {
                        dataOut.SetPixel(h, w, 0xFFFFFF);
                    }
                }
            }
            dataOut.UnlockBits();
            dataRead.UnlockBits();
            mainForm.Picture = new Bitmap(inputBitmap);
        }
        public void EntropySelection(Bitmap inputBitmap)
        {
            inputBitmap = histogramEqualization2(new Bitmap(inputBitmap));
            inputBitmap = ApplyGrayScale(new Bitmap(inputBitmap));
            CustomBitmapProcessing data = new CustomBitmapProcessing(inputBitmap);

            ulong[] histogramTable = CreateHistogram(inputBitmap);
            int     imageSize      = inputBitmap.Width * inputBitmap.Height;
            int     Height         = inputBitmap.Height;
            int     Width          = inputBitmap.Width;

            double[] p            = new double[256];
            double[] entropyTable = new double[256];
            data.LockBits();

            for (int i = 0; i < 256; i++)
            {
                p[i] = (double)histogramTable[i] / (double)imageSize;
            }

            for (int i = 0; i < 256; i++)
            {
                entropyTable[i] = p[i] * Math.Log10(p[i]);
            }

            double entropyMin = entropyTable[0];
            int    index      = 0;

            for (int i = 0; i < 256; i++)
            {
                if (entropyMin > entropyTable[i])
                {
                    entropyMin = entropyTable[i];
                    index      = i;
                }
            }
            for (int i = 0; i < Height; i++)
            {
                for (int j = 0; j < Width; j++)
                {
                    int grey = (data.GetPixel(i, j) & 0xFF);
                    if (grey >= index)
                    {
                        grey = 255;
                    }
                    else
                    {
                        grey = 0;
                    }
                    int rgb = grey + (grey << 8) + (grey << 16);
                    data.SetPixel(i, j, rgb);
                }
            }
            data.UnlockBits();
            mainForm.Picture = new Bitmap(inputBitmap);
        }
Пример #8
0
        private void ApplyMaskThreaded(CustomBitmapProcessing data, CustomBitmapProcessing dataOut, int[,] mask, int startI, int startJ, int endI, int endJ, bool CornerMask)
        {
            size.X = data.Height;
            size.Y = data.Width;
            int MaskPositionHelper = (mask.GetLength(0) - 1) / 2;
            int maskSize           = mask.GetLength(0);
            int sumMask            = 0;

            foreach (var item in mask)
            {
                sumMask += item;
            }
            if (sumMask == 0)
            {
                sumMask = 1;
            }
            for (int i = startI; i < endI; i++)
            {
                //Parallel.For(startJ, endJ, j =>
                for (int j = startJ; j < endJ; j++)
                {
                    int r = 0, g = 0, b = 0;
                    if (i < 0 || j < 0)
                    {
                        throw new IndexOutOfRangeException();
                    }
                    for (int ii = 0; ii < maskSize; ii++)
                    {
                        for (int jj = 0; jj < maskSize; jj++)
                        {
                            int rgb = data.GetPixel(mod1(i, ii - MaskPositionHelper), mod2(j, jj - MaskPositionHelper));
                            r += (rgb & 0x0000FF) * mask[ii, jj];
                            g += ((rgb >> 8) & 0x0000FF) * mask[ii, jj];
                            b += ((rgb >> 16) & 0x0000FF) * mask[ii, jj];
                            // MaskPositionHelper2--;
                        }
                        //MaskPositionHelper1--;
                    }
                    int rgbOut = 0;
                    if (CornerMask)
                    {
                        rgbOut = RgbVal((r / sumMask) + 128) + (RgbVal((g / sumMask) + 128) << 8) + (RgbVal((b / sumMask) + 128) << 16);
                    }
                    else
                    {
                        rgbOut = RgbVal(r / sumMask) + (RgbVal(g / sumMask) << 8) + (RgbVal(b / sumMask) << 16);
                    }
                    dataOut.SetPixel(i, j, rgbOut);
                }//);
                mainForm.Invoke((MethodInvoker) delegate {
                    mainForm.toolStripProgressBar1.Value++;
                });
            }
        }
Пример #9
0
        public void imageThinning(Bitmap inputBitmap, int iterations)
        {
            CustomBitmapProcessing data = new CustomBitmapProcessing(inputBitmap);
            Bitmap tempo = new Bitmap(inputBitmap);

            data.LockBits();
            p.X = data.Height;
            p.Y = data.Width;
            for (int i = 0; i < iterations; ++i)
            {
                Bitmap imageCopy             = new Bitmap(tempo);
                CustomBitmapProcessing data2 = new CustomBitmapProcessing(imageCopy);
                data2.LockBits();
                for (int h = 1; h < data.Height - 1; ++h)
                {
                    for (int w = 1; w < data.Width - 1; ++w)
                    {
                        if (data.GetPixel(mod1(h - 1), mod2(w - 1)) == 0 &&
                            data.GetPixel(mod1(h - 1), mod2(w)) == 0 &&
                            data.GetPixel(mod1(h - 1), mod2(w + 1)) == 0 &&
                            data.GetPixel(mod1(h), mod2(w - 1)) == 0 &&
                            data.GetPixel(mod1(h), mod2(w)) == 0 &&
                            data.GetPixel(mod1(h), mod2(w + 1)) == 0 &&
                            data.GetPixel(mod1(h + 1), mod2(w - 1)) == 0 &&
                            data.GetPixel(mod1(h + 1), mod2(w)) == 0 &&
                            data.GetPixel(mod1(h + 1), mod2(w + 1)) == 0)
                        {
                            data2.SetPixel(h, w, 0xFFFFFF);
                        }
                    }
                }

                for (int h = 0; h < data.Height; ++h)
                {
                    for (int w = 0; w < data.Width; ++w)
                    {
                        //int pixel1 = data.GetPixel(w, h);
                        int pixel2 = data2.GetPixel(h, w);

                        if (pixel2 == 0xFFFFFF)
                        {
                            data.SetPixel(h, w, 0xFFFFFF);
                        }
                    }
                }
                data2.UnlockBits();
            }
            data.UnlockBits();
            mainForm.Picture = new Bitmap(inputBitmap);
        }
        private ulong[] CreateHistogram(Bitmap inputBitmap)
        {
            ulong[] histogram           = new ulong[256];
            CustomBitmapProcessing data = new CustomBitmapProcessing(inputBitmap);

            data.LockBits();
            for (int i = 0; i < data.Height; i++)
            {
                for (int j = 0; j < data.Width; j++)
                {
                    int gray = (data.GetPixel(i, j) & 0xFF);
                    histogram[gray]++;
                }
            }
            data.UnlockBits();
            return(histogram);
        }
Пример #11
0
        public Bitmap grayscale2(Bitmap bitmap)
        {
            CustomBitmapProcessing data = new CustomBitmapProcessing(bitmap);

            data.LockBits();
            for (int i = 0; i < data.Height; i++)
            {
                for (int j = 0; j < data.Width; j++)
                {
                    int  rgb    = data.GetPixel(i, j);
                    byte grey   = (byte)(Math.Ceiling((decimal)(0.299 * (rgb & 0xFF) + 0.587 * ((rgb >> 8) & 0xFF) + 0.114 * ((rgb >> 16) & 0xFF))));
                    int  rgbOut = grey + (grey << 8) + (grey << 16);
                    data.SetPixel(i, j, rgbOut);
                }
            }
            data.UnlockBits();
            return(bitmap);
        }
        private Bitmap ApplyThreshold(Bitmap inputBitmap, int threshold)
        {
            CustomBitmapProcessing data = new CustomBitmapProcessing(inputBitmap);

            data.LockBits();
            for (int i = 0; i < data.Height; i++)
            {
                for (int j = 0; j < data.Width; j++)
                {
                    if ((data.GetPixel(i, j) & 0xFF) >= threshold)
                    {
                        int rgb = 255 + (255 << 8) + (255 << 16);
                        data.SetPixel(i, j, rgb);
                    }
                    else
                    {
                        data.SetPixel(i, j, 0);
                    }
                }
            }
            data.UnlockBits();
            mainForm.Picture = new Bitmap(inputBitmap);
            return(new Bitmap(inputBitmap));
        }
Пример #13
0
        private void ParallelInvoke(Bitmap inputBitmap, int size)
        {
            CustomBitmapProcessing data    = new CustomBitmapProcessing(new Bitmap(inputBitmap));
            CustomBitmapProcessing dataOut = new CustomBitmapProcessing(inputBitmap);

            data.LockBits();
            dataOut.LockBits();
            mainForm.Invoke((MethodInvoker) delegate {
                mainForm.toolStripProgressBar1.Maximum = (data.Height) * 2;
            });
            Parallel.Invoke(
                () => ApplyMedianMaskThreaded(data, dataOut, 0, 0, data.Height / 2, data.Width / 2, size),
                () => ApplyMedianMaskThreaded(data, dataOut, 0, data.Width / 2, data.Height / 2, data.Width, size),
                () => ApplyMedianMaskThreaded(data, dataOut, data.Height / 2, 0, data.Height, data.Width / 2, size),
                () => ApplyMedianMaskThreaded(data, dataOut, data.Height / 2, data.Width / 2, data.Height, data.Width, size));
            data.UnlockBits();
            dataOut.UnlockBits();
            mainForm.Invoke((MethodInvoker) delegate
            {
                mainForm.toolStripProgressBar1.Maximum = 100;
                mainForm.toolStripProgressBar1.Value   = 0;
                mainForm.Picture = inputBitmap;
            });
        }
Пример #14
0
        public void imageThickening(Bitmap inputBitmap, int iterations)
        {
            CustomBitmapProcessing dataReadOnly = new CustomBitmapProcessing(new Bitmap(inputBitmap)); // data read
            CustomBitmapProcessing dataOut      = new CustomBitmapProcessing(inputBitmap);
            Bitmap tempo = new Bitmap(inputBitmap);

            dataReadOnly.LockBits();
            dataOut.LockBits();
            p.X = dataReadOnly.Height;
            p.Y = dataReadOnly.Width;
            for (int i = 0; i < iterations; ++i)
            {
                CustomBitmapProcessing data1 = new CustomBitmapProcessing(new Bitmap(tempo));
                data1.LockBits();

                for (int h = 0; h < dataReadOnly.Height; ++h)
                {
                    for (int w = 1; w < dataReadOnly.Width - 1; ++w)
                    {
                        //Pierwsza maska
                        if ((dataReadOnly.GetPixel(mod1(h - 1), mod2(w - 1)) == 0 &&
                             dataReadOnly.GetPixel(mod1(h - 1), w) == 0 &&
                             dataReadOnly.GetPixel(mod1(h - 1), mod2(w + 1)) == 0 &&
                             dataReadOnly.GetPixel(h, mod2(w - 1)) == 0 &&
                             dataReadOnly.GetPixel(h, w) == 0xFFFFFF &&
                             dataReadOnly.GetPixel(mod1(h + 1), mod2(w + 1)) == 0xFFFFFF)

                            || (dataReadOnly.GetPixel(mod1(h), mod2(w - 1)) == 0 &&
                                dataReadOnly.GetPixel(mod1(h + 1), mod2(w - 1)) == 0 &&
                                dataReadOnly.GetPixel(mod1(h + 1), mod2(w)) == 0 &&
                                dataReadOnly.GetPixel(mod1(h + 1), mod2(w + 1)) == 0 &&
                                dataReadOnly.GetPixel(h, w) == 0xFFFFFF &&
                                dataReadOnly.GetPixel(mod1(h - 1), mod2(w + 1)) == 0xFFFFFF))
                        {
                            data1.SetPixel(h, w, 0);
                        }
                        else
                        {
                            data1.SetPixel(h, w, 0xFFFFFF);
                        }

                        //Druga maska
                        if ((dataReadOnly.GetPixel(mod1(h - 1), mod2(w - 1)) == 0 &&
                             dataReadOnly.GetPixel(mod1(h), mod2(w - 1)) == 0 &&
                             dataReadOnly.GetPixel(mod1(h + 1), mod2(w - 1)) == 0 &&
                             dataReadOnly.GetPixel(mod1(h + 1), mod2(w)) == 0 &&
                             dataReadOnly.GetPixel(h, w) == 0xFFFFFF &&
                             dataReadOnly.GetPixel(mod1(h - 1), mod2(w + 1)) == 0xFFFFFF)

                            || (dataReadOnly.GetPixel(mod1(h + 1), mod2(w)) == 0 &&
                                dataReadOnly.GetPixel(mod1(h + 1), mod2(w + 1)) == 0 &&
                                dataReadOnly.GetPixel(mod1(h), mod2(w + 1)) == 0 &&
                                dataReadOnly.GetPixel(mod1(h - 1), mod2(w + 1)) == 0 &&
                                dataReadOnly.GetPixel(h, w) == 0xFFFFFF &&
                                dataReadOnly.GetPixel(mod1(h - 1), mod2(w - 1)) == 0xFFFFFF))
                        {
                            data1.SetPixel(h, w, 0);
                        }
                        else if (data1.GetPixel(h, w) != 0)
                        {
                            data1.SetPixel(h, w, 0xFFFFFF);
                        }

                        //Trzecia maska
                        if ((dataReadOnly.GetPixel(mod1(h + 1), mod2(w - 1)) == 0 &&
                             dataReadOnly.GetPixel(mod1(h + 1), mod2(w)) == 0 &&
                             dataReadOnly.GetPixel(mod1(h + 1), mod2(w + 1)) == 0 &&
                             dataReadOnly.GetPixel(mod1(h), mod2(w + 1)) == 0 &&
                             dataReadOnly.GetPixel(h, w) == 0xFFFFFF &&
                             dataReadOnly.GetPixel(mod1(h - 1), mod2(w - 1)) == 0xFFFFFF)

                            || (dataReadOnly.GetPixel(mod1(h - 1), mod2(w - 1)) == 0 &&
                                dataReadOnly.GetPixel(mod1(h - 1), mod2(w)) == 0 &&
                                dataReadOnly.GetPixel(mod1(h - 1), mod2(w + 1)) == 0 &&
                                dataReadOnly.GetPixel(mod1(h), mod2(w + 1)) == 0 &&
                                dataReadOnly.GetPixel(h, w) == 0xFFFFFF &&
                                dataReadOnly.GetPixel(mod1(h + 1), mod2(w - 1)) == 0xFFFFFF))
                        {
                            data1.SetPixel(h, w, 0);
                        }
                        else if (data1.GetPixel(h, w) != 0)
                        {
                            data1.SetPixel(h, w, 0xFFFFFF);
                        }
                        //Czwarta maska
                        if ((dataReadOnly.GetPixel(mod1(h - 1), mod2(w)) == 0 &&
                             dataReadOnly.GetPixel(mod1(h - 1), mod2(w + 1)) == 0 &&
                             dataReadOnly.GetPixel(mod1(h), mod2(w + 1)) == 0 &&
                             dataReadOnly.GetPixel(mod1(h + 1), mod2(w + 1)) == 0 &&
                             dataReadOnly.GetPixel(h, w) == 0xFFFFFF &&
                             dataReadOnly.GetPixel(mod1(h + 1), mod2(w - 1)) == 0xFFFFFF)

                            || (dataReadOnly.GetPixel(mod1(h - 1), mod2(w - 1)) == 0 &&
                                dataReadOnly.GetPixel(mod1(h), mod2(w - 1)) == 0 &&
                                dataReadOnly.GetPixel(mod1(h + 1), mod2(w - 1)) == 0 &&
                                dataReadOnly.GetPixel(mod1(h - 1), mod2(w)) == 0 &&
                                dataReadOnly.GetPixel(h, w) == 0xFFFFFF &&
                                dataReadOnly.GetPixel(mod1(h + 1), mod2(w + 1)) == 0xFFFFFF))
                        {
                            data1.SetPixel(h, w, 0);
                        }
                        else if (data1.GetPixel(h, w) != 0)
                        {
                            data1.SetPixel(h, w, 0xFFFFFF);
                        }

                        int pix1 = (dataReadOnly.GetPixel(h, w));
                        if ((data1.GetPixel(h, w) & 0xFF) == 0)
                        {
                            pix1 = 0;
                        }
                        dataOut.SetPixel(h, w, pix1);
                    }
                }
                data1.UnlockBits();
            }
            dataReadOnly.UnlockBits();
            dataOut.UnlockBits();
            mainForm.Picture = new Bitmap(inputBitmap);
        }
Пример #15
0
        private void KuwaharaBlur(Bitmap inputBitmap, int Size)
        {
            CustomBitmapProcessing tempData = new CustomBitmapProcessing(new Bitmap(inputBitmap));
            CustomBitmapProcessing newData  = new CustomBitmapProcessing(inputBitmap);

            tempData.LockBits();
            newData.LockBits();
            int[] ApetureMinX = { -(Size / 2), 0, -(Size / 2), 0 };
            int[] ApetureMaxX = { 0, (Size / 2), 0, (Size / 2) };
            int[] ApetureMinY = { -(Size / 2), -(Size / 2), 0, 0 };
            int[] ApetureMaxY = { 0, 0, (Size / 2), (Size / 2) };
            for (int y = 0; y < newData.Height; ++y)
            {
                for (int x = 0; x < newData.Width; ++x)
                {
                    int[] RValues   = { 0, 0, 0, 0 };
                    int[] GValues   = { 0, 0, 0, 0 };
                    int[] BValues   = { 0, 0, 0, 0 };
                    int[] NumPixels = { 0, 0, 0, 0 };
                    int[] MaxRValue = { 0, 0, 0, 0 };
                    int[] MaxGValue = { 0, 0, 0, 0 };
                    int[] MaxBValue = { 0, 0, 0, 0 };
                    int[] MinRValue = { 255, 255, 255, 255 };
                    int[] MinGValue = { 255, 255, 255, 255 };
                    int[] MinBValue = { 255, 255, 255, 255 };
                    for (int i = 0; i < 4; ++i)
                    {
                        for (int x2 = ApetureMinX[i]; x2 < ApetureMaxX[i]; ++x2)
                        {
                            int TempX = x + x2;
                            if (TempX >= 0 && TempX < inputBitmap.Width)
                            {
                                for (int y2 = ApetureMinY[i]; y2 < ApetureMaxY[i]; ++y2)
                                {
                                    int TempY = y + y2;
                                    if (TempY >= 0 && TempY < inputBitmap.Height)
                                    {
                                        int TempColor = tempData.GetPixel(TempY, TempX);
                                        RValues[i] += TempColor & 0xFF;
                                        GValues[i] += (TempColor >> 8) & 0xFF;
                                        BValues[i] += (TempColor >> 16) & 0xFF;
                                        int[] tempColor = { TempColor& 0xFF, (TempColor >> 8) & 0xFF, (TempColor >> 16) & 0xFF };
                                        if (tempColor[0] > MaxRValue[i])
                                        {
                                            MaxRValue[i] = tempColor[0];
                                        }
                                        else if (tempColor[0] < MinRValue[i])
                                        {
                                            MinRValue[i] = tempColor[0];
                                        }

                                        if (tempColor[1] > MaxGValue[i])
                                        {
                                            MaxGValue[i] = tempColor[1];
                                        }
                                        else if (tempColor[1] < MinGValue[i])
                                        {
                                            MinGValue[i] = tempColor[1];
                                        }

                                        if (tempColor[2] > MaxBValue[i])
                                        {
                                            MaxBValue[i] = tempColor[2];
                                        }
                                        else if (tempColor[2] < MinBValue[i])
                                        {
                                            MinBValue[i] = tempColor[2];
                                        }
                                        ++NumPixels[i];
                                    }
                                }
                            }
                        }
                    }
                    int j             = 0;
                    int MinDifference = 10000;
                    for (int i = 0; i < 4; ++i)
                    {
                        int CurrentDifference = (MaxRValue[i] - MinRValue[i]) + (MaxGValue[i] - MinGValue[i]) + (MaxBValue[i] - MinBValue[i]);
                        if (CurrentDifference < MinDifference && NumPixels[i] > 0)
                        {
                            j             = i;
                            MinDifference = CurrentDifference;
                        }
                    }
                    int rgbOut = (RValues[j] / NumPixels[j]) + ((GValues[j] / NumPixels[j]) << 8) + ((BValues[j] / NumPixels[j]) << 16);
                    newData.SetPixel(y, x, rgbOut);
                }
                mainForm.Invoke((MethodInvoker) delegate {
                    mainForm.toolStripProgressBar1.Value = (y * 100 / newData.Height);
                });
            }
            tempData.UnlockBits();
            newData.UnlockBits();
            mainForm.Invoke((MethodInvoker) delegate {
                mainForm.toolStripProgressBar1.Value = 0;
                //mainForm.pictureBox1.Image = inputBitmap;
                mainForm.Picture = new Bitmap(inputBitmap);
            });
        }
        public void NiblackBinarization(Bitmap inputBitmap, int blockSize, double niblackConstant)
        {
            inputBitmap = ApplyGrayScale(new Bitmap(inputBitmap));
            CustomBitmapProcessing data      = new CustomBitmapProcessing(inputBitmap);
            CustomBitmapProcessing data_help = new CustomBitmapProcessing(new Bitmap(inputBitmap));

            data.LockBits();
            data_help.LockBits();
            if (blockSize < 2)
            {
                blockSize = 2;
            }
            int itemCounter = blockSize * blockSize; // pomocnik do odchylenia standardowego i do średniej

            blockSize /= 2;
            int height = inputBitmap.Height;
            int width  = inputBitmap.Width;

            p = new Point(height, width);
            for (int i = 0; i < height; i = i + blockSize)
            {
                for (int j = 0; j < width; j = j + blockSize)
                {
                    int    maskHeightValidator = i + blockSize; // Prevents from taking pixels out of boundary
                    int    maskWidthValidator  = j + blockSize; // Prevents from taking pixels out of boundary
                    double meanVal             = 0;             // średnia
                    double stdVal = 0;                          // odchylenie standardowe
                    for (int x = i - blockSize; x < ValX(maskHeightValidator); x++)
                    {
                        for (int y = j - blockSize; y < ValY(maskWidthValidator); y++)
                        {
                            meanVal += (data_help.GetPixel(ValX(x), ValY(y)) & 0xFF);
                        }
                    }
                    meanVal /= itemCounter;

                    for (int x = i - blockSize; x < ValX(maskHeightValidator); x++)
                    {
                        for (int l = j - blockSize; l < ValY(maskWidthValidator); l++)
                        {
                            stdVal += Math.Pow((data_help.GetPixel(ValX(x), ValY(l)) & 0xFF) - meanVal, 2);
                        }
                    }
                    stdVal = Math.Sqrt(stdVal / itemCounter);
                    double threshold = (meanVal + niblackConstant * stdVal);
                    for (int k = i - blockSize; k < ValX(maskHeightValidator); k++)
                    {
                        for (int l = j - blockSize; l < ValY(maskWidthValidator); l++)
                        {
                            if ((data_help.GetPixel(ValX(k), ValY(l)) & 0xFF) < threshold)
                            {
                                data.SetPixel(ValX(k), ValY(l), 0);
                            }
                            else
                            {
                                data.SetPixel(ValX(k), ValY(l), (255 + (255 << 8) + (255 << 16)));
                            }
                        }
                    }
                }
            }
            data.UnlockBits();
            mainForm.Picture = inputBitmap;
        }
        public void HistogramNormalization(Bitmap inputBitmap)
        {
            inputBitmap = ApplyGrayScale(new Bitmap(inputBitmap));
            int width  = inputBitmap.Width;
            int height = inputBitmap.Height;
            CustomBitmapProcessing data = new CustomBitmapProcessing(inputBitmap);

            data.LockBits();
            int[,] imageMatrix = new int[height, width];
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    imageMatrix[i, j] = (data.GetPixel(i, j) & 0xFF);
                }
            }
            double mean = 0;

            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    mean += imageMatrix[i, j];
                }
            }
            mean /= (width * height);
            double var = 0;

            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    var += Math.Pow((imageMatrix[i, j] - mean), 2);
                }
            }
            var /= (height * width * 255); //255 for white color
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    double normalizedPixel = 0;
                    double squareError     = 0;

                    if (imageMatrix[i, j] > mean)
                    {
                        squareError     = Math.Pow((imageMatrix[i, j] - mean), 2);
                        normalizedPixel = (GOAL_MEAN + Math.Sqrt(((GOAL_VARIANCE * squareError / var))));
                    }
                    else
                    {
                        squareError     = (imageMatrix[i, j] - mean) * (imageMatrix[i, j] - mean);
                        normalizedPixel = (GOAL_MEAN - Math.Sqrt(((GOAL_VARIANCE * squareError / var))));
                    }
                    int rgb   = (int)-normalizedPixel;
                    int color = rgb + (rgb << 8) + (rgb << 16);
                    data.SetPixel(i, j, color);
                }
            }
            data.UnlockBits();
            mainForm.Invoke((MethodInvoker) delegate {
                mainForm.toolStripProgressBar1.Value = 0;
                mainForm.Picture = new Bitmap(inputBitmap);
            });
        }
        private void histogramEqualization(Bitmap inputBitmap)
        {
            CustomBitmapProcessing dataOut = new CustomBitmapProcessing(inputBitmap);

            dataOut.LockBits();
            uint    pixels = (uint)dataOut.Height * (uint)dataOut.Width;
            decimal Const = (decimal)255 / (decimal)pixels;
            int     R, G, B;

            int[] cdfR = new int[256];
            int[] cdfG = new int[256];
            int[] cdfB = new int[256];
            int   progressBarMax = dataOut.Height * 2, progrsbar2nd = dataOut.Height;

            for (int i = 0; i < dataOut.Height; i++) // Tworzenie histogramu
            {
                for (int j = 0; j < dataOut.Width; j++)
                {
                    int rgb    = dataOut.GetPixel(i, j);
                    int RValue = (rgb & 0xFF);
                    int GValue = ((rgb >> 8) & 0xFF);
                    int BValue = ((rgb >> 16) & 0xFF);
                    cdfR[RValue]++;
                    cdfG[GValue]++;
                    cdfB[BValue]++;
                }
                mainForm.Invoke((MethodInvoker) delegate
                {
                    mainForm.toolStripProgressBar1.Value = (i * 100) / progressBarMax;
                });
            }
            for (int r = 1; r <= 255; r++) // Tworzenie histogramu skumulowanego
            {
                cdfR[r] += cdfR[r - 1];
                cdfG[r] += cdfG[r - 1];
                cdfB[r] += cdfB[r - 1];
            }

            for (int i = 0; i < dataOut.Height; i++)
            {
                for (int j = 0; j < dataOut.Width; j++)
                //Parallel.For(0, dataOut.Width, j =>
                {
                    int pixelColor = dataOut.GetPixel(i, j);
                    R          = (int)((decimal)cdfR[pixelColor & 0xFF] * Const);
                    G          = (int)((decimal)cdfG[((pixelColor >> 8) & 0xFF)] * Const);
                    B          = (int)((decimal)cdfB[((pixelColor >> 16) & 0xFF)] * Const);
                    pixelColor = R + (G << 8) + (B << 16);
                    dataOut.SetPixel(i, j, pixelColor);
                }
                mainForm.Invoke((MethodInvoker) delegate
                {
                    mainForm.toolStripProgressBar1.Value = ((i + progrsbar2nd) * 100) / progressBarMax;
                });
            }
            dataOut.UnlockBits();
            mainForm.Invoke((MethodInvoker) delegate {
                mainForm.toolStripProgressBar1.Value = 0;
                mainForm.Picture = new Bitmap(inputBitmap);
                //mainForm.pictureBox1.Image = inputBitmap;
            });
        }