using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Collections;namespace DigitalImageProcess { public class Process { /// <summary> /// 改进的中值滤波算法去噪点 /// </summary> /// <param name="image">要处理的图片</param> /// <param name="FilterSize">滤镜大小FilterSize * FilterSize,即半径</param> /// <returns>处理后的图象</returns> public static Image MedianProcess(Image image, int FilterSize) { int Height = image.Height; int Width = image.Width; Bitmap bitmap = new Bitmap(Width, Height); Bitmap MyBitmap = (Bitmap)image; int x, y; ArrayList al = new ArrayList(); int numofpixel = FilterSize; int square = numofpixel * numofpixel; int s; int i, j; //--!!!!!!!!!!!!!!下面开始窗口为n×n中值滤波!!!!!!!!!!!!!!!! //在中值滤波基础上加以改进,再进行象素点替换时,判断是否为噪点。 //判断方法为:一般噪点的和边缘点都是灰度剧变的象素,所以中值滤镜会把部分边缘点都进行改变 // 但是噪点基本都是领域象素的极大值或极小值,而边缘不是,所以在替换之前,先判断 // 该点是不是领域象素的极值,如果是,证明此为噪点,否则不是。这样能去除突发性噪点 // 减小替换边缘点和其他象素点的可能。 for (y = 0; y < bitmap.Height ; y++) { for (x = 0; x < bitmap.Width ; x++) { al.Clear(); for (i = numofpixel / -2; i <= numofpixel / 2; i++) { for (j = numofpixel / -2; j <= numofpixel / 2; j++) { if ( x + i > -1 && y + j > -1 && x + i < Width && y + j < Height) al.Add(MyBitmap.GetPixel(x + i, y + j).ToArgb()); } } if (al.Count < square) { //对不足滤镜窗口的大小的象素,把原来的象素写回 bitmap.SetPixel(x, y, MyBitmap.GetPixel(x, y)); } else { //足够构成滤镜大小的,进行中值滤波。 int oldcolor = (int)al[square / 2];
for (j = 0; j < square; j++) //冒泡排序,计算中值 { for (i = j + 1; i < square; i++) { if ((int)al[j] > (int)al[i]) { s = (int)al[j]; al[j] = (int)al[i]; al[i] = s; } } }
if (oldcolor == (int)al[0] || oldcolor == (int)al[square - 1]) bitmap.SetPixel(x, y, Color.FromArgb((int)al[square / 2])); //给有效值付中值 else bitmap.SetPixel(x, y, Color.FromArgb(oldcolor)); } } } return (Image)bitmap; } /// <summary> /// 图像的逆反处理算法 /// </summary> /// <param name="image">要处理的图象</param> /// <returns>处理后的图象</returns> public static Image ReverseProcess(Image image) { int Height = image.Height; int Width = image.Width; Bitmap bitmap = new Bitmap(Width, Height); Bitmap MyBitmap = (Bitmap)image; int x, y; Color pixel; for (x = 0; x < Width; x++) { for (y = 0; y < Height; y++) { pixel = MyBitmap.GetPixel(x, y); int r = pixel.R; int g = pixel.G; int b = pixel.B; r = 256 - r; g = 256 - g; b = 256 - b; if (r > 255) r = 255; if (g > 255) g = 255; if (b > 255) b = 255; bitmap.SetPixel(x, y, Color.FromArgb(r, g, b)); } } return (Image)bitmap; } /// <summary> /// 图像的平滑处理 /// </summary> /// <param name="image">要处理的图象</param> /// <param name="FilterSize">滤镜大小,半径</param> /// <returns>处理后的图象</returns> public static Image BlurProcess(Image image, int FilterSize) { int Height = image.Height; int Width = image.Width; Bitmap bitmap = new Bitmap(Width, Height); Bitmap MyBitmap = (Bitmap)image; int x, y; ArrayList alr = new ArrayList(); ArrayList alg = new ArrayList(); ArrayList alb = new ArrayList(); int numofpixel = FilterSize; int square = numofpixel * numofpixel; for (x = 0; x < Width; x++) { for (y = 0; y < Height; y++) { alb.Clear(); alr.Clear(); alg.Clear(); for (int i = numofpixel / -2; i <= numofpixel / 2; i++) { for (int j = numofpixel / -2; j <= numofpixel / 2; j++) { if (x + i > -1 && y + j > -1 && x + i < Width && y + j < Height) { alr.Add((int)MyBitmap.GetPixel(x + i, y + j).R); alg.Add((int)MyBitmap.GetPixel(x + i, y + j).G); alb.Add((int)MyBitmap.GetPixel(x + i, y + j).B); } } } if (alr.Count < square) { //对不足滤镜窗口的大小的象素,把原来的象素写回 bitmap.SetPixel(x, y, MyBitmap.GetPixel(x, y)); } else { int avgr = 0, avgg = 0, avgb = 0; for (int i = 0; i < square; i++) { avgr += (int)alr[i]; avgg += (int)alg[i]; avgb += (int)alb[i]; } avgr /= square; avgg /= square; avgb /= square; bitmap.SetPixel(x, y, Color.FromArgb(avgr, avgg, avgb)); } } } return (Image)bitmap; }
旋转 private void InitializeBitmap() { try { bitmap1 = (Bitmap)Bitmap.FromFile(@"C:\Documents and Settings\" + @"All Users\Documents\My Music\music.bmp"); PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize; PictureBox1.Image = bitmap1; } catch(System.IO.FileNotFoundException) { MessageBox.Show("There was an error." + "Check the path to the bitmap."); } }private void Button1_Click(System.Object sender, System.EventArgs e) { if (bitmap1 != null) { bitmap1.RotateFlip(RotateFlipType.Rotate180FlipY); PictureBox1.Image = bitmap1; }}
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Collections;namespace DigitalImageProcess
{
public class Process
{
/// <summary>
/// 改进的中值滤波算法去噪点
/// </summary>
/// <param name="image">要处理的图片</param>
/// <param name="FilterSize">滤镜大小FilterSize * FilterSize,即半径</param>
/// <returns>处理后的图象</returns>
public static Image MedianProcess(Image image, int FilterSize)
{
int Height = image.Height;
int Width = image.Width;
Bitmap bitmap = new Bitmap(Width, Height);
Bitmap MyBitmap = (Bitmap)image;
int x, y;
ArrayList al = new ArrayList();
int numofpixel = FilterSize;
int square = numofpixel * numofpixel;
int s;
int i, j; //--!!!!!!!!!!!!!!下面开始窗口为n×n中值滤波!!!!!!!!!!!!!!!!
//在中值滤波基础上加以改进,再进行象素点替换时,判断是否为噪点。
//判断方法为:一般噪点的和边缘点都是灰度剧变的象素,所以中值滤镜会把部分边缘点都进行改变
// 但是噪点基本都是领域象素的极大值或极小值,而边缘不是,所以在替换之前,先判断
// 该点是不是领域象素的极值,如果是,证明此为噪点,否则不是。这样能去除突发性噪点
// 减小替换边缘点和其他象素点的可能。
for (y = 0; y < bitmap.Height ; y++)
{
for (x = 0; x < bitmap.Width ; x++)
{
al.Clear();
for (i = numofpixel / -2; i <= numofpixel / 2; i++)
{
for (j = numofpixel / -2; j <= numofpixel / 2; j++)
{
if ( x + i > -1 && y + j > -1 && x + i < Width && y + j < Height)
al.Add(MyBitmap.GetPixel(x + i, y + j).ToArgb());
}
}
if (al.Count < square)
{ //对不足滤镜窗口的大小的象素,把原来的象素写回
bitmap.SetPixel(x, y, MyBitmap.GetPixel(x, y));
}
else
{
//足够构成滤镜大小的,进行中值滤波。
int oldcolor = (int)al[square / 2];
for (j = 0; j < square; j++) //冒泡排序,计算中值
{
for (i = j + 1; i < square; i++)
{
if ((int)al[j] > (int)al[i])
{
s = (int)al[j];
al[j] = (int)al[i];
al[i] = s;
}
}
}
if (oldcolor == (int)al[0] || oldcolor == (int)al[square - 1])
bitmap.SetPixel(x, y, Color.FromArgb((int)al[square / 2])); //给有效值付中值
else
bitmap.SetPixel(x, y, Color.FromArgb(oldcolor));
}
}
}
return (Image)bitmap;
} /// <summary>
/// 图像的逆反处理算法
/// </summary>
/// <param name="image">要处理的图象</param>
/// <returns>处理后的图象</returns>
public static Image ReverseProcess(Image image)
{
int Height = image.Height;
int Width = image.Width;
Bitmap bitmap = new Bitmap(Width, Height);
Bitmap MyBitmap = (Bitmap)image;
int x, y;
Color pixel;
for (x = 0; x < Width; x++)
{
for (y = 0; y < Height; y++)
{
pixel = MyBitmap.GetPixel(x, y);
int r = pixel.R;
int g = pixel.G;
int b = pixel.B; r = 256 - r;
g = 256 - g;
b = 256 - b; if (r > 255)
r = 255;
if (g > 255)
g = 255;
if (b > 255)
b = 255; bitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
}
}
return (Image)bitmap;
} /// <summary>
/// 图像的平滑处理
/// </summary>
/// <param name="image">要处理的图象</param>
/// <param name="FilterSize">滤镜大小,半径</param>
/// <returns>处理后的图象</returns>
public static Image BlurProcess(Image image, int FilterSize)
{
int Height = image.Height;
int Width = image.Width;
Bitmap bitmap = new Bitmap(Width, Height);
Bitmap MyBitmap = (Bitmap)image;
int x, y;
ArrayList alr = new ArrayList();
ArrayList alg = new ArrayList();
ArrayList alb = new ArrayList();
int numofpixel = FilterSize;
int square = numofpixel * numofpixel; for (x = 0; x < Width; x++)
{
for (y = 0; y < Height; y++)
{
alb.Clear();
alr.Clear();
alg.Clear();
for (int i = numofpixel / -2; i <= numofpixel / 2; i++)
{
for (int j = numofpixel / -2; j <= numofpixel / 2; j++)
{
if (x + i > -1 && y + j > -1 && x + i < Width && y + j < Height)
{
alr.Add((int)MyBitmap.GetPixel(x + i, y + j).R);
alg.Add((int)MyBitmap.GetPixel(x + i, y + j).G);
alb.Add((int)MyBitmap.GetPixel(x + i, y + j).B);
}
}
}
if (alr.Count < square)
{ //对不足滤镜窗口的大小的象素,把原来的象素写回
bitmap.SetPixel(x, y, MyBitmap.GetPixel(x, y));
}
else
{
int avgr = 0, avgg = 0, avgb = 0;
for (int i = 0; i < square; i++)
{
avgr += (int)alr[i];
avgg += (int)alg[i];
avgb += (int)alb[i];
}
avgr /= square;
avgg /= square;
avgb /= square; bitmap.SetPixel(x, y, Color.FromArgb(avgr, avgg, avgb)); }
}
} return (Image)bitmap;
}
private void InitializeBitmap()
{
try
{
bitmap1 = (Bitmap)Bitmap.FromFile(@"C:\Documents and Settings\" +
@"All Users\Documents\My Music\music.bmp");
PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
PictureBox1.Image = bitmap1;
}
catch(System.IO.FileNotFoundException)
{
MessageBox.Show("There was an error." +
"Check the path to the bitmap.");
}
}private void Button1_Click(System.Object sender, System.EventArgs e)
{ if (bitmap1 != null)
{
bitmap1.RotateFlip(RotateFlipType.Rotate180FlipY);
PictureBox1.Image = bitmap1;
}}
Image img = Image.FromFile("F:\\test.jpg");
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawImage(img, 0, 0,100,100);//原图
GraphicsState state=g.Save();
g.TranslateTransform(100, 100);
g.DrawImage(img, 0, 0,100,100);//平移副本
g.Restore(state);
g.TranslateTransform(100, 200);
g.RotateTransform(45);
g.DrawImage(img, 0, 0,100,100);//旋转45度副本
g.Restore(state);
}