CImageHandler.cs 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. using OTSDataType;
  2. using OTSModelSharp.ImageProcess;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using static OTSDataType.otsdataconst;
  10. namespace OTSModelSharp.ServiceInterface
  11. {
  12. using OpenCvSharp;
  13. using OTSCLRINTERFACE;
  14. using OTSIMGPROC;
  15. using OTSModelSharp.DTLBase;
  16. using System.Drawing.Imaging;
  17. using System.Runtime.InteropServices;
  18. using System.Windows;
  19. public class CImageHandler:IImageProcess
  20. {
  21. #region 通过byte数组生成BMP图像文件
  22. /// <summary>
  23. /// 将一个byte的数组转换为8bit灰度位图
  24. /// </summary>
  25. /// <param name="data">数组</param>
  26. /// <param name="width">图像宽度</param>
  27. /// <param name="height">图像高度</param>
  28. /// <returns>位图</returns>
  29. public static Bitmap ToGrayBitmap(byte[] data, int width, int height)
  30. {
  31. if (width == 0 || height == 0)
  32. return null;
  33. //// 申请目标位图的变量,并将其内存区域锁定
  34. Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
  35. //// BitmapData这部分内容 需要 using System.Drawing.Imaging;
  36. BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height),
  37. ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
  38. //// 获取图像参数
  39. // 扫描线的宽度
  40. int stride = bmpData.Stride;
  41. // 显示宽度与扫描线宽度的间隙
  42. int offset = stride - width;
  43. // 获取bmpData的内存起始位置
  44. IntPtr iptr = bmpData.Scan0;
  45. // 用stride宽度,表示这是内存区域的大小
  46. int scanBytes = stride * height;
  47. //// 下面把原始的显示大小字节数组转换为内存中实际存放的字节数组
  48. int posScan = 0;
  49. int posReal = 0;// 分别设置两个位置指针,指向源数组和目标数组
  50. byte[] pixelValues = new byte[scanBytes]; //为目标数组分配内存
  51. //for (int x = height-1;x>=0 ; x--) data[startIndex+ y];//
  52. for (int x = 0; x < height; x++)
  53. {
  54. int startIndex = x * width;
  55. //// 下面的循环节是模拟行扫描
  56. for (int y = 0; y < width; y++)
  57. {
  58. pixelValues[posScan++] = data[posReal++];
  59. }
  60. posScan += offset; //行扫描结束,要将目标位置指针移过那段“间隙”
  61. }
  62. //// 用Marshal的Copy方法,将刚才得到的内存字节数组复制到BitmapData中
  63. System.Runtime.InteropServices.Marshal.Copy(pixelValues, 0, iptr, scanBytes);
  64. bmp.UnlockBits(bmpData); // 解锁内存区域
  65. //// 下面的代码是为了修改生成位图的索引表,从伪彩修改为灰度
  66. ColorPalette tempPalette;
  67. using (Bitmap tempBmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
  68. {
  69. tempPalette = tempBmp.Palette;
  70. }
  71. for (int i = 0; i < 256; i++)
  72. {
  73. tempPalette.Entries[i] = Color.FromArgb(i, i, i);
  74. }
  75. bmp.Palette = tempPalette;
  76. return bmp;
  77. }
  78. #endregion
  79. public static byte[] BitmapToGrayByte(Bitmap bitmap)
  80. {
  81. // 申请目标位图的变量,并将其内存区域锁定
  82. BitmapData bitmapDat = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
  83. // 获取bmpData的内存起始位置
  84. IntPtr intPtr = bitmapDat.Scan0;
  85. byte[] image = new byte[bitmap.Width * bitmap.Height];//原始数据
  86. Marshal.Copy(intPtr, image, 0, bitmap.Width * bitmap.Height); // 将数据复制到byte数组中,
  87. //解锁内存区域
  88. bitmap.UnlockBits(bitmapDat);
  89. return image;
  90. }
  91. //获取测量的BSE图
  92. //COTSSample WSample: 工作样品测量
  93. //Byte[] BSEImage: 带背景图数据
  94. //int iHeight: 图像高度
  95. //int iWidth: 图像宽度
  96. //Byte[]BSEImageNoBG : 去背景图数据
  97. public bool GetBSEImage(COTSImageProcParam ImgProcPrm, double pixelSize, byte[] BSEImage, int iHeight, int iWidth, ref byte[] BSEImageNoBG)
  98. {
  99. Rectangle rect = new Rectangle();
  100. rect.Height = iHeight;
  101. rect.Width = iWidth;
  102. CBSEImgClr pBSEImageIn = new CBSEImgClr(rect);
  103. CBSEImgClr pBSEImageOut = new CBSEImgClr(rect);
  104. //pBSEImageIn.SetImageRect(rect);
  105. pBSEImageIn.SetImageData(BSEImage,iWidth, iHeight );
  106. //pBSEImageOut.SetImageRect(rect);
  107. if (null == ImgProcPrm)
  108. {
  109. return false;
  110. }
  111. RemoveBackGround(pBSEImageIn, ImgProcPrm, pixelSize ,ref pBSEImageOut);
  112. BSEImageNoBG = pBSEImageOut.GetImageDataPtr();
  113. return true;
  114. }
  115. /// <summary>
  116. /// 获取测量的BSE图
  117. /// </summary>
  118. /// <param name="BSEImage">BSE原数据</param>
  119. /// <param name="iHeight">图像高度</param>
  120. /// <param name="iWidth">图像宽度</param>
  121. /// <param name="grayStart"></param>
  122. /// <param name="grayEnd"></param>
  123. /// <param name="BSEImageNoBG">去背景图数据</param>
  124. /// <returns></returns>
  125. public bool GetBSEImage(byte[] BSEImage, int iHeight, int iWidth, int grayStart, int grayEnd, ref byte[] BSEImageNoBG)
  126. {
  127. Rectangle rect = new Rectangle();
  128. rect.Height = iHeight;
  129. rect.Width = iWidth;
  130. CBSEImgClr pBSEImageIn = new CBSEImgClr(rect);
  131. CBSEImgClr pBSEImageOut = new CBSEImgClr(rect);
  132. //pBSEImageIn.SetImageRect(rect);
  133. pBSEImageIn.SetImageData(BSEImage, iWidth, iHeight );
  134. CIntRangeClr cIntRangeClr = new CIntRangeClr();
  135. cIntRangeClr.SetStart(grayStart);
  136. cIntRangeClr.SetEnd(grayEnd);
  137. OTSCLRINTERFACE.ImageProForClr imgpro = new OTSCLRINTERFACE.ImageProForClr();
  138. int num=0;
  139. imgpro.GetSpecialGrayRangeImage(pBSEImageIn, cIntRangeClr, pBSEImageOut,ref num);
  140. for (int i = 0; i < iWidth; i++)
  141. {
  142. for (int j = 0; j < iHeight; j++)
  143. {
  144. var bse = pBSEImageOut.GetBSEValue(i, j);
  145. if (bse == 255)
  146. {
  147. var originalBse = pBSEImageIn.GetBSEValue(i, j);
  148. pBSEImageOut.SetBSEValue(i, j, originalBse);
  149. }
  150. else
  151. {
  152. pBSEImageOut.SetBSEValue(i, j, 255);
  153. }
  154. }
  155. }
  156. BSEImageNoBG = pBSEImageOut.GetImageDataPtr();
  157. return true;
  158. }
  159. // remove background
  160. public void RemoveBackGround(CBSEImgClr a_pImgIn, COTSImageProcParam a_pImgProcessParam, double a_pixelSize, ref CBSEImgClr a_pImgOut)
  161. {
  162. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  163. List<COTSParticleClr> specialGreyparts = new List<COTSParticleClr>();
  164. if (!RemoveBGAndGetParts(a_pImgIn, a_pImgProcessParam, a_pixelSize, ref parts))
  165. {
  166. return;
  167. }
  168. if (a_pImgProcessParam.GetSpecialGreyRangeParam().GetIsToRun())
  169. {
  170. var param = a_pImgProcessParam.GetSpecialGreyRangeParam();
  171. var ranges = param.GetSpecialGreyRanges();
  172. foreach (var r in ranges)
  173. {
  174. CIntRangeClr r1 = new CIntRangeClr();
  175. r1.SetStart(r.range.GetStart());
  176. r1.SetEnd(r.range.GetEnd());
  177. CDoubleRangeClr r2 = new CDoubleRangeClr();
  178. r2.SetStart(r.diameterRange.GetStart());
  179. r2.SetEnd(r.diameterRange.GetEnd());
  180. GetParticlesBySpecialGray(a_pImgIn, r1, r2, a_pixelSize, ref specialGreyparts);
  181. }
  182. }
  183. for (int i = 0; i < a_pImgOut.GetWidth(); i++)
  184. {
  185. for (int j = 0; j < a_pImgOut.GetHeight(); j++)
  186. {
  187. a_pImgOut.SetBSEValue(i, j, 255);
  188. }
  189. }
  190. if (specialGreyparts.Count > 0)
  191. {
  192. foreach (var p in specialGreyparts)
  193. {
  194. foreach (var s in p.GetFeature().GetSegmentsList())
  195. {
  196. for (int i = s.GetStart(); i < s.GetStart() + s.GetLength(); i++)
  197. {
  198. var bseValue = a_pImgIn.GetBSEValue(i, s.GetHeight());
  199. a_pImgOut.SetBSEValue(i, s.GetHeight(), bseValue);
  200. }
  201. }
  202. }
  203. }
  204. foreach (var p in parts)
  205. {
  206. foreach (var s in p.GetFeature().GetSegmentsList())
  207. {
  208. for (int i = s.GetStart(); i < s.GetStart() + s.GetLength(); i++)
  209. {
  210. var bseValue = a_pImgIn.GetBSEValue(i, s.GetHeight());
  211. a_pImgOut.SetBSEValue(i, s.GetHeight(), bseValue);
  212. }
  213. }
  214. }
  215. return;
  216. }
  217. public void BDilate3(string source, string target, int wDegree, int rows, int columns)
  218. {
  219. throw new NotImplementedException();
  220. }
  221. public void BErode3(string source, string target, int wDegree, int rows, int columns)
  222. {
  223. throw new NotImplementedException();
  224. }
  225. public bool CalParticleImageProp( COTSParticleClr part, double a_pixelSize)
  226. {
  227. OTSCLRINTERFACE.ImageProForClr imgpro = new OTSCLRINTERFACE.ImageProForClr();
  228. imgpro.CalcuParticleImagePropertes(part,a_pixelSize);
  229. return true;
  230. }
  231. public bool RemoveBGAndGetParts(CBSEImgClr img, COTSImageProcParam a_pImgProcessParam,double a_pixelSize,ref List<COTSParticleClr> parts)
  232. {
  233. OTSCLRINTERFACE.ImageProForClr imgpro = new OTSCLRINTERFACE.ImageProForClr();
  234. OTSCLRINTERFACE.COTSImgProcPrmClr prm = GetImageProcPrmClr(a_pImgProcessParam);
  235. OTSCLRINTERFACE.COTSFieldDataClr flddataclr = new OTSCLRINTERFACE.COTSFieldDataClr();
  236. if (!imgpro.GetFieldDataFromImage(img, prm, a_pixelSize, flddataclr))
  237. {
  238. return false;
  239. }
  240. parts = flddataclr.GetParticleList();
  241. return true;
  242. }
  243. public bool GetParticlesBySpecialGray(CBSEImgClr img, CIntRangeClr grayrange, CDoubleRangeClr diameterRange,double a_pixelSize, ref List<COTSParticleClr> parts)
  244. {
  245. OTSCLRINTERFACE.ImageProForClr imgpro = new OTSCLRINTERFACE.ImageProForClr();
  246. //OTSCLRINTERFACE.COTSImgProcPrmClr prm = GetImageProcPrmClr(a_pImgProcessParam);
  247. OTSCLRINTERFACE.COTSFieldDataClr flddataclr = new OTSCLRINTERFACE.COTSFieldDataClr();
  248. imgpro.GetParticlesBySpecialPartGrayRange(img, grayrange,diameterRange,a_pixelSize, flddataclr);
  249. parts = flddataclr.GetParticleList();
  250. return true;
  251. }
  252. private COTSImgProcPrmClr GetImageProcPrmClr(COTSImageProcParam a_oSource)
  253. {
  254. COTSImgProcPrmClr prmclr = new COTSImgProcPrmClr();
  255. OTSCLRINTERFACE.CDoubleRangeClr r1 = new OTSCLRINTERFACE.CDoubleRangeClr(a_oSource.GetIncAreaRange().GetStart(), a_oSource.GetIncAreaRange().GetEnd());
  256. prmclr.SetIncArea(r1);
  257. OTSCLRINTERFACE.CIntRangeClr r2= new OTSCLRINTERFACE.CIntRangeClr(a_oSource.GetBGGray().GetStart(), a_oSource.GetBGGray().GetEnd());
  258. prmclr.SetBGGray(r2);
  259. OTSCLRINTERFACE.CIntRangeClr r3= new OTSCLRINTERFACE.CIntRangeClr(a_oSource.GetParticleGray().GetStart(), a_oSource.GetParticleGray().GetEnd());
  260. prmclr.SetParticleGray(r3);
  261. prmclr.SetBGRemoveType((int)a_oSource.GetBGRemoveType());
  262. prmclr.SetAutoBGRemoveType((int)a_oSource.GetAutoBGRemoveType());
  263. prmclr.SetErrodDilateParam(a_oSource.GetErrodDilateParam());
  264. prmclr.SetOverlapParam(a_oSource.GetOverlapParam());
  265. return prmclr;
  266. }
  267. public bool MergeBigBoundaryParticles(List<COTSFieldData> allFields, double pixelSize, int scanFieldSize, System.Drawing.Size ResolutionSize, ref List<COTSParticleClr> mergedParts)
  268. {
  269. List<COTSFieldDataClr> fldclrs = new List<COTSFieldDataClr>();
  270. foreach (var f in allFields)
  271. {
  272. COTSFieldDataClr fldclr = new COTSFieldDataClr();
  273. PointF p1 = f.GetOTSPosition();
  274. System.Drawing.Point p2 = new System.Drawing.Point((int)p1.X, (int)p1.Y);
  275. fldclr.SetPosition(p2);
  276. fldclr.SetImageWidth(f.Width);
  277. fldclr.SetImageHeight(f.Height);
  278. var parts = f.GetListAnalysisParticles();
  279. foreach (var p in parts)
  280. {
  281. fldclr.AddParticle(p);
  282. }
  283. fldclrs.Add(fldclr);
  284. }
  285. OTSCLRINTERFACE.ImageProForClr imgpro = new OTSCLRINTERFACE.ImageProForClr();
  286. imgpro.MergeBigBoundaryParticles(fldclrs, pixelSize, scanFieldSize, ResolutionSize, mergedParts);
  287. return true;
  288. }
  289. /// <summary>
  290. /// 根据Segment寻找边缘坐标点
  291. /// </summary>
  292. /// <param name="width"></param>
  293. /// <param name="height"></param>
  294. /// <param name="segmentClrs"></param>
  295. /// <returns></returns>
  296. public static List<System.Drawing.Point> FindContoursBySegment(int width, int height, List<COTSSegmentClr> segmentClrs)
  297. {
  298. List<System.Drawing.Point> points = new List<System.Drawing.Point>();
  299. using (Mat mat = new Mat(height, width, MatType.CV_8UC1, new Scalar(0)))
  300. {
  301. for (int i = 0; i < segmentClrs.Count; i++)
  302. {
  303. Cv2.Line(mat, new OpenCvSharp.Point(segmentClrs[i].GetStart(), segmentClrs[i].GetHeight()), new OpenCvSharp.Point(segmentClrs[i].GetStart() + segmentClrs[i].GetLength(), segmentClrs[i].GetHeight()), Scalar.White, 1, LineTypes.AntiAlias);
  304. }
  305. Point[][] contours;
  306. HierarchyIndex[] hierarchy;
  307. Cv2.FindContours(mat, out contours, out hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxSimple, null);
  308. for (int i = 0; i < contours.ToList().Count(); i++)
  309. {
  310. for (int j = 0; j < contours[i].Count(); j++)
  311. {
  312. System.Drawing.Point point = new System.Drawing.Point(contours[i][j].X, contours[i][j].Y);
  313. if (!points.Contains(point))
  314. {
  315. points.Add(point);
  316. }
  317. }
  318. }
  319. }
  320. return points;
  321. }
  322. public bool RepeatedParticleTreatment(List<COTSFieldData> allFields, COTSSample theSample, string stdPath)
  323. {
  324. int maxPartCount = theSample.GetMsrParams().GetXRayParam().GetXrayLimit();
  325. int overlap = theSample.GetMsrParams().GetImageProcessParam().GetOverlapParam();
  326. double pixelSize = theSample.CalculatePixelSize();
  327. System.Drawing.Size resolutionSize = theSample.GetResolutionSize();
  328. int scanFieldSizeX = theSample.GetSEMDataMsr().GetScanFieldSize();
  329. int scanFieldSizeY = scanFieldSizeX * resolutionSize.Height / resolutionSize.Width;
  330. int offsetX = scanFieldSizeX - (int)(overlap * pixelSize);
  331. int offsetY = scanFieldSizeY - (int)(overlap * pixelSize);
  332. List<COTSParticleClr> deletePartList = new List<COTSParticleClr>();
  333. foreach (var item in allFields)
  334. {
  335. if (!item.GetIsMeasureComplete())
  336. {
  337. break;
  338. }
  339. //找到上下左右四帧图
  340. List<COTSFieldData> leftField = allFields.Where(a => a.OTSPos == new System.Drawing.PointF(item.OTSPos.X - offsetX, item.OTSPos.Y)).ToList();
  341. List<COTSFieldData> rightField = allFields.Where(a => a.OTSPos == new System.Drawing.PointF(item.OTSPos.X + offsetX, item.OTSPos.Y)).ToList();
  342. List<COTSFieldData> upField = allFields.Where(a => a.OTSPos == new System.Drawing.PointF(item.OTSPos.X, item.OTSPos.Y + offsetY)).ToList();
  343. List<COTSFieldData> downField = allFields.Where(a => a.OTSPos == new System.Drawing.PointF(item.OTSPos.X, item.OTSPos.Y - offsetY)).ToList();
  344. //判断是否有效
  345. if (leftField.Count() == 1)//包含左帧图
  346. {
  347. if (leftField[0].GetIsMeasureComplete() == true)//存在测量帧图
  348. {
  349. //寻找重叠颗粒
  350. deletePartList.AddRange(FieldFun(leftField[0], item, "left_right", resolutionSize, overlap));
  351. }
  352. }
  353. if (rightField.Count() == 1)//包含右帧图
  354. {
  355. if (rightField[0].GetIsMeasureComplete() == true)//存在测量帧图
  356. {
  357. //寻找重叠颗粒
  358. deletePartList.AddRange(FieldFun(item, rightField[0], "left_right", resolutionSize, overlap));
  359. }
  360. }
  361. if (upField.Count() == 1)//包含上帧图
  362. {
  363. if (upField[0].GetIsMeasureComplete() == true)//存在测量帧图
  364. {
  365. //寻找重叠颗粒
  366. deletePartList.AddRange(FieldFun(upField[0], item, "up_down", resolutionSize, overlap));
  367. }
  368. }
  369. if (downField.Count() == 1)//包含下帧图
  370. {
  371. if (downField[0].GetIsMeasureComplete() == true)//存在测量帧图
  372. {
  373. //寻找重叠颗粒
  374. deletePartList.AddRange(FieldFun(item, downField[0], "up_down", resolutionSize, overlap));
  375. }
  376. }
  377. }
  378. //数据库操作
  379. SQLiteHelper sQLiteHelper = new SQLiteHelper(stdPath);
  380. sQLiteHelper.GetDBConnection();
  381. sQLiteHelper.BeginTransaction();
  382. deletePartList = deletePartList.Distinct().ToList();//去重
  383. if (!sQLiteHelper.DeletePartForTransaction(deletePartList))
  384. {
  385. return false;
  386. }
  387. sQLiteHelper.CommitTransaction();
  388. return true;
  389. }
  390. private List<COTSParticleClr> FieldFun(COTSFieldData left_upField, COTSFieldData right_downField, string style, System.Drawing.Size resolutionSize, int overlap)
  391. {
  392. List<COTSParticleClr> particleClrs = new List<COTSParticleClr>();
  393. if (style == "left_right")
  394. {
  395. //寻找左帧图的右侧区域颗粒
  396. double left_upField_sum = 0;
  397. double right_downField_sum = 0;
  398. foreach (var upParticles in left_upField.GetListAnalysisParticles())
  399. {
  400. Rectangle upRectangle = (Rectangle)upParticles.GetParticleRect();
  401. if (upRectangle.Top > resolutionSize.Height - overlap / 2)//未跨界
  402. {
  403. left_upField_sum += upParticles.GetActualArea();
  404. }
  405. }
  406. foreach (var downParticles in right_downField.GetListAnalysisParticles())
  407. {
  408. Rectangle downRectangle = (Rectangle)downParticles.GetParticleRect();
  409. if (downRectangle.Top > resolutionSize.Height - overlap / 2)//未跨界
  410. {
  411. right_downField_sum += downParticles.GetActualArea();
  412. }
  413. }
  414. if (left_upField_sum < right_downField_sum)
  415. {
  416. foreach (var leftParticles in left_upField.GetListAnalysisParticles())
  417. {
  418. Rectangle leftRectangle = (Rectangle)leftParticles.GetParticleRect();
  419. if (leftRectangle.Left > resolutionSize.Width - overlap)//未跨界
  420. {
  421. particleClrs.Add(leftParticles);
  422. }
  423. }
  424. }
  425. else
  426. {
  427. foreach (var downParticles in right_downField.GetListAnalysisParticles())
  428. {
  429. Rectangle downRectangle = (Rectangle)downParticles.GetParticleRect();
  430. if (downRectangle.Right < overlap / 2)//未跨界
  431. {
  432. particleClrs.Add(downParticles);
  433. }
  434. }
  435. }
  436. }
  437. else
  438. {
  439. //寻找上帧图的下侧区域颗粒
  440. double left_upField_sum = 0;
  441. double right_downField_sum = 0;
  442. foreach (var upParticles in left_upField.GetListAnalysisParticles())
  443. {
  444. Rectangle upRectangle = (Rectangle)upParticles.GetParticleRect();
  445. if (upRectangle.Top > resolutionSize.Height - overlap / 2)//未跨界
  446. {
  447. left_upField_sum += upParticles.GetActualArea();
  448. }
  449. }
  450. foreach (var downParticles in right_downField.GetListAnalysisParticles())
  451. {
  452. Rectangle downRectangle = (Rectangle)downParticles.GetParticleRect();
  453. if (downRectangle.Top > resolutionSize.Height - overlap / 2)//未跨界
  454. {
  455. right_downField_sum += downParticles.GetActualArea();
  456. }
  457. }
  458. if (left_upField_sum < right_downField_sum)
  459. {
  460. foreach (var upParticles in left_upField.GetListAnalysisParticles())
  461. {
  462. Rectangle upRectangle = (Rectangle)upParticles.GetParticleRect();
  463. if (upRectangle.Top > resolutionSize.Height - overlap / 2)//未跨界
  464. {
  465. particleClrs.Add(upParticles);
  466. }
  467. }
  468. }
  469. else
  470. {
  471. foreach (var downParticles in right_downField.GetListAnalysisParticles())
  472. {
  473. Rectangle downRectangle = (Rectangle)downParticles.GetParticleRect();
  474. if (downRectangle.Bottom < overlap / 2)//未跨界
  475. {
  476. particleClrs.Add(downParticles);
  477. }
  478. }
  479. }
  480. }
  481. return particleClrs;
  482. }
  483. public Mat CombinImageX(Mat[] list_mats, int OverlapParam, int type)
  484. {
  485. List<Mat> matStitch = new List<Mat>();//拼接
  486. List<Mat> matCombin = new List<Mat>();//合并
  487. for (int i = 0; i < list_mats.Count(); i++)
  488. {
  489. if (i == 0)//首张
  490. {
  491. matCombin.Add(new Mat(list_mats[i], new Rect(0, 0, list_mats[i].Width - OverlapParam - 100, list_mats[i].Height)));
  492. matStitch.Add(new Mat(list_mats[i], new Rect(list_mats[i].Width - OverlapParam - 100, 0, OverlapParam + 100, list_mats[i].Height)));
  493. }
  494. else if(i == list_mats.Count() - 1)//末张
  495. {
  496. matStitch.Add(new Mat(list_mats[i], new Rect(0, 0, OverlapParam + 100, list_mats[i].Height)));
  497. matCombin.Add(new Mat(list_mats[i], new Rect(OverlapParam + 100, 0, list_mats[i].Width - OverlapParam - 100, list_mats[i].Height)));
  498. }
  499. else
  500. {
  501. matStitch.Add(new Mat(list_mats[i], new Rect(0, 0, OverlapParam + 100, list_mats[i].Height)));
  502. matCombin.Add(new Mat(list_mats[i], new Rect(OverlapParam + 100, 0, list_mats[i].Width - (OverlapParam + 100) * 2, list_mats[i].Height)));
  503. matStitch.Add(new Mat(list_mats[i], new Rect(list_mats[i].Width - OverlapParam - 100, 0, OverlapParam + 100, list_mats[i].Height)));
  504. }
  505. }
  506. for (int i = 0; i < matStitch.Count; i += 2)
  507. {
  508. if (matStitch.Count == 1)
  509. {
  510. matCombin.Insert(i + 1, matStitch[i]);
  511. }
  512. else
  513. {
  514. matCombin.Insert(i + 1, StitchImageX((int)(OverlapParam / 2 * 1.2), type, matStitch[i], matStitch[i + 1]));
  515. }
  516. }
  517. Mat pano = new OpenCvSharp.Mat();
  518. Cv2.HConcat(matCombin.ToArray(), pano);
  519. return pano;
  520. }
  521. public Mat CombinImageY(Mat[] list_mats, int OverlapParam, int type)
  522. {
  523. List<Mat> matStitch = new List<Mat>();//拼接
  524. List<Mat> matCombin = new List<Mat>();//合并
  525. for (int i = 0; i < list_mats.Count(); i++)
  526. {
  527. if (i == 0)//首张
  528. {
  529. matCombin.Add(new Mat(list_mats[i], new Rect(0, 0, list_mats[i].Width, list_mats[i].Height - OverlapParam - 100)));
  530. matStitch.Add(new Mat(list_mats[i], new Rect(0, list_mats[i].Height - OverlapParam - 100, list_mats[i].Width, OverlapParam + 100)));
  531. }
  532. else if (i == list_mats.Count() - 1)//末张
  533. {
  534. matStitch.Add(new Mat(list_mats[i], new Rect(0, 0, list_mats[i].Width, OverlapParam + 100)));
  535. matCombin.Add(new Mat(list_mats[i], new Rect(0, OverlapParam + 100, list_mats[i].Width, list_mats[i].Height - OverlapParam - 100)));
  536. }
  537. else
  538. {
  539. matStitch.Add(new Mat(list_mats[i], new Rect(0, 0, list_mats[i].Width, OverlapParam + 100)));
  540. matCombin.Add(new Mat(list_mats[i], new Rect(0, OverlapParam + 100, list_mats[i].Width, list_mats[i].Height - (OverlapParam + 100) * 2)));
  541. matStitch.Add(new Mat(list_mats[i], new Rect(0, list_mats[i].Height - OverlapParam - 100, list_mats[i].Width, OverlapParam + 100)));
  542. }
  543. }
  544. for (int i = 0; i < matStitch.Count; i += 2)
  545. {
  546. if (matStitch.Count == 1)
  547. {
  548. matCombin.Insert(i + 1, matStitch[i]);
  549. }
  550. else
  551. {
  552. matCombin.Insert(i + 1, StitchImageY(OverlapParam, type, matStitch[i], matStitch[i + 1]));
  553. }
  554. }
  555. Mat pano = new OpenCvSharp.Mat();
  556. Cv2.VConcat(matCombin.ToArray(), pano);
  557. return pano;
  558. }
  559. public struct MStitch
  560. {
  561. public int Pwidth;//单幅图像的宽度
  562. public int Pheight;//单幅图像的高度
  563. public int W_min;//最小的重叠区域宽度
  564. public int W_max;//最大的重叠区域宽度
  565. public int H_min;//最小的重叠区域高度
  566. public double minval;//块过滤阈值
  567. public Mat im;//图像信息
  568. }
  569. public struct ImageParam
  570. {
  571. public int W_box;//宽度信息
  572. public int H_box;//高度信息
  573. public int bdown;//上下信息
  574. public MStitch mStitch; //参数结构
  575. public Mat im;//图像信息
  576. }
  577. /// <summary>
  578. /// 横向拼图
  579. /// </summary>
  580. public Mat StitchImageX(int min_w, int type, Mat newImg1, Mat newImg2)
  581. {
  582. MStitch mStitch = new MStitch();
  583. mStitch.Pwidth = newImg1.Width;
  584. mStitch.Pheight = newImg1.Height;
  585. mStitch.W_min = min_w;
  586. mStitch.W_max = min_w;
  587. mStitch.H_min = newImg1.Height;
  588. mStitch.minval = 255;
  589. mStitch.im = newImg1;
  590. ImageParam imageParam = Fun_Match(newImg2, mStitch);
  591. imageParam.im = newImg2;
  592. if (type == 2)
  593. {
  594. return Fun_Stitch(imageParam);
  595. }
  596. else
  597. {
  598. return Fun_StitchRGB(imageParam);
  599. }
  600. }
  601. /// <summary>
  602. /// 纵向拼图
  603. /// </summary>
  604. public Mat StitchImageY(int min_w, int type, Mat newImg1, Mat newImg2)
  605. {
  606. Cv2.Transpose(newImg1, newImg1);
  607. Cv2.Flip(newImg1, newImg1, FlipMode.X);
  608. Cv2.Transpose(newImg2, newImg2);
  609. Cv2.Flip(newImg2, newImg2, FlipMode.X);
  610. MStitch mStitch = new MStitch();
  611. mStitch.Pwidth = newImg1.Width;
  612. mStitch.Pheight = newImg1.Height;
  613. mStitch.W_min = min_w - 100;
  614. mStitch.W_max = min_w + 100;
  615. mStitch.H_min = newImg1.Height;
  616. mStitch.minval = 255;
  617. mStitch.im = newImg1;
  618. ImageParam imageParam = Fun_Match(newImg2, mStitch);
  619. imageParam.im = newImg2;
  620. Mat result = type == 2 ? Fun_Stitch(imageParam) : Fun_StitchRGB(imageParam);
  621. Cv2.Transpose(result, result);
  622. Cv2.Flip(result, result, FlipMode.Y);
  623. return result;
  624. }
  625. public static ImageParam Fun_Match(Mat im2, MStitch mStitch)
  626. {
  627. ImageParam imageParam = new ImageParam();
  628. double imsum = 0;
  629. int x1 = 0;
  630. int y1 = 0;
  631. int x2 = 0;
  632. int y2 = 0;
  633. int w_ind = 0;
  634. int h_ind = 0;
  635. //在上窗口所有匹配块内进行搜索
  636. for (int w = mStitch.W_min; w <= mStitch.W_max; w++)
  637. {
  638. for (int h = mStitch.H_min; h <= mStitch.Pheight; h++)
  639. {
  640. imsum = 0;//块差分集初始化
  641. x2 = 1;
  642. for (x1 = mStitch.Pwidth - w; x1 <= mStitch.Pwidth; x1 += 5)
  643. {
  644. y2 = 1;
  645. for (y1 = mStitch.Pheight - h + 1; y1 <= mStitch.Pheight; y1 += 5)
  646. {
  647. //块差分集计算
  648. CheckRC(ref x1, ref y1, mStitch.im);
  649. CheckRC(ref x2, ref y2, im2);
  650. imsum = imsum + Math.Abs(mStitch.im.At<Vec3b>(y1, x1).Item0 - im2.At<Vec3b>(y2, x2).Item0);
  651. y2 = y2 + 5;
  652. }
  653. x2 = x2 + 5;
  654. }
  655. //阈值更新
  656. if (imsum * 5 * 5 <= mStitch.minval * w * h)
  657. {
  658. mStitch.minval = imsum * 5 * 5 / (w * h);
  659. w_ind = w;
  660. h_ind = h;
  661. }
  662. }
  663. }
  664. imageParam.W_box = w_ind;
  665. imageParam.H_box = h_ind;
  666. imageParam.bdown = 1;
  667. //在下窗口所有匹配块内进行搜索
  668. Parallel.For(mStitch.W_min, mStitch.W_max, w =>
  669. {
  670. Parallel.For(mStitch.H_min, mStitch.Pheight, h =>
  671. {
  672. imsum = 0;//块差分集初始化
  673. x2 = 1;
  674. for (x1 = mStitch.Pwidth - w; x1 <= mStitch.Pwidth; x1 += 5)
  675. {
  676. y1 = 1;
  677. for (y2 = mStitch.Pheight - h + 1; y2 <= mStitch.Pheight; y2 += 5)
  678. {
  679. //块差分集计算
  680. CheckRC(ref x1, ref y1, mStitch.im);
  681. CheckRC(ref x2, ref y2, im2);
  682. imsum = imsum + Math.Abs(mStitch.im.At<Vec3b>(y1, x1).Item0 - im2.At<Vec3b>(y2, x2).Item0);
  683. y1 = y1 + 5;
  684. }
  685. x2 = x2 + 5;
  686. }
  687. //阈值更新
  688. if (imsum * 5 * 5 <= mStitch.minval * w * h)
  689. {
  690. mStitch.minval = imsum * 5 * 5 / (w * h);
  691. w_ind = w;
  692. h_ind = h;
  693. imageParam.bdown = 0;
  694. }
  695. });
  696. });
  697. imageParam.mStitch = mStitch;
  698. return imageParam;
  699. }
  700. public static void CheckRC(ref int x, ref int y, Mat im)
  701. {
  702. //图像矩阵访问有效性检测
  703. // 输入参数:
  704. // x——列
  705. // y——行
  706. // im——图像矩阵
  707. // 输出参数:
  708. // x——列
  709. // y——行
  710. y = Math.Max(y, 1);
  711. y = Math.Min(y, im.Height - 1);
  712. x = Math.Max(x, 1);
  713. x = Math.Min(x, im.Width - 1);
  714. }
  715. public Mat Fun_Stitch(ImageParam imageParam)
  716. {
  717. //图像融合
  718. //输入参数:
  719. //im2——待融合图像
  720. //W_box——宽度信息
  721. //H_box——高度信息
  722. //bdown——上下信息
  723. //MStitch——参数结构
  724. //输出参数:
  725. //MStitch——参数结构
  726. //im——融合图像
  727. Mat img = imageParam.im;
  728. int x1 = 0;
  729. int y1 = 0;
  730. int x2 = 0;
  731. int y2 = 0;
  732. double w = 0.5; //融合权值
  733. if (imageParam.bdown == 1)
  734. {
  735. //下区域重叠
  736. x2 = 1;
  737. //融合重叠区域
  738. for (x1 = imageParam.mStitch.Pwidth - imageParam.W_box; x1 < imageParam.mStitch.Pwidth; x1++)
  739. {
  740. y2 = 1;
  741. for (y1 = imageParam.mStitch.Pheight - imageParam.H_box + 1; y1 < imageParam.mStitch.Pheight; y1++)
  742. {
  743. //安全性检测
  744. CheckRC(ref x1, ref y1, imageParam.mStitch.im);
  745. CheckRC(ref x2, ref y2, imageParam.im);
  746. //融合权值
  747. w = (double)x2 / (double)imageParam.W_box;
  748. //加权融合
  749. double ColorRGB = imageParam.mStitch.im.At<Vec3b>(y1, x1).Item0 * (1.0 - w) + imageParam.im.At<Vec3b>(y2, x2).Item0 * w;
  750. imageParam.mStitch.im.Set<Vec3b>(y1, x1, new Vec3b((byte)ColorRGB, (byte)ColorRGB, (byte)ColorRGB));
  751. y2 = y2 + 1;
  752. }
  753. x2 = x2 + 1;
  754. }
  755. }
  756. else
  757. {
  758. //上区域重叠
  759. x2 = 1;
  760. //融合重叠区域
  761. for (x1 = imageParam.mStitch.Pwidth - imageParam.W_box; x1 < imageParam.mStitch.Pwidth; x1++)
  762. {
  763. y2 = 1;
  764. for (y1 = imageParam.mStitch.Pheight - imageParam.H_box + 1; y1 < imageParam.mStitch.Pheight; y1++)
  765. {
  766. //安全性检测
  767. CheckRC(ref x1, ref y1, imageParam.mStitch.im);
  768. CheckRC(ref x2, ref y2, imageParam.im);
  769. //融合权值
  770. w = (double)x2 / (double)imageParam.W_box;
  771. //加权融合
  772. double ColorRGB = imageParam.mStitch.im.At<Vec3b>(y1, x1).Item0 * (1.0 - w) + imageParam.im.At<Vec3b>(y2, x2).Item0 * w;
  773. imageParam.mStitch.im.Set<Vec3b>(y1, x1, new Vec3b((byte)ColorRGB, (byte)ColorRGB, (byte)ColorRGB));
  774. y2 = y2 + 1;
  775. }
  776. x2 = x2 + 1;
  777. }
  778. }
  779. //最终图
  780. img = new Mat(imageParam.mStitch.Pheight, imageParam.mStitch.Pwidth + imageParam.im.Width - x2 + 1, MatType.CV_8UC3);
  781. //分离出重叠区域
  782. Rect m_select = new Rect(x2 - 1, 0, imageParam.im.Width - x2 + 1, imageParam.mStitch.Pheight);
  783. Mat imgSwitch = new Mat(imageParam.im, m_select);
  784. Cv2.HConcat(imageParam.mStitch.im, imgSwitch, img);
  785. return img;
  786. }
  787. public Mat Fun_StitchRGB(ImageParam imageParam)
  788. {
  789. //图像融合
  790. //输入参数:
  791. //im2——待融合图像
  792. //W_box——宽度信息
  793. //H_box——高度信息
  794. //bdown——上下信息
  795. //MStitch——参数结构
  796. //输出参数:
  797. //MStitch——参数结构
  798. //im——融合图像
  799. Mat img = imageParam.im;
  800. int x1 = 0;
  801. int y1 = 0;
  802. int x2 = 0;
  803. int y2 = 0;
  804. double w = 0.5; //融合权值
  805. if (imageParam.bdown == 1)
  806. {
  807. //下区域重叠
  808. x2 = 1;
  809. //融合重叠区域
  810. for (x1 = imageParam.mStitch.Pwidth - imageParam.W_box; x1 < imageParam.mStitch.Pwidth; x1++)
  811. {
  812. y2 = 1;
  813. for (y1 = imageParam.mStitch.Pheight - imageParam.H_box + 1; y1 < imageParam.mStitch.Pheight; y1++)
  814. {
  815. //安全性检测
  816. CheckRC(ref x1, ref y1, imageParam.mStitch.im);
  817. CheckRC(ref x2, ref y2, imageParam.im);
  818. //融合权值
  819. w = (double)x2 / (double)imageParam.W_box;
  820. //加权融合
  821. double ColorR = imageParam.mStitch.im.At<Vec3b>(y1, x1).Item0 * (1.0 - w) + imageParam.im.At<Vec3b>(y2, x2).Item0 * w;
  822. double ColorG = imageParam.mStitch.im.At<Vec3b>(y1, x1).Item1 * (1.0 - w) + imageParam.im.At<Vec3b>(y2, x2).Item1 * w;
  823. double ColorB = imageParam.mStitch.im.At<Vec3b>(y1, x1).Item2 * (1.0 - w) + imageParam.im.At<Vec3b>(y2, x2).Item2 * w;
  824. if (imageParam.mStitch.im.At<Vec3b>(y1, x1).Item0 == imageParam.mStitch.im.At<Vec3b>(y1, x1).Item1 &&
  825. imageParam.mStitch.im.At<Vec3b>(y1, x1).Item1 == imageParam.mStitch.im.At<Vec3b>(y1, x1).Item2 &&
  826. imageParam.im.At<Vec3b>(y2, x2).Item0 == imageParam.im.At<Vec3b>(y2, x2).Item1 &&
  827. imageParam.im.At<Vec3b>(y2, x2).Item1 == imageParam.im.At<Vec3b>(y2, x2).Item2)
  828. {
  829. imageParam.mStitch.im.Set<Vec3b>(y1, x1, new Vec3b((byte)ColorR, (byte)ColorG, (byte)ColorB));
  830. }
  831. else
  832. {
  833. }
  834. y2 = y2 + 1;
  835. }
  836. x2 = x2 + 1;
  837. }
  838. }
  839. else
  840. {
  841. //上区域重叠
  842. x2 = 1;
  843. //融合重叠区域
  844. for (x1 = imageParam.mStitch.Pwidth - imageParam.W_box; x1 < imageParam.mStitch.Pwidth; x1++)
  845. {
  846. y2 = 1;
  847. for (y1 = imageParam.mStitch.Pheight - imageParam.H_box + 1; y1 < imageParam.mStitch.Pheight; y1++)
  848. {
  849. //安全性检测
  850. CheckRC(ref x1, ref y1, imageParam.mStitch.im);
  851. CheckRC(ref x2, ref y2, imageParam.im);
  852. //融合权值
  853. w = (double)x2 / (double)imageParam.W_box;
  854. //加权融合
  855. double ColorR = imageParam.mStitch.im.At<Vec3b>(y1, x1).Item0 * (1.0 - w) + imageParam.im.At<Vec3b>(y2, x2).Item0 * w;
  856. double ColorG = imageParam.mStitch.im.At<Vec3b>(y1, x1).Item1 * (1.0 - w) + imageParam.im.At<Vec3b>(y2, x2).Item1 * w;
  857. double ColorB = imageParam.mStitch.im.At<Vec3b>(y1, x1).Item2 * (1.0 - w) + imageParam.im.At<Vec3b>(y2, x2).Item2 * w;
  858. if (imageParam.mStitch.im.At<Vec3b>(y1, x1).Item0 == imageParam.mStitch.im.At<Vec3b>(y1, x1).Item1 &&
  859. imageParam.mStitch.im.At<Vec3b>(y1, x1).Item1 == imageParam.mStitch.im.At<Vec3b>(y1, x1).Item2 &&
  860. imageParam.im.At<Vec3b>(y2, x2).Item0 == imageParam.im.At<Vec3b>(y2, x2).Item1 &&
  861. imageParam.im.At<Vec3b>(y2, x2).Item1 == imageParam.im.At<Vec3b>(y2, x2).Item2)
  862. {
  863. imageParam.mStitch.im.Set<Vec3b>(y1, x1, new Vec3b((byte)ColorR, (byte)ColorG, (byte)ColorB));
  864. }
  865. else
  866. {
  867. }
  868. y2 = y2 + 1;
  869. }
  870. x2 = x2 + 1;
  871. }
  872. }
  873. //最终图
  874. img = new Mat(imageParam.mStitch.Pheight, imageParam.mStitch.Pwidth + imageParam.im.Width - x2 + 1, MatType.CV_8UC3);
  875. //分离出重叠区域
  876. Rect m_select = new Rect(x2 - 1, 0, imageParam.im.Width - x2 + 1, imageParam.mStitch.Pheight);
  877. Mat imgSwitch = new Mat(imageParam.im, m_select);
  878. Cv2.HConcat(imageParam.mStitch.im, imgSwitch, img);
  879. return img;
  880. }
  881. }
  882. }