CImageHandler.cs 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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 StitchImageXGrid(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 - 50;
  680. mStitch.W_max = min_w + 50;
  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 StitchImageYGrid(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;
  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. /// <summary>
  720. /// 横向拼图
  721. /// </summary>
  722. public Mat StitchImageX(int min_w, int type, Mat newImg1, Mat newImg2)
  723. {
  724. MStitch mStitch = new MStitch();
  725. mStitch.Pwidth = newImg1.Width;
  726. mStitch.Pheight = newImg1.Height;
  727. mStitch.W_min = min_w;
  728. mStitch.W_max = min_w;
  729. mStitch.H_min = newImg1.Height;
  730. mStitch.minval = 255;
  731. mStitch.im = newImg1;
  732. ImageParam imageParam = Fun_Match(newImg2, mStitch);
  733. imageParam.im = newImg2;
  734. if (type == 2)
  735. {
  736. return Fun_Stitch(imageParam);
  737. }
  738. else
  739. {
  740. return Fun_StitchRGB(imageParam);
  741. }
  742. }
  743. /// <summary>
  744. /// 纵向拼图
  745. /// </summary>
  746. public Mat StitchImageY(int min_w, int type, Mat newImg1, Mat newImg2)
  747. {
  748. Cv2.Transpose(newImg1, newImg1);
  749. Cv2.Flip(newImg1, newImg1, FlipMode.X);
  750. Cv2.Transpose(newImg2, newImg2);
  751. Cv2.Flip(newImg2, newImg2, FlipMode.X);
  752. MStitch mStitch = new MStitch();
  753. mStitch.Pwidth = newImg1.Width;
  754. mStitch.Pheight = newImg1.Height;
  755. mStitch.W_min = min_w - 50;
  756. mStitch.W_max = min_w - 50;
  757. mStitch.H_min = newImg1.Height - 20;
  758. mStitch.minval = 255;
  759. mStitch.im = newImg1;
  760. ImageParam imageParam = Fun_Match(newImg2, mStitch);
  761. imageParam.im = newImg2;
  762. Mat result = type == 2 ? Fun_Stitch(imageParam) : Fun_StitchRGB(imageParam);
  763. Cv2.Transpose(result, result);
  764. Cv2.Flip(result, result, FlipMode.Y);
  765. return result;
  766. }
  767. public static ImageParam Fun_Match(Mat im2, MStitch mStitch)
  768. {
  769. ImageParam imageParam = new ImageParam();
  770. double imsum = 0;
  771. int x1 = 0;
  772. int y1 = 0;
  773. int x2 = 0;
  774. int y2 = 0;
  775. int w_ind = 0;
  776. int h_ind = 0;
  777. //在上窗口所有匹配块内进行搜索
  778. for (int w = mStitch.W_min; w <= mStitch.W_max; w++)
  779. {
  780. for (int h = mStitch.H_min; h <= mStitch.Pheight; h++)
  781. {
  782. imsum = 0;//块差分集初始化
  783. x2 = 1;
  784. for (x1 = mStitch.Pwidth - w; x1 <= mStitch.Pwidth; x1 += 5)
  785. {
  786. y2 = 1;
  787. for (y1 = mStitch.Pheight - h + 1; y1 <= mStitch.Pheight; y1 += 5)
  788. {
  789. //块差分集计算
  790. CheckRC(ref x1, ref y1, mStitch.im);
  791. CheckRC(ref x2, ref y2, im2);
  792. imsum = imsum + Math.Abs(mStitch.im.At<Vec3b>(y1, x1).Item0 - im2.At<Vec3b>(y2, x2).Item0);
  793. y2 = y2 + 5;
  794. }
  795. x2 = x2 + 5;
  796. }
  797. //阈值更新
  798. if (imsum * 5 * 5 <= mStitch.minval * w * h)
  799. {
  800. mStitch.minval = imsum * 5 * 5 / (w * h);
  801. w_ind = w;
  802. h_ind = h;
  803. }
  804. }
  805. }
  806. imageParam.W_box = w_ind;
  807. imageParam.H_box = h_ind;
  808. imageParam.bdown = 1;
  809. //在下窗口所有匹配块内进行搜索
  810. Parallel.For(mStitch.W_min, mStitch.W_max, w =>
  811. {
  812. Parallel.For(mStitch.H_min, mStitch.Pheight, h =>
  813. {
  814. imsum = 0;//块差分集初始化
  815. x2 = 1;
  816. for (x1 = mStitch.Pwidth - w; x1 <= mStitch.Pwidth; x1 += 5)
  817. {
  818. y1 = 1;
  819. for (y2 = mStitch.Pheight - h + 1; y2 <= mStitch.Pheight; y2 += 5)
  820. {
  821. //块差分集计算
  822. CheckRC(ref x1, ref y1, mStitch.im);
  823. CheckRC(ref x2, ref y2, im2);
  824. imsum = imsum + Math.Abs(mStitch.im.At<Vec3b>(y1, x1).Item0 - im2.At<Vec3b>(y2, x2).Item0);
  825. y1 = y1 + 5;
  826. }
  827. x2 = x2 + 5;
  828. }
  829. //阈值更新
  830. if (imsum * 5 * 5 <= mStitch.minval * w * h)
  831. {
  832. mStitch.minval = imsum * 5 * 5 / (w * h);
  833. w_ind = w;
  834. h_ind = h;
  835. imageParam.bdown = 0;
  836. }
  837. });
  838. });
  839. imageParam.mStitch = mStitch;
  840. return imageParam;
  841. }
  842. public static void CheckRC(ref int x, ref int y, Mat im)
  843. {
  844. //图像矩阵访问有效性检测
  845. // 输入参数:
  846. // x——列
  847. // y——行
  848. // im——图像矩阵
  849. // 输出参数:
  850. // x——列
  851. // y——行
  852. y = Math.Max(y, 1);
  853. y = Math.Min(y, im.Height - 1);
  854. x = Math.Max(x, 1);
  855. x = Math.Min(x, im.Width - 1);
  856. }
  857. public Mat Fun_Stitch(ImageParam imageParam)
  858. {
  859. //图像融合
  860. //输入参数:
  861. //im2——待融合图像
  862. //W_box——宽度信息
  863. //H_box——高度信息
  864. //bdown——上下信息
  865. //MStitch——参数结构
  866. //输出参数:
  867. //MStitch——参数结构
  868. //im——融合图像
  869. Mat img = imageParam.im;
  870. int x1 = 0;
  871. int y1 = 0;
  872. int x2 = 0;
  873. int y2 = 0;
  874. double w = 0.5; //融合权值
  875. if (imageParam.bdown == 1)
  876. {
  877. //下区域重叠
  878. x2 = 1;
  879. //融合重叠区域
  880. for (x1 = imageParam.mStitch.Pwidth - imageParam.W_box; x1 < imageParam.mStitch.Pwidth; x1++)
  881. {
  882. y2 = 1;
  883. for (y1 = imageParam.mStitch.Pheight - imageParam.H_box + 1; y1 < imageParam.mStitch.Pheight; y1++)
  884. {
  885. //安全性检测
  886. CheckRC(ref x1, ref y1, imageParam.mStitch.im);
  887. CheckRC(ref x2, ref y2, imageParam.im);
  888. //融合权值
  889. w = (double)x2 / (double)imageParam.W_box;
  890. //加权融合
  891. double ColorRGB = imageParam.mStitch.im.At<Vec3b>(y1, x1).Item0 * (1.0 - w) + imageParam.im.At<Vec3b>(y2, x2).Item0 * w;
  892. imageParam.mStitch.im.Set<Vec3b>(y1, x1, new Vec3b((byte)ColorRGB, (byte)ColorRGB, (byte)ColorRGB));
  893. y2 = y2 + 1;
  894. }
  895. x2 = x2 + 1;
  896. }
  897. }
  898. else
  899. {
  900. //上区域重叠
  901. x2 = 1;
  902. //融合重叠区域
  903. for (x1 = imageParam.mStitch.Pwidth - imageParam.W_box; x1 < imageParam.mStitch.Pwidth; x1++)
  904. {
  905. y2 = 1;
  906. for (y1 = imageParam.mStitch.Pheight - imageParam.H_box + 1; y1 < imageParam.mStitch.Pheight; y1++)
  907. {
  908. //安全性检测
  909. CheckRC(ref x1, ref y1, imageParam.mStitch.im);
  910. CheckRC(ref x2, ref y2, imageParam.im);
  911. //融合权值
  912. w = (double)x2 / (double)imageParam.W_box;
  913. //加权融合
  914. double ColorRGB = imageParam.mStitch.im.At<Vec3b>(y1, x1).Item0 * (1.0 - w) + imageParam.im.At<Vec3b>(y2, x2).Item0 * w;
  915. imageParam.mStitch.im.Set<Vec3b>(y1, x1, new Vec3b((byte)ColorRGB, (byte)ColorRGB, (byte)ColorRGB));
  916. y2 = y2 + 1;
  917. }
  918. x2 = x2 + 1;
  919. }
  920. }
  921. //最终图
  922. img = new Mat(imageParam.mStitch.Pheight, imageParam.mStitch.Pwidth + imageParam.im.Width - x2 + 1, MatType.CV_8UC3);
  923. //分离出重叠区域
  924. Rect m_select = new Rect(x2 - 1, 0, imageParam.im.Width - x2 + 1, imageParam.mStitch.Pheight);
  925. Mat imgSwitch = new Mat(imageParam.im, m_select);
  926. Cv2.HConcat(imageParam.mStitch.im, imgSwitch, img);
  927. return img;
  928. }
  929. public Mat Fun_StitchRGB(ImageParam imageParam)
  930. {
  931. //图像融合
  932. //输入参数:
  933. //im2——待融合图像
  934. //W_box——宽度信息
  935. //H_box——高度信息
  936. //bdown——上下信息
  937. //MStitch——参数结构
  938. //输出参数:
  939. //MStitch——参数结构
  940. //im——融合图像
  941. Mat img = imageParam.im;
  942. int x1 = 0;
  943. int y1 = 0;
  944. int x2 = 0;
  945. int y2 = 0;
  946. double w = 0.5; //融合权值
  947. if (imageParam.bdown == 1)
  948. {
  949. //下区域重叠
  950. x2 = 1;
  951. //融合重叠区域
  952. for (x1 = imageParam.mStitch.Pwidth - imageParam.W_box; x1 < imageParam.mStitch.Pwidth; x1++)
  953. {
  954. y2 = 1;
  955. for (y1 = imageParam.mStitch.Pheight - imageParam.H_box + 1; y1 < imageParam.mStitch.Pheight; y1++)
  956. {
  957. //安全性检测
  958. CheckRC(ref x1, ref y1, imageParam.mStitch.im);
  959. CheckRC(ref x2, ref y2, imageParam.im);
  960. //融合权值
  961. w = (double)x2 / (double)imageParam.W_box;
  962. //加权融合
  963. double ColorR = imageParam.mStitch.im.At<Vec3b>(y1, x1).Item0 * (1.0 - w) + imageParam.im.At<Vec3b>(y2, x2).Item0 * w;
  964. double ColorG = imageParam.mStitch.im.At<Vec3b>(y1, x1).Item1 * (1.0 - w) + imageParam.im.At<Vec3b>(y2, x2).Item1 * w;
  965. double ColorB = imageParam.mStitch.im.At<Vec3b>(y1, x1).Item2 * (1.0 - w) + imageParam.im.At<Vec3b>(y2, x2).Item2 * w;
  966. if (imageParam.mStitch.im.At<Vec3b>(y1, x1).Item0 == imageParam.mStitch.im.At<Vec3b>(y1, x1).Item1 &&
  967. imageParam.mStitch.im.At<Vec3b>(y1, x1).Item1 == imageParam.mStitch.im.At<Vec3b>(y1, x1).Item2 &&
  968. imageParam.im.At<Vec3b>(y2, x2).Item0 == imageParam.im.At<Vec3b>(y2, x2).Item1 &&
  969. imageParam.im.At<Vec3b>(y2, x2).Item1 == imageParam.im.At<Vec3b>(y2, x2).Item2)
  970. {
  971. imageParam.mStitch.im.Set<Vec3b>(y1, x1, new Vec3b((byte)ColorR, (byte)ColorG, (byte)ColorB));
  972. }
  973. else
  974. {
  975. }
  976. y2 = y2 + 1;
  977. }
  978. x2 = x2 + 1;
  979. }
  980. }
  981. else
  982. {
  983. //上区域重叠
  984. x2 = 1;
  985. //融合重叠区域
  986. for (x1 = imageParam.mStitch.Pwidth - imageParam.W_box; x1 < imageParam.mStitch.Pwidth; x1++)
  987. {
  988. y2 = 1;
  989. for (y1 = imageParam.mStitch.Pheight - imageParam.H_box + 1; y1 < imageParam.mStitch.Pheight; y1++)
  990. {
  991. //安全性检测
  992. CheckRC(ref x1, ref y1, imageParam.mStitch.im);
  993. CheckRC(ref x2, ref y2, imageParam.im);
  994. //融合权值
  995. w = (double)x2 / (double)imageParam.W_box;
  996. //加权融合
  997. double ColorR = imageParam.mStitch.im.At<Vec3b>(y1, x1).Item0 * (1.0 - w) + imageParam.im.At<Vec3b>(y2, x2).Item0 * w;
  998. double ColorG = imageParam.mStitch.im.At<Vec3b>(y1, x1).Item1 * (1.0 - w) + imageParam.im.At<Vec3b>(y2, x2).Item1 * w;
  999. double ColorB = imageParam.mStitch.im.At<Vec3b>(y1, x1).Item2 * (1.0 - w) + imageParam.im.At<Vec3b>(y2, x2).Item2 * w;
  1000. if (imageParam.mStitch.im.At<Vec3b>(y1, x1).Item0 == imageParam.mStitch.im.At<Vec3b>(y1, x1).Item1 &&
  1001. imageParam.mStitch.im.At<Vec3b>(y1, x1).Item1 == imageParam.mStitch.im.At<Vec3b>(y1, x1).Item2 &&
  1002. imageParam.im.At<Vec3b>(y2, x2).Item0 == imageParam.im.At<Vec3b>(y2, x2).Item1 &&
  1003. imageParam.im.At<Vec3b>(y2, x2).Item1 == imageParam.im.At<Vec3b>(y2, x2).Item2)
  1004. {
  1005. imageParam.mStitch.im.Set<Vec3b>(y1, x1, new Vec3b((byte)ColorR, (byte)ColorG, (byte)ColorB));
  1006. }
  1007. else
  1008. {
  1009. }
  1010. y2 = y2 + 1;
  1011. }
  1012. x2 = x2 + 1;
  1013. }
  1014. }
  1015. //最终图
  1016. img = new Mat(imageParam.mStitch.Pheight, imageParam.mStitch.Pwidth + imageParam.im.Width - x2 + 1, MatType.CV_8UC3);
  1017. //分离出重叠区域
  1018. Rect m_select = new Rect(x2 - 1, 0, imageParam.im.Width - x2 + 1, imageParam.mStitch.Pheight);
  1019. Mat imgSwitch = new Mat(imageParam.im, m_select);
  1020. Cv2.HConcat(imageParam.mStitch.im, imgSwitch, img);
  1021. return img;
  1022. }
  1023. }
  1024. }