CImageHandler.cs 43 KB

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