资源描述:
《c#图像预处理灰度化和二值化,以及c#图像处理方法》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、c#图像预处理灰度化和二值化,以及c#图像处理方法usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespace_2图像灰度化/////////////////////////////////采用加权平均值法ret=(int)(
2、curColor.R*0.299+curColor.G*0.587+curColor.B*0.114);{publicpartialclassForm1:Form{privatestringcur;//文件位置privateSystem.Drawing.BitmapcurBitmap;//图像对象privateHiPerfTimermyTimer;publicForm1(){InitializeComponent();myTimer=newHiPerfTimer();}privatevoidForm1_Load(o
3、bjectsender,EventArgse){}privatevoidopen_Click(objectsender,EventArgse){OpenFileDialogod=newOpenFileDialog();od.Filter="所有图像文件
4、*.bmp;*.pcx;*.jpg;*.gif"+"*.tif;*.ico;*.dxf;*.cgm;*.cdr
5、"+"位图(*.bmp;*.jpg;*.png;...)
6、*.bmp;*.jpg;*.png";od.Title="打开图像文件";od.ShowHelp
7、=true;if(od.ShowDialog()==DialogResult.OK){cur=od.FileName;try{curBitmap=(Bitmap)Image.FromFile(cur);}catch(Exceptionex){MessageBox.Show(ex.Message);}Console.WriteLine(cur);Console.WriteLine(curBitmap);}Invalidate();}privatevoidclose_Click(objectsender,EventAr
8、gse){this.Close();}privatevoidsave_Click(objectsender,EventArgse){if(curBitmap==null){return;}SaveFileDialogsd=newSaveFileDialog();sd.Title="保存为";sd.OverwritePrompt=true;sd.Filter="bmp文件(*.bmp)
9、*.bmp
10、"+"gif文件(*.gif)
11、*.gif"+"jpeg文件(*..jpg)
12、*.jpg"+"png文件(*.png)
13、
14、*.png";sd.ShowHelp=true;if(sd.ShowDialog()==DialogResult.OK){stringsrc=sd.FileName;stringext=src.Remove(0,src.Length-3);switch(ext){case"bmp":curBitmap.Save(src,System.Drawing.Imaging.ImageFormat.Bmp);break;case"jpp":curBitmap.Save(src,System.Drawing.Imaging.I
15、mageFormat.Jpeg);break;case"gif":curBitmap.Save(src,System.Drawing.Imaging.ImageFormat.Gif);break;case"tif":curBitmap.Save(src,System.Drawing.Imaging.ImageFormat.Tiff);break;case"png":curBitmap.Save(src,System.Drawing.Imaging.ImageFormat.Png);break;default:bre
16、ak;}}}privatevoidForm1_Paint(objectsender,PaintEventArgse){Graphicsg=e.Graphics;if(curBitmap!=null){g.DrawImage(curBitmap,160,20,curBitmap.Width,curBitmap.Height);}}privatevoidpixe