Tools.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using AForge;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using VisualMath.Accord.Imaging;
  9. using VisualMath.Accord.Imaging.Filters;
  10. using VisualMath.Accord.Math;
  11. namespace VisualMath
  12. {
  13. public class Tools
  14. {
  15. private TimeSpan m_startTime;
  16. private UInt64 m_duration = 0;
  17. public Tools()
  18. {
  19. m_startTime = new TimeSpan(DateTime.Now.Ticks);
  20. }
  21. public Tools(UInt64 durationMs)
  22. {
  23. m_startTime = new TimeSpan(DateTime.Now.Ticks);
  24. m_duration = durationMs;
  25. }
  26. /// <summary>
  27. /// 集成Accord.NET的算法计算拼接坐标
  28. /// </summary>
  29. /// <param name="matSrc"></param>
  30. /// <param name="matTo"></param>
  31. /// <param name="errorFlag"></param>
  32. /// <returns></returns>
  33. public static List<Point> ProcessImageMethod(Bitmap img1)
  34. {
  35. IntPoint[] harrisPoints1;
  36. IntPoint[] harrisPoints2;
  37. // Step 1: Detect feature points using Harris Corners Detector
  38. Accord.Imaging.HarrisCornersDetector harris = new Accord.Imaging.HarrisCornersDetector(0.04f, 1000f);
  39. return harris.ProcessImageMethod(img1);
  40. }
  41. /// <summary>
  42. /// 集成Accord.NET的算法计算拼接坐标
  43. /// </summary>
  44. /// <param name="matSrc"></param>
  45. /// <param name="matTo"></param>
  46. /// <param name="errorFlag"></param>
  47. /// <returns></returns>
  48. public static System.Drawing.Point MatchByAccordDotNet(Bitmap img1, Bitmap img2, out Boolean errorFlag)
  49. {
  50. errorFlag = true;
  51. System.Drawing.Point movePoint = new System.Drawing.Point(-1, -1);
  52. // Step 1: Detect feature points using Harris Corners Detector
  53. HarrisCornersDetector harris = new HarrisCornersDetector(0.04f, 1000f);
  54. IntPoint[] harrisPoints1;
  55. IntPoint[] harrisPoints2;
  56. harrisPoints1 = harris.ProcessImage(img1).ToArray();
  57. harrisPoints2 = harris.ProcessImage(img2).ToArray();
  58. if (harrisPoints1.Length < 3 || harrisPoints2.Length < 3)
  59. {
  60. return new System.Drawing.Point(0, 0);
  61. }
  62. // Step 2: Match feature points using a correlation measure
  63. CorrelationMatching matcher = new CorrelationMatching(9);
  64. IntPoint[][] matches = matcher.Match(img1, img2, harrisPoints1, harrisPoints2);
  65. // Get the two sets of points
  66. IntPoint[] correlationPoints1 = matches[0];
  67. IntPoint[] correlationPoints2 = matches[1];
  68. if (correlationPoints1.Length > 4 && correlationPoints2.Length > 4)
  69. {
  70. // Step 3: Create the homography matrix using a robust estimator
  71. RansacHomographyEstimator ransac = new RansacHomographyEstimator(0.001, 0.99);
  72. MatrixH homography = ransac.Estimate(correlationPoints1, correlationPoints2);
  73. // Plot RANSAC results against correlation results
  74. correlationPoints1/*IntPoint[] inliers1*/ = correlationPoints1.Submatrix(ransac.Inliers);
  75. correlationPoints2/*IntPoint[] inliers2*/ = correlationPoints2.Submatrix(ransac.Inliers);
  76. }
  77. //// Concatenate the two images in a single image (just to show on screen)
  78. //Concatenate concat = new Concatenate(img1);
  79. //Bitmap img3 = concat.Apply(img2);
  80. // Show the marked correlations in the concatenated image
  81. PairsMarker pairs = new PairsMarker(
  82. correlationPoints1/*inliers1*/, // Add image1's width to the X points to show the markings correctly
  83. correlationPoints2/*inliers2.Apply(p => new IntPoint(p.X + img1.Width, p.Y))*/);
  84. //// Step 4: Project and blend the second image using the homography
  85. //Blend blend = new Blend(homography, img1);
  86. //pictureBox.Image = blend.Apply(img2);
  87. movePoint = new System.Drawing.Point(0, 0);
  88. //数组按照元素个数由多到少排序
  89. var arrXMatches = new List<int>();
  90. var arrYMatches = new List<int>();
  91. double scaleI = 0.9;
  92. float XCompute = 0; float YCompute = 0; int coutPt = 0;
  93. for (int i = 0; i < pairs.Points1.Length; i++)
  94. {
  95. arrXMatches.Add((int)(pairs.Points2[i].X - pairs.Points1[i].X));
  96. arrYMatches.Add((int)(pairs.Points2[i].Y - pairs.Points1[i].Y));
  97. }
  98. int[] arrX = arrXMatches.ToArray();
  99. int[] arrY = arrYMatches.ToArray();
  100. Array.Sort(arrX);
  101. Array.Sort(arrY);
  102. int valX1 = arrX[0];//当前
  103. int valX2 = 0;//上一个
  104. int timeX1 = 1;//当前
  105. int timeX2 = 0;//上一个
  106. for (int sortI = 1; sortI < arrX.Length; sortI++)
  107. {
  108. if (arrX[sortI] == valX1)
  109. {
  110. timeX1++;
  111. }
  112. else
  113. {
  114. if (timeX1 > timeX2)
  115. {
  116. valX2 = valX1;
  117. timeX2 = timeX1;
  118. }
  119. valX1 = arrX[sortI];
  120. timeX1 = 1;
  121. }
  122. }
  123. if (timeX1 > timeX2)
  124. {
  125. valX2 = valX1;
  126. timeX2 = timeX1;
  127. }
  128. int valY1 = arrY[0];//当前
  129. int valY2 = 0;//上一个
  130. int timeY1 = 1;//当前
  131. int timeY2 = 0;//上一个
  132. for (int sortI = 1; sortI < arrY.Length; sortI++)
  133. {
  134. if (arrY[sortI] == valY1)
  135. {
  136. timeY1++;
  137. }
  138. else
  139. {
  140. if (timeY1 > timeY2)
  141. {
  142. valY2 = valY1;
  143. timeY2 = timeY1;
  144. }
  145. valY1 = arrY[sortI];
  146. timeY1 = 1;
  147. }
  148. }
  149. if (timeY1 > timeY2)
  150. {
  151. valY2 = valY1;
  152. timeY2 = timeY1;
  153. }
  154. movePoint.X = valX2;
  155. movePoint.Y = valY2;
  156. errorFlag = false;
  157. return movePoint;
  158. }
  159. public TimeSpan StartTime
  160. {
  161. set
  162. {
  163. m_startTime = value;
  164. }
  165. }
  166. public void Restart()
  167. {
  168. m_startTime = new TimeSpan(DateTime.Now.Ticks);
  169. }
  170. public bool Timeout()
  171. {
  172. bool result = false;
  173. UInt64 intervalTime = 0;
  174. TimeSpan stopTime = new TimeSpan(DateTime.Now.Ticks);
  175. intervalTime = (UInt64)m_startTime.Subtract(stopTime).Duration().TotalMilliseconds;
  176. if (intervalTime > m_duration)
  177. {
  178. result = true;
  179. }
  180. return result;
  181. }
  182. }
  183. }