StitchingController.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. using OpenCvSharp.Extensions;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace PaintDotNet.Adjust
  10. {
  11. public class StitchingController
  12. {
  13. public int MathSelect = 3;
  14. /// <summary>
  15. /// 一次:0/连续:1
  16. /// </summary>
  17. public int Mode;
  18. public int State;
  19. /// <summary>
  20. /// 列数
  21. /// </summary>
  22. public readonly int RowN;
  23. /// <summary>
  24. /// 列数
  25. /// </summary>
  26. public readonly int ColumnN;
  27. /// <summary>
  28. /// 拼接率
  29. /// </summary>
  30. public readonly double StitchRate;
  31. Queue<Bitmap> _matBuffer = new Queue<Bitmap>();
  32. public List<Point> _offsetList;
  33. int _counterSum;
  34. int _counterDone;
  35. public Bitmap _img0;
  36. public Bitmap _img1;
  37. public int ImageWidth;
  38. public int ImageHeight;
  39. public OpenCvSharp.Mat _matResult;
  40. public System.Drawing.Bitmap Result
  41. {
  42. get { lock (lockobj) { return _matResult == null ? null : BitmapConverter.ToBitmap(_matResult); } }
  43. }
  44. public bool IsDone { get; private set; } = false;
  45. public Task StitchingTask;
  46. public StitchingController(int columnN, int rowN, double stitchRate)
  47. {
  48. ColumnN = columnN;
  49. RowN = rowN;
  50. StitchRate = stitchRate;
  51. Mode = 1;
  52. }
  53. public StitchingController(int columnN, double stitichRate, Bitmap[] mats)
  54. {
  55. ColumnN = columnN;
  56. RowN = mats.Length / ColumnN;
  57. foreach (var mat in mats) Add(mat);
  58. Mode = 0;
  59. }
  60. public void Add(Bitmap mat)
  61. {
  62. if (_counterDone == 0)//第一张
  63. {
  64. _img1 = mat;
  65. ImageWidth = mat.Width;
  66. ImageHeight = mat.Height;
  67. _matResult = BitmapConverter.ToMat(mat);
  68. _counterDone++;
  69. }
  70. else
  71. {
  72. _matBuffer.Enqueue(mat);
  73. }
  74. _counterSum++;
  75. }
  76. public void Start()
  77. {
  78. StitchingTask = new Task(Runtime);
  79. StitchingTask.Start();
  80. }
  81. public void Stop()
  82. {
  83. IsDone = true;
  84. }
  85. public void Runtime()
  86. {
  87. while (!IsDone)
  88. {
  89. if (Mode == 1 && _counterDone == ColumnN * RowN)
  90. {
  91. Stop();
  92. return;
  93. }
  94. if (_matBuffer.Count == 0)
  95. {
  96. if (Mode == 0)
  97. {
  98. Stop();
  99. return;
  100. }
  101. Thread.Sleep(50);
  102. }
  103. else
  104. {
  105. _img0 = _matBuffer.Dequeue();
  106. Stitch();
  107. _img1 = _img0;
  108. _counterDone++;
  109. }
  110. }
  111. }
  112. public void SynRun()
  113. {
  114. if (_matBuffer.Count == 0)
  115. {
  116. if (Mode == 0)
  117. {
  118. Stop();
  119. return;
  120. }
  121. }
  122. else
  123. {
  124. _img0 = _matBuffer.Dequeue();
  125. Stitch();
  126. _img1 = _img0;
  127. _counterDone++;
  128. }
  129. if (_counterDone == ColumnN * RowN)
  130. {
  131. Stop();
  132. }
  133. }
  134. public void Stitch()
  135. {
  136. bool error = true;
  137. Point movep = new Point(0, 0);
  138. int dir = Dir();
  139. Bitmap img0;
  140. Bitmap img1;
  141. //切图
  142. {
  143. switch (dir)
  144. {
  145. case 1:
  146. LastX += (int)(ImageWidth * (1 - StitchRate));
  147. img0 = AdjustIntent.Cut(_img0, StitchRate, AdjustIntent.Side.Left);
  148. img1 = AdjustIntent.Cut(_img1, StitchRate, AdjustIntent.Side.Right);
  149. break;
  150. case 2:
  151. LastY += (int)(ImageHeight * (1 - StitchRate));
  152. img0 = AdjustIntent.Cut(_img0, StitchRate, AdjustIntent.Side.Top);
  153. img1 = AdjustIntent.Cut(_img1, StitchRate, AdjustIntent.Side.Buttom);
  154. break;
  155. case 3:
  156. LastX -= (int)(ImageWidth * (1 - StitchRate));
  157. img0 = AdjustIntent.Cut(_img0, StitchRate, AdjustIntent.Side.Right);
  158. img1 = AdjustIntent.Cut(_img1, StitchRate, AdjustIntent.Side.Left);
  159. break;
  160. default:
  161. img0 = _img0;
  162. img1 = _img1;
  163. break;
  164. }
  165. }
  166. //拼接算法 1
  167. if (error && (MathSelect & 1) > 0)
  168. {
  169. movep = AdjustIntent.MatchPicOneByOne(img0, img1, out error);
  170. }
  171. ////拼接算法 2
  172. if (error && (MathSelect & 2) > 0)
  173. {
  174. movep = VisualMath.Tools.MatchByAccordDotNet(img0, img1, out error);
  175. }
  176. //按位置偏移
  177. if (error)
  178. {
  179. #region 计算偏移
  180. movep = new Point(0, 0);
  181. }
  182. #endregion
  183. #region 拼接图片
  184. lock (lockobj)
  185. _matResult = MatMerge(BitmapConverter.ToMat(_img0), _matResult, movep);
  186. #endregion
  187. }
  188. private int LastX;
  189. private int LastY;
  190. private object lockobj = new object();
  191. /// <summary>
  192. /// 计算拼接方向
  193. /// </summary>
  194. /// <returns>[1]右;[2]下;[3]左</returns>
  195. private int Dir()
  196. {
  197. var rowIndex = _counterDone / ColumnN;
  198. var colIndex = _counterDone % ColumnN;
  199. var directionRect = 1;
  200. if (colIndex == 0)
  201. {
  202. directionRect = 2;
  203. }
  204. else if (rowIndex % 2 == 1)
  205. {
  206. directionRect = 3;
  207. }
  208. return directionRect;
  209. }
  210. private Point DefaultOffset()
  211. {
  212. int x = (int)(ImageWidth * StitchRate);
  213. int y = (int)(ImageHeight * StitchRate);
  214. int dir = Dir();
  215. switch (dir)
  216. {
  217. case 1:
  218. return new Point(x, 0);
  219. case 2:
  220. return new Point(0, y);
  221. case 3:
  222. return new Point(-x, 0);
  223. default:
  224. return new Point(0, 0);
  225. }
  226. }
  227. /// <summary>
  228. /// 等待拼图全部完成
  229. /// </summary>
  230. public void Wait()
  231. {
  232. while (!IsDone)
  233. {
  234. Thread.Sleep(50);
  235. }
  236. }
  237. public void Dispose()
  238. {
  239. IsDone = true;
  240. Wait();
  241. _matResult.Dispose();
  242. GC.Collect();
  243. }
  244. /// <summary>
  245. /// 利用Opencv 将新图贴到大图上
  246. /// </summary>
  247. /// <param name="matNew"></param>
  248. /// <param name="matBig"></param>
  249. /// <param name="point"></param>
  250. /// <returns></returns>
  251. public OpenCvSharp.Mat MatMerge(OpenCvSharp.Mat matNew, OpenCvSharp.Mat matBig, System.Drawing.Point point)
  252. {
  253. LastX += point.X;
  254. LastY += point.Y;
  255. var result = new OpenCvSharp.Mat();
  256. int srcOriX = 0;
  257. int srcOriY = 0;
  258. int newOriX = 0;
  259. int newOriY = 0;
  260. //得到变换矩阵后拼接图像
  261. int viewWidth = matBig.Width;// 2448;
  262. int viewHeight = matBig.Height;// 2040;
  263. if (LastX <= 0)
  264. {
  265. viewWidth = (int)(viewWidth - LastX);
  266. srcOriX = (int)(-LastX);
  267. newOriX = 0;
  268. LastX = 0;
  269. }
  270. else
  271. {
  272. viewWidth = (int)Math.Max(LastX + matNew.Width, matBig.Width);
  273. newOriX = (int)LastX;
  274. }
  275. if (LastY <= 0)
  276. {
  277. viewHeight = (int)(viewHeight - LastY);
  278. srcOriY = (int)(-LastY);
  279. newOriY = 0;
  280. LastY = 0;
  281. }
  282. else
  283. {
  284. newOriY = (int)LastY;
  285. viewHeight = (int)Math.Max(matBig.Height, matNew.Height + LastY);
  286. }
  287. result = new OpenCvSharp.Mat(new OpenCvSharp.Size(viewWidth, viewHeight), matNew.Type());
  288. OpenCvSharp.Mat tempMatSrc = matNew.Clone();
  289. //复制原有区域part1(<--用户选中的区域)到新图像(<--原图像)
  290. OpenCvSharp.Mat mask = tempMatSrc.CvtColor(OpenCvSharp.ColorConversionCodes.RGBA2GRAY);
  291. OpenCvSharp.Mat posSrc = new OpenCvSharp.Mat(result, new OpenCvSharp.Rect(newOriX, newOriY, matNew.Width, matNew.Height));
  292. tempMatSrc.CopyTo(posSrc, mask);//原图像复制到新图像
  293. OpenCvSharp.Mat tempMat = matBig.Clone();
  294. //复制原有区域part2(<--用户选中的区域)到新图像(<--原图像)
  295. mask = tempMat.CvtColor(OpenCvSharp.ColorConversionCodes.RGBA2GRAY);
  296. OpenCvSharp.Mat pos = new OpenCvSharp.Mat(result, new OpenCvSharp.Rect(srcOriX, srcOriY, matBig.Width, matBig.Height));
  297. tempMat.CopyTo(pos, mask);//原图像复制到新图像
  298. return result;
  299. }
  300. }
  301. }