CImageHandler.cs 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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, int overlap, 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, overlap, 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. List<COTSParticleClr> updatePartList= new List<COTSParticleClr>();
  334. Dictionary<COTSParticleClr, COTSParticleClr> combinBorderParts = new Dictionary<COTSParticleClr, COTSParticleClr>();
  335. List<string> combinBorderParts_style = new List<string>();
  336. foreach (var item in allFields)
  337. {
  338. if (!item.GetIsMeasureComplete())
  339. {
  340. break;
  341. }
  342. //找到上下左右四帧图
  343. List<COTSFieldData> leftField = allFields.Where(a => a.OTSPos == new System.Drawing.PointF(item.OTSPos.X - offsetX, item.OTSPos.Y)).ToList();
  344. List<COTSFieldData> rightField = allFields.Where(a => a.OTSPos == new System.Drawing.PointF(item.OTSPos.X + offsetX, item.OTSPos.Y)).ToList();
  345. List<COTSFieldData> upField = allFields.Where(a => a.OTSPos == new System.Drawing.PointF(item.OTSPos.X, item.OTSPos.Y + offsetY)).ToList();
  346. List<COTSFieldData> downField = allFields.Where(a => a.OTSPos == new System.Drawing.PointF(item.OTSPos.X, item.OTSPos.Y - offsetY)).ToList();
  347. //判断是否有效
  348. if (leftField.Count() == 1)//包含左帧图
  349. {
  350. if (leftField[0].GetIsMeasureComplete() == true)//存在测量帧图
  351. {
  352. //寻找重叠颗粒
  353. deletePartList.AddRange(FieldFun(leftField[0], item, "left_right", resolutionSize, overlap, ref combinBorderParts, ref combinBorderParts_style));
  354. }
  355. }
  356. if (rightField.Count() == 1)//包含右帧图
  357. {
  358. if (rightField[0].GetIsMeasureComplete() == true)//存在测量帧图
  359. {
  360. //寻找重叠颗粒
  361. deletePartList.AddRange(FieldFun(item, rightField[0], "left_right", resolutionSize, overlap, ref combinBorderParts, ref combinBorderParts_style));
  362. }
  363. }
  364. if (upField.Count() == 1)//包含上帧图
  365. {
  366. if (upField[0].GetIsMeasureComplete() == true)//存在测量帧图
  367. {
  368. //寻找重叠颗粒
  369. deletePartList.AddRange(FieldFun(upField[0], item, "up_down", resolutionSize, overlap, ref combinBorderParts, ref combinBorderParts_style));
  370. }
  371. }
  372. if (downField.Count() == 1)//包含下帧图
  373. {
  374. if (downField[0].GetIsMeasureComplete() == true)//存在测量帧图
  375. {
  376. //寻找重叠颗粒
  377. deletePartList.AddRange(FieldFun(item, downField[0], "up_down", resolutionSize, overlap, ref combinBorderParts, ref combinBorderParts_style));
  378. }
  379. }
  380. }
  381. //数据库操作
  382. SQLiteHelper sQLiteHelper = new SQLiteHelper(stdPath);
  383. sQLiteHelper.GetDBConnection();
  384. sQLiteHelper.BeginTransaction();
  385. deletePartList = deletePartList.Distinct().ToList();//去重
  386. if (!sQLiteHelper.DeletePartForTransaction(deletePartList))//删除重复颗粒
  387. {
  388. return false;
  389. }
  390. if (!sQLiteHelper.CombinPartForTransaction(allFields, combinBorderParts, updatePartList, combinBorderParts_style, resolutionSize))//合并大颗粒
  391. {
  392. return false;
  393. }
  394. if (!sQLiteHelper.UpdatePartForTransaction(updatePartList))//修改Segment
  395. {
  396. return false;
  397. }
  398. sQLiteHelper.CommitTransaction();
  399. return true;
  400. }
  401. private List<COTSParticleClr> FieldFun(COTSFieldData left_upField, COTSFieldData right_downField, string style, System.Drawing.Size resolutionSize, int overlap, ref Dictionary<COTSParticleClr, COTSParticleClr> combinBorderParts, ref List<string> combinBorderParts_style)
  402. {
  403. List<COTSParticleClr> particleClrs = new List<COTSParticleClr>();
  404. combinBorderParts = new Dictionary<COTSParticleClr, COTSParticleClr>();
  405. if (style == "left_right")
  406. {
  407. //寻找左帧图的右侧区域颗粒
  408. double left_upField_sum = 0;
  409. double right_downField_sum = 0;
  410. List<COTSParticleClr> leftBorderParts = new List<COTSParticleClr>();
  411. List<COTSParticleClr> rightBorderParts = new List<COTSParticleClr>();
  412. foreach (var leftParticles in left_upField.GetListAnalysisParticles())
  413. {
  414. Rectangle leftRectangle = (Rectangle)leftParticles.GetParticleRect();
  415. if (leftRectangle.Right > resolutionSize.Width - overlap / 2)//未跨界
  416. {
  417. left_upField_sum += leftParticles.GetActualArea();
  418. }
  419. if (leftRectangle.Right == resolutionSize.Width || leftRectangle.Right == resolutionSize.Width - 1)//边界
  420. {
  421. leftBorderParts.Add(leftParticles);
  422. }
  423. }
  424. foreach (var rightParticles in right_downField.GetListAnalysisParticles())
  425. {
  426. Rectangle rightRectangle = (Rectangle)rightParticles.GetParticleRect();
  427. if (rightRectangle.Left > overlap / 2)//未跨界
  428. {
  429. right_downField_sum += rightParticles.GetActualArea();
  430. }
  431. if (rightRectangle.Left == 0)//边界
  432. {
  433. rightBorderParts.Add(rightParticles);
  434. }
  435. }
  436. foreach (var leftBorder in leftBorderParts)
  437. {
  438. Rectangle leftRectangle = (Rectangle)leftBorder.GetParticleRect();
  439. foreach (var rightBorder in rightBorderParts)
  440. {
  441. Rectangle rightRectangle = (Rectangle)rightBorder.GetParticleRect();
  442. bool isTrue = false;
  443. for (int i = leftRectangle.Y; i < leftRectangle.Y + leftRectangle.Height; i++)
  444. {
  445. if (i > rightRectangle.Y && i < rightRectangle.Y + rightRectangle.Height)
  446. {
  447. combinBorderParts.Add(leftBorder, rightBorder);
  448. combinBorderParts_style.Add("left");
  449. isTrue = true;
  450. break;
  451. }
  452. }
  453. if (isTrue)
  454. {
  455. break;
  456. }
  457. }
  458. }
  459. if (left_upField_sum < right_downField_sum)
  460. {
  461. foreach (var leftParticles in left_upField.GetListAnalysisParticles())
  462. {
  463. Rectangle leftRectangle = (Rectangle)leftParticles.GetParticleRect();
  464. if (leftRectangle.Left > resolutionSize.Width - overlap)//未跨界
  465. {
  466. if (!combinBorderParts.ContainsKey(leftParticles))
  467. {
  468. particleClrs.Add(leftParticles);
  469. }
  470. }
  471. }
  472. }
  473. else
  474. {
  475. foreach (var downParticles in right_downField.GetListAnalysisParticles())
  476. {
  477. Rectangle downRectangle = (Rectangle)downParticles.GetParticleRect();
  478. if (downRectangle.Right < overlap / 2)//未跨界
  479. {
  480. if (!combinBorderParts.ContainsValue(downParticles))
  481. {
  482. particleClrs.Add(downParticles);
  483. }
  484. }
  485. }
  486. }
  487. }
  488. else
  489. {
  490. //寻找上帧图的下侧区域颗粒
  491. double left_upField_sum = 0;
  492. double right_downField_sum = 0;
  493. List<COTSParticleClr> upBorderParts = new List<COTSParticleClr>();
  494. List<COTSParticleClr> downBorderParts = new List<COTSParticleClr>();
  495. foreach (var upParticles in left_upField.GetListAnalysisParticles())
  496. {
  497. Rectangle upRectangle = (Rectangle)upParticles.GetParticleRect();
  498. if (upRectangle.Top > resolutionSize.Height - overlap / 2)//未跨界
  499. {
  500. left_upField_sum += upParticles.GetActualArea();
  501. }
  502. if (upRectangle.Bottom == resolutionSize.Height || upRectangle.Bottom == resolutionSize.Height - 1)//边界
  503. {
  504. upBorderParts.Add(upParticles);
  505. }
  506. }
  507. foreach (var downParticles in right_downField.GetListAnalysisParticles())
  508. {
  509. Rectangle downRectangle = (Rectangle)downParticles.GetParticleRect();
  510. if (downRectangle.Top > resolutionSize.Height - overlap / 2)//未跨界
  511. {
  512. right_downField_sum += downParticles.GetActualArea();
  513. }
  514. if (downRectangle.Top == 0)//边界
  515. {
  516. downBorderParts.Add(downParticles);
  517. }
  518. else if (downRectangle.Top == 1)//边界
  519. {
  520. downBorderParts.Add(downParticles);
  521. }
  522. }
  523. foreach (var upBorder in upBorderParts)
  524. {
  525. Rectangle upRectangle = (Rectangle)upBorder.GetParticleRect();
  526. foreach (var downBorder in downBorderParts)
  527. {
  528. Rectangle downRectangle = (Rectangle)downBorder.GetParticleRect();
  529. bool isTrue = false;
  530. for (int i = upRectangle.X; i < upRectangle.X + upRectangle.Width; i++)
  531. {
  532. if (i > downRectangle.X && i < downRectangle.X + downRectangle.Width)
  533. {
  534. combinBorderParts.Add(upBorder, downBorder);
  535. combinBorderParts_style.Add("up");
  536. isTrue = true;
  537. break;
  538. }
  539. }
  540. if (isTrue)
  541. {
  542. break;
  543. }
  544. }
  545. }
  546. if (left_upField_sum < right_downField_sum)
  547. {
  548. foreach (var upParticles in left_upField.GetListAnalysisParticles())
  549. {
  550. Rectangle upRectangle = (Rectangle)upParticles.GetParticleRect();
  551. if (upRectangle.Top > resolutionSize.Height - overlap / 2)//未跨界
  552. {
  553. if (!combinBorderParts.ContainsKey(upParticles))
  554. {
  555. particleClrs.Add(upParticles);
  556. }
  557. }
  558. }
  559. }
  560. else
  561. {
  562. foreach (var downParticles in right_downField.GetListAnalysisParticles())
  563. {
  564. Rectangle downRectangle = (Rectangle)downParticles.GetParticleRect();
  565. if (downRectangle.Bottom < overlap / 2)//未跨界
  566. {
  567. if (!combinBorderParts.ContainsValue(downParticles))
  568. {
  569. particleClrs.Add(downParticles);
  570. }
  571. }
  572. }
  573. }
  574. }
  575. return particleClrs;
  576. }
  577. public Mat CombinImageX(Mat[] list_mats, int OverlapParam, int type)
  578. {
  579. List<Mat> matStitch = new List<Mat>();//拼接
  580. List<Mat> matCombin = new List<Mat>();//合并
  581. for (int i = 0; i < list_mats.Count(); i++)
  582. {
  583. if (i == 0)//首张
  584. {
  585. matCombin.Add(new Mat(list_mats[i], new Rect(0, 0, list_mats[i].Width - OverlapParam - 100, list_mats[i].Height)));
  586. matStitch.Add(new Mat(list_mats[i], new Rect(list_mats[i].Width - OverlapParam - 100, 0, OverlapParam + 100, list_mats[i].Height)));
  587. }
  588. else if(i == list_mats.Count() - 1)//末张
  589. {
  590. matStitch.Add(new Mat(list_mats[i], new Rect(0, 0, OverlapParam + 100, list_mats[i].Height)));
  591. matCombin.Add(new Mat(list_mats[i], new Rect(OverlapParam + 100, 0, list_mats[i].Width - OverlapParam - 100, list_mats[i].Height)));
  592. }
  593. else
  594. {
  595. matStitch.Add(new Mat(list_mats[i], new Rect(0, 0, OverlapParam + 100, list_mats[i].Height)));
  596. matCombin.Add(new Mat(list_mats[i], new Rect(OverlapParam + 100, 0, list_mats[i].Width - (OverlapParam + 100) * 2, list_mats[i].Height)));
  597. matStitch.Add(new Mat(list_mats[i], new Rect(list_mats[i].Width - OverlapParam - 100, 0, OverlapParam + 100, list_mats[i].Height)));
  598. }
  599. }
  600. for (int i = 0; i < matStitch.Count; i += 2)
  601. {
  602. if (matStitch.Count == 1)
  603. {
  604. matCombin.Insert(i + 1, matStitch[i]);
  605. }
  606. else
  607. {
  608. matCombin.Insert(i + 1, StitchImageX((int)(OverlapParam / 2 * 1.2), type, matStitch[i], matStitch[i + 1]));
  609. }
  610. }
  611. Mat pano = new OpenCvSharp.Mat();
  612. Cv2.HConcat(matCombin.ToArray(), pano);
  613. return pano;
  614. }
  615. public Mat CombinImageY(Mat[] list_mats, int OverlapParam, int type)
  616. {
  617. List<Mat> matStitch = new List<Mat>();//拼接
  618. List<Mat> matCombin = new List<Mat>();//合并
  619. for (int i = 0; i < list_mats.Count(); i++)
  620. {
  621. if (i == 0)//首张
  622. {
  623. matCombin.Add(new Mat(list_mats[i], new Rect(0, 0, list_mats[i].Width, list_mats[i].Height - OverlapParam - 100)));
  624. matStitch.Add(new Mat(list_mats[i], new Rect(0, list_mats[i].Height - OverlapParam - 100, list_mats[i].Width, OverlapParam + 100)));
  625. }
  626. else if (i == list_mats.Count() - 1)//末张
  627. {
  628. matStitch.Add(new Mat(list_mats[i], new Rect(0, 0, list_mats[i].Width, OverlapParam + 100)));
  629. matCombin.Add(new Mat(list_mats[i], new Rect(0, OverlapParam + 100, list_mats[i].Width, list_mats[i].Height - OverlapParam - 100)));
  630. }
  631. else
  632. {
  633. matStitch.Add(new Mat(list_mats[i], new Rect(0, 0, list_mats[i].Width, OverlapParam + 100)));
  634. matCombin.Add(new Mat(list_mats[i], new Rect(0, OverlapParam + 100, list_mats[i].Width, list_mats[i].Height - (OverlapParam + 100) * 2)));
  635. matStitch.Add(new Mat(list_mats[i], new Rect(0, list_mats[i].Height - OverlapParam - 100, list_mats[i].Width, OverlapParam + 100)));
  636. }
  637. }
  638. for (int i = 0; i < matStitch.Count; i += 2)
  639. {
  640. if (matStitch.Count == 1)
  641. {
  642. matCombin.Insert(i + 1, matStitch[i]);
  643. }
  644. else
  645. {
  646. matCombin.Insert(i + 1, StitchImageY(OverlapParam, type, matStitch[i], matStitch[i + 1]));
  647. }
  648. }
  649. Mat pano = new OpenCvSharp.Mat();
  650. Cv2.VConcat(matCombin.ToArray(), pano);
  651. return pano;
  652. }
  653. public struct MStitch
  654. {
  655. public int Pwidth;//单幅图像的宽度
  656. public int Pheight;//单幅图像的高度
  657. public int W_min;//最小的重叠区域宽度
  658. public int W_max;//最大的重叠区域宽度
  659. public int H_min;//最小的重叠区域高度
  660. public double minval;//块过滤阈值
  661. public Mat im;//图像信息
  662. }
  663. public struct ImageParam
  664. {
  665. public int W_box;//宽度信息
  666. public int H_box;//高度信息
  667. public int bdown;//上下信息
  668. public MStitch mStitch; //参数结构
  669. public Mat im;//图像信息
  670. }
  671. /// <summary>
  672. /// 横向拼图
  673. /// </summary>
  674. public Mat StitchImageX(int min_w, int type, Mat newImg1, Mat newImg2)
  675. {
  676. MStitch mStitch = new MStitch();
  677. mStitch.Pwidth = newImg1.Width;
  678. mStitch.Pheight = newImg1.Height;
  679. mStitch.W_min = min_w;
  680. mStitch.W_max = min_w;
  681. mStitch.H_min = newImg1.Height;
  682. mStitch.minval = 255;
  683. mStitch.im = newImg1;
  684. ImageParam imageParam = Fun_Match(newImg2, mStitch);
  685. imageParam.im = newImg2;
  686. if (type == 2)
  687. {
  688. return Fun_Stitch(imageParam);
  689. }
  690. else
  691. {
  692. return Fun_StitchRGB(imageParam);
  693. }
  694. }
  695. /// <summary>
  696. /// 纵向拼图
  697. /// </summary>
  698. public Mat StitchImageY(int min_w, int type, Mat newImg1, Mat newImg2)
  699. {
  700. Cv2.Transpose(newImg1, newImg1);
  701. Cv2.Flip(newImg1, newImg1, FlipMode.X);
  702. Cv2.Transpose(newImg2, newImg2);
  703. Cv2.Flip(newImg2, newImg2, FlipMode.X);
  704. MStitch mStitch = new MStitch();
  705. mStitch.Pwidth = newImg1.Width;
  706. mStitch.Pheight = newImg1.Height;
  707. mStitch.W_min = min_w - 50;
  708. mStitch.W_max = min_w - 50;
  709. mStitch.H_min = newImg1.Height - 20;
  710. mStitch.minval = 255;
  711. mStitch.im = newImg1;
  712. ImageParam imageParam = Fun_Match(newImg2, mStitch);
  713. imageParam.im = newImg2;
  714. Mat result = type == 2 ? Fun_Stitch(imageParam) : Fun_StitchRGB(imageParam);
  715. Cv2.Transpose(result, result);
  716. Cv2.Flip(result, result, FlipMode.Y);
  717. return result;
  718. }
  719. public static ImageParam Fun_Match(Mat im2, MStitch mStitch)
  720. {
  721. ImageParam imageParam = new ImageParam();
  722. double imsum = 0;
  723. int x1 = 0;
  724. int y1 = 0;
  725. int x2 = 0;
  726. int y2 = 0;
  727. int w_ind = 0;
  728. int h_ind = 0;
  729. //在上窗口所有匹配块内进行搜索
  730. for (int w = mStitch.W_min; w <= mStitch.W_max; w++)
  731. {
  732. for (int h = mStitch.H_min; h <= mStitch.Pheight; h++)
  733. {
  734. imsum = 0;//块差分集初始化
  735. x2 = 1;
  736. for (x1 = mStitch.Pwidth - w; x1 <= mStitch.Pwidth; x1 += 5)
  737. {
  738. y2 = 1;
  739. for (y1 = mStitch.Pheight - h + 1; y1 <= mStitch.Pheight; y1 += 5)
  740. {
  741. //块差分集计算
  742. CheckRC(ref x1, ref y1, mStitch.im);
  743. CheckRC(ref x2, ref y2, im2);
  744. imsum = imsum + Math.Abs(mStitch.im.At<Vec3b>(y1, x1).Item0 - im2.At<Vec3b>(y2, x2).Item0);
  745. y2 = y2 + 5;
  746. }
  747. x2 = x2 + 5;
  748. }
  749. //阈值更新
  750. if (imsum * 5 * 5 <= mStitch.minval * w * h)
  751. {
  752. mStitch.minval = imsum * 5 * 5 / (w * h);
  753. w_ind = w;
  754. h_ind = h;
  755. }
  756. }
  757. }
  758. imageParam.W_box = w_ind;
  759. imageParam.H_box = h_ind;
  760. imageParam.bdown = 1;
  761. //在下窗口所有匹配块内进行搜索
  762. Parallel.For(mStitch.W_min, mStitch.W_max, w =>
  763. {
  764. Parallel.For(mStitch.H_min, mStitch.Pheight, h =>
  765. {
  766. imsum = 0;//块差分集初始化
  767. x2 = 1;
  768. for (x1 = mStitch.Pwidth - w; x1 <= mStitch.Pwidth; x1 += 5)
  769. {
  770. y1 = 1;
  771. for (y2 = mStitch.Pheight - h + 1; y2 <= mStitch.Pheight; y2 += 5)
  772. {
  773. //块差分集计算
  774. CheckRC(ref x1, ref y1, mStitch.im);
  775. CheckRC(ref x2, ref y2, im2);
  776. imsum = imsum + Math.Abs(mStitch.im.At<Vec3b>(y1, x1).Item0 - im2.At<Vec3b>(y2, x2).Item0);
  777. y1 = y1 + 5;
  778. }
  779. x2 = x2 + 5;
  780. }
  781. //阈值更新
  782. if (imsum * 5 * 5 <= mStitch.minval * w * h)
  783. {
  784. mStitch.minval = imsum * 5 * 5 / (w * h);
  785. w_ind = w;
  786. h_ind = h;
  787. imageParam.bdown = 0;
  788. }
  789. });
  790. });
  791. imageParam.mStitch = mStitch;
  792. return imageParam;
  793. }
  794. public static void CheckRC(ref int x, ref int y, Mat im)
  795. {
  796. //图像矩阵访问有效性检测
  797. // 输入参数:
  798. // x——列
  799. // y——行
  800. // im——图像矩阵
  801. // 输出参数:
  802. // x——列
  803. // y——行
  804. y = Math.Max(y, 1);
  805. y = Math.Min(y, im.Height - 1);
  806. x = Math.Max(x, 1);
  807. x = Math.Min(x, im.Width - 1);
  808. }
  809. public Mat Fun_Stitch(ImageParam imageParam)
  810. {
  811. //图像融合
  812. //输入参数:
  813. //im2——待融合图像
  814. //W_box——宽度信息
  815. //H_box——高度信息
  816. //bdown——上下信息
  817. //MStitch——参数结构
  818. //输出参数:
  819. //MStitch——参数结构
  820. //im——融合图像
  821. Mat img = imageParam.im;
  822. int x1 = 0;
  823. int y1 = 0;
  824. int x2 = 0;
  825. int y2 = 0;
  826. double w = 0.5; //融合权值
  827. if (imageParam.bdown == 1)
  828. {
  829. //下区域重叠
  830. x2 = 1;
  831. //融合重叠区域
  832. for (x1 = imageParam.mStitch.Pwidth - imageParam.W_box; x1 < imageParam.mStitch.Pwidth; x1++)
  833. {
  834. y2 = 1;
  835. for (y1 = imageParam.mStitch.Pheight - imageParam.H_box + 1; y1 < imageParam.mStitch.Pheight; y1++)
  836. {
  837. //安全性检测
  838. CheckRC(ref x1, ref y1, imageParam.mStitch.im);
  839. CheckRC(ref x2, ref y2, imageParam.im);
  840. //融合权值
  841. w = (double)x2 / (double)imageParam.W_box;
  842. //加权融合
  843. double ColorRGB = imageParam.mStitch.im.At<Vec3b>(y1, x1).Item0 * (1.0 - w) + imageParam.im.At<Vec3b>(y2, x2).Item0 * w;
  844. imageParam.mStitch.im.Set<Vec3b>(y1, x1, new Vec3b((byte)ColorRGB, (byte)ColorRGB, (byte)ColorRGB));
  845. y2 = y2 + 1;
  846. }
  847. x2 = x2 + 1;
  848. }
  849. }
  850. else
  851. {
  852. //上区域重叠
  853. x2 = 1;
  854. //融合重叠区域
  855. for (x1 = imageParam.mStitch.Pwidth - imageParam.W_box; x1 < imageParam.mStitch.Pwidth; x1++)
  856. {
  857. y2 = 1;
  858. for (y1 = imageParam.mStitch.Pheight - imageParam.H_box + 1; y1 < imageParam.mStitch.Pheight; y1++)
  859. {
  860. //安全性检测
  861. CheckRC(ref x1, ref y1, imageParam.mStitch.im);
  862. CheckRC(ref x2, ref y2, imageParam.im);
  863. //融合权值
  864. w = (double)x2 / (double)imageParam.W_box;
  865. //加权融合
  866. double ColorRGB = imageParam.mStitch.im.At<Vec3b>(y1, x1).Item0 * (1.0 - w) + imageParam.im.At<Vec3b>(y2, x2).Item0 * w;
  867. imageParam.mStitch.im.Set<Vec3b>(y1, x1, new Vec3b((byte)ColorRGB, (byte)ColorRGB, (byte)ColorRGB));
  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. public Mat Fun_StitchRGB(ImageParam imageParam)
  882. {
  883. //图像融合
  884. //输入参数:
  885. //im2——待融合图像
  886. //W_box——宽度信息
  887. //H_box——高度信息
  888. //bdown——上下信息
  889. //MStitch——参数结构
  890. //输出参数:
  891. //MStitch——参数结构
  892. //im——融合图像
  893. Mat img = imageParam.im;
  894. int x1 = 0;
  895. int y1 = 0;
  896. int x2 = 0;
  897. int y2 = 0;
  898. double w = 0.5; //融合权值
  899. if (imageParam.bdown == 1)
  900. {
  901. //下区域重叠
  902. x2 = 1;
  903. //融合重叠区域
  904. for (x1 = imageParam.mStitch.Pwidth - imageParam.W_box; x1 < imageParam.mStitch.Pwidth; x1++)
  905. {
  906. y2 = 1;
  907. for (y1 = imageParam.mStitch.Pheight - imageParam.H_box + 1; y1 < imageParam.mStitch.Pheight; y1++)
  908. {
  909. //安全性检测
  910. CheckRC(ref x1, ref y1, imageParam.mStitch.im);
  911. CheckRC(ref x2, ref y2, imageParam.im);
  912. //融合权值
  913. w = (double)x2 / (double)imageParam.W_box;
  914. //加权融合
  915. double ColorR = imageParam.mStitch.im.At<Vec3b>(y1, x1).Item0 * (1.0 - w) + imageParam.im.At<Vec3b>(y2, x2).Item0 * w;
  916. double ColorG = imageParam.mStitch.im.At<Vec3b>(y1, x1).Item1 * (1.0 - w) + imageParam.im.At<Vec3b>(y2, x2).Item1 * w;
  917. double ColorB = imageParam.mStitch.im.At<Vec3b>(y1, x1).Item2 * (1.0 - w) + imageParam.im.At<Vec3b>(y2, x2).Item2 * w;
  918. if (imageParam.mStitch.im.At<Vec3b>(y1, x1).Item0 == imageParam.mStitch.im.At<Vec3b>(y1, x1).Item1 &&
  919. imageParam.mStitch.im.At<Vec3b>(y1, x1).Item1 == imageParam.mStitch.im.At<Vec3b>(y1, x1).Item2 &&
  920. imageParam.im.At<Vec3b>(y2, x2).Item0 == imageParam.im.At<Vec3b>(y2, x2).Item1 &&
  921. imageParam.im.At<Vec3b>(y2, x2).Item1 == imageParam.im.At<Vec3b>(y2, x2).Item2)
  922. {
  923. imageParam.mStitch.im.Set<Vec3b>(y1, x1, new Vec3b((byte)ColorR, (byte)ColorG, (byte)ColorB));
  924. }
  925. else
  926. {
  927. }
  928. y2 = y2 + 1;
  929. }
  930. x2 = x2 + 1;
  931. }
  932. }
  933. else
  934. {
  935. //上区域重叠
  936. x2 = 1;
  937. //融合重叠区域
  938. for (x1 = imageParam.mStitch.Pwidth - imageParam.W_box; x1 < imageParam.mStitch.Pwidth; x1++)
  939. {
  940. y2 = 1;
  941. for (y1 = imageParam.mStitch.Pheight - imageParam.H_box + 1; y1 < imageParam.mStitch.Pheight; y1++)
  942. {
  943. //安全性检测
  944. CheckRC(ref x1, ref y1, imageParam.mStitch.im);
  945. CheckRC(ref x2, ref y2, imageParam.im);
  946. //融合权值
  947. w = (double)x2 / (double)imageParam.W_box;
  948. //加权融合
  949. double ColorR = imageParam.mStitch.im.At<Vec3b>(y1, x1).Item0 * (1.0 - w) + imageParam.im.At<Vec3b>(y2, x2).Item0 * w;
  950. double ColorG = imageParam.mStitch.im.At<Vec3b>(y1, x1).Item1 * (1.0 - w) + imageParam.im.At<Vec3b>(y2, x2).Item1 * w;
  951. double ColorB = imageParam.mStitch.im.At<Vec3b>(y1, x1).Item2 * (1.0 - w) + imageParam.im.At<Vec3b>(y2, x2).Item2 * w;
  952. if (imageParam.mStitch.im.At<Vec3b>(y1, x1).Item0 == imageParam.mStitch.im.At<Vec3b>(y1, x1).Item1 &&
  953. imageParam.mStitch.im.At<Vec3b>(y1, x1).Item1 == imageParam.mStitch.im.At<Vec3b>(y1, x1).Item2 &&
  954. imageParam.im.At<Vec3b>(y2, x2).Item0 == imageParam.im.At<Vec3b>(y2, x2).Item1 &&
  955. imageParam.im.At<Vec3b>(y2, x2).Item1 == imageParam.im.At<Vec3b>(y2, x2).Item2)
  956. {
  957. imageParam.mStitch.im.Set<Vec3b>(y1, x1, new Vec3b((byte)ColorR, (byte)ColorG, (byte)ColorB));
  958. }
  959. else
  960. {
  961. }
  962. y2 = y2 + 1;
  963. }
  964. x2 = x2 + 1;
  965. }
  966. }
  967. //最终图
  968. img = new Mat(imageParam.mStitch.Pheight, imageParam.mStitch.Pwidth + imageParam.im.Width - x2 + 1, MatType.CV_8UC3);
  969. //分离出重叠区域
  970. Rect m_select = new Rect(x2 - 1, 0, imageParam.im.Width - x2 + 1, imageParam.mStitch.Pheight);
  971. Mat imgSwitch = new Mat(imageParam.im, m_select);
  972. Cv2.HConcat(imageParam.mStitch.im, imgSwitch, img);
  973. return img;
  974. }
  975. }
  976. }