C#图片处理

news2024/11/26 0:40:30

查找图片所在位置


原理:使用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

界面效果
在这里插入图片描述
在这里插入图片描述
数字提取
在这里插入图片描述
网上找的一张游戏界面图

在这里插入图片描述

    /// <summary>
    /// 图像处理转换
    /// </summary>
    public class ImgConvert
    {
        /// <summary>
        /// 图片脱色(灰度)
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="Case"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 图片暗角
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 加马赛克
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 底片
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 浮雕
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 图片柔化
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 图片锐化
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 图片雾化
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 翻转
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        public static Bitmap TurnOver(Bitmap bitmap)
        {
            Bitmap newbitmap = bitmap.Clone() as Bitmap;
            newbitmap.RotateFlip(RotateFlipType.Rotate90FlipNone);
            return newbitmap;
        }

        /// <summary>
        /// 直方图均衡化
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        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;
        }

        / <summary>
        / 对比度扩展
        / </summary>
        / <param name="bitmap"></param>
        / <returns></returns>
        //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;
        //}

        /// <summary>
        /// 3 X 3 阈值滤波
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 均值滤波
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 中值滤波
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="n"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 3×3 低通滤波
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// Kirsch锐化
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// Laplace锐化
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// Prewitt锐化
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// Roberts锐化
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// Sobel锐化
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 透明化
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 指定颜色透明化
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="R"></param>
        /// <param name="G"></param>
        /// <param name="B"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 指定颜色替换
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="R"></param>
        /// <param name="G"></param>
        /// <param name="B"></param>
        /// <param name="newR"></param>
        /// <param name="newG"></param>
        /// <param name="newB"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 指定颜色保留(其余透明化)
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="R"></param>
        /// <param name="G"></param>
        /// <param name="B"></param>
        /// <returns></returns>
        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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/764862.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【LeetCode热题100】哈希篇

两数之和 给定一个整数数组 nums 和一个整数目标值 target&#xff0c;请你在该数组中找出 和为目标值 target 的那 两个 整数&#xff0c;并返回它们的数组下标。 你可以假设每种输入只会对应一个答案。但是&#xff0c;数组中同一个元素在答案里不能重复出现。 你可以按任…

短视频抖音账号矩阵系统源码开发分享

引用&#xff1a;MySQL数据库&#xff0c;NGINX&#xff0c;PHP7.4&#xff0c;MySQL5.7&#xff0c;redis 媒体组件 组件 描述 image 图片 图片。支持 JPG、PNG、SVG、WEBP、GIF 等格式。 video 视频 视频组件。相关 API 请参考 tt.createVideoContext。 开发背景&…

Fiddler的使用方法介绍

FIDDLER下载地址fiddler Fiddler可以帮您记录&#xff0c;调试Microsoft Internet Explorer与Web应用程序的交互&#xff0c;找到Web程序运行性能的瓶颈&#xff0c;还有如查看向Web服务器发送cookies的内容&#xff0c;下载内容的大小等功能。 说多一点是&#xff0c;Fiddler站…

docker-compose部署sentinel-dashboard-1.8.5

一、创建文件夹 mkdir -p /docker/alibaba/sentinel/{config,data,logs} 二、拷贝jar包进sentinel目录下 三、Dockerfile文件 FROM openjdk:8-jre MAINTAINER yh COPY ./sentinel-dashboard.jar /app.jar EXPOSE 8718 ENTRYPOINT ["java", "-jar", &quo…

【Linux C】fseek函数使用小结

0x00 前言 演示使用的Linux版本&#xff08;#cat /etc/issue&#xff09;&#xff1a;Ubuntu 18.04.6 LTS \n \l 最后更新日期&#xff1a;2023.7.17 0x01 fseek函数使用小结 1.函数描述 设置stream文件的位置指针偏移到指定位置1。 2.声明 #include <stdio.h> in…

LVS—Linux Virtual Server

集群和分布式 系统性能拓展方式&#xff1a; Scale UP&#xff1a;垂直拓展&#xff0c;提高单个服务器的性能&#xff0c;如增加CPU、内存等&#xff0c;单台服务器的性能是有上限的&#xff0c;不可能无限制垂直拓展。Scale Out&#xff1a;水平拓展&#xff0c;增加设备&a…

用Python采集电商平台商品数据进行可视化分析

目录标题 前言环境使用:模块使用:基本流程思路:代码展示获取数据扩展知识数据可视化 尾语 前言 嗨喽~大家好呀&#xff0c;这里是魔王呐 ❤ ~! 环境使用: python 3.8 解释器 pycharm 编辑器 模块使用: 第三方模块 需要安装 requests —> 发送 HTTP请求 内置模块 不需…

嵌入式处理器常用存储介质

一、存储器 二、XIP设备 XIP(excute in place)&#xff0c;本地执行。像上述nor flash这种设备&#xff0c;上电后不需要初始化&#xff0c;CPU通过地址总线和数据总线直连的&#xff0c;可以直接访问并取址执行&#xff0c;称之为XIP设备。 像emmc这种&#xff0c;在SOC内部有…

JVM打印GC信息

-XX:PrintGCDetails 一、代码 public class GcDemo {public static void main(String[] args) {Object obj new Object();obj null;System.gc();} } 二、配置 三、测试 [GC (System.gc()) [PSYoungGen: 5242K->872K(152576K)] 5242K->880K(500736K), 0.0011529 sec…

如何使用 Amazon Systems Manager 集中管理 Amazon IoT Greengrass 设备

对于边缘设备管理员来说&#xff0c;远程管理大量不同的系统和应用程序会是一项富有挑战性的任务。Amazon IoT Greengrass 可帮助这些系统管理员管理其边缘设备应用程序堆栈。不过&#xff0c;这些设备上的系统软件必须通过与其大型 IT 企业的运营策略一致的运营策略来单独更新…

文件共享服务器

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 目录 前言 一、概述 二、FTP连接类型 三、端口 四、工作模式 五、安装配置 1、 服务名 2、主配置文件 3、 用户控制文件 4、 用户验证类型 5、 安装 6、 匿名用户验证 7、本…

python操作Elasticsearch数据库

Elasticsearch&#xff08;ES&#xff09;,ES是一个开源的高扩展的分布式全站搜索引擎&#xff0c;是整个Elastic Stack技术栈的核心。它可以近乎实时的存储、检索数据&#xff1b;本身扩展性很好&#xff0c;可以扩展到上百台服务器&#xff0c;处理PB级别的数据。 安装好ES之…

go mod 设置国内源 windows 环境 win10

启用 go module 功能 go env -w GO111MODULEon 配置 goproxy 变量 go env -w GOPROXYhttps://goproxy.cn,direct 下载包就行了&#xff0c;速度飞快 go mod tidy 检测 goproxy 是否配置好 运行 go env | findstr goproxy 查看 goproxy Go module 从 Go v1.12 版本开始存在&a…

springboot3+vue3 前后端分离项目模板

更好的阅读体验 \color{red}{更好的阅读体验} 更好的阅读体验 项目地址 功能简介-视频演示 功能概要 支持登录和注册功能。支持密码找回和邮箱验证。支持“记住我”功能。包含完整的上述功能的基础页面。 登录功能 支持“用户名/密码”登录。登录可选“记住我”&#xff0c;勾…

域名+云服务器+springBoo:怎样零成本进行云服务器的学习

零成本进行云服务器的学习 1.项目开发需要学习云服务器的使用 2.申请域名 为了让服务器运行我的springBoot项目&#xff0c;我们首先需要一个域名&#xff0c;很多网站提供免费域名的使用申请&#xff0c;我在阿里云上申请了一年域名的体验。 3.将域名绑定到三丰云&#xff0c…

WCF的IIS宿主部署

IIS的web服务开启网上已经很多&#xff0c;不再赘述。 关键点&#xff1a;WCF发布的文件夹要加上IIS的权限能够读取。否则报没有权限。

动态规划——删除并获得点数

题目链接 leetcode在线oj题——删除并获得点数 题目描述 给你一个整数数组 nums &#xff0c;你可以对它进行一些操作。 每次操作中&#xff0c;选择任意一个 nums[i] &#xff0c;删除它并获得 nums[i] 的点数。之后&#xff0c;你必须删除 所有 等于 nums[i] - 1 和 nums…

SQL性能分析——执行频率、慢查询日志、profile详情

1.SQL的执行频率 2.慢查询日志 show variables like slow_query_log;修改完成以后&#xff0c;再次查询以后发现&#xff0c;slow_query_log的开关时开着的。 3.profile详情

BGP实验--联邦

1&#xff0c;AS1存在两个环回&#xff0c;一个地址为192.168.1.0/24 该地址不嫩在任何协议中宣告 AS3中存在两个环回&#xff0c;一个地址为192.168.2.0/24该地址不能再任何协议中宣告&#xff0c;最终要求这两个环回可用互相通讯 AS1中的另一个环回为10.0.0.0 24&#…

在 Linux 系统上下载 Android SDK

使用ubuntu系统进行车机开发&#xff0c;今天开始配置环境&#xff0c;首先是下载android studio&#xff0c;然后下载android sdk&#xff0c;这里需要注意的是linux系统不能使用windows系统下的Android sdk&#xff0c;亲测会出现各种问题。 常规思路&#xff0c;下载sdk&am…