using AForge; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using VisualMath.Accord.Imaging; using VisualMath.Accord.Imaging.Filters; using VisualMath.Accord.Math; namespace VisualMath { public class Tools { private TimeSpan m_startTime; private UInt64 m_duration = 0; public Tools() { m_startTime = new TimeSpan(DateTime.Now.Ticks); } public Tools(UInt64 durationMs) { m_startTime = new TimeSpan(DateTime.Now.Ticks); m_duration = durationMs; } /// /// 集成Accord.NET的算法计算拼接坐标 /// /// /// /// /// public static List ProcessImageMethod(Bitmap img1) { IntPoint[] harrisPoints1; IntPoint[] harrisPoints2; // Step 1: Detect feature points using Harris Corners Detector Accord.Imaging.HarrisCornersDetector harris = new Accord.Imaging.HarrisCornersDetector(0.04f, 1000f); return harris.ProcessImageMethod(img1); } /// /// 集成Accord.NET的算法计算拼接坐标 /// /// /// /// /// public static System.Drawing.Point MatchByAccordDotNet(Bitmap img1, Bitmap img2, out Boolean errorFlag) { errorFlag = true; System.Drawing.Point movePoint = new System.Drawing.Point(-1, -1); // Step 1: Detect feature points using Harris Corners Detector HarrisCornersDetector harris = new HarrisCornersDetector(0.04f, 1000f); IntPoint[] harrisPoints1; IntPoint[] harrisPoints2; harrisPoints1 = harris.ProcessImage(img1).ToArray(); harrisPoints2 = harris.ProcessImage(img2).ToArray(); if (harrisPoints1.Length < 3 || harrisPoints2.Length < 3) { return new System.Drawing.Point(0, 0); } // Step 2: Match feature points using a correlation measure CorrelationMatching matcher = new CorrelationMatching(9); IntPoint[][] matches = matcher.Match(img1, img2, harrisPoints1, harrisPoints2); // Get the two sets of points IntPoint[] correlationPoints1 = matches[0]; IntPoint[] correlationPoints2 = matches[1]; if (correlationPoints1.Length > 4 && correlationPoints2.Length > 4) { // Step 3: Create the homography matrix using a robust estimator RansacHomographyEstimator ransac = new RansacHomographyEstimator(0.001, 0.99); MatrixH homography = ransac.Estimate(correlationPoints1, correlationPoints2); // Plot RANSAC results against correlation results correlationPoints1/*IntPoint[] inliers1*/ = correlationPoints1.Submatrix(ransac.Inliers); correlationPoints2/*IntPoint[] inliers2*/ = correlationPoints2.Submatrix(ransac.Inliers); } //// Concatenate the two images in a single image (just to show on screen) //Concatenate concat = new Concatenate(img1); //Bitmap img3 = concat.Apply(img2); // Show the marked correlations in the concatenated image PairsMarker pairs = new PairsMarker( correlationPoints1/*inliers1*/, // Add image1's width to the X points to show the markings correctly correlationPoints2/*inliers2.Apply(p => new IntPoint(p.X + img1.Width, p.Y))*/); //// Step 4: Project and blend the second image using the homography //Blend blend = new Blend(homography, img1); //pictureBox.Image = blend.Apply(img2); movePoint = new System.Drawing.Point(0, 0); //数组按照元素个数由多到少排序 var arrXMatches = new List(); var arrYMatches = new List(); double scaleI = 0.9; float XCompute = 0; float YCompute = 0; int coutPt = 0; for (int i = 0; i < pairs.Points1.Length; i++) { arrXMatches.Add((int)(pairs.Points2[i].X - pairs.Points1[i].X)); arrYMatches.Add((int)(pairs.Points2[i].Y - pairs.Points1[i].Y)); } int[] arrX = arrXMatches.ToArray(); int[] arrY = arrYMatches.ToArray(); Array.Sort(arrX); Array.Sort(arrY); int valX1 = arrX[0];//当前 int valX2 = 0;//上一个 int timeX1 = 1;//当前 int timeX2 = 0;//上一个 for (int sortI = 1; sortI < arrX.Length; sortI++) { if (arrX[sortI] == valX1) { timeX1++; } else { if (timeX1 > timeX2) { valX2 = valX1; timeX2 = timeX1; } valX1 = arrX[sortI]; timeX1 = 1; } } if (timeX1 > timeX2) { valX2 = valX1; timeX2 = timeX1; } int valY1 = arrY[0];//当前 int valY2 = 0;//上一个 int timeY1 = 1;//当前 int timeY2 = 0;//上一个 for (int sortI = 1; sortI < arrY.Length; sortI++) { if (arrY[sortI] == valY1) { timeY1++; } else { if (timeY1 > timeY2) { valY2 = valY1; timeY2 = timeY1; } valY1 = arrY[sortI]; timeY1 = 1; } } if (timeY1 > timeY2) { valY2 = valY1; timeY2 = timeY1; } movePoint.X = valX2; movePoint.Y = valY2; errorFlag = false; return movePoint; } public TimeSpan StartTime { set { m_startTime = value; } } public void Restart() { m_startTime = new TimeSpan(DateTime.Now.Ticks); } public bool Timeout() { bool result = false; UInt64 intervalTime = 0; TimeSpan stopTime = new TimeSpan(DateTime.Now.Ticks); intervalTime = (UInt64)m_startTime.Subtract(stopTime).Duration().TotalMilliseconds; if (intervalTime > m_duration) { result = true; } return result; } } }