Browse Source

git-svn-id: https://emgucv.svn.sourceforge.net/svnroot/emgucv/trunk@232 d7f09016-e345-0410-b530-edf29a71df78

UWP10 1.3.0
canming 17 years ago
parent
commit
d2730ed3b0
  1. 876
      Emgu.CV.Test/AutoTestImage.cs
  2. 48
      Emgu.CV.Test/AutoTestVarious.cs
  3. 4
      Emgu.CV/Capture/Capture.cs
  4. 28
      Emgu.CV/Image.cs
  5. 10
      Emgu.CV/Matrix.cs

876
Emgu.CV.Test/AutoTestImage.cs

@ -16,458 +16,444 @@ using System.Threading;
namespace Emgu.CV.Test
{
[TestFixture]
public class AutoTestImage
{
[Test]
public void TestRunningAvg()
{
Image<Gray, Single> img1 = new Image<Gray, float>(100, 40, new Gray(100));
Image<Gray, Single> img2 = new Image<Gray, float>(100, 40, new Gray(50));
img1.RunningAvg(img2, 0.5);
}
[Test]
public void TestSetValue()
{
Image<Bgr, Single> img1 = new Image<Bgr, float>(50, 20, new Bgr(8.0, 1.0, 2.0));
for (int i = 0; i < img1.Width; i++)
for (int j = 0; j < img1.Height; j++)
{
Bgr c = img1[j, i];
Assert.IsTrue(c.Equals(new Bgr(8.0, 1.0, 2.0)));
}
}
[Test]
public void TestMinMax()
{
Image<Gray, Byte> img1 = new Image<Gray, Byte>(50, 60);
System.Random r = new Random();
using (Image<Gray, Byte> img2 = img1.Convert<Byte>(delegate(Byte f) { return (Byte)r.Next(255); }))
[TestFixture]
public class AutoTestImage
{
[Test]
public void TestRunningAvg()
{
Image<Gray, Single> img1 = new Image<Gray, float>(100, 40, new Gray(100));
Image<Gray, Single> img2 = new Image<Gray, float>(100, 40, new Gray(50));
img1.RunningAvg(img2, 0.5);
}
[Test]
public void TestSetValue()
{
Image<Bgr, Single> img1 = new Image<Bgr, float>(50, 20, new Bgr(8.0, 1.0, 2.0));
for (int i = 0; i < img1.Width; i++)
for (int j = 0; j < img1.Height; j++)
{
img2._Max(120.0);
for (int i = 0; i < img2.Width; i++)
for (int j = 0; j < img2.Height; j++)
Assert.GreaterOrEqual(img2[j, i].Intensity, 120.0);
Bgr c = img1[j, i];
Assert.IsTrue(c.Equals(new Bgr(8.0, 1.0, 2.0)));
}
using (Image<Gray, Byte> img2 = img1.Convert<Byte>(delegate(Byte f) { return (Byte)r.Next(255); }))
{
img2._Min(120.0);
for (int i = 0; i < img2.Width; i++)
for (int j = 0; j < img2.Height; j++)
Assert.GreaterOrEqual(120.0, img2[j, i].Intensity);
}
using (Image<Gray, Byte> img2 = img1.Convert<Byte>(delegate(Byte f) { return (Byte)r.Next(255); }))
using (Image<Gray, Byte> img3 = img1.Convert<Byte>(delegate(Byte f) { return (Byte)r.Next(255); }))
using (Image<Gray, Byte> img4 = img2.Max(img3))
{
for (int i = 0; i < img2.Width; i++)
for (int j = 0; j < img2.Height; j++)
{
Point2D<int> location = new Point2D<int>(i, j);
Assert.GreaterOrEqual(img4[location].Intensity, img2[location].Intensity);
Assert.GreaterOrEqual(img4[j, i].Intensity, img3[j, i].Intensity);
}
}
/*
using (Image<Gray, Byte> img2 = img1.Convert<Byte>(delegate(Byte f) { return (Byte)r.Next(255); }))
using (Image<Gray, Byte> img3 = img1.Convert<Byte>(delegate(Byte f) { return (Byte)r.Next(255); }))
using (Image<Gray, Byte> img4 = img2.Min(img3))
{
for (int i = 0; i < img2.Width; i++)
for (int j = 0; j < img2.Height; j++)
{
Assert.GreaterOrEqual(img2.GetPixel(new Point2D<int>(i, j)).Intensity, img4.GetPixel(new Point2D<int>(i, j)).Intensity);
Assert.GreaterOrEqual(img3.GetPixel(new Point2D<int>(i, j)).Intensity, img4.GetPixel(new Point2D<int>(i, j)).Intensity);
}
}*/
}
[Test]
public void TestAvgSdv()
{
Image<Gray, Single> img1 = new Image<Gray, float>(50, 20);
img1.SetRandNormal(new MCvScalar(100), new MCvScalar(30));
Gray mean;
MCvScalar std;
img1.AvgSdv(out mean, out std);
}
[Test]
public void TestGenericOperation()
{
Image<Gray, Single> img1 = new Image<Gray, float>(50, 20);
img1.ROI = new Rectangle<double>(10, 50, 19, 1);
img1.SetValue(5.0);
Image<Gray, Single> img2 = new Image<Gray, float>(50, 20);
img2.ROI = new Rectangle<double>(0, 40, 20, 2);
img2.SetValue(new Gray(2.0));
Assert.AreEqual(img1.Width, img2.Width);
Assert.AreEqual(img1.Height, img2.Height);
DateTime t1 = DateTime.Now;
Image<Gray, Single> img3 = img1.Add(img2);
DateTime t2 = DateTime.Now;
Image<Gray, Single> img4 = img1.Convert<Single, Single>(img2, delegate(Single v1, Single v2) { return v1 + v2; });
DateTime t3 = DateTime.Now;
Image<Gray, Single> img5 = img3.AbsDiff(img4);
DateTime t4 = DateTime.Now;
double sum1 = img5.GetSum().Intensity;
DateTime t5 = DateTime.Now;
Single sum2 = 0.0f;
img5.Action(delegate(Single v) { sum2 += v; });
DateTime t6 = DateTime.Now;
/*
TimeSpan ts1 = t2.Subtract(t1);
TimeSpan ts2 = t3.Subtract(t2);
TimeSpan ts3 = t5.Subtract(t4);
TimeSpan ts4 = t6.Subtract(t5);
Trace.WriteLine(String.Format("CV Add : {0} milliseconds", ts1.TotalMilliseconds));
Trace.WriteLine(String.Format("Generic Add: {0} milliseconds", ts2.TotalMilliseconds));
Trace.WriteLine(String.Format("CV Sum : {0} milliseconds", ts3.TotalMilliseconds));
Trace.WriteLine(String.Format("Generic Sum: {0} milliseconds", ts4.TotalMilliseconds));
Trace.WriteLine(String.Format("Abs Diff = {0}", sum1));
Trace.WriteLine(String.Format("Abs Diff = {0}", sum2));*/
Assert.AreEqual(sum1, sum2);
img3.Dispose();
img4.Dispose();
img5.Dispose();
t1 = DateTime.Now;
img3 = img1.Mul(2.0);
t2 = DateTime.Now;
img4 = img1.Convert<Single>(delegate(Single v1) { return v1 * 2.0f; });
t3 = DateTime.Now;
/*
ts1 = t2.Subtract(t1);
ts2 = t3.Subtract(t2);
Trace.WriteLine(String.Format("CV Mul : {0} milliseconds", ts1.TotalMilliseconds));
Trace.WriteLine(String.Format("Generic Mul: {0} milliseconds", ts2.TotalMilliseconds));
*/
Assert.IsTrue(img3.Equals(img4));
img3.Dispose();
img4.Dispose();
t1 = DateTime.Now;
img3 = img1.Add(img1);
img4 = img3.Add(img1);
t2 = DateTime.Now;
img5 = img1.Convert<Single, Single, Single>(img1, img1, delegate(Single v1, Single v2, Single v3) { return v1 + v2 + v3; });
t3 = DateTime.Now;
/*
ts1 = t2.Subtract(t1);
ts2 = t3.Subtract(t2);
Trace.WriteLine(String.Format("CV Sum (3 images) : {0} milliseconds", ts1.TotalMilliseconds));
Trace.WriteLine(String.Format("Generic Sum (3 images): {0} milliseconds", ts2.TotalMilliseconds));
*/
Assert.IsTrue(img5.Equals(img4));
img3.Dispose();
img4.Dispose();
img5.Dispose();
img1.Dispose();
img2.Dispose();
Image<Gray, Byte> gimg1 = new Image<Gray, Byte>(400, 300, new Gray(30));
Image<Gray, Byte> gimg2 = gimg1.Convert<Byte>(delegate(Byte b) { return (Byte)(255 - b); });
gimg1.Dispose();
gimg2.Dispose();
}
[Test]
public void TestConvertDepth()
{
Image<Gray, Byte> img1 = new Image<Gray, byte>(100, 100, new Gray(10.0));
img1.SetRandUniform(new MCvScalar(0, 0, 0), new MCvScalar(255, 255, 255));
Image<Gray, Single> img2 = img1.ConvertScale<Single>(2.0, 0.0);
Image<Gray, Byte> img3 = img2.ConvertScale<Byte>(0.5, 0.0);
Assert.IsTrue(img3.Equals(img1));
Image<Gray, Double> img4 = img1.Convert<Gray, Double>();
Image<Gray, Byte> img5 = img4.Convert<Gray, Byte>();
Assert.IsTrue(img5.Equals(img1));
}
[Test]
public void TestMemory()
{
for (int i = 0; i <= 500; i++)
{
Image<Bgr, Single> img = new Image<Bgr, Single>(1000, 1000, new Bgr());
}
}
[Test]
public void TestConversion()
{
Image<Bgr, Single> img1 = new Image<Bgr, Single>(100, 100);
img1.SetRandUniform(new MCvScalar(), new MCvScalar(255.0, 255.0, 255.0));
Image<Xyz, Single> img2 = img1.Convert<Xyz, Single>();
Image<Gray, Byte> img3 = img1.Convert<Gray, Byte>();
}
[Test]
public void TestGenericSetColor()
{
Image<Bgr, Byte> img1 = new Image<Bgr, Byte>(20, 40, new Bgr());
int flag = 0;
Image<Bgr, Byte> img2 = img1.Convert<Byte>(
delegate(Byte b)
{
return ((flag++ % 3) == 0) ? (Byte)255 : (Byte)0;
});
img1.SetValue(new Bgr(255, 0, 0));
Assert.IsTrue(img1.Equals(img2));
}
[Test]
public void TestRuntimeSerialize()
{
Image<Bgr, Byte> img = new Image<Bgr, byte>(100, 80);
using (MemoryStream ms = new MemoryStream())
{
img.SetRandNormal(new MCvScalar(100, 100, 100), new MCvScalar(50, 50, 50));
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
formatter.Serialize(ms, img);
Byte[] bytes = ms.GetBuffer();
using (MemoryStream ms2 = new MemoryStream(bytes))
{
Image<Bgr, Byte> img2 = (Image<Bgr, Byte>)formatter.Deserialize(ms2);
Assert.IsTrue(img.Equals(img2));
}
}
}
[Test]
public void TestSampleLine()
{
Image<Bgr, Byte> img = new Image<Bgr, byte>(101, 133);
img.SetRandUniform(new MCvScalar(), new MCvScalar(255, 255, 255));
Byte[,] buffer = img.Sample(new LineSegment2D<int>(new Point2D<int>(0, 0), new Point2D<int>(0, 100)));
for (int i = 0; i < 100; i++)
Assert.IsTrue(img[i, 0].Equals(new Bgr(buffer[i, 0], buffer[i, 1], buffer[i, 2])));
}
[Test]
public void TestGetSize()
{
Image<Bgr, Byte> img = new Image<Bgr, byte>(10, 10, new Bgr(255, 255, 255));
Point2D<int> size = img.Size;
}
[Test]
public void TestXmlSerialize()
{
Image<Bgr, Byte> img = new Image<Bgr, byte>(100, 80);
}
[Test]
public void TestMinMax()
{
Image<Gray, Byte> img1 = new Image<Gray, Byte>(50, 60);
System.Random r = new Random();
using (Image<Gray, Byte> img2 = img1.Convert<Byte>(delegate(Byte f) { return (Byte)r.Next(255); }))
{
img2._Max(120.0);
for (int i = 0; i < img2.Width; i++)
for (int j = 0; j < img2.Height; j++)
Assert.GreaterOrEqual(img2[j, i].Intensity, 120.0);
}
using (Image<Gray, Byte> img2 = img1.Convert<Byte>(delegate(Byte f) { return (Byte)r.Next(255); }))
{
img2._Min(120.0);
for (int i = 0; i < img2.Width; i++)
for (int j = 0; j < img2.Height; j++)
Assert.GreaterOrEqual(120.0, img2[j, i].Intensity);
}
using (Image<Gray, Byte> img2 = img1.Convert<Byte>(delegate(Byte f) { return (Byte)r.Next(255); }))
using (Image<Gray, Byte> img3 = img1.Convert<Byte>(delegate(Byte f) { return (Byte)r.Next(255); }))
using (Image<Gray, Byte> img4 = img2.Max(img3))
{
for (int i = 0; i < img2.Width; i++)
for (int j = 0; j < img2.Height; j++)
{
Point2D<int> location = new Point2D<int>(i, j);
Assert.GreaterOrEqual(img4[location].Intensity, img2[location].Intensity);
Assert.GreaterOrEqual(img4[j, i].Intensity, img3[j, i].Intensity);
}
}
/*
using (Image<Gray, Byte> img2 = img1.Convert<Byte>(delegate(Byte f) { return (Byte)r.Next(255); }))
using (Image<Gray, Byte> img3 = img1.Convert<Byte>(delegate(Byte f) { return (Byte)r.Next(255); }))
using (Image<Gray, Byte> img4 = img2.Min(img3))
{
for (int i = 0; i < img2.Width; i++)
for (int j = 0; j < img2.Height; j++)
{
Assert.GreaterOrEqual(img2.GetPixel(new Point2D<int>(i, j)).Intensity, img4.GetPixel(new Point2D<int>(i, j)).Intensity);
Assert.GreaterOrEqual(img3.GetPixel(new Point2D<int>(i, j)).Intensity, img4.GetPixel(new Point2D<int>(i, j)).Intensity);
}
}*/
}
[Test]
public void TestAvgSdv()
{
Image<Gray, Single> img1 = new Image<Gray, float>(50, 20);
img1.SetRandNormal(new MCvScalar(100), new MCvScalar(30));
Gray mean;
MCvScalar std;
img1.AvgSdv(out mean, out std);
}
[Test]
public void TestGenericOperation()
{
Image<Gray, Single> img1 = new Image<Gray, float>(50, 20);
img1.ROI = new Rectangle<double>(10, 50, 19, 1);
img1.SetValue(5.0);
Image<Gray, Single> img2 = new Image<Gray, float>(50, 20);
img2.ROI = new Rectangle<double>(0, 40, 20, 2);
img2.SetValue(new Gray(2.0));
Assert.AreEqual(img1.Width, img2.Width);
Assert.AreEqual(img1.Height, img2.Height);
DateTime t1 = DateTime.Now;
Image<Gray, Single> img3 = img1.Add(img2);
DateTime t2 = DateTime.Now;
Image<Gray, Single> img4 = img1.Convert<Single, Single>(img2, delegate(Single v1, Single v2) { return v1 + v2; });
DateTime t3 = DateTime.Now;
Image<Gray, Single> img5 = img3.AbsDiff(img4);
DateTime t4 = DateTime.Now;
double sum1 = img5.GetSum().Intensity;
DateTime t5 = DateTime.Now;
Single sum2 = 0.0f;
img5.Action(delegate(Single v) { sum2 += v; });
DateTime t6 = DateTime.Now;
/*
TimeSpan ts1 = t2.Subtract(t1);
TimeSpan ts2 = t3.Subtract(t2);
TimeSpan ts3 = t5.Subtract(t4);
TimeSpan ts4 = t6.Subtract(t5);
Trace.WriteLine(String.Format("CV Add : {0} milliseconds", ts1.TotalMilliseconds));
Trace.WriteLine(String.Format("Generic Add: {0} milliseconds", ts2.TotalMilliseconds));
Trace.WriteLine(String.Format("CV Sum : {0} milliseconds", ts3.TotalMilliseconds));
Trace.WriteLine(String.Format("Generic Sum: {0} milliseconds", ts4.TotalMilliseconds));
Trace.WriteLine(String.Format("Abs Diff = {0}", sum1));
Trace.WriteLine(String.Format("Abs Diff = {0}", sum2));*/
Assert.AreEqual(sum1, sum2);
img3.Dispose();
img4.Dispose();
img5.Dispose();
t1 = DateTime.Now;
img3 = img1.Mul(2.0);
t2 = DateTime.Now;
img4 = img1.Convert<Single>(delegate(Single v1) { return v1 * 2.0f; });
t3 = DateTime.Now;
/*
ts1 = t2.Subtract(t1);
ts2 = t3.Subtract(t2);
Trace.WriteLine(String.Format("CV Mul : {0} milliseconds", ts1.TotalMilliseconds));
Trace.WriteLine(String.Format("Generic Mul: {0} milliseconds", ts2.TotalMilliseconds));
*/
Assert.IsTrue(img3.Equals(img4));
img3.Dispose();
img4.Dispose();
t1 = DateTime.Now;
img3 = img1.Add(img1);
img4 = img3.Add(img1);
t2 = DateTime.Now;
img5 = img1.Convert<Single, Single, Single>(img1, img1, delegate(Single v1, Single v2, Single v3) { return v1 + v2 + v3; });
t3 = DateTime.Now;
/*
ts1 = t2.Subtract(t1);
ts2 = t3.Subtract(t2);
Trace.WriteLine(String.Format("CV Sum (3 images) : {0} milliseconds", ts1.TotalMilliseconds));
Trace.WriteLine(String.Format("Generic Sum (3 images): {0} milliseconds", ts2.TotalMilliseconds));
*/
Assert.IsTrue(img5.Equals(img4));
img3.Dispose();
img4.Dispose();
img5.Dispose();
img1.Dispose();
img2.Dispose();
Image<Gray, Byte> gimg1 = new Image<Gray, Byte>(400, 300, new Gray(30));
Image<Gray, Byte> gimg2 = gimg1.Convert<Byte>(delegate(Byte b) { return (Byte)(255 - b); });
gimg1.Dispose();
gimg2.Dispose();
}
[Test]
public void TestConvertDepth()
{
Image<Gray, Byte> img1 = new Image<Gray, byte>(100, 100, new Gray(10.0));
img1.SetRandUniform(new MCvScalar(0, 0, 0), new MCvScalar(255, 255, 255));
Image<Gray, Single> img2 = img1.ConvertScale<Single>(2.0, 0.0);
Image<Gray, Byte> img3 = img2.ConvertScale<Byte>(0.5, 0.0);
Assert.IsTrue(img3.Equals(img1));
Image<Gray, Double> img4 = img1.Convert<Gray, Double>();
Image<Gray, Byte> img5 = img4.Convert<Gray, Byte>();
Assert.IsTrue(img5.Equals(img1));
}
[Test]
public void TestMemory()
{
for (int i = 0; i <= 500; i++)
{
Image<Bgr, Single> img = new Image<Bgr, Single>(1000, 1000, new Bgr());
}
}
[Test]
public void TestConversion()
{
Image<Bgr, Single> img1 = new Image<Bgr, Single>(100, 100);
img1.SetRandUniform(new MCvScalar(), new MCvScalar(255.0, 255.0, 255.0));
Image<Xyz, Single> img2 = img1.Convert<Xyz, Single>();
Image<Gray, Byte> img3 = img1.Convert<Gray, Byte>();
}
[Test]
public void TestGenericSetColor()
{
Image<Bgr, Byte> img1 = new Image<Bgr, Byte>(20, 40, new Bgr());
int flag = 0;
Image<Bgr, Byte> img2 = img1.Convert<Byte>(
delegate(Byte b)
{
return ((flag++ % 3) == 0) ? (Byte)255 : (Byte)0;
});
img1.SetValue(new Bgr(255, 0, 0));
Assert.IsTrue(img1.Equals(img2));
}
[Test]
public void TestRuntimeSerialize()
{
Image<Bgr, Byte> img = new Image<Bgr, byte>(100, 80);
using (MemoryStream ms = new MemoryStream())
{
img.SetRandNormal(new MCvScalar(100, 100, 100), new MCvScalar(50, 50, 50));
img.SerializationCompressionRatio = 9;
XmlDocument doc1 = Toolbox.XmlSerialize<Image<Bgr, Byte>>(img);
String str = doc1.OuterXml;
Image<Bgr, Byte> img2 = Toolbox.XmlDeserialize<Image<Bgr, Byte>>(doc1);
Assert.IsTrue(img.Equals(img2));
img.SerializationCompressionRatio = 9;
XmlDocument doc2 = Toolbox.XmlSerialize<Image<Bgr, Byte>>(img);
Image<Bgr, Byte> img3 = Toolbox.XmlDeserialize<Image<Bgr, Byte>>(doc2);
Assert.IsTrue(img.Equals(img3));
XmlDocument doc3 = new XmlDocument();
doc3.LoadXml(str);
Image<Bgr, Byte> img4 = Toolbox.XmlDeserialize<Image<Bgr, Byte>>(doc3);
Assert.IsTrue(img.Equals(img4));
}
[Test]
public void TestRotation()
{
Image<Bgr, Byte> img = new Image<Bgr, byte>(100, 80);
img.SetRandNormal(new MCvScalar(100, 100, 100), new MCvScalar(50, 50, 50));
img.Rotate(90, new Bgr());
}
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
formatter.Serialize(ms, img);
Byte[] bytes = ms.GetBuffer();
[Test]
public void TestConstructor()
{
for (int i = 0; i < 20; i++)
using (MemoryStream ms2 = new MemoryStream(bytes))
{
Image<Gray, Byte> img = new Image<Gray, Byte>(500, 500, new Gray());
Assert.AreEqual(0, System.Convert.ToInt32(img.GetSum().Intensity));
Image<Bgr, Byte> img2 = (Image<Bgr, Byte>)formatter.Deserialize(ms2);
Assert.IsTrue(img.Equals(img2));
}
for (int i = 0; i < 20; i++)
}
}
[Test]
public void TestSampleLine()
{
Image<Bgr, Byte> img = new Image<Bgr, byte>(101, 133);
img.SetRandUniform(new MCvScalar(), new MCvScalar(255, 255, 255));
Byte[,] buffer = img.Sample(new LineSegment2D<int>(new Point2D<int>(0, 0), new Point2D<int>(0, 100)));
for (int i = 0; i < 100; i++)
Assert.IsTrue(img[i, 0].Equals(new Bgr(buffer[i, 0], buffer[i, 1], buffer[i, 2])));
}
[Test]
public void TestGetSize()
{
Image<Bgr, Byte> img = new Image<Bgr, byte>(10, 10, new Bgr(255, 255, 255));
Point2D<int> size = img.Size;
}
[Test]
public void TestXmlSerialize()
{
Image<Bgr, Byte> img = new Image<Bgr, byte>(100, 80);
img.SetRandNormal(new MCvScalar(100, 100, 100), new MCvScalar(50, 50, 50));
img.SerializationCompressionRatio = 9;
XmlDocument doc1 = Toolbox.XmlSerialize<Image<Bgr, Byte>>(img);
String str = doc1.OuterXml;
Image<Bgr, Byte> img2 = Toolbox.XmlDeserialize<Image<Bgr, Byte>>(doc1);
Assert.IsTrue(img.Equals(img2));
img.SerializationCompressionRatio = 9;
XmlDocument doc2 = Toolbox.XmlSerialize<Image<Bgr, Byte>>(img);
Image<Bgr, Byte> img3 = Toolbox.XmlDeserialize<Image<Bgr, Byte>>(doc2);
Assert.IsTrue(img.Equals(img3));
XmlDocument doc3 = new XmlDocument();
doc3.LoadXml(str);
Image<Bgr, Byte> img4 = Toolbox.XmlDeserialize<Image<Bgr, Byte>>(doc3);
Assert.IsTrue(img.Equals(img4));
}
[Test]
public void TestRotation()
{
Image<Bgr, Byte> img = new Image<Bgr, byte>(100, 80);
img.SetRandNormal(new MCvScalar(100, 100, 100), new MCvScalar(50, 50, 50));
img.Rotate(90, new Bgr());
}
[Test]
public void TestConstructor()
{
for (int i = 0; i < 20; i++)
{
Image<Gray, Byte> img = new Image<Gray, Byte>(500, 500, new Gray());
Assert.AreEqual(0, System.Convert.ToInt32(img.GetSum().Intensity));
}
for (int i = 0; i < 20; i++)
{
Image<Bgr, Single> img = new Image<Bgr, Single>(500, 500);
Assert.IsTrue(img.GetSum().Equals(new Bgr(0.0, 0.0, 0.0)));
}
Image<Bgr, Byte> img2 = new Image<Bgr, byte>(1, 2);
Assert.AreEqual(img2.Data.GetLength(1), 4);
Byte[, ,] data = new Byte[,,] { { { 255, 0, 0 } }, { { 0, 255, 0 } } };
Image<Bgr, Byte> img3 = new Image<Bgr, byte>(data);
Image<Gray, Single> img4 = new Image<Gray, float>("stuff.jpg");
Image<Bgr, Single> img5 = new Image<Bgr, float>("stuff.jpg");
Bitmap bmp = new Bitmap("stuff.jpg");
Image<Bgr, Single> img6 = new Image<Bgr, float>(bmp);
Image<Hsv, Single> img7 = new Image<Hsv, float>("stuff.jpg");
Image<Hsv, Byte> img8 = new Image<Hsv, byte>("stuff.jpg");
}
[Test]
public void TestSubR()
{
Image<Bgr, Byte> img = new Image<Bgr, Byte>(101, 133);
Assert.IsTrue(img.Not().Equals(255 - img));
}
[Test]
public void TestConvolutionAndLaplace()
{
Image<Gray, Byte> image = new Image<Gray, byte>(300, 400);
image.SetRandUniform(new MCvScalar(0.0), new MCvScalar(255.0));
Image<Gray, float> laplace = image.Laplace(1);
float[,] k = { {0, 1, 0},
{1, -4, 1},
{0, 1, 0}};
ConvolutionKernelF kernel = new ConvolutionKernelF(k);
Image<Gray, float> convoluted = image * kernel;
Assert.IsTrue(laplace.Equals(convoluted));
}
[Test]
public void TestBitmapConstructor()
{
#region test byte images
Image<Bgr, Byte> image1 = new Image<Bgr, byte>(201, 401);
image1.SetRandUniform(new MCvScalar(), new MCvScalar(255.0, 255.0, 255.0));
Bitmap bmp = image1.ToBitmap();
Image<Bgr, Byte> image2 = new Image<Bgr, byte>(bmp);
Assert.IsTrue(image1.Equals(image2));
Image<Gray, Byte> image3 = new Image<Gray, byte>(11, 7);
image3.SetRandUniform(new MCvScalar(), new MCvScalar(255.0, 255.0, 255.0));
bmp = image3.ToBitmap();
Image<Gray, Byte> image4 = new Image<Gray, byte>(bmp);
Assert.IsTrue(image3.Equals(image4));
#endregion
#region test single images
Image<Bgr, Single> image5 = new Image<Bgr, Single>(201, 401);
image5.SetRandUniform(new MCvScalar(), new MCvScalar(255.0, 255.0, 255.0));
Bitmap bmp2 = image5.ToBitmap();
#endregion
}
[Test]
public void TestSplitMerge()
{
Image<Bgr, Byte> img1 = new Image<Bgr, byte>(301, 234);
img1.SetRandUniform(new MCvScalar(), new MCvScalar(255, 255, 255));
Image<Gray, Byte>[] channels = img1.Split();
Image<Bgr, Byte> img2 = new Image<Bgr, byte>(channels);
Assert.IsTrue(img1.Equals(img2));
}
[Test]
public void TestAcc()
{
Image<Gray, Single> img1 = new Image<Gray, Single>(300, 200);
img1.SetRandUniform(new MCvScalar(0), new MCvScalar(255));
Image<Gray, Single> img2 = new Image<Gray, Single>(300, 200);
img2.SetRandUniform(new MCvScalar(0), new MCvScalar(255));
Image<Gray, Single> img3 = img1.Copy();
img3.Acc(img2);
Assert.IsTrue(img3.Equals(img1 + img2));
}
[Test]
public void TestCanny()
{
Image<Bgr, Byte> image = new Image<Bgr, byte>("stuff.jpg");
//make sure canny works for multi channel image
Image<Bgr, Byte> image2 = image.Canny(new Bgr(200, 200, 200), new Bgr(100, 100, 100));
}
[Test]
public void TestInplaceFlip()
{
Image<Bgr, byte> image = new Image<Bgr, byte>(20, 20);
image.SetRandUniform(new MCvScalar(), new MCvScalar(255, 255, 255));
Image<Bgr, byte> imageOld = image.Copy();
image._Flip(Emgu.CV.CvEnum.FLIP.VERTICAL);
for (int i = 0; i < image.Rows; i++)
for (int j = 0; j < image.Cols; j++)
{
Image<Bgr, Single> img = new Image<Bgr, Single>(500, 500);
Assert.IsTrue(img.GetSum().Equals(new Bgr(0.0, 0.0, 0.0)));
Bgr c1 = image[i, j];
Bgr c2 = imageOld[image.Rows - i - 1, j];
Assert.IsTrue(c1.Equals(c2));
}
Image<Bgr, Byte> img2 = new Image<Bgr, byte>(1, 2);
Assert.AreEqual(img2.Data.GetLength(1), 4);
Byte[, ,] data = new Byte[,,] { { { 255, 0, 0 } }, { { 0, 255, 0 } } };
Image<Bgr, Byte> img3 = new Image<Bgr, byte>(data);
Image<Gray, Single> img4 = new Image<Gray, float>("stuff.jpg");
Image<Bgr, Single> img5 = new Image<Bgr, float>("stuff.jpg");
Bitmap bmp = new Bitmap("stuff.jpg");
Image<Bgr, Single> img6 = new Image<Bgr, float>(bmp);
}
[Test]
public void TestSubR()
{
Image<Bgr, Byte> img = new Image<Bgr, Byte>(101, 133);
Assert.IsTrue(img.Not().Equals(255 - img));
}
[Test]
public void TestConvolutionAndLaplace()
{
Image<Gray, Byte> image = new Image<Gray, byte>(300, 400);
image.SetRandUniform(new MCvScalar(0.0), new MCvScalar(255.0));
Image<Gray, float> laplace = image.Laplace(1);
float[,] k = { {0, 1, 0},
{1, -4, 1},
{0, 1, 0}};
ConvolutionKernelF kernel = new ConvolutionKernelF(k);
Image<Gray, float> convoluted = image * kernel;
Assert.IsTrue(laplace.Equals(convoluted));
}
[Test]
public void TestBitmapConstructor()
{
#region test byte images
Image<Bgr, Byte> image1 = new Image<Bgr, byte>(201, 401);
image1.SetRandUniform(new MCvScalar(), new MCvScalar(255.0, 255.0, 255.0));
Bitmap bmp = image1.ToBitmap();
Image<Bgr, Byte> image2 = new Image<Bgr, byte>(bmp);
Assert.IsTrue(image1.Equals(image2));
Image<Gray, Byte> image3 = new Image<Gray, byte>(11, 7);
image3.SetRandUniform(new MCvScalar(), new MCvScalar(255.0, 255.0, 255.0));
bmp = image3.ToBitmap();
Image<Gray, Byte> image4 = new Image<Gray, byte>(bmp);
Assert.IsTrue(image3.Equals(image4));
#endregion
#region test single images
Image<Bgr, Single> image5 = new Image<Bgr, Single>(201, 401);
image5.SetRandUniform(new MCvScalar(), new MCvScalar(255.0, 255.0, 255.0));
Bitmap bmp2 = image5.ToBitmap();
#endregion
}
[Test]
public void TestSplitMerge()
{
Image<Bgr, Byte> img1 = new Image<Bgr, byte>(301, 234);
img1.SetRandUniform(new MCvScalar(), new MCvScalar(255, 255, 255));
Image<Gray, Byte>[] channels = img1.Split();
Image<Bgr, Byte> img2 = new Image<Bgr, byte>(channels);
Assert.IsTrue(img1.Equals(img2));
}
[Test]
public void TestAcc()
{
Image<Gray, Single> img1 = new Image<Gray, Single>(300, 200);
img1.SetRandUniform(new MCvScalar(0), new MCvScalar(255));
Image<Gray, Single> img2 = new Image<Gray, Single>(300, 200);
img2.SetRandUniform(new MCvScalar(0), new MCvScalar(255));
Image<Gray, Single> img3 = img1.Copy();
img3.Acc(img2);
Assert.IsTrue(img3.Equals(img1 + img2));
}
[Test]
public void TestCanny()
{
Image<Bgr, Byte> image = new Image<Bgr, byte>("stuff.jpg");
//make sure canny works for multi channel image
Image<Bgr, Byte> image2 = image.Canny(new Bgr(200, 200, 200), new Bgr(100, 100, 100));
}
[Test]
public void TestVideoWriter()
{
using (VideoWriter writer = new VideoWriter("tmp.avi", 2, 200, 100, true))
{
for (int i = 0; i < 100; i++)
{
Image<Bgr, Byte> img1 = new Image<Bgr, byte>(200, 100);
img1.SetRandUniform(new MCvScalar(), new MCvScalar(255, 255, 255));
writer.WriteFrame(img1);
}
}
FileInfo fi = new FileInfo("tmp.avi");
Assert.AreNotEqual(fi.Length, 0);
}
[Test]
public void TestInplaceFlip()
{
Image<Bgr, byte> image = new Image<Bgr, byte>(20, 20);
image.SetRandUniform(new MCvScalar(), new MCvScalar(255, 255, 255));
Image<Bgr, byte> imageOld = image.Copy();
image._Flip(Emgu.CV.CvEnum.FLIP.VERTICAL);
for (int i = 0; i < image.Rows; i++)
for (int j = 0; j < image.Cols; j++)
{
Bgr c1 = image[i, j];
Bgr c2 = imageOld[image.Rows - i - 1, j ];
Assert.IsTrue(c1.Equals(c2));
}
}
[Test]
public void TestMoment()
{
Image<Gray, byte> image = new Image<Gray, byte>(100, 200);
image.SetRandUniform(new MCvScalar(), new MCvScalar(255));
image.ThresholdToZero(new Gray(120));
MCvMoments moment = image.GetMoments(true);
}
}
}
[Test]
public void TestMoment()
{
Image<Gray, byte> image = new Image<Gray, byte>(100, 200);
image.SetRandUniform(new MCvScalar(), new MCvScalar(255));
image.ThresholdToZero(new Gray(120));
MCvMoments moment = image.GetMoments(true);
}
}
}

48
Emgu.CV.Test/AutoTestVarious.cs

@ -145,6 +145,13 @@ namespace Emgu.CV.Test
MCvMoments moment = cs.GetMoments();
Assert.IsTrue(moment.GravityCenter.Equals(rect2.Center));
}
using (MemStorage stor = new MemStorage())
{
Image<Gray, Byte> img2 = new Image<Gray, byte>(300, 200);
Contour c = img2.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST, stor);
Assert.AreEqual(c, null);
}
}
}
@ -327,14 +334,14 @@ namespace Emgu.CV.Test
{
int width = 300;
int height = 400;
Image<Bgr, Byte> bg = new Image<Bgr,byte>(width, height);
Image<Bgr, Byte> bg = new Image<Bgr, byte>(width, height);
bg.SetRandNormal(new MCvScalar(), new MCvScalar(100, 100, 100));
Image<Bgr, Byte> img1 = bg.Copy();
img1.Draw(new Rectangle<double>(new Point2D<double>(width>>1, height >>1), width/10, height/10), new Bgr(Color.Red), -1);
img1.Draw(new Rectangle<double>(new Point2D<double>(width >> 1, height >> 1), width / 10, height / 10), new Bgr(Color.Red), -1);
Image<Bgr, Byte> img2 = bg.Copy();
img2.Draw(new Rectangle<double>(new Point2D<double>(width>>1 + 10 , height >>1), width/10, height/10), new Bgr(Color.Red), -1);
img2.Draw(new Rectangle<double>(new Point2D<double>(width >> 1 + 10, height >> 1), width / 10, height / 10), new Bgr(Color.Red), -1);
BackgroundStatisticsModel model1 = new BackgroundStatisticsModel(img1, Emgu.CV.CvEnum.BG_STAT_TYPE.GAUSSIAN_BG_MODEL);
model1.Update(img2);
@ -345,5 +352,40 @@ namespace Emgu.CV.Test
//Application.Run(new ImageViewer(model2.Foreground));
//Application.Run(new ImageViewer(model.BackGround));
}
[Test]
public void TestVideoWriter()
{
int numberOfFrames = 1000;
int width = 300;
int height = 200;
using (VideoWriter writer = new VideoWriter("tmp.avi", 2, width, height, true))
{
for (int i = 0; i < numberOfFrames; i++)
{
Image<Bgr, Byte> img1 = new Image<Bgr, byte>(width, height);
img1.SetRandUniform(new MCvScalar(), new MCvScalar(255, 255, 255));
writer.WriteFrame(img1);
}
}
FileInfo fi = new FileInfo("tmp.avi");
Assert.AreNotEqual(fi.Length, 0);
using (Capture capture = new Capture("tmp.avi"))
{
Image<Bgr, Byte> img2 = capture.QueryFrame();
int count = 0;
while (img2 != null)
{
Assert.AreEqual(img2.Width, width);
Assert.AreEqual(img2.Height, height);
img2 = capture.QueryFrame();
count++;
}
Assert.AreEqual(numberOfFrames, count);
}
File.Delete(fi.FullName);
}
}
}

4
Emgu.CV/Capture/Capture.cs

@ -210,6 +210,10 @@ namespace Emgu.CV
#else
IntPtr img = CvInvoke.cvQueryFrame(Ptr);
#endif
if (img == IntPtr.Zero)
{
return null;
}
MIplImage iplImage = (MIplImage)Marshal.PtrToStructure(img, typeof(MIplImage));

28
Emgu.CV/Image.cs

@ -108,13 +108,16 @@ namespace Emgu.CV
//TODO: fix the following to handle the case when input image has non 4-align byte in a row
Emgu.Util.Toolbox.memcpy(_dataHandle.AddrOfPinnedObject(), mptr.imageData, mptr.widthStep * mptr.height);
CvInvoke.cvReleaseImage(ref ptr);
#endregion
}
else
{ //if the file format cannot be recognized by OpenCV
if (System.Array.Exists(_bitmapFormats, fi.Extension.ToLower().Equals))
{
Bitmap = new Bitmap(fi.FullName);
using(Bitmap bmp = new Bitmap(fi.FullName))
Bitmap = bmp;
}
else
throw new FileLoadException(String.Format("Unable to load file of type {0}", fi.Extension));
@ -926,32 +929,41 @@ namespace Emgu.CV
#region Contour detection
/// <summary>
/// Find contours
/// Find a list of contours using simple approximation method.
/// </summary>
/// <returns>Contours</returns>
/// <returns>
/// Contour if there is any;
/// null if no contour is found
/// </returns>
public Contour FindContours()
{
return FindContours(CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, CvEnum.RETR_TYPE.CV_RETR_LIST);
}
/// <summary>
/// Find contours
/// Find contours
/// </summary>
/// <param name="method">The type of approximation method</param>
/// <param name="type">The retrival type</param>
/// <returns>Contours</returns>
/// <returns>
/// Contour if there is any;
/// null if no contour is found
/// </returns>
public Contour FindContours(CvEnum.CHAIN_APPROX_METHOD method, CvEnum.RETR_TYPE type)
{
return FindContours(method, type, new MemStorage());
}
/// <summary>
/// Find contours
/// Find contours using the specific memory storage
/// </summary>
/// <param name="method">The type of approximation method</param>
/// <param name="type">The retrival type</param>
/// <param name="stor">The storage used by the sequences</param>
/// <returns>Contours</returns>
/// <returns>
/// Contour if there is any;
/// null if no contour is found
/// </returns>
public Contour FindContours(CvEnum.CHAIN_APPROX_METHOD method, CvEnum.RETR_TYPE type, MemStorage stor)
{
IntPtr seq = IntPtr.Zero;
@ -977,6 +989,8 @@ namespace Emgu.CV
type,
method,
new MCvPoint(0, 0));
if (seq == IntPtr.Zero)
return null;
}
return new Contour(seq, stor);
}

10
Emgu.CV/Matrix.cs

@ -16,6 +16,8 @@ namespace Emgu.CV
{
private TDepth[,] _array;
private readonly static int _sizeOfHeader = Marshal.SizeOf(typeof(MCvMat));
#region Constructors
/// <summary>
/// The default constructor which allows Data to be set later on
@ -112,11 +114,14 @@ namespace Emgu.CV
Debug.Assert(value != null, "The Array cannot be null");
if (_ptr == IntPtr.Zero)
_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MCvMat)));
{
_ptr = Marshal.AllocHGlobal(_sizeOfHeader);
GC.AddMemoryPressure(_sizeOfHeader);
}
if (_dataHandle.IsAllocated)
_dataHandle.Free(); //free the data handle
Debug.Assert(!_dataHandle.IsAllocated, "Handle should be free");
Debug.Assert(!_dataHandle.IsAllocated, "Handle should be freed");
_array = value;
_dataHandle = GCHandle.Alloc(_array, GCHandleType.Pinned);
@ -576,6 +581,7 @@ namespace Emgu.CV
if (_ptr != IntPtr.Zero)
{
Marshal.Release(_ptr);
GC.RemoveMemoryPressure(_sizeOfHeader);
_ptr = IntPtr.Zero;
}

Loading…
Cancel
Save