运算符重载 (重载运算符 +, -, *,实现图像与图像,图像与值的运算)
Image<Gray,Byte> img1;
//Do somthing to initialize img1
Image<Gray,Byte> img2;
//Do somthing to initialize img2
IMage<Gray,Byte> img3=(img1+img2-2.0)*0.5;
像素操作(赋值,取反等)
Image<Gray, Byte> img1 = new Image<Gray, Byte>(400, 300, new Gray(30));
Image<Gray, Byte> img2 = img1.Not();
Image<Gray, Byte> img3 =
img1.Convert<Byte>( delegate(Byte b) { return (Byte) (255-b); } );
Image<Gray, Single> img4 = img1.Convert<Single>(
delegate(Byte b) { return (Single) Math.cos( b * b / 255.0); } );
注意.And(),.Xor(),.Not()等运算会返回计算结果而原始图像内容未发生改变,
._And(),._Not()则是在图像本身进行运算(而不返回),计算完成后图像内容已改变
元素类型转换(极其方便,一条语句基本上可以搞定大部分的基础转换)
Image<Bgr, Byte> img1;
Image<Gray, Single> img2 = img1.Convert<Gray, Single>();
Bitmap bmp=img2.ToBitmap();
序列化(XML文档)
序列化:将object obj的内容写到XML文档
反序列化:从XML文档构建obj对象
StringBuilder sb = new StringBuilder();
(new XmlSerializer(typeof(Image<Bgr, Byte>))).Serialize(new StringWriter(sb), obj);
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(sb.ToString());
Image<Bgr, Byte> image = (Image<Bgr, Byte>)
(new XmlSerializer(typeof(Image<Bgr, Byte>))).Deserialize(new XmlNodeReader(xDoc));
完整的示例
using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace EmguImageProcess
{
public class ImageProcess
{
private Image<Bgr, Byte> image = null;
/// <summary>
/// Form中的图片控件(请使用Designer设计器)
/// </summary>
// private Emgu.CV.UI.ImageBox imageBox1;
// private System.WIndows.Forms.PictureBox pictureBox1;
///
public void MenuItemOpenFile_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image File(*.jpg;*.png;*.*)|*.*";
ofd.Multiselect = false;
if (ofd.ShowDialog() == DialogResult.OK)
{
image = new Image<Bgr, byte>(ofd.FileName);
}
}
private void MenuItemDisplayImage_Click(object sender, EventArgs e)
{
if (image == null) return;
imageBox1.Image = image;
pictureBox1.Image = image.ToBitmap();
}
private void MenuItemToGray_Click(object sender, EventArgs e)
{
if (image == null) return;
Image<Gray, Byte> gray = image.Convert<Gray, Byte>();
imageBox1.Image = gray;
}
private void MenuItemConsine_Click(object sender, EventArgs e)
{
if (image == null) return;
Image<Bgr, Single> cosGray = image.Convert<Single>(
delegate(Byte b) { return (Single)Math.Cos(b * b / 255.0); });
imageBox1.Image = cosGray;
}
private void MenuItemXmlSerial_Click(object sender, EventArgs e)
{
if (image == null) return;
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "XML File(*.xml)|*.xml";
if (sfd.ShowDialog() == DialogResult.OK)
{
StringBuilder sb = new StringBuilder();
(new XmlSerializer(typeof(Image<Bgr, Byte>))).Serialize(new StringWriter(sb), image);
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(sb.ToString());
xDoc.Save(sfd.FileName);
}
}
private void MenuItemXmlLoad_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "XML File(*.xml)|*.xml";
ofd.Multiselect = false;
if (ofd.ShowDialog() == DialogResult.OK)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(ofd.FileName);
image = (Image<Bgr, Byte>)(new XmlSerializer(
typeof(Image<Bgr, Byte>))).Deserialize(new XmlNodeReader(xDoc));
}
}
}
}