You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

483 lines
17 KiB

  1. //----------------------------------------------------------------------------
  2. // Copyright (C) 2004-2020 by EMGU Corporation. All rights reserved.
  3. //----------------------------------------------------------------------------
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Runtime.InteropServices;
  10. using System.Runtime.Serialization;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Xml;
  14. using System.Xml.Linq;
  15. using Emgu.CV;
  16. using Emgu.CV.Structure;
  17. using Emgu.Util;
  18. #if VS_TEST
  19. using Microsoft.VisualStudio.TestTools.UnitTesting;
  20. using TestAttribute = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute;
  21. using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute;
  22. #elif NETFX_CORE
  23. using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
  24. using TestAttribute = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
  25. using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
  26. #else
  27. using NUnit.Framework;
  28. #endif
  29. namespace Emgu.CV.Test
  30. {
  31. [TestFixture]
  32. public class AutoTestMatrix
  33. {
  34. [Test]
  35. public void TestInvert()
  36. {
  37. Matrix<Single> m = new Matrix<Single>(3, 3);
  38. Matrix<Single> mInvert = new Matrix<Single>(3, 3);
  39. m.SetIdentity();
  40. CvInvoke.Invert(m, mInvert, Emgu.CV.CvEnum.DecompMethod.LU);
  41. EmguAssert.IsTrue(m.Equals(mInvert));
  42. }
  43. [Test]
  44. public void TestGetData()
  45. {
  46. float[,] data = new float[2, 3];
  47. data[0, 0] = 1; data[0, 1] = 2; data[0, 2] = 3;
  48. data[1, 0] = 4; data[1, 1] = 5; data[1, 2] = 6;
  49. GCHandle dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
  50. using (Matrix<float> m = new Matrix<float>(data.GetLength(0), data.GetLength(1), dataHandle.AddrOfPinnedObject()))
  51. {
  52. float[,] data2 = m.Data;
  53. for (int i = 0; i < data2.GetLength(0); i++)
  54. for (int j = 0; j < data.GetLength(1); j++)
  55. EmguAssert.IsTrue(data2[i, j] == data[i, j]);
  56. }
  57. dataHandle.Free();
  58. }
  59. [Test]
  60. public void TestSolve()
  61. {
  62. Matrix<Single> lhs = new Matrix<Single>(3, 3);
  63. lhs.SetIdentity();
  64. Matrix<Single> rhs = new Matrix<Single>(new float[,] { { 0.1f }, { 0.2f }, { 0.5f } });
  65. Matrix<Single> result = new Matrix<float>(3, 1);
  66. CvInvoke.Solve(lhs, rhs, result, CvEnum.DecompMethod.LU);
  67. EmguAssert.AreEqual(rhs[0, 0], result[0, 0]);
  68. EmguAssert.AreEqual(rhs[1, 0], result[1, 0]);
  69. EmguAssert.AreEqual(rhs[2, 0], result[2, 0]);
  70. }
  71. [Test]
  72. public void TestNot()
  73. {
  74. Matrix<byte> m = new Matrix<byte>(10, 8);
  75. m.SetValue(1.0);
  76. m._Not();
  77. byte[,] d2 = m.Data;
  78. foreach (byte v in d2)
  79. EmguAssert.IsTrue(254.0 == v);
  80. }
  81. [Test]
  82. public void TestArithmatic()
  83. {
  84. Matrix<byte> m = new Matrix<byte>(10, 8);
  85. m.SetRandNormal(new MCvScalar(), new MCvScalar(30));
  86. Matrix<byte> mMultiplied = m.Mul(2.0);
  87. for (int i = 0; i < m.Rows; i++)
  88. for (int j = 0; j < m.Cols; j++)
  89. EmguAssert.IsTrue(m[i, j] * 2 == mMultiplied[i, j]);
  90. }
  91. [Test]
  92. public void TestCvInvoke()
  93. {
  94. IntPtr mat = CvInvoke.cvCreateMat(10, 10, Emgu.CV.CvEnum.DepthType.Cv32F);
  95. CvInvoke.cvReleaseMat(ref mat);
  96. mat = CvInvoke.cvCreateMat(10, 10, Emgu.CV.CvEnum.DepthType.Cv32S);
  97. CvInvoke.cvReleaseMat(ref mat);
  98. }
  99. /// <summary>
  100. /// Test the matrix constructor that accepts a two dimensional array as input
  101. /// </summary>
  102. [Test]
  103. public void TestData()
  104. {
  105. Byte[,] data = new Byte[20, 30];
  106. Random r = new Random();
  107. Byte[] bytes = new Byte[data.Length];
  108. r.NextBytes(bytes);
  109. for (int i = 0; i < data.GetLength(0); i++)
  110. for (int j = 0; j < data.GetLength(1); j++)
  111. data[i, j] = bytes[i * data.GetLength(1) + j];
  112. Matrix<Byte> m = new Matrix<byte>(data);
  113. Byte[,] data2 = m.Data;
  114. for (int i = 0; i < data.GetLength(0); i++)
  115. for (int j = 0; j < data.GetLength(1); j++)
  116. {
  117. EmguAssert.AreEqual(data[i, j], data2[i, j]);
  118. EmguAssert.AreEqual(data[i, j], m[i, j]);
  119. }
  120. }
  121. /// <summary>
  122. /// Test the matrix transpose function for matrix of Byte
  123. /// </summary>
  124. [Test]
  125. public void TestTransposeByteMatrix()
  126. {
  127. using (Matrix<Byte> mat = new Matrix<Byte>(1, 10))
  128. {
  129. mat.SetRandUniform(new MCvScalar(0.0), new MCvScalar(255.0));
  130. Matrix<Byte> matT = mat.Transpose();
  131. for (int i = 0; i < matT.Rows; i++)
  132. for (int j = 0; j < matT.Cols; j++)
  133. EmguAssert.AreEqual(matT[i, j], mat[j, i]);
  134. }
  135. }
  136. [Test]
  137. public void TestTransposeFloatMatrix()
  138. {
  139. using (Matrix<float> mat = new Matrix<float>(1, 3))
  140. {
  141. mat.SetRandUniform(new MCvScalar(-1000.0), new MCvScalar(1000.0));
  142. Matrix<float> matT = mat.Transpose();
  143. for (int i = 0; i < matT.Rows; i++)
  144. for (int j = 0; j < matT.Cols; j++)
  145. EmguAssert.AreEqual(matT[i, j], mat[j, i]);
  146. }
  147. }
  148. [Test]
  149. public void TestGetDiagColRow()
  150. {
  151. Matrix<double> m = new Matrix<double>(new double[,] { { 1, 2 }, { 3, 4 } });
  152. Matrix<double> diag = m.GetDiag();
  153. EmguAssert.IsTrue(diag[0, 0] == 1);
  154. EmguAssert.IsTrue(diag[1, 0] == 4);
  155. EmguAssert.IsTrue(diag.Sum == m.Trace.V0);
  156. Matrix<double> col1 = m.GetCol(1);
  157. EmguAssert.IsTrue(col1[0, 0] == 2);
  158. EmguAssert.IsTrue(col1[1, 0] == 4);
  159. EmguAssert.IsTrue(col1.Sum == 2 + 4);
  160. Matrix<double> row1 = m.GetRow(1);
  161. EmguAssert.IsTrue(row1[0, 0] == 3);
  162. EmguAssert.IsTrue(row1[0, 1] == 4);
  163. EmguAssert.IsTrue(row1.Sum == 3 + 4);
  164. }
  165. [Test]
  166. public void TestXmlSerializeAndDeserialize()
  167. {
  168. using (Matrix<Byte> mat = new Matrix<byte>(50, 60))
  169. {
  170. mat.SetRandUniform(new MCvScalar(0), new MCvScalar(255));
  171. #if !WINDOWS_PHONE_APP
  172. XDocument doc = Toolbox.XmlSerialize<Matrix<Byte>>(mat);
  173. //Trace.WriteLine(doc.OuterXml);
  174. using (Matrix<Byte> mat2 = Toolbox.XmlDeserialize<Matrix<Byte>>(doc))
  175. EmguAssert.IsTrue(mat.Equals(mat2));
  176. #endif
  177. }
  178. }
  179. #if !NETFX_CORE
  180. [Test]
  181. public void TestRuntimeSerialize1()
  182. {
  183. Matrix<Byte> mat = new Matrix<Byte>(100, 80, 2);
  184. mat.SetRandNormal(new MCvScalar(100, 100, 100), new MCvScalar(50, 50, 50));
  185. System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
  186. formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  187. Byte[] bytes;
  188. using (MemoryStream ms = new MemoryStream())
  189. {
  190. formatter.Serialize(ms, mat);
  191. bytes = ms.GetBuffer();
  192. }
  193. using (MemoryStream ms2 = new MemoryStream(bytes))
  194. {
  195. Matrix<Byte> mat2 = (Matrix<Byte>)formatter.Deserialize(ms2);
  196. EmguAssert.IsTrue(mat.Equals(mat2));
  197. }
  198. }
  199. [Test]
  200. public void TestRuntimeSerialize2()
  201. {
  202. Random r = new Random();
  203. double[,,] data = new double[100, 80, 2];
  204. for (int i = 0; i < data.GetLength(0); i++)
  205. for (int j = 0; j < data.GetLength(1); j++)
  206. for (int k = 0; k < data.GetLength(2); k++)
  207. data[i, j, k] = r.NextDouble();
  208. GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
  209. Matrix<Double> mat = new Matrix<Double>(data.GetLength(0), data.GetLength(1), data.GetLength(2), handle.AddrOfPinnedObject(), sizeof(double) * data.GetLength(1) * data.GetLength(2));
  210. System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
  211. formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  212. Byte[] bytes;
  213. using (MemoryStream ms = new MemoryStream())
  214. {
  215. formatter.Serialize(ms, mat);
  216. bytes = ms.GetBuffer();
  217. }
  218. using (MemoryStream ms2 = new MemoryStream(bytes))
  219. {
  220. Matrix<Double> mat2 = (Matrix<double>)formatter.Deserialize(ms2);
  221. EmguAssert.IsTrue(mat.Equals(mat2));
  222. }
  223. handle.Free();
  224. }
  225. [Test]
  226. public void TestRuntimeSerialize3()
  227. {
  228. MCvPoint3D32f[] data = new MCvPoint3D32f[] { new MCvPoint3D32f() };
  229. GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
  230. Matrix<float> mat = new Matrix<float>(data.GetLength(0), 1, 3, handle.AddrOfPinnedObject(), sizeof(float) * 3);
  231. System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
  232. formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  233. Byte[] bytes;
  234. using (MemoryStream ms = new MemoryStream())
  235. {
  236. formatter.Serialize(ms, mat);
  237. bytes = ms.GetBuffer();
  238. }
  239. using (MemoryStream ms2 = new MemoryStream(bytes))
  240. {
  241. Matrix<float> mat2 = (Matrix<float>)formatter.Deserialize(ms2);
  242. EmguAssert.IsTrue(mat.Equals(mat2));
  243. }
  244. handle.Free();
  245. }
  246. #endif
  247. #if !WINDOWS_PHONE_APP //bad garbage collector in Windows phone results in failed test?
  248. [Test]
  249. public void TestStressTestMatrixGC()
  250. {
  251. int i = 0;
  252. //try
  253. {
  254. for (i = 0; i < 500; i++)
  255. {
  256. Matrix<Single> mat = new Matrix<float>(500, 500);
  257. //Thread.Sleep(5);
  258. }
  259. }
  260. //catch (Exception)
  261. {
  262. }
  263. //finally
  264. {
  265. //Trace.WriteLine(i);
  266. }
  267. }
  268. #endif
  269. [Test]
  270. public void TestSubMatrix()
  271. {
  272. Matrix<float> mat = new Matrix<float>(30, 40);
  273. mat.SetRandUniform(new MCvScalar(0), new MCvScalar(255));
  274. Matrix<float> submat = mat.GetSubRect(new Rectangle(5, 5, 15, 15));
  275. for (int i = 0; i < 15; i++)
  276. for (int j = 0; j < 15; j++)
  277. EmguAssert.AreEqual(mat[i + 5, j + 5], submat[i, j]);
  278. Matrix<float> secondRow = mat.GetRow(1);
  279. for (int i = 0; i < mat.Cols; i++)
  280. {
  281. EmguAssert.AreEqual(mat[1, i], secondRow[0, i]);
  282. }
  283. Matrix<float> thirdCol = mat.GetCol(2);
  284. for (int i = 0; i < mat.Rows; i++)
  285. {
  286. EmguAssert.AreEqual(mat[i, 2], thirdCol[i, 0]);
  287. }
  288. Matrix<float> diagonal = mat.GetDiag();
  289. for (int i = 0; i < Math.Min(mat.Rows, mat.Cols); i++)
  290. {
  291. EmguAssert.AreEqual(diagonal[i, 0], mat[i, i]);
  292. }
  293. }
  294. [Test]
  295. public void TestMinMax()
  296. {
  297. Matrix<float> mat = new Matrix<float>(30, 40);
  298. mat.SetRandUniform(new MCvScalar(0), new MCvScalar(255));
  299. double min, max;
  300. Point minLoc, maxLoc;
  301. mat.MinMax(out min, out max, out minLoc, out maxLoc, null);
  302. }
  303. [Test]
  304. public void TestMinMax2()
  305. {
  306. Matrix<Single> matrix = new Matrix<Single>(10, 10);
  307. matrix.SetValue(5);
  308. matrix[5, 5] = 10;
  309. matrix[3, 3] = 0;
  310. double minVal = 5;
  311. double maxVal = 5;
  312. Point minLoc = new Point();
  313. Point maxLoc = new Point();
  314. matrix.MinMax(out minVal, out maxVal, out minLoc, out maxLoc);
  315. EmguAssert.IsTrue(minVal == 0);
  316. EmguAssert.IsTrue(maxVal == 10);
  317. EmguAssert.IsTrue(minLoc.Equals(new Point(3, 3)));
  318. EmguAssert.IsTrue(maxLoc.Equals(new Point(5, 5)));
  319. }
  320. [Test]
  321. public void TestConcate()
  322. {
  323. Matrix<float> mat = new Matrix<float>(30, 40);
  324. mat.SetRandUniform(new MCvScalar(0), new MCvScalar(255));
  325. Matrix<float> m1 = mat.GetSubRect(new Rectangle(0, 0, mat.Cols, 20));
  326. Matrix<float> m2 = mat.GetSubRect(new Rectangle(0, 20, mat.Cols, mat.Rows - 20));
  327. Matrix<float> mat2 = m1.ConcateVertical(m2);
  328. EmguAssert.IsTrue(mat.Equals(mat2));
  329. Matrix<float> m3 = mat.GetSubRect(new Rectangle(0, 0, 10, mat.Rows));
  330. Matrix<float> m4 = mat.GetSubRect(new Rectangle(10, 0, mat.Cols - 10, mat.Rows));
  331. Matrix<float> mat3 = m3.ConcateHorizontal(m4);
  332. EmguAssert.IsTrue(mat.Equals(mat3));
  333. Matrix<float> m5 = mat.GetRows(0, 5, 1);
  334. Matrix<float> m6 = mat.GetRows(5, 6, 1);
  335. Matrix<float> m7 = mat.GetRows(6, mat.Rows, 1);
  336. EmguAssert.IsTrue(mat.RemoveRows(5, 6).Equals(m5.ConcateVertical(m7)));
  337. EmguAssert.IsTrue(mat.RemoveRows(0, 1).Equals(mat.GetRows(1, mat.Rows, 1)));
  338. EmguAssert.IsTrue(mat.RemoveRows(mat.Rows - 1, mat.Rows).Equals(mat.GetRows(0, mat.Rows - 1, 1)));
  339. }
  340. [Test]
  341. public void TestMultiChannelMatrix()
  342. {
  343. Matrix<float> m = new Matrix<float>(10, 20, 2);
  344. m.SetRandUniform(new MCvScalar(), new MCvScalar(255, 255));
  345. EmguAssert.AreEqual(10, m.Rows);
  346. EmguAssert.AreEqual(20, m.Cols);
  347. EmguAssert.AreEqual(2, m.NumberOfChannels);
  348. #if !WINDOWS_PHONE_APP
  349. XDocument xDoc = Toolbox.XmlSerialize<Matrix<float>>(m);
  350. Matrix<float> m2 = Toolbox.XmlDeserialize<Matrix<float>>(xDoc);
  351. EmguAssert.IsTrue(m.Equals(m2));
  352. #endif
  353. }
  354. [Test]
  355. public void TestEigenVV()
  356. {
  357. int size = 3;
  358. Matrix<float> tmp = new Matrix<float>(size, size);
  359. tmp.SetRandNormal(new MCvScalar(0), new MCvScalar(1));
  360. Matrix<float> symMat = new Matrix<float>(tmp.Size);
  361. CvInvoke.MulTransposed(tmp, symMat, false, null, 1.0, CvEnum.DepthType.Cv32S);
  362. Matrix<float> clone = symMat.Clone();
  363. Matrix<float> evects = new Matrix<float>(symMat.Size);
  364. Matrix<float> evals = new Matrix<float>(symMat.Rows, 1);
  365. CvInvoke.Eigen(symMat, evals, evects);
  366. }
  367. [Test]
  368. public void TestSparseMatrix()
  369. {
  370. int[] dimension = new int[2];
  371. dimension[0] = 1000000;
  372. dimension[1] = 1000000;
  373. //without sparase matrix, a matrix of this size is almost impossible to create in memory
  374. using (SparseMatrix<double> m1 = new SparseMatrix<double>(dimension))
  375. {
  376. m1[3, 10009] = 2.0;
  377. EmguAssert.AreEqual(2.0, m1[3, 10009]);
  378. }
  379. }
  380. [Test]
  381. public void TestMatrixSubtract()
  382. {
  383. Matrix<float> f = new Matrix<float>(600, 480);
  384. //set the value to 300
  385. f.SetValue(new MCvScalar(300));
  386. f -= 10;
  387. using (ScalarArray sa = new ScalarArray(290))
  388. using (Mat absDiff = new Mat())
  389. {
  390. //calculate the different of the value in f mat with 290
  391. CvInvoke.AbsDiff(f, sa, absDiff);
  392. int nonZeroCount = CvInvoke.CountNonZero(absDiff);
  393. //Console.WriteLine(String.Format("number of elements that is not 290: {0}", nonZeroCount));
  394. }
  395. }
  396. /*
  397. [Test]
  398. public void TestDataContractSerializer()
  399. {
  400. DataContractSerializer serializer = new DataContractSerializer(typeof(Image<Bgr, float>));
  401. Image<Bgr, float> img1 = new Image<Bgr,float>(5, 3);
  402. Byte[] bytes = null;
  403. using (MemoryStream ms = new MemoryStream())
  404. {
  405. serializer.WriteObject(ms, img1);
  406. bytes = ms.ToArray();
  407. }
  408. Image<Bgr, float> img2;
  409. DataContractSerializer deserializer = new DataContractSerializer(typeof(Image<Bgr, float>));
  410. using (MemoryStream ms = new MemoryStream(bytes))
  411. {
  412. img2 = deserializer.ReadObject(ms) as Image<Bgr, float>;
  413. }
  414. Debug.Assert(img1.Equals(img2));
  415. }*/
  416. }
  417. }