C#图片处理

查找图片所在位置


原理:使用OpenCvSharp对比查找小图片在大图片上的位置

private static System.Drawing.Point Find(Mat BackGround, Mat Identify, double threshold = 0.8)
{
     using (Mat res = new Mat(BackGround.Rows - Identify.Rows + 1, BackGround.Cols - Identify.Cols + 1, MatType.CV_32FC1))
     {
         Mat gref = BackGround.CvtColor(ColorConversionCodes.BGR2GRAY);
         Mat gtpl = Identify.CvtColor(ColorConversionCodes.BGR2GRAY);

         Cv2.MatchTemplate(gref, gtpl, res, TemplateMatchModes.CCoeffNormed);
         Cv2.Threshold(res, res, 0.8, 1.0, ThresholdTypes.Tozero);

         double minval, maxval;
         OpenCvSharp.Point minloc, maxloc;

         Cv2.MinMaxLoc(res, out minval, out maxval, out minloc, out maxloc);

         if (maxval >= threshold)
         {
             return new System.Drawing.Point(maxloc.X,maxloc.Y);
         }
         return new System.Drawing.Point(0, 0);
     }
 }

图片转化


引用的博客比较多,这是其中几个引用,其他的找不到原出处了

https://blog.csdn.net/wchstrife/article/details/78984735?ydreferer=aHR0cHM6Ly9jbi5iaW5nLmNvbS8%3D
https://blog.csdn.net/jiangxinyu/article/details/6222322
https://blog.csdn.net/fangyu723/article/details/108240479

界面效果
C#图片处理_第1张图片
C#图片处理_第2张图片
数字提取
C#图片处理_第3张图片
网上找的一张游戏界面图

C#图片处理_第4张图片

    /// 
    /// 图像处理转换
    /// 
    public class ImgConvert
    {
        /// 
        /// 图片脱色(灰度)
        /// 
        /// 
        /// 
        /// 
        public static Bitmap DeColor(Bitmap bitmap,int Case)
        {
            int Height = bitmap.Height;
            int Width = bitmap.Width;
            Bitmap newBitmap = new Bitmap(Width, Height);
            Color pixel;
            for (int x = 0; x < Width; x++)
                for (int y = 0; y < Height; y++)
                {
                    pixel = bitmap.GetPixel(x, y);
                    int r, g, b, Result = 0;
                    r = pixel.R;
                    g = pixel.G;
                    b = pixel.B;
                    switch (Case)
                    {
                        case 0://平均值法
                            Result = ((r + g + b) / 3);
                            break;
                        case 1://最大值法
                            Result = r > g ? r : g;
                            Result = Result > b ? Result : b;
                            break;
                        case 2://加权平均值法1
                            Result = ((int)(0.7 * r) + (int)(0.2 * g) + (int)(0.1 * b));
                            break;
                        case 3://加权平均值法2
                            Result = ((int)(0.3 * r) + (int)(0.59 * g) + (int)(0.11 * b));
                            break;
                    }
                    newBitmap.SetPixel(x, y, Color.FromArgb(Result, Result, Result));
                }
            return newBitmap;
        }

        /// 
        /// 图片暗角
        /// 
        /// 
        /// 
        public static Bitmap DarkCorner(Bitmap bitmap)
        {
            Bitmap newbitmap = bitmap.Clone() as Bitmap;
            int width = newbitmap.Width;
            int height = newbitmap.Height;
            float cx = width / 2;
            float cy = height / 2;
            float maxDist = cx * cx + cy * cy;
            float currDist = 0, factor;
            Color pixel;

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    currDist = ((float)i - cx) * ((float)i - cx) + ((float)j - cy) * ((float)j - cy);
                    factor = currDist / maxDist;

                    pixel = newbitmap.GetPixel(i, j);
                    int red = (int)(pixel.R * (1 - factor));
                    int green = (int)(pixel.G * (1 - factor));
                    int blue = (int)(pixel.B * (1 - factor));
                    newbitmap.SetPixel(i, j, Color.FromArgb(red, green, blue));
                }
            }
            return newbitmap;
        }

        /// 
        /// 加马赛克
        /// 
        /// 
        /// 
        public static Bitmap Mosaic(Bitmap bitmap)
        {
            Bitmap newbitmap = bitmap.Clone() as Bitmap;
            int RIDIO = 20;//马赛克的尺度,默认为周围两个像素
            for (int h = 0; h < newbitmap.Height; h += RIDIO)
            {
                for (int w = 0; w < newbitmap.Width; w += RIDIO)
                {
                    int avgRed = 0, avgGreen = 0, avgBlue = 0;
                    int count = 0;
                    //取周围的像素
                    for (int x = w; (x < w + RIDIO && x < newbitmap.Width); x++)
                    {
                        for (int y = h; (y < h + RIDIO && y < newbitmap.Height); y++)
                        {
                            Color pixel = newbitmap.GetPixel(x, y);
                            avgRed += pixel.R;
                            avgGreen += pixel.G;
                            avgBlue += pixel.B;
                            count++;
                        }
                    }

                    //取平均值
                    avgRed = avgRed / count;
                    avgBlue = avgBlue / count;
                    avgGreen = avgGreen / count;

                    //设置颜色
                    for (int x = w; (x < w + RIDIO && x < newbitmap.Width); x++)
                    {
                        for (int y = h; (y < h + RIDIO && y < newbitmap.Height); y++)
                        {
                            Color newColor = Color.FromArgb(avgRed, avgGreen, avgBlue);
                            newbitmap.SetPixel(x, y, newColor);
                        }
                    }
                }
            }
            return newbitmap;
        }

        /// 
        /// 底片
        /// 
        /// 
        /// 
        public static Bitmap Negative(Bitmap bitmap)
        {
            int Height = bitmap.Height;
            int Width = bitmap.Width;
            Bitmap newbitmap = new Bitmap(Width, Height);
            Color pixel;
            for (int x = 1; x < Width; x++)
            {
                for (int y = 1; y < Height; y++)
                {
                    int r, g, b;
                    pixel = bitmap.GetPixel(x, y);
                    r = 255 - pixel.R;
                    g = 255 - pixel.G;
                    b = 255 - pixel.B;
                    newbitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
                }
            }
            return newbitmap;
        }

        /// 
        /// 浮雕
        /// 
        /// 
        /// 
        public static Bitmap Relief(Bitmap bitmap)
        {
            int Height = bitmap.Height;
            int Width = bitmap.Width;
            Bitmap newbitmap = new Bitmap(Width, Height);
            Color pixel1, pixel2;
            for (int x = 0; x < Width - 1; x++)
            {
                for (int y = 0; y < Height - 1; y++)
                {
                    int r = 0, g = 0, b = 0;
                    pixel1 = bitmap.GetPixel(x, y);
                    pixel2 = bitmap.GetPixel(x + 1, y + 1);
                    r = Math.Abs(pixel1.R - pixel2.R + 128);
                    g = Math.Abs(pixel1.G - pixel2.G + 128);
                    b = Math.Abs(pixel1.B - pixel2.B + 128);
                    if (r > 255)
                        r = 255;
                    if (r < 0)
                        r = 0;
                    if (g > 255)
                        g = 255;
                    if (g < 0)
                        g = 0;
                    if (b > 255)
                        b = 255;
                    if (b < 0)
                        b = 0;
                    newbitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
                }
            }
            return newbitmap;
        }

        /// 
        /// 图片柔化
        /// 
        /// 
        /// 
        public static Bitmap Soften(Bitmap bitmap)
        {
            int width = bitmap.Width;
            int height = bitmap.Height;
            Bitmap newbitmap = new Bitmap(width, height);
            Color pixel;
            //高斯模板
            int[] Gauss = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
            for (int x = 1; x < width - 1; x++)
                for (int y = 1; y < height - 1; y++)
                {
                    int r = 0, g = 0, b = 0;
                    int Index = 0;
                    for (int col = -1; col <= 1; col++)
                        for (int row = -1; row <= 1; row++)
                        {
                            pixel = bitmap.GetPixel(x + row, y + col);
                            r += pixel.R * Gauss[Index];
                            g += pixel.G * Gauss[Index];
                            b += pixel.B * Gauss[Index];
                            Index++;
                        }
                    r /= 16;
                    g /= 16;
                    b /= 16;
                    //处理颜色值溢出
                    r = r > 255 ? 255 : r;
                    r = r < 0 ? 0 : r;
                    g = g > 255 ? 255 : g;
                    g = g < 0 ? 0 : g;
                    b = b > 255 ? 255 : b;
                    b = b < 0 ? 0 : b;
                    newbitmap.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b));
                }
            return newbitmap;
        }

        /// 
        /// 图片锐化
        /// 
        /// 
        /// 
        public static Bitmap Sharpen(Bitmap bitmap)
        {
            int Width = bitmap.Width;
            int Height = bitmap.Height;
            Bitmap newBitmap = new Bitmap(Width, Height);
            Color pixel;
            //拉普拉斯模板
            int[] Laplacian = { -1, -1, -1, -1, 9, -1, -1, -1, -1 };
            for (int x = 1; x < Width - 1; x++)
            {
                for (int y = 1; y < Height - 1; y++)
                {
                    int r = 0, g = 0, b = 0;
                    int Index = 0;
                    for (int col = -1; col <= 1; col++)
                        for (int row = -1; row <= 1; row++)
                        {
                            pixel = bitmap.GetPixel(x + row, y + col); r += pixel.R * Laplacian[Index];
                            g += pixel.G * Laplacian[Index];
                            b += pixel.B * Laplacian[Index];
                            Index++;
                        }
                    //处理颜色值溢出
                    r = r > 255 ? 255 : r;
                    r = r < 0 ? 0 : r;
                    g = g > 255 ? 255 : g;
                    g = g < 0 ? 0 : g;
                    b = b > 255 ? 255 : b;
                    b = b < 0 ? 0 : b;
                    newBitmap.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b));
                }
            }
            return newBitmap;
        }

        /// 
        /// 图片雾化
        /// 
        /// 
        /// 
        public static Bitmap Nebulization(Bitmap bitmap)
        {
            int Width = bitmap.Width;
            int Height = bitmap.Height;
            Bitmap newBitmap = new Bitmap(Width, Height);
            Color pixel;
            for (int x = 1; x < Width - 1; x++)
            {
                for (int y = 1; y < Height - 1; y++)
                {
                    System.Random MyRandom = new Random();
                    int k = MyRandom.Next(123456);
                    //像素块大小
                    int dx = x + k % 19;
                    int dy = y + k % 19;
                    if (dx >= Width)
                        dx = Width - 1;
                    if (dy >= Height)
                        dy = Height - 1;
                    pixel = bitmap.GetPixel(dx, dy);
                    newBitmap.SetPixel(x, y, pixel);
                }
            }  
            return newBitmap;
        }

        /// 
        /// 翻转
        /// 
        /// 
        /// 
        public static Bitmap TurnOver(Bitmap bitmap)
        {
            Bitmap newbitmap = bitmap.Clone() as Bitmap;
            newbitmap.RotateFlip(RotateFlipType.Rotate90FlipNone);
            return newbitmap;
        }

        /// 
        /// 直方图均衡化
        /// 
        /// 
        /// 
        public static Bitmap HistAverage(Bitmap bitmap)
        {
            Bitmap newbitmap = bitmap.Clone() as Bitmap;
            int iw = bitmap.Width, ih = bitmap.Height;
            int[] hist = ImgConvertHelper.GetHist(bitmap, iw, ih);
            Color c = new Color();
            double p = (double)255 / (iw * ih);
            double[] sum = new double[256];
            int[] outg = new int[256];
            int r, g, b;
            sum[0] = hist[0];
            for (int i = 1; i < 256; i++)
                sum[i] = sum[i - 1] + hist[i];

            //灰度变换:i-->outg[i]	
            for (int i = 0; i < 256; i++)
                outg[i] = (int)(p * sum[i]);

            for (int j = 0; j < ih; j++)
            {
                for (int i = 0; i < iw; i++)
                {
                    r = (newbitmap.GetPixel(i, j)).R;
                    g = (newbitmap.GetPixel(i, j)).G;
                    b = (newbitmap.GetPixel(i, j)).B;
                    c = Color.FromArgb(outg[r], outg[g], outg[b]);
                    bitmap.SetPixel(i, j, c);
                }
            }
            return newbitmap;
        }

        / 
        / 对比度扩展
        / 
        / 
        / 
        //public static Bitmap Contrast(Bitmap bitmap)
        //{
        //    Bitmap newbitmap = bitmap.Clone() as Bitmap;
        //    int x1 = Convert.ToInt32(dialog.getX01);
        //    int y1 = Convert.ToInt32(dialog.getY01);
        //    int x2 = Convert.ToInt32(dialog.getX02);
        //    int y2 = Convert.ToInt32(dialog.getY02);


        //    //计算灰度映射表
        //    int[] pixMap = pixelsMap(x1, y1, x2, y2);


        //    //线性拉伸
        //    bm = stretch(bm, pixMap, iw, ih);
        //    return newbitmap;
        //}

        /// 
        /// 3 X 3 阈值滤波
        /// 
        /// 
        /// 
        public static Bitmap ThresholdFilter(Bitmap bitmap)
        {
            int iw = bitmap.Width, ih = bitmap.Height;
            Bitmap newbitmap = bitmap.Clone() as Bitmap;
            int avr,          //灰度平均 
                sum,          //灰度和
                num = 0,      //计数器
                nT = 4,       //计数器阈值
                T = 50;       //阈值
            int pij, pkl,     //(i,j),(i+k,j+l)处灰度值
                err;          //误差


            for (int j = 1; j < ih - 1; j++)
            {
                for (int i = 1; i < iw - 1; i++)
                {
                    //取3×3块的9个象素, 求和
                    sum = 0;
                    for (int k = -1; k < 2; k++)
                    {
                        for (int l = -1; l < 2; l++)
                        {
                            if ((k != 0) || (l != 0))
                            {
                                pkl = (bitmap.GetPixel(i + k, j + l)).R;
                                pij = (bitmap.GetPixel(i, j)).R;
                                err = Math.Abs(pkl - pij);
                                sum = sum + pkl;
                                if (err > T) num++;
                            }
                        }
                    }
                    avr = (int)(sum / 8.0f);         //平均值
                    if (num > nT)
                        newbitmap.SetPixel(i, j, Color.FromArgb(avr, avr, avr));
                }
            }
            return newbitmap;
        }

        /// 
        /// 均值滤波
        /// 
        /// 
        /// 
        public static Bitmap AverageFilter(Bitmap bitmap)
        {
            int iw = bitmap.Width, ih = bitmap.Height;
            Bitmap newbitmap = bitmap.Clone() as Bitmap;
            for (int j = 1; j < ih - 1; j++)
            {
                for (int i = 1; i < iw - 1; i++)
                {
                    int avr;
                    int avr1;
                    int avr2;
                    int sum = 0;
                    int sum1 = 0;
                    int sum2 = 0;
                    for (int k = -1; k <= 1; k++)
                    {
                        for (int l = -1; l <= 1; l++)
                        {
                            sum = sum + (bitmap.GetPixel(i + k, j + 1).R);
                            sum1 = sum1 + (bitmap.GetPixel(i + k, j + 1).G);
                            sum2 = sum2 + (bitmap.GetPixel(i + k, j + 1).B);
                        }
                    }
                    avr = (int)(sum / 9.0f);
                    avr1 = (int)(sum1 / 9.0f);
                    avr2 = (int)(sum2 / 9.0f);
                    newbitmap.SetPixel(i, j, Color.FromArgb(avr, avr1, avr2));
                }
            }
            return newbitmap;
        }

        /// 
        /// 中值滤波
        /// 
        /// 
        /// 
        /// 
        public static Bitmap MedianFilter(Bitmap bitmap,int n)
        {
            int iw = bitmap.Width, ih = bitmap.Height;
            Bitmap newbitmap = bitmap.Clone() as Bitmap;
            for (int j = 2; j < ih - 2; j++)
            {
                int[] dt;
                int[] dt1;
                int[] dt2;
                for (int i = 2; i < iw - 2; i++)
                {
                    int m = 0, r = 0, r1 = 0, r2 = 0, a = 0, b = 0;
                    if (n == 3)
                    {
                        dt = new int[25];
                        dt1 = new int[25];
                        dt2 = new int[25];
                        //取5×5块的25个象素
                        for (int k = -2; k < 3; k++)
                        {
                            for (int l = -2; l < 3; l++)
                            {
                                //取(i+k,j+l)处的象素,赋于数组dt
                                dt[m] = (bitmap.GetPixel(i + k, j + l)).R;
                                dt1[a] = (bitmap.GetPixel(i + k, j + l)).G;
                                dt2[b] = (bitmap.GetPixel(i + k, j + l)).B;
                                m++;
                                a++;
                                b++;
                            }
                        }
                        //冒泡排序,输出中值
                        r = ImgConvertHelper.MedianSorter(dt, 25); //中值      
                        r1 = ImgConvertHelper.MedianSorter(dt1, 25);
                        r2 = ImgConvertHelper.MedianSorter(dt2, 25);
                    }
                    else if (n == 1)
                    {
                        dt = new int[5];

                        //取1×5窗口5个像素
                        dt[0] = (bitmap.GetPixel(i, j - 2)).R;
                        dt[1] = (bitmap.GetPixel(i, j - 1)).R;
                        dt[2] = (bitmap.GetPixel(i, j)).R;
                        dt[3] = (bitmap.GetPixel(i, j + 1)).R;
                        dt[4] = (bitmap.GetPixel(i, j + 2)).R;
                        r = ImgConvertHelper.MedianSorter(dt, 5);   //中值
                        dt1 = new int[5];


                        //取1×5窗口5个像素
                        dt1[0] = (bitmap.GetPixel(i, j - 2)).G;
                        dt1[1] = (bitmap.GetPixel(i, j - 1)).G;
                        dt1[2] = (bitmap.GetPixel(i, j)).G;
                        dt1[3] = (bitmap.GetPixel(i, j + 1)).G;
                        dt1[4] = (bitmap.GetPixel(i, j + 2)).G;
                        r1 = ImgConvertHelper.MedianSorter(dt1, 5);   //中值   
                        dt2 = new int[5];


                        //取1×5窗口5个像素
                        dt2[0] = (bitmap.GetPixel(i, j - 2)).B;
                        dt2[1] = (bitmap.GetPixel(i, j - 1)).B;
                        dt2[2] = (bitmap.GetPixel(i, j)).B;
                        dt2[3] = (bitmap.GetPixel(i, j + 1)).B;
                        dt2[4] = (bitmap.GetPixel(i, j + 2)).B;
                        r2 = ImgConvertHelper.MedianSorter(dt2, 5);   //中值                           
                    }
                    else if (n == 2)
                    {
                        dt = new int[5];


                        //取5×1窗口5个像素
                        dt[0] = (bitmap.GetPixel(i - 2, j)).R;
                        dt[1] = (bitmap.GetPixel(i - 1, j)).R;
                        dt[2] = (bitmap.GetPixel(i, j)).R;
                        dt[3] = (bitmap.GetPixel(i + 1, j)).R;
                        dt[4] = (bitmap.GetPixel(i + 2, j)).R;
                        r = ImgConvertHelper.MedianSorter(dt, 5);  //中值 dt = new int[5];


                        //取5×1窗口5个像素
                        dt1 = new int[5];
                        dt1[0] = (bitmap.GetPixel(i - 2, j)).G;
                        dt1[1] = (bitmap.GetPixel(i - 1, j)).G;
                        dt1[2] = (bitmap.GetPixel(i, j)).G;
                        dt1[3] = (bitmap.GetPixel(i + 1, j)).G;
                        dt1[4] = (bitmap.GetPixel(i + 2, j)).G;
                        r1 = ImgConvertHelper.MedianSorter(dt1, 5);  //中值       

                        //取5×1窗口5个像素
                        dt2 = new int[5];
                        dt2[0] = (bitmap.GetPixel(i - 2, j)).B;
                        dt2[1] = (bitmap.GetPixel(i - 1, j)).B;
                        dt2[2] = (bitmap.GetPixel(i, j)).B;
                        dt2[3] = (bitmap.GetPixel(i + 1, j)).B;
                        dt2[4] = (bitmap.GetPixel(i + 2, j)).B;
                        r2 = ImgConvertHelper.MedianSorter(dt2, 5);  //中值       

                    }
                    newbitmap.SetPixel(i, j, Color.FromArgb(r, r1, r2));         //输出                  
                }
            }
            return newbitmap;
        }

        /// 
        /// 3×3 低通滤波
        /// 
        /// 
        /// 
        public static Bitmap LowpassFilter(Bitmap bitmap, int n)
        {
            int iw = bitmap.Width, ih = bitmap.Height;
            Bitmap newbitmap = bitmap.Clone() as Bitmap;
            int[,] h;

            //定义扩展输入图像矩阵
            int[,] ex_inpix = ImgConvertHelper.Exinpix(bitmap, iw, ih);

            //低通滤波
            for (int j = 1; j < ih + 1; j++)
            {
                for (int i = 1; i < iw + 1; i++)
                {
                    int r = 0, sum = 0;

                    //低通模板		
                    h = ImgConvertHelper.LowMatrix(n);

                    //求3×3窗口9个像素加权和
                    for (int k = -1; k < 2; k++)
                        for (int l = -1; l < 2; l++)
                            sum = sum + h[k + 1, l + 1] * ex_inpix[i + k, j + l];

                    if (n == 1)
                        r = (int)(sum / 9);       //h1平均值
                    else if (n == 2)
                        r = (int)(sum / 10);      //h2
                    else if (n == 3)
                        r = (int)(sum / 16);      //h3 
                    newbitmap.SetPixel(i - 1, j - 1, Color.FromArgb(r, r, r));    //输出                    
                }
            }
            return newbitmap;
        }

        /// 
        /// Kirsch锐化
        /// 
        /// 
        /// 
        public static Bitmap KirschSharpen(Bitmap bitmap)
        {
            int iw = bitmap.Width, ih = bitmap.Height;
            Bitmap newbitmap = bitmap.Clone() as Bitmap;
            Color c = new Color();
            int i, j, r;
            int[,] inr = new int[iw, ih]; //红色分量矩阵
            int[,] ing = new int[iw, ih]; //绿色分量矩阵
            int[,] inb = new int[iw, ih]; //蓝色分量矩阵
            int[,] gray = new int[iw, ih];//灰度图像矩阵	

            //转变为灰度图像矩阵

            for (j = 0; j < ih; j++)
            {
                for (i = 0; i < iw; i++)
                {
                    c = bitmap.GetPixel(i, j);
                    inr[i, j] = c.R;
                    ing[i, j] = c.G;
                    inb[i, j] = c.B;
                    gray[i, j] = (int)((c.R + c.G + c.B) / 3.0);
                }
            }
            int[,] kir0 = {{ 5, 5, 5},
                               {-3, 0,-3},
                               {-3,-3,-3}},//kir0

                       kir1 =  {{-3, 5, 5},
                                {-3, 0, 5},
                                {-3,-3,-3}},//kir1

                       kir2 = {{-3,-3, 5},
                               {-3, 0, 5},
                               {-3,-3, 5}},//kir2

                       kir3 = {{-3,-3,-3},
                               {-3, 0, 5},
                               {-3, 5, 5}},//kir3

                       kir4 = {{-3,-3,-3},
                               {-3, 0,-3},
                               { 5, 5, 5}},//kir4

                       kir5 = {{-3,-3,-3},
                               { 5, 0,-3},
                               { 5, 5,-3}},//kir5

                       kir6 = {{ 5,-3,-3},
                               { 5, 0,-3},
                               { 5,-3,-3}},//kir6

                       kir7 = {{ 5, 5,-3},
                               { 5, 0,-3},
                               {-3,-3,-3}};//kir7	
                                           //边缘检测

            int[,] edge0 = new int[iw, ih];

            int[,] edge1 = new int[iw, ih];

            int[,] edge2 = new int[iw, ih];

            int[,] edge3 = new int[iw, ih];

            int[,] edge4 = new int[iw, ih];

            int[,] edge5 = new int[iw, ih];

            int[,] edge6 = new int[iw, ih];

            int[,] edge7 = new int[iw, ih];

            edge0 = ImgConvertHelper.EdgeEnhance(gray, kir0, iw, ih);
            edge1 = ImgConvertHelper.EdgeEnhance(gray, kir1, iw, ih);
            edge2 = ImgConvertHelper.EdgeEnhance(gray, kir2, iw, ih);
            edge3 = ImgConvertHelper.EdgeEnhance(gray, kir3, iw, ih);
            edge4 = ImgConvertHelper.EdgeEnhance(gray, kir4, iw, ih);
            edge5 = ImgConvertHelper.EdgeEnhance(gray, kir5, iw, ih);
            edge6 = ImgConvertHelper.EdgeEnhance(gray, kir6, iw, ih);
            edge7 = ImgConvertHelper.EdgeEnhance(gray, kir7, iw, ih);

            int[] tem = new int[8];
            int max;
            for (j = 0; j < ih; j++)
            {
                for (i = 0; i < iw; i++)
                {
                    tem[0] = edge0[i, j];
                    tem[1] = edge1[i, j];
                    tem[2] = edge2[i, j];
                    tem[3] = edge3[i, j];
                    tem[4] = edge4[i, j];
                    tem[5] = edge5[i, j];
                    tem[6] = edge6[i, j];
                    tem[7] = edge7[i, j];
                    max = 0;
                    for (int k = 0; k < 8; k++)
                        if (tem[k] > max) max = tem[k];
                    if (max > 255) max = 255;
                    r = 255 - max;
                    newbitmap.SetPixel(i, j, Color.FromArgb(r, r, r));
                }
            }
            return newbitmap;
        }

        /// 
        /// Laplace锐化
        /// 
        /// 
        /// 
        public static Bitmap LaplaceSharpen(Bitmap bitmap)
        {
            int iw = bitmap.Width, ih = bitmap.Height;
            Bitmap newbitmap = bitmap.Clone() as Bitmap;
            Color c = new Color();
            int i, j, r;
            int[,] inr = new int[iw, ih]; //红色分量矩阵
            int[,] ing = new int[iw, ih]; //绿色分量矩阵
            int[,] inb = new int[iw, ih]; //蓝色分量矩阵
            int[,] gray = new int[iw, ih];//灰度图像矩阵	

            //转变为灰度图像矩阵

            for (j = 0; j < ih; j++)
            {
                for (i = 0; i < iw; i++)
                {
                    c = bitmap.GetPixel(i, j);
                    inr[i, j] = c.R;
                    ing[i, j] = c.G;
                    inb[i, j] = c.B;
                    gray[i, j] = (int)((c.R + c.G + c.B) / 3.0);
                }
            }
            int[,] lap1 = {{ 1, 1, 1},
                               { 1,-8, 1},
                               { 1, 1, 1}};

            //边缘增强
            int[,] edge = ImgConvertHelper.EdgeEnhance(gray, lap1, iw, ih);

            for (j = 0; j < ih; j++)
            {
                for (i = 0; i < iw; i++)
                {
                    r = edge[i, j];
                    if (r > 255) r = 255;

                    if (r < 0) r = 0;
                    c = Color.FromArgb(r, r, r);
                    newbitmap.SetPixel(i, j, c);
                }
            }
            return newbitmap;
        }

        /// 
        /// Prewitt锐化
        /// 
        /// 
        /// 
        public static Bitmap PrewittSharpen(Bitmap bitmap)
        {
            int iw = bitmap.Width, ih = bitmap.Height;
            Bitmap newbitmap = bitmap.Clone() as Bitmap;
            Color c = new Color();
            int i, j, r;
            int[,] inr = new int[iw, ih]; //红色分量矩阵
            int[,] ing = new int[iw, ih]; //绿色分量矩阵
            int[,] inb = new int[iw, ih]; //蓝色分量矩阵
            int[,] gray = new int[iw, ih];//灰度图像矩阵	

            //转变为灰度图像矩阵

            for (j = 0; j < ih; j++)
            {
                for (i = 0; i < iw; i++)
                {
                    c = bitmap.GetPixel(i, j);
                    inr[i, j] = c.R;
                    ing[i, j] = c.G;
                    inb[i, j] = c.B;
                    gray[i, j] = (int)((c.R + c.G + c.B) / 3.0);
                }
            }
            //Prewitt算子D_x模板
            int[,] pre1 = {{ 1, 0,-1},
                               { 1, 0,-1},
                               { 1, 0,-1}};

            //Prewitt算子D_y模板
            int[,] pre2 = {{ 1, 1, 1},
                               { 0, 0, 0},
                               {-1,-1,-1}};
            int[,] edge1 = ImgConvertHelper.EdgeEnhance(gray, pre1, iw, ih);

            int[,] edge2 = ImgConvertHelper.EdgeEnhance(gray, pre2, iw, ih);
            for (j = 0; j < ih; j++)
            {
                for (i = 0; i < iw; i++)
                {
                    r = Math.Max(edge1[i, j], edge2[i, j]);

                    if (r > 255) r = 255;
                    c = Color.FromArgb(r, r, r);
                    newbitmap.SetPixel(i, j, c);
                }
            }
            return newbitmap;
        }

        /// 
        /// Roberts锐化
        /// 
        /// 
        /// 
        public static Bitmap RobertsSharpen(Bitmap bitmap)
        {
            int iw = bitmap.Width, ih = bitmap.Height;
            Bitmap newbitmap = bitmap.Clone() as Bitmap;
            int r, r0, r1, r2, r3, g, g0, g1, g2, g3, b, b0, b1, b2, b3;
            int[,] inr = new int[iw, ih];//红色分量矩阵
            int[,] ing = new int[iw, ih];//绿色分量矩阵
            int[,] inb = new int[iw, ih];//蓝色分量矩阵
            int[,] gray = new int[iw, ih];//灰度图像矩阵	             

            for (int j = 1; j < ih - 1; j++)
            {
                for (int i = 1; i < iw - 1; i++)
                {
                    r0 = (bitmap.GetPixel(i, j)).R;
                    r1 = (bitmap.GetPixel(i, j + 1)).R;
                    r2 = (bitmap.GetPixel(i + 1, j)).R;
                    r3 = (bitmap.GetPixel(i + 1, j + 1)).R;

                    r = (int)Math.Sqrt((r0 - r3) * (r0 - r3) + (r1 - r2) * (r1 - r2));

                    g0 = (bitmap.GetPixel(i, j)).G;
                    g1 = (bitmap.GetPixel(i, j + 1)).G;
                    g2 = (bitmap.GetPixel(i + 1, j)).G;
                    g3 = (bitmap.GetPixel(i + 1, j + 1)).G;
                    g = (int)Math.Sqrt((g0 - g3) * (g0 - g3) + (g1 - g2) * (g1 - g2));

                    b0 = (bitmap.GetPixel(i, j)).B;
                    b1 = (bitmap.GetPixel(i, j + 1)).B;
                    b2 = (bitmap.GetPixel(i + 1, j)).B;
                    b3 = (bitmap.GetPixel(i + 1, j + 1)).B;
                    b = (int)Math.Sqrt((b0 - b3) * (b0 - b3)
                      + (b1 - b2) * (b1 - b2));

                    if (r < 0)
                        r = 0;                                       //黑色,边缘点
                    if (r > 255)
                        r = 255;

                    newbitmap.SetPixel(i, j, Color.FromArgb(r, r, r));
                }
            }
            return newbitmap;
        }

        /// 
        /// Sobel锐化
        /// 
        /// 
        /// 
        public static Bitmap SobelSharpen(Bitmap bitmap)
        {
            int iw = bitmap.Width, ih = bitmap.Height;
            Bitmap newbitmap = bitmap.Clone() as Bitmap;
            Color c = new Color();
            int i, j, r;
            int[,] inr = new int[iw, ih]; //红色分量矩阵
            int[,] ing = new int[iw, ih]; //绿色分量矩阵
            int[,] inb = new int[iw, ih]; //蓝色分量矩阵
            int[,] gray = new int[iw, ih];//灰度图像矩阵	

            //转变为灰度图像矩阵

            for (j = 0; j < ih; j++)
            {
                for (i = 0; i < iw; i++)
                {
                    c = bitmap.GetPixel(i, j);
                    inr[i, j] = c.R;
                    ing[i, j] = c.G;
                    inb[i, j] = c.B;
                    gray[i, j] = (int)((c.R + c.G + c.B) / 3.0);
                }
            }
            int[,] sob1 = {{ 1, 0,-1},
                               { 2, 0,-2},
                               { 1, 0,-1}};
            int[,] sob2 = {{ 1, 2, 1},
                               { 0, 0, 0},
                               {-1,-2,-1}};


            int[,] edge1 = ImgConvertHelper.EdgeEnhance(gray, sob1, iw, ih);
            int[,] edge2 = ImgConvertHelper.EdgeEnhance(gray, sob2, iw, ih);
            for (j = 0; j < ih; j++)
            {
                for (i = 0; i < iw; i++)
                {
                    r = Math.Max(edge1[i, j], edge2[i, j]);
                    if (r > 255) r = 255;
                    c = Color.FromArgb(r, r, r);
                    newbitmap.SetPixel(i, j, c);
                }
            }
            return newbitmap;
        }

        /// 
        /// 透明化
        /// 
        /// 
        /// 
        public static Bitmap Transparent(Bitmap bitmap)
        {
            int iw = bitmap.Width, ih = bitmap.Height;
            Bitmap newbitmap = new Bitmap(iw, ih);
            int alpha = 0;
            Color demo;
            Color pixel;
            for (int x = 0; x < iw; x++)
            {
                for (int y = 0; y < ih; y++)
                {
                    demo = bitmap.GetPixel(1, 1);
                    pixel = bitmap.GetPixel(x, y);
                    int R = demo.R;
                    int G = demo.G;
                    int B = demo.B;
                    int r1 = pixel.R;
                    int g1 = pixel.G;
                    int b1 = pixel.B;
                    int a = 40;  //RGB误差范围
                    if (Math.Abs(R - r1) < a && Math.Abs(G - g1) < a && Math.Abs(B - b1) < a)
                    {
                        alpha = 0;  //RGB在色差范围内,透明度为0
                    }
                    else
                    {
                        alpha = 255;
                    }
                    newbitmap.SetPixel(x, y, Color.FromArgb(alpha, r1, g1, b1));
                }
            }
            return newbitmap;
        }

        /// 
        /// 指定颜色透明化
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static Bitmap Transparent(Bitmap bitmap, int R, int G, int B)
        {
            int iw = bitmap.Width, ih = bitmap.Height;
            Bitmap newbitmap = new Bitmap(iw, ih);
            int alpha = 0;
            Color pixel;
            for (int x = 0; x < iw; x++)
            {
                for (int y = 0; y < ih; y++)
                {
                    pixel = bitmap.GetPixel(x, y);
                    int r1 = pixel.R;
                    int g1 = pixel.G;
                    int b1 = pixel.B;
                    int a = 40;  //色差范围值
                    if (Math.Abs(R - r1) < a && Math.Abs(G - g1) < a && Math.Abs(B - b1) < a)
                    {
                        alpha = 0;    //若两种颜色比较接近,透明度设为0
                    }
                    else
                    {
                        alpha = 255;
                    }
                    newbitmap.SetPixel(x, y, Color.FromArgb(alpha, r1, g1, b1));
                }
            }
            return newbitmap;
        }

        /// 
        /// 指定颜色替换
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static Bitmap ColorReplace(Bitmap bitmap, int R, int G, int B, int newR, int newG, int newB)
        {
            int iw = bitmap.Width, ih = bitmap.Height;
            Bitmap newbitmap = new Bitmap(iw, ih);
            Color pixel;
            for (int x = 0; x < iw; x++)
            {
                for (int y = 0; y < ih; y++)
                {
                    pixel = bitmap.GetPixel(x, y);
                    int r1 = pixel.R;
                    int g1 = pixel.G;
                    int b1 = pixel.B;
                    int a = 40;
                    if (Math.Abs(R - r1) < a && Math.Abs(G - g1) < a && Math.Abs(B - b1) < a)
                    {
                        newbitmap.SetPixel(x, y, Color.FromArgb(newR, newG, newB));
                    }
                    else
                    {
                        newbitmap.SetPixel(x, y, Color.FromArgb(r1, g1, b1));
                    }
                }
            }
            return newbitmap;
        }

        /// 
        /// 指定颜色保留(其余透明化)
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static Bitmap ColorRetain(Bitmap bitmap, int R, int G, int B)
        {
            // 色差值
            int difference = 40;
            int iw = bitmap.Width, ih = bitmap.Height;
            Bitmap newbitmap = new Bitmap(iw, ih);
            int alpha = 0;
            Color pixel;
            for (int x = 0; x < iw; x++)
            {
                for (int y = 0; y < ih; y++)
                {
                    pixel = bitmap.GetPixel(x, y);
                    int r1 = pixel.R;
                    int g1 = pixel.G;
                    int b1 = pixel.B;
                    if (Math.Abs(R - r1) < difference && Math.Abs(G - g1) < difference && Math.Abs(B - b1) < difference)
                    {
                        alpha = 0;
                        r1 = 0;
                        b1 = 0;
                        g1 = 0;
                    }
                    else
                    {
                        alpha = 255;
                        r1 = 255;
                        b1 = 255;
                        g1 = 255;
                    }
                    newbitmap.SetPixel(x, y, Color.FromArgb(alpha, r1, g1, b1));
                }
            }
            return newbitmap;
        }
    }

我的代码
https://download.csdn.net/download/qq_21703215/88055234

你可能感兴趣的:(c#,图像处理)