标签:
程序如下;
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Emgu.CV; using Emgu.CV.CvEnum; using Emgu.CV.Structure; using System.Xml; using System.Xml.Serialization; using System.IO; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// 显示蓝色图 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { Image<Bgr, Byte> img1 = new Image<Bgr, byte>(320, 240, new Bgr(255, 0, 0)); pictureBox1.Image = img1.ToBitmap(); } /// <summary> /// 从对话框打开一副图片; /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { string strFileName = string.Empty; OpenFileDialog ofd = new OpenFileDialog(); if(ofd.ShowDialog()==DialogResult.OK) { Image<Bgr, Byte> img1 = new Image<Bgr, byte>(ofd.FileName); pictureBox1.Image = img1.ToBitmap(); } } /// <summary> /// 对像素点进行操作; /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button3_Click(object sender, EventArgs e) { Image<Bgr, Byte> img1 = new Image<Bgr, byte>(320, 240, new Bgr(250, 0, 0)); Byte b1 = 255; Bgr yellow = new Bgr(0, 255, 255);//可以把RGB值理解成向量的叠加; //First for (int i=20;i<60;i++) { for(int j=20;j<60;j++) { img1.Data[i, j, 0] = 0; img1.Data[i, j, 1] = b1; img1.Data[i, j, 2] = b1; } } //Second for(int i=120;i<160;i++) { for(int j=20;j<60;j++) { img1[i, j] = yellow; } } //Third byte[, ,] data = img1.Data; //只传递地址; for(int i=20;i<60;i++) { for(int j=120;j<160;j++) { data[i, j, 0] = 0; data[i, j, 1] = b1; data[i, j, 2] = b1; } } pictureBox1.Image = img1.ToBitmap(); } /// <summary> /// 图像叠加; /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button4_Click(object sender, EventArgs e) { Image<Bgr, Byte> imgBlue = new Image<Bgr, byte>(320, 240, new Bgr(255, 0, 0)); Image<Bgr, Byte> imgGreen = new Image<Bgr, byte>(320, 240, new Bgr(0, 255, 0)); Image<Bgr, Byte> imgRed = new Image<Bgr, byte>(320, 240, new Bgr(0, 0, 255)); Image<Bgr, Byte> img1 = imgBlue + imgGreen + imgRed; pictureBox1.Image = img1.ToBitmap(); } /// <summary> /// 对每个像素点进行变换; /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button5_Click(object sender, EventArgs e) { //you need choose a gray picture Image<Gray, Byte> imgGray = new Image<Gray, byte>("E:\\testpictures\\rice.png"); //single 是单精度浮点数 Image<Gray, Single> img1 = imgGray.Convert<Single>( delegate(Byte b) { return (Single)Math.Sin(b * b / 255.0); } ); pictureBox1.Image = img1.ToBitmap(); img1.Dispose();//如果不加这个,系统也会自动注销图片,但是尽量手动注销; } /// <summary> /// Image转换为XML /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button6_Click(object sender, EventArgs e) { string strFileName = string.Empty; OpenFileDialog ofd = new OpenFileDialog(); if(ofd.ShowDialog()==DialogResult.OK) { Image<Bgr, Byte> img1 = new Image<Bgr, byte>(ofd.FileName); pictureBox1.Image = img1.ToBitmap(); //注意引用systom.xml和 io等; StringBuilder sb1 = new StringBuilder(); (new XmlSerializer(typeof(Image<Bgr, Byte>))).Serialize(new StringWriter(sb1), img1); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(sb1.ToString()); xmlDoc.Save("image.xml"); } } /// <summary> /// XML转换为image,需要加载xml文件,我用的是上个按钮生成的xml文件; /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button7_Click(object sender, EventArgs e) { string strFileName = string.Empty; OpenFileDialog ofd = new OpenFileDialog(); if (ofd.ShowDialog() == DialogResult.OK) { XmlDocument doc = new XmlDocument(); doc.Load(ofd.FileName); Image<Bgr, Byte> img1 = (Image<Bgr, byte>) (new XmlSerializer(typeof(Image<Bgr, Byte>))).Deserialize(new XmlNodeReader(doc)); pictureBox1.Image = img1.ToBitmap(); } } } }
第二个程序完。
C# + WinForm + EmguCV 学习二:创建和操作图片
标签:
原文地址:http://blog.csdn.net/shawncheer/article/details/51366451