Пример #1
0
        public static Bitmap EdgeDetectConvolution(Bitmap b, EdgeDetectionMethod nType, byte nThreshold)
        {
            ConvMatrix m = new ConvMatrix();

            // First we need to keep a copy of the original Bitmap.
            using (Bitmap bTemp = (Bitmap)b.Clone())
            {
                try
                {
                    switch (nType)
                    {
                    case EdgeDetectionMethod.Sobel:
                        m.SetAll(0);
                        m.TopLeft  = m.BottomLeft = 1;
                        m.TopRight = m.BottomRight = -1;
                        m.MidLeft  = 2;
                        m.MidRight = -2;
                        m.Offset   = 0;
                        break;

                    case EdgeDetectionMethod.Prewitt:
                        m.SetAll(0);
                        m.TopLeft  = m.MidLeft = m.BottomLeft = -1;
                        m.TopRight = m.MidRight = m.BottomRight = 1;
                        m.Offset   = 0;
                        break;

                    case EdgeDetectionMethod.Kirsh:
                        m.SetAll(-3);
                        m.Pixel   = 0;
                        m.TopLeft = m.MidLeft = m.BottomLeft = 5;
                        m.Offset  = 0;
                        break;
                    }
                    BitmapFilter.Conv3x3(b, m);

                    switch (nType)
                    {
                    case EdgeDetectionMethod.Sobel:
                        m.SetAll(0);
                        m.TopLeft    = m.TopRight = 1;
                        m.BottomLeft = m.BottomRight = -1;
                        m.TopMid     = 2;
                        m.BottomMid  = -2;
                        m.Offset     = 0;
                        break;

                    case EdgeDetectionMethod.Prewitt:
                        m.SetAll(0);
                        m.BottomLeft = m.BottomMid = m.BottomRight = -1;
                        m.TopLeft    = m.TopMid = m.TopRight = 1;
                        m.Offset     = 0;
                        break;

                    case EdgeDetectionMethod.Kirsh:
                        m.SetAll(-3);
                        m.Pixel      = 0;
                        m.BottomLeft = m.BottomMid = m.BottomRight = 5;
                        m.Offset     = 0;
                        break;
                    }
                    BitmapFilter.Conv3x3(bTemp, m);
                }
                catch
                { throw; }

                // GDI+ lies - return format is BGR, not RGB.
                BitmapData bmpData  = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                BitmapData bmpData2 = bTemp.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

                try
                {
                    int stride = bmpData.Stride;

                    unsafe
                    {
                        byte *p  = (byte *)(void *)bmpData.Scan0;
                        byte *p2 = (byte *)(void *)bmpData2.Scan0;

                        int nOffset = stride - b.Width * 3;
                        int nWidth  = b.Width * 3;

                        int nPixel = 0;

                        for (int y = 0; y < b.Height; ++y)
                        {
                            for (int x = 0; x < nWidth; ++x)
                            {
                                nPixel = (int)System.Math.Sqrt((p[0] * p[0]) + (p2[0] * p2[0]));
                                if (nPixel < nThreshold)
                                {
                                    nPixel = nThreshold;
                                }
                                if (nPixel > 255)
                                {
                                    nPixel = 255;
                                }
                                p[0] = (byte)nPixel;
                                ++p;
                                ++p2;
                            }
                            p  += nOffset;
                            p2 += nOffset;
                        }
                    }
                }
                catch
                { throw; }
                finally
                {
                    b.UnlockBits(bmpData);
                    bTemp.UnlockBits(bmpData2);
                }
            }
            return(b);
        }
Пример #2
0
        public static Bitmap EdgeDetectConvolution(Bitmap b, EdgeDetectionMethod nType, byte nThreshold)
        {
            ConvMatrix m = new ConvMatrix();

            // First we need to keep a copy of the original Bitmap.
            using (Bitmap bTemp = (Bitmap)b.Clone())
            {
                try
                {
                    switch (nType)
                    {
                        case EdgeDetectionMethod.Sobel:
                            m.SetAll(0);
                            m.TopLeft = m.BottomLeft = 1;
                            m.TopRight = m.BottomRight = -1;
                            m.MidLeft = 2;
                            m.MidRight = -2;
                            m.Offset = 0;
                            break;
                        case EdgeDetectionMethod.Prewitt:
                            m.SetAll(0);
                            m.TopLeft = m.MidLeft = m.BottomLeft = -1;
                            m.TopRight = m.MidRight = m.BottomRight = 1;
                            m.Offset = 0;
                            break;
                        case EdgeDetectionMethod.Kirsh:
                            m.SetAll(-3);
                            m.Pixel = 0;
                            m.TopLeft = m.MidLeft = m.BottomLeft = 5;
                            m.Offset = 0;
                            break;
                    }
                    BitmapFilter.Conv3x3(b, m);

                    switch (nType)
                    {
                        case EdgeDetectionMethod.Sobel:
                            m.SetAll(0);
                            m.TopLeft = m.TopRight = 1;
                            m.BottomLeft = m.BottomRight = -1;
                            m.TopMid = 2;
                            m.BottomMid = -2;
                            m.Offset = 0;
                            break;
                        case EdgeDetectionMethod.Prewitt:
                            m.SetAll(0);
                            m.BottomLeft = m.BottomMid = m.BottomRight = -1;
                            m.TopLeft = m.TopMid = m.TopRight = 1;
                            m.Offset = 0;
                            break;
                        case EdgeDetectionMethod.Kirsh:
                            m.SetAll(-3);
                            m.Pixel = 0;
                            m.BottomLeft = m.BottomMid = m.BottomRight = 5;
                            m.Offset = 0;
                            break;
                    }
                    BitmapFilter.Conv3x3(bTemp, m);
                }
                catch
                { throw; }

                // GDI+ lies - return format is BGR, not RGB.
                BitmapData bmpData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                BitmapData bmpData2 = bTemp.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

                try
                {
                    int stride = bmpData.Stride;

                    unsafe
                    {
                        byte* p = (byte*)(void*)bmpData.Scan0;
                        byte* p2 = (byte*)(void*)bmpData2.Scan0;

                        int nOffset = stride - b.Width * 3;
                        int nWidth = b.Width * 3;

                        int nPixel = 0;

                        for (int y = 0; y < b.Height; ++y)
                        {
                            for (int x = 0; x < nWidth; ++x)
                            {
                                nPixel = (int)System.Math.Sqrt((p[0] * p[0]) + (p2[0] * p2[0]));
                                if (nPixel < nThreshold) nPixel = nThreshold;
                                if (nPixel > 255) nPixel = 255;
                                p[0] = (byte)nPixel;
                                ++p;
                                ++p2;
                            }
                            p += nOffset;
                            p2 += nOffset;
                        }
                    }
                }
                catch
                { throw; }
                finally
                {
                    b.UnlockBits(bmpData);
                    bTemp.UnlockBits(bmpData2);
                }
            }
            return b;
        }