/// <summary> /// 將每點像素色彩轉換成灰階值 /// </summary> public void ConvertGrayByPixels(int mode = 0) { if (mode == 0) { for (int i = 0; i < BmpSource.Height; i++) { for (int j = 0; j < BmpSource.Width; j++) { int grayValue = GetGrayValue(BmpSource.GetPixel(j, i)); BmpSource.SetPixel(j, i, Color.FromArgb(grayValue, grayValue, grayValue)); } } } else if (mode == 1) { for (int x = 0; x < BmpSource.Width; x++) { for (int y = 0; y < BmpSource.Height; y++) { Color color = BmpSource.GetPixel(x, y); int gray = (color.R + color.G + color.B) / 3; BmpSource.SetPixel(x, y, Color.FromArgb(gray, gray, gray)); } } } }
/// <summary> /// 將每點像素色彩轉換成相反顏色 /// </summary> public void InvertColorByPixels() { for (int x = 0; x < BmpSource.Width; x++) { for (int y = 0; y < BmpSource.Height; y++) { Color color = BmpSource.GetPixel(x, y); BmpSource.SetPixel(x, y, Color.FromArgb(255 - color.R, 255 - color.G, 255 - color.B)); } } }
/// <summary> /// 清除邊框處理 /// </summary> /// <param name="borderWidth"></param> public void ClearPictureBorder(int pBorderWidth) { for (int i = 0; i < BmpSource.Height; i++) { for (int j = 0; j < BmpSource.Width; j++) { if (i < pBorderWidth || j < pBorderWidth || j > BmpSource.Width - 1 - pBorderWidth || i > BmpSource.Height - 1 - pBorderWidth) { BmpSource.SetPixel(j, i, Color.FromArgb(255, 255, 255)); } } } }
/// <summary> /// 噪音線處理 /// </summary> public void RemoteNoiseLineByPixels() { for (int i = 0; i < BmpSource.Height; i++) { for (int j = 0; j < BmpSource.Width; j++) { int grayValue = BmpSource.GetPixel(j, i).R; if (grayValue <= 255 && grayValue >= 160) { BmpSource.SetPixel(j, i, Color.FromArgb(255, 255, 255)); } } } }
/// <summary> /// 將每點像素色彩對比調整 /// </summary> public void ConvertGrayToBlackByPixels(int value = 160) { for (int x = 0; x < BmpSource.Width; x++) { for (int y = 0; y < BmpSource.Height; y++) { Color color = BmpSource.GetPixel(x, y); if (color.R < value || color.G < value || color.B < value) { BmpSource.SetPixel(x, y, Color.FromArgb(0, 0, 0)); } else { BmpSource.SetPixel(x, y, Color.FromArgb(255, 255, 255)); } } } }
/// <summary> /// 噪音點處理 /// </summary> public void RemoteNoisePointByPixels() { List <NoisePoint> points = new List <NoisePoint>(); for (int k = 0; k < 5; k++) { for (int i = 0; i < BmpSource.Height; i++) { for (int j = 0; j < BmpSource.Width; j++) { int flag = 0; int garyVal = 255; // 檢查上相鄰像素 if (i - 1 > 0 && BmpSource.GetPixel(j, i - 1).R != garyVal) { flag++; } if (i + 1 < BmpSource.Height && BmpSource.GetPixel(j, i + 1).R != garyVal) { flag++; } if (j - 1 > 0 && BmpSource.GetPixel(j - 1, i).R != garyVal) { flag++; } if (j + 1 < BmpSource.Width && BmpSource.GetPixel(j + 1, i).R != garyVal) { flag++; } if (i - 1 > 0 && j - 1 > 0 && BmpSource.GetPixel(j - 1, i - 1).R != garyVal) { flag++; } if (i + 1 < BmpSource.Height && j - 1 > 0 && BmpSource.GetPixel(j - 1, i + 1).R != garyVal) { flag++; } if (i - 1 > 0 && j + 1 < BmpSource.Width && BmpSource.GetPixel(j + 1, i - 1).R != garyVal) { flag++; } if (i + 1 < BmpSource.Height && j + 1 < BmpSource.Width && BmpSource.GetPixel(j + 1, i + 1).R != garyVal) { flag++; } if (flag < 3) { points.Add(new NoisePoint() { X = j, Y = i }); } } } foreach (NoisePoint point in points) { BmpSource.SetPixel(point.X, point.Y, Color.FromArgb(255, 255, 255)); } } }