123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- 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;
- }
- /// <summary>
- /// 集成Accord.NET的算法计算拼接坐标
- /// </summary>
- /// <param name="matSrc"></param>
- /// <param name="matTo"></param>
- /// <param name="errorFlag"></param>
- /// <returns></returns>
- public static List<Point> 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);
- }
- /// <summary>
- /// 集成Accord.NET的算法计算拼接坐标
- /// </summary>
- /// <param name="matSrc"></param>
- /// <param name="matTo"></param>
- /// <param name="errorFlag"></param>
- /// <returns></returns>
- 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<int>();
- var arrYMatches = new List<int>();
- 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;
- }
- }
- }
|