用C#制作图像处理软件及其原理
来源:互联网 发布:unity3d 虚幻4 知乎 编辑:程序博客网 时间:2024/05/16 02:06
其实我非常不赞成选用哪种语言开发哪个系统比较占优势, 哪个语言对于哪些问题的解决更得心应手, 这会让我们一方面花大量的时间与精力学好几门我们不一定喜欢(也许是非常抵触)的语言, 一方面养成了我们不去解决困难问题习惯, 我们更多的是找借口: "这种技术需要某某语言才能实现,我无法完成这个任务...!". 我想要说的是, 如果认为哪个技术非得要哪种特定的语言来实现, 只能说明我们的 "处世不深".
乔峰没有段誉的六脉神剑, 没有全真,玉女合壁的玉女素心剑, 没有梅超风的毒龙鞭法, 那些玄, 奇, 怪, 异武功他一样不会, 但他能成为武林第一除了有包容世 界的胸怀外, 更重要的是他把降龙十八掌运用的如火纯青。其实任何武功如果能掌握它的命门要术, 得其精髓, 都会天下无敌!
开源当然好, 如果不给开源呢? 没有关系, 任何语言, 只要功夫到家, 我们一样可以懂得任何软件或组件的运行机制, 只要时机成熟, 我们同样可以制作符合自己个性的软件或组件;因此别再抱怨你不会 java , 不会 C++, 不会 C#, 我要说的是, 你哪怕只会一个简单的 vb, 简单的 c, 照样可以制作您需要的东西.
语言没有最好的, 适合自己的就是最好的, 运用到如火纯青便是最好的.
PhotoShop 是一款最流行的图像处理软件, 已经相当成熟, 其中对图像显示效果的处理能力更牛B;用起来美得嘴里直流油, 遗憾的人家就是不开源, 没办法, 自已动手做一个? 呵呵, "一切皆有可能!"
这里我用 C# 制作了一款个性的图像处理软件, 列出主要功能的关键代码部分, 以示对开源支持.
一. 以底片效果显示图像
原理: 底片中的每个像素点颜色值都是正常图像对应像素点颜色值的取反( 满色为255).
( 关于颜色的相关技术请参考其他资料, 此处不补课 )
1.通过 GetPixel 方法获得图像中每一点像素的值,
2.再使用 SetPixel 方法将取反后的颜色值设置到对应的点...
try
{
int Height = this.pictureBox1.Image.Height;
int Width = this.pictureBox1.Image.Width;
Bitmap newbitmap =new Bitmap(Width, Height);
Bitmap oldbitmap = (Bitmap)this.pictureBox1.Image;
Color pixel;
for (int x= 1; x< Width; x++)
{
for (int y= 1; y< Height; y++)
{
int r, g, b;
pixel = oldbitmap.GetPixel(x, y);
r = 255 - pixel.R;
g = 255 - pixel.G;
b = 255 - pixel.B;
newbitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
}
}
this.pictureBox1.Image= newbitmap;
}
二. 以浮雕效果显示图像
原理: 通过对图像像素点的像素值与相邻像素点的像素值相减后加上128, 然后作为新的像素点的值...
try
{
int Height = this.pictureBox1.Image.Height;
int Width = this.pictureBox1.Image.Width;
Bitmap newBitmap =new Bitmap(Width, Height);
Bitmap oldBitmap = (Bitmap)this.pictureBox1.Image;
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 = oldBitmap.GetPixel(x, y);
pixel2 = oldBitmap.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));
}
}
this.pictureBox1.Image= newBitmap;
}
三. 以黑白效果显示图像
原理: 彩色图像处理成黑白效果时常用的三种方法. 1.最大值法, 2.平均值法, 3.加权平均值法..
平均值法为例: 是取每一个像素点颜色值的 红,绿, 蓝. 相加除以 3 做为该像素点新的颜色值
即: ((R + G + B) / 3) .
注: 每种方法产生黑白效果的最终效果不一样, 加权平均值法产生的黑白效果最 "真实" .
try
{
int Height = this.pictureBox1.Image.Height;
int Width = this.pictureBox1.Image.Width;
Bitmap newBitmap =new Bitmap(Width, Height);
Bitmap oldBitmap = (Bitmap)this.pictureBox1.Image;
Color pixel;
for (int x= 0; x< Width; x++)
for (int y= 0; y< Height; y++)
{
pixel = oldBitmap.GetPixel(x, y);
int r, g, b, Result= 0;
r = pixel.R;
g = pixel.G;
b = pixel.B;
//实例程序以加权平均值法产生黑白图像
int iType = 2 ;
switch (iType)
{
case 0://平均值法
Result = ((r+ g + b) /3);
break;
case 1://最大值法
Result = r> g ? r : g;
Result = Result> b ? Result : b;
break;
case 2://加权平均值法
Result = ((int)(0.7* r) + (int)(0.2* g) + (int)(0.1* b));
break;
}
newBitmap.SetPixel(x, y, Color.FromArgb(Result, Result, Result));
}
this.pictureBox1.Image= newBitmap;
}
四. 以柔化效果显示图像
原理: 当前像素点与周围像素点的颜色差距较大时取其平均值 ...算法: 高斯模板
try
{
int Height = this.pictureBox1.Image.Height;
int Width = this.pictureBox1.Image.Width;
Bitmap bitmap =new Bitmap(Width, Height);
Bitmap MyBitmap = (Bitmap)this.pictureBox1.Image;
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 = MyBitmap.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;
bitmap.SetPixel(x -1, y - 1, Color.FromArgb(r, g, b));
}
this.pictureBox1.Image= bitmap;
}
五. 以锐化效果显示图像
原理: 获取图像有关形体的边缘, 并突出显示.
try
{
int Height = this.pictureBox1.Image.Height;
int Width = this.pictureBox1.Image.Width;
newBitmap =new Bitmap(Width, Height);
// Bitmap newBitmap = new Bitmap(Width, Height);
Bitmap oldBitmap = (Bitmap)this.pictureBox1.Image;
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 = oldBitmap.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));
}
this.pictureBox1.Image= newBitmap;
}
六. 以雾化效果显示图像
原理: 图像的雾化处理不是基于图像中像素点之间的计算,而是给图像像素的颜色值引入一定的随机值, 使图像具有毛玻璃带水雾般的效果..
以雾化效果显示图像关键代码
try
{
int Height = this.pictureBox1.Image.Height;
int Width = this.pictureBox1.Image.Width;
Bitmap newBitmap =new Bitmap(Width, Height);
Bitmap oldBitmap = (Bitmap)this.pictureBox1.Image;
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 = oldBitmap.GetPixel(dx, dy);
newBitmap.SetPixel(x, y, pixel);
}
this.pictureBox1.Image= newBitmap;
}
七. 以光照效果显示图像
原理: 按照一定的规则对图像中某范围内像素的亮度进行处理后, 能够产生类似光照的效果...
Graphics MyGraphics =this.pictureBox1.CreateGraphics();
MyGraphics.Clear(Color.White);
Bitmap MyBmp =new Bitmap(this.pictureBox1.Image,this.pictureBox1.Width,this.pictureBox1.Height);
int MyWidth= MyBmp.Width;
int MyHeight= MyBmp.Height;
Bitmap MyImage = MyBmp.Clone(new RectangleF(0,0, MyWidth, MyHeight), System.Drawing.Imaging.PixelFormat.DontCare);
int A = Width /2;
int B = Height /2;
//MyCenter图片中心点,发亮此值会让强光中心发生偏移
Point MyCenter =new Point(MyWidth/ 2, MyHeight/ 2);
//R强光照射面的半径,即”光晕”
int R = Math.Min(MyWidth/ 2, MyHeight/ 2);
for (int i= MyWidth - 1; i>= 1; i--)
{
for (int j= MyHeight - 1; j>= 1; j--)
{
float MyLength= (float)Math.Sqrt(Math.Pow((i- MyCenter.X),2) + Math.Pow((j- MyCenter.Y),2));
//如果像素位于”光晕”之内
if (MyLength< R)
{
Color MyColor = MyImage.GetPixel(i, j);
int r, g, b;
//220亮度增加常量,该值越大,光亮度越强
float MyPixel= 220.0f* (1.0f- MyLength / R);
r = MyColor.R+ (int)MyPixel;
r = Math.Max(0, Math.Min(r,255));
g = MyColor.G+ (int)MyPixel;
g = Math.Max(0, Math.Min(g,255));
b = MyColor.B+ (int)MyPixel;
b = Math.Max(0, Math.Min(b,255));
//将增亮后的像素值回写到位图
Color MyNewColor = Color.FromArgb(255, r, g, b);
MyImage.SetPixel(i, j, MyNewColor);
}
}
//重新绘制图片
MyGraphics.DrawImage(MyImage, new Rectangle(0,0, MyWidth, MyHeight));
}
八. 以百叶效果显示图像
原理: 根据图像的高度或宽度和定制的百叶窗显示条宽度计算百叶窗显示的条目数量
try
{
MyBitmap = (Bitmap)this.pictureBox1.Image.Clone();
int dw = MyBitmap.Width / 30;
int dh = MyBitmap.Height;
Graphics g =this.pictureBox1.CreateGraphics();
g.Clear(Color.Gray);
Point[] MyPoint =new Point[30];
for (int x= 0; x< 30; x++)
{
MyPoint[x].Y =0;
MyPoint[x].X = x* dw;
}
Bitmap bitmap =new Bitmap(MyBitmap.Width, MyBitmap.Height);
for (int i= 0; i< dw; i++)
{
for (int j= 0; j< 30; j++)
{
for (int k= 0; k< dh; k++)
{
bitmap.SetPixel(MyPoint[j].X + i, MyPoint[j].Y+ k,
MyBitmap.GetPixel(MyPoint[j].X + i, MyPoint[j].Y+ k));
}
}
this.pictureBox1.Refresh();
this.pictureBox1.Image= bitmap;
System.Threading.Thread.Sleep(100);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示");
}
try
{
MyBitmap = (Bitmap)this.pictureBox1.Image.Clone();
int dh = MyBitmap.Height / 20;
int dw = MyBitmap.Width;
Graphics g =this.pictureBox1.CreateGraphics();
g.Clear(Color.Gray);
Point[] MyPoint =new Point[20];
for (int y= 0; y< 20; y++)
{
MyPoint[y].X =0;
MyPoint[y].Y = y* dh;
}
Bitmap bitmap =new Bitmap(MyBitmap.Width, MyBitmap.Height);
for (int i= 0; i< dh; i++)
{
for (int j= 0; j< 20; j++)
{
for (int k= 0; k< dw; k++)
{
bitmap.SetPixel(MyPoint[j].X + k, MyPoint[j].Y+ i, MyBitmap.GetPixel(MyPoint[j].X+ k, MyPoint[j].Y+ i));
}
}
this.pictureBox1.Refresh();
this.pictureBox1.Image= bitmap;
System.Threading.Thread.Sleep(100);
}
}
九. 马赛克效果显示图像
原理: 确定随机位置点和确定图像块的大小, 然后随机改变确定每一个图像块中像素的值...
try
{
int dw = MyBitmap.Width / 50;
int dh = MyBitmap.Height / 50;
Graphics g =this.pictureBox1.CreateGraphics();
g.Clear(Color.Gray);
Point[] MyPoint =new Point[2500];
for (int x= 0; x< 50; x++)
for (int y= 0; y< 50; y++)
{
MyPoint[x *50 + y].X= x * dw;
MyPoint[x *50 + y].Y= y * dh;
}
Bitmap bitmap =new Bitmap(MyBitmap.Width, MyBitmap.Height);
for (int i= 0; i< 10000; i++)
{
System.Random MyRandom =new Random();
int iPos = MyRandom.Next(2500);
for (int m= 0; m< dw; m++)
for (int n= 0; n< dh; n++)
{
bitmap.SetPixel(MyPoint[iPos].X + m, MyPoint[iPos].Y+ n, MyBitmap.GetPixel(MyPoint[iPos].X+ m, MyPoint[iPos].Y+ n));
}
this.pictureBox1.Refresh();
this.pictureBox1.Image= bitmap;
}
for (int i= 0; i< 2500; i++)
for (int m= 0; m< dw; m++)
for (int n= 0; n< dh; n++)
{
bitmap.SetPixel(MyPoint[i].X + m, MyPoint[i].Y+ n, MyBitmap.GetPixel(MyPoint[i].X+ m, MyPoint[i].Y+ n));
}
this.pictureBox1.Refresh();
this.pictureBox1.Image= bitmap;
}
十. 以油画效果显示图像
原理: 图像每个像素点所在的坐标值整除一个随机数, 记为iModel,然后将该点的RGB值设置成附近iModel点之内的任一点
Graphics g =this.panel1.CreateGraphics();
//Bitmap bitmap = this.MyBitmap;
//取得图片尺寸
int width = MyBitmap.Width;
int height = MyBitmap.Height;
RectangleF rect =new RectangleF(0,0, width, height);
Bitmap img = MyBitmap.Clone(rect, System.Drawing.Imaging.PixelFormat.DontCare);
//产生随机数序列
Random rnd =new Random();
//取不同的值决定油画效果的不同程度
int iModel = 2;
int i = width - iModel;
while (i > 1)
{
int j = height - iModel;
while (j > 1)
{
int iPos = rnd.Next(100000)% iModel;
//将该点的RGB值设置成附近iModel点之内的任一点
Color color = img.GetPixel(i+ iPos, j + iPos);
img.SetPixel(i, j, color);
j = j - 1;
}
i = i - 1;
}
//重新绘制图像
g.Clear(Color.White);
g.DrawImage(img, new Rectangle(0,0, width, height));
十一. 以扭曲效果显示图像
原理: 将每一个像素的 RGB 值加上它自身的坐标值即可
Size offset =new Size (w++,h++);//设置偏移量
Graphics g = panel1.CreateGraphics();
Rectangle rect =this.panel1.ClientRectangle;
Point[] points =new Point[3];
points[0] = new Point(rect.Left+offset.Width ,rect.Top+offset .Height);
points[1] = new Point(rect.Right, rect.Top+ offset.Height);
points[2] = new Point(rect.Left, rect.Bottom- offset.Height);
g.Clear(Color.White);
g.DrawImage(MyBitmap, points);
十二. 以积木效果显示图像
原理: Color 类的 FromArgb 方法定义一组颜色, 然后使用 Bitmap 对象的 SetPixel 方法为图像中的各像素点重新着色..
try
{
Graphics myGraphics =this.panel1.CreateGraphics ();
//Bitmap myBitmap = new Bitmap(this.BackgroundImage);
int myWidth, myHeight, i, j, iAvg, iPixel;
Color myColor, myNewColor;
RectangleF myRect;
myWidth = MyBitmap.Width;
myHeight = MyBitmap.Height;
myRect = new RectangleF(0,0, myWidth, myHeight);
Bitmap bitmap = MyBitmap.Clone(myRect, System.Drawing.Imaging.PixelFormat.DontCare);
i = 0;
while (i < myWidth -1)
{
j = 0;
while (j < myHeight - 1)
{
myColor = bitmap.GetPixel(i, j);
iAvg = (myColor.R+ myColor.G+ myColor.B)/ 3;
iPixel = 0;
if (iAvg >= 128)
iPixel = 255;
else
iPixel = 0;
myNewColor = Color.FromArgb(255, iPixel, iPixel, iPixel);
bitmap.SetPixel(i, j, myNewColor);
j = j + 1;
}
i = i + 1;
}
myGraphics.Clear(Color.WhiteSmoke);
myGraphics.DrawImage(bitmap, new Rectangle(0,0, myWidth, myHeight));
}
说明: 其实图像显示效果无法一一枚举.这里只列出最常用的12 种效果关键代码部分.
全部的代码演示会有后续补充.
这个图像处理软件是我和几个朋友在工作之余所做. 这里只贴出部分功能,
技术有限, 错误难免, 出错之处还望指出.
制作这个软件没有什么特别的目的, 只是心血来潮, 只是觉得好玩.
- 用C#制作图像处理软件及其原理
- 如何制作图像处理软件?
- mfc制作图像处理软件总结(一)
- 图像*处理软件
- 基于GDI+的图像处理软件制作(VC)
- 烈火图像处理软件 绿色
- HALCON图像处理软件
- Dalsa - 图像处理软件
- FreeImage的图像处理软件
- HALCON图像处理软件
- 图像处理软件介绍
- 五款开源图像处理软件
- Android 图像处理软件
- VC++图像处理软件
- 自己用C++写的图像处理软件试用版下载
- Sapera Processing 5.0 图像处理软件
- GIMP 2.6 GNU 图像处理软件
- Imatest图像处理软件 Imatest Master
- JS判断选择的时间是否大于当前时间
- 自己写的Base64编码
- js获取css属性
- 双代号网络图快速计算时差法
- Clojure 学习入门(16)—— 正则表达式
- 用C#制作图像处理软件及其原理
- 如何清理AWR数据
- 常用收藏网站
- Struts2的注解功能
- Linux内核spin_lock 要防止该锁在中断处理程序调用
- Android server启动过程源代码分析
- Linux命令之定位与查找
- 为RMI实现类Jini的发现机制
- Silverlight利用Application_Startup获取web项目中的初始化参数