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

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

在这里插入图片描述

    /// <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://加权平均值法1Result = ((int)(0.7 * r) + (int)(0.2 * g) + (int)(0.1 * b));break;case 3://加权平均值法2Result = ((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)处的象素,赋于数组dtdt[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);      //h2else 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}},//kir0kir1 =  {{-3, 5, 5},{-3, 0, 5},{-3,-3,-3}},//kir1kir2 = {{-3,-3, 5},{-3, 0, 5},{-3,-3, 5}},//kir2kir3 = {{-3,-3,-3},{-3, 0, 5},{-3, 5, 5}},//kir3kir4 = {{-3,-3,-3},{-3, 0,-3},{ 5, 5, 5}},//kir4kir5 = {{-3,-3,-3},{ 5, 0,-3},{ 5, 5,-3}},//kir5kir6 = {{ 5,-3,-3},{ 5, 0,-3},{ 5,-3,-3}},//kir6kir7 = {{ 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.mzph.cn/news/4138.shtml

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

相关文章

为什么项目可见性难以实现?该如何提高?

在项目和专业服务管理中&#xff0c;失败有时难以避免。沟通不足和需求定义不明确被认为是造成失败的最大原因&#xff0c;这意味着项目可见性和信息流动至关重要。 什么是项目可见性&#xff1f; 项目可见性是组织项目相关信息的方式&#xff0c;以便所有团队成员、项目经理…

LeetCode 75 第五题(345)反转字符串中的元音字母

题目: 示例: 分析: 给一个字符串,将里面的元音字母反转,并且保持非元音字母不变(包括顺序). 字符串反转类型的题,我们都可以使用双指针来解决:定义首尾指针,分别向中间靠拢,直到首尾指针都指向了元音字母,然后交换首尾指针所指的字母,如此不会影响到非元音字母,同时也将元音字…

pytest实现用例间参数传递的方式

pytest实现用例间参数传递的方式 一、通过conftest创建全局变量二、使用tmpdir_factory方法 我们在做接口自动化测试的时候&#xff0c;会经常遇到这种场景&#xff1a;接口A的返回结果中的某个字段&#xff0c;是接口B的某个字段的入参。如果是使用postman&#xff0c;那我们可…

CSS:给子元素设置了浮动,页面缩放的时候,子元素往下掉

前言 给子元素设置了浮动&#xff0c;页面缩放的时候&#xff0c;子元素往下掉 html代码&#xff1a; <div class"father"><div class"child1"></div><div class"child2"></div> </div>css代码 .child1…

Spring Batch之读数据库——JdbcCursorItemReader之使用框架提供的BeanPropertyRowMapper(三十六)

一、BeanPropertyRowMapper介绍 参考我的另一篇博客&#xff1a; Spring Batch之读数据库——JdbcCursorItemReader&#xff08;三十五&#xff09;_人……杰的博客-CSDN博客 二、项目实例 1.项目框架 2.代码实现 BatchMain.java: package com.xj.demo27;import org.spri…

中金:龙湖基本面稳健,股价超跌具备配置价值

恒大2.4万亿元的天量债务爆出后&#xff0c;让本就信心不足的房地产行业&#xff0c;越发雪上加霜&#xff0c;房企股价遭遇集体下挫&#xff0c;业内公认的万科、龙湖、保利、中海等“优等生”也不免被波及。多家证券机构提醒&#xff0c;行业预期降至冰点的情况下&#xff0c…

oc基本控件2

// // ViewController.m // OcDemoTest // // Created by Mac on 2023/7/14. //#import "ViewController.h"interface ViewController () // label property (weak, nonatomic) IBOutlet UIImageView *imageView; // Use of undeclared identifier // 全局propert…

创建型模式

创建型模式&#xff08;Creational Pattern&#xff09;关注对象的创建过程&#xff0c;是一类最常用的设计模式&#xff0c;在软件开发中应用非常广泛。创建型模式将对象的创建和使用分离&#xff0c;在使用对象时无须关心对象的创建细节&#xff0c;从而降低系统的耦合度&…

学堂在线数据结构(上)(2023春)邓俊辉 课后题

The reverse number of a sequence is defined as the total number of reversed pairs in the sequence, and the total number of element comparisons performed by the insertion sort in the list of size n is: 一个序列的逆序数定义为该序列中的逆序对总数&#xff0c;…

pandas 笔记:pivot_table 数据透视表\pivot

1 基本使用方法 pandas.pivot_table(data, valuesNone, indexNone, columnsNone, aggfuncmean, fill_valueNone, marginsFalse, dropnaTrue, margins_nameAll, observedFalse, sortTrue)2 主要参数 dataDataFramevalues要进行聚合的列index在数据透视表索引&#xff08;index…

自动驾驶代客泊车AVP摄像头与ECU交互需求规范

目录 1 文档范围及控制方法... 5 1.1 目的.... 5 1.2 文档授权... 5 1.3 文档变更管理... 5 1.4 缩写.... 5 1.5 术语.... 5 2 系统组成... 6 2.1 系统框图... 6 2.2 电源供应和时序要求... 7 2.2.1 摄像头供电控制... 7 2.2.2 摄像头上电时序要求…

【产品经理】TO B市场分析

市场分析是一个独立而又宏大的学科领域&#xff0c;并且具体使用中&#xff0c;目标和个体不同&#xff0c;分析的方式方法也不同。TO B产品的市场分析是对市场环境、市场规模、性质、特征、竞品进行分析&#xff0c;从而寻找和研究潜在需求的市场机会&#xff0c;帮助产品经理…

[洛谷]P8662 [蓝桥杯 2018 省 AB] 全球变暖(dfs)

读题不规范&#xff0c;做题两年半&#xff01; 注意&#xff1a;被海水淹没后的陆地应用另一个字符表示&#xff0c;而不是把它变为海洋&#xff0c;这个点可以便利&#xff0c;但不能被当作起点&#xff0c;不然就只有 36 分。 ACocde: #include<bits/stdc.h> using…

浅谈无人机遥感图像拼接与处理方法

遥感&#xff08;RS-Remote Sensing&#xff09;——不接触物体本身&#xff0c;用传感器收集目标物的电磁波信息&#xff0c;经处理、分析后&#xff0c;识别目标物&#xff0c;揭示其几何、物理性质和相互关系及其变化规律的现代科学技术。 换言之&#xff0c;即是“遥远的感…

opencv -13 掩模

什么是掩膜&#xff1f; 在OpenCV中&#xff0c;掩模&#xff08;mask&#xff09;是一个与图像具有相同大小的二进制图像&#xff0c;用于指定哪些像素需要进行操作或被考虑。掩模通常用于选择特定区域或进行像素级别的过滤操作。 OpenCV 中的很多函数都会指定一个掩模&…

2. CSS3的新特性

2.1 CSS3的现状 ●新增的CSS3特性有兼容性问题, ie9才支持 ●移动端支持优于PC端 ●不断改进中 ●应用相对广泛 ●现阶段主要学习: 新增选择器和盒子模型以及其他特性 CSS3给我们新增了选择器,可以更加便捷,更加自由的选择目标元素&#xff1a; 1.属性选择器 2.结构伪类选择器…

电子病历编辑器源码

电子病历系统采取结构化与自由式录入的新模式&#xff0c;自由书写&#xff0c;轻松录入。化实现病人医疗记录&#xff08;包含有首页、病程记录、检查检验结果、医嘱、手术记录、护理记录等等。&#xff09;的保存、管理、传输和重现&#xff0c;取代手写纸张病历。不仅实现了…

朝花夕拾 - 2023 精神错乱记录

jsliang 的精神错乱记录&#xff0c;一点 2023 小思考。 也许我们曾偏离航道&#xff0c;但请不要放弃抵达终点 前言 在 2020.11 过来珠海&#xff0c;来到金山工作 2 年半的时间里&#xff1a; 在工作上&#xff0c;更换了 3 个小团队&#xff0c;达到了每年一换在工作上&…

autok3s k3d rancher研究

参考 功能介绍 | Rancher文档AutoK3s 是用于简化 K3s 集群管理的轻量级工具&#xff0c;您可以使用 AutoK3s 在任何地方运行 K3s 服务。http://docs.rancher.cn/docs/k3s/autok3s/_index 什么是 AutoK3s k3s是经过完全认证的 Kubernetes 产品&#xff0c;在某些情况下可以替…

linux之Ubuntu系列(四)用户管理 用户和权限 chmod 超级用户root, R、W、X、T、S 软链接和硬链接 shell

r(Read&#xff0c;读取)&#xff1a;对文件而言&#xff0c;具有读取文件内容的权限&#xff1b;对目录来说&#xff0c;具有浏览目 录的权限。 w(Write,写入)&#xff1a;对文件而言&#xff0c;具有新增、修改文件内容的权限&#xff1b;对目录来说&#xff0c;具有删除、移…