OTSImageDisHelp.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. using OpenCvSharp;
  2. using OTSCLRINTERFACE;
  3. using OTSCommon.DBOperate.Model;
  4. using OTSIncAReportApp.DataOperation.DataAccess;
  5. using OTSIncAReportApp.OTSRstMgrFunction;
  6. using OTSIncAReportGraph.Controls;
  7. using OTSModelSharp.DTLBase;
  8. using OTSModelSharp.ServiceCenter;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Drawing;
  12. using System.Drawing.Drawing2D;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Runtime.Serialization.Formatters.Binary;
  16. using static OTSDataType.otsdataconst;
  17. using Point = System.Drawing.Point;
  18. namespace OTSIncAReportGraph.OTSIncAReportGraphFuncation
  19. {
  20. public class OTSImageDisHelp
  21. {
  22. #region 定义变量
  23. public ResultFile resultFile = null;
  24. public ParticleSegmentation m_ParticleSegmentation;
  25. public Point OTSLeftBottomPoint;
  26. public Point OTSRightUpPoint;
  27. public RectangleF m_originalBackRect;
  28. public double m_pixelSize;
  29. private List<Point> fieldCenterList;
  30. NLog.Logger log;
  31. //电镜设置对象
  32. public ServiceInterface.HardwareController m_cfun = null;
  33. #endregion
  34. #region 构造函数
  35. public OTSImageDisHelp( ResultFile result)
  36. {
  37. resultFile = result;
  38. FieldData fieldData = new FieldData(result.FilePath);
  39. List<Field> fieldlist1 = fieldData.GetFieldList();
  40. resultFile.List_OTSField = fieldlist1;
  41. m_cfun = ServiceInterface.HardwareController.GetSemController();
  42. List<Field> fieldlist = resultFile.List_OTSField;
  43. int fieldwidth;
  44. SizeF s = GetFieldImageSize();
  45. fieldwidth = (int)s.Width;
  46. fieldCenterList = new List<Point>();
  47. for (int i = 0; i < fieldlist.Count(); i++)
  48. {
  49. Point ls_point = new Point() { X = fieldlist[i].FieldPosX, Y = fieldlist[i].FieldPosY };
  50. fieldCenterList.Add(ls_point);
  51. }
  52. SizeF physicalfiledsize = GetPhysicalFieldWidthAndHeight();
  53. m_pixelSize = Convert.ToDouble(physicalfiledsize.Width) / Convert.ToDouble(fieldwidth);
  54. OTSLeftBottomPoint = GetOTSCoordLeftBottomPoint(fieldCenterList, (int)physicalfiledsize.Width, (int)physicalfiledsize.Height);
  55. OTSRightUpPoint = GetOTSCoordRightUpPoint(fieldCenterList, (int)physicalfiledsize.Width, (int)physicalfiledsize.Height);
  56. m_originalBackRect = new RectangleF(0, 0, (float)((OTSRightUpPoint.X - OTSLeftBottomPoint.X)/m_pixelSize), (float)((OTSRightUpPoint.Y - OTSLeftBottomPoint.Y)/m_pixelSize));
  57. log = NLog.LogManager.GetCurrentClassLogger();
  58. }
  59. #endregion
  60. #region 封装自定义方法
  61. private Point GetOTSCoordLeftBottomPoint(List<Point> allFldPos,int fieldwidth,int fieldheight)
  62. {
  63. //the ots coordinate system is always positive on the right and up direction.So the leftbottom point would be the smallest point.
  64. //找出最小的x,y用来做偏移运算
  65. int i_offset_x = 1000000000;
  66. int i_offset_y = 1000000000;
  67. //先取出最小的x,y
  68. for (int i = 0; i < allFldPos.Count; i++)
  69. {
  70. if (i_offset_x > allFldPos[i].X)
  71. {
  72. i_offset_x = allFldPos[i].X;
  73. }
  74. if (i_offset_y > allFldPos[i].Y)
  75. {
  76. i_offset_y = allFldPos[i].Y;
  77. }
  78. }
  79. return new Point(i_offset_x-fieldwidth/2, i_offset_y-fieldheight/2);//the field pos is the center point position.
  80. }
  81. private Point GetOTSCoordRightUpPoint(List<Point> allFldPos, int fieldwidth, int fieldheight)
  82. {
  83. //the ots coordinate system is always positive on the right and up direction.So the leftbottom point would be the biggest point.
  84. //找出最小的x,y用来做偏移运算
  85. int i_offset_x = -1000000000;
  86. int i_offset_y = -1000000000;
  87. //先取出最小的x,y
  88. for (int i = 0; i < allFldPos.Count; i++)
  89. {
  90. if (i_offset_x < allFldPos[i].X)
  91. {
  92. i_offset_x = allFldPos[i].X;
  93. }
  94. if (i_offset_y < allFldPos[i].Y)
  95. {
  96. i_offset_y = allFldPos[i].Y;
  97. }
  98. }
  99. return new Point(i_offset_x + fieldwidth / 2, i_offset_y + fieldheight / 2);//the field pos is the center point position.
  100. }
  101. public PointF ConvertOTSCoordToScreenCoord( PointF currenFldOTSPos)//
  102. {
  103. var OTSPoint=new PointF(currenFldOTSPos.X - OTSLeftBottomPoint.X, currenFldOTSPos.Y - OTSLeftBottomPoint.Y);
  104. int screenHeight = (int)m_originalBackRect.Height + (0 - (int)(Convert.ToDouble(OTSPoint.Y) / m_pixelSize));//because the screen coordinate is downward rightward positive,so we need to translate the Y coordinate of the OTS system which is upward rightward positive.
  105. var screenPoint = new PointF(OTSPoint.X/(float)m_pixelSize, Convert.ToSingle(screenHeight));
  106. var curP = new PointF((screenPoint.X + m_originalBackRect.X), (screenPoint.Y + m_originalBackRect.Y));
  107. return curP;
  108. }
  109. public SizeF GetPhysicalFieldWidthAndHeight()
  110. {
  111. SizeF ret_rect = new SizeF(0, 0);
  112. int width = resultFile.GetImageWidth();
  113. int height = resultFile.GetImageHeight();
  114. double retioOfHeightWidth =(double) height;
  115. retioOfHeightWidth /= width;
  116. double d_scanFieldSize_width = resultFile.GetScanFieldSizeX();
  117. double d_scanFieldSize_height = 0;
  118. if (d_scanFieldSize_width != 0)
  119. d_scanFieldSize_height = d_scanFieldSize_width * retioOfHeightWidth;
  120. ret_rect.Width = (int)d_scanFieldSize_width;
  121. ret_rect.Height = (int)d_scanFieldSize_height;
  122. return ret_rect;
  123. }
  124. public SizeF GetFieldImageSize()
  125. {
  126. SizeF s = new SizeF();
  127. int width = resultFile.GetImageWidth();
  128. int height = resultFile.GetImageHeight();
  129. s.Width = width;
  130. s.Height = height;
  131. return s;
  132. }
  133. #endregion
  134. #region 电镜操作相关方法
  135. /// <summary>
  136. /// 连接电镜,分布图使用
  137. /// </summary>
  138. public bool ConnectToIpcSvr()
  139. {
  140. //和电镜建立通讯连接
  141. return m_cfun.Connect();
  142. }
  143. /// <summary>
  144. /// 移动电镜到指定的X,Y坐标上,R坐标使用原先的值进行移动
  145. /// </summary>
  146. /// <param name="PositionX"></param>
  147. /// <param name="PositionY"></param>
  148. public void MoveSemToPointXY(double in_PositionX, double in_PositionY)
  149. {
  150. log.Trace("Begin MoveSemToPointXY:(" +in_PositionX.ToString()+","+in_PositionY.ToString()+")");
  151. //首先获取电镜当前的位置,并记录原R值
  152. double ls_PositionX = 0;
  153. double ls_PositionY = 0;
  154. double ls_PositionR = 0;
  155. if (m_cfun.Connect())
  156. {
  157. m_cfun.GetSemPositionXY(ref ls_PositionX, ref ls_PositionY, ref ls_PositionR);
  158. }
  159. else
  160. {
  161. log.Error("Failed to GetSemPositionXY");
  162. return;
  163. }
  164. if (m_cfun.Connect())
  165. {
  166. m_cfun.MoveSEMToPoint(new Point((int)in_PositionX, (int)in_PositionY), ls_PositionR);
  167. }
  168. }
  169. #endregion
  170. #region //--------------------------------------颗粒分布图相关部份---------------------------------------------------------------------
  171. /// <summary>
  172. /// 传入颗粒的tagid和fieldid,来获取该颗粒下对应的xray数据
  173. /// </summary>
  174. /// <param name="in_clr_tagid"></param>
  175. /// <param name="in_clr_fieldid"></param>
  176. /// <param name="Search_xray"></param>
  177. /// <param name="Analysis_xray"></param>
  178. public void GetXrayByParticleTagIDAndFieldID_ForDrawDistrbutionImageAndBSE(int in_clr_tagid, int in_clr_fieldid, out uint[] Search_xray, out uint[] Analysis_xray, out int xray_id, out List<Element> list_celementchemistryclr)
  179. {
  180. Search_xray = new uint[2000];
  181. Analysis_xray = new uint[2000];
  182. xray_id = 0;
  183. list_celementchemistryclr = new List<Element>();
  184. //防止为空校验判断
  185. if (resultFile.List_OTSField == null)
  186. return;
  187. Particle particle = resultFile.List_OTSField.Find(x => x.FieldID == in_clr_fieldid).ParticleList.Find(x => x.ParticleId == in_clr_tagid);
  188. var tmpPart = new ParticleData(resultFile.FilePath).GetParticleXrayDataByFidAndPid(Convert.ToString(particle.FieldId), Convert.ToString(particle.XrayId));
  189. if (tmpPart != null)
  190. {
  191. particle.XRayData = tmpPart.XRayData;
  192. if (particle.XrayId > -1)
  193. {
  194. for (int i = 0; i < 2000; i++)
  195. {
  196. Analysis_xray[i] = BitConverter.ToUInt32(particle.XRayData, i * 4);
  197. }
  198. Search_xray = Analysis_xray;
  199. xray_id = particle.XrayId;
  200. list_celementchemistryclr = particle.ElementList;
  201. }
  202. }
  203. }
  204. /// <summary>
  205. /// 传入所有的物理field坐标点,和单个物理field的宽高,返回所有field的左上角位置,和整个field组成的rect大小
  206. /// </summary>
  207. /// <param name="in_list_point"></param>
  208. /// <param name="in_width"></param>
  209. /// <param name="in_height"></param>
  210. /// <returns></returns>
  211. public Rectangle GetWlRectTopLeftAndRect(List<Point> in_list_point, int in_width, int in_height)
  212. {
  213. //分别获取整个rect的xy最小值和最大值
  214. int i_rect_x_min = 100000000;
  215. int i_rect_y_min = 100000000;
  216. int i_rect_x_max = -100000000;
  217. int i_rect_y_max = -100000000;
  218. for (int i = 0; i < in_list_point.Count; i++)
  219. {
  220. if (i_rect_x_min > in_list_point[i].X)
  221. i_rect_x_min = in_list_point[i].X;
  222. if (i_rect_y_min > in_list_point[i].Y)
  223. i_rect_y_min = in_list_point[i].Y;
  224. if (i_rect_x_max < in_list_point[i].X)
  225. i_rect_x_max = in_list_point[i].X;
  226. if (i_rect_y_max < in_list_point[i].Y)
  227. i_rect_y_max = in_list_point[i].Y;
  228. }
  229. Rectangle ret_rect = new Rectangle(i_rect_x_min, i_rect_y_min,
  230. i_rect_x_max - i_rect_x_min, i_rect_y_max - i_rect_y_min);
  231. return ret_rect;
  232. }
  233. /// <summary>
  234. /// 根据Field的ID,来获取Field列表中对应FIeld的OTS 坐标
  235. /// </summary>
  236. /// <param name="in_fieldid"></param>
  237. /// <returns></returns>
  238. public Point GetOTSPointByFieldID(List<DisplayRectangle> in_list_dfield, int in_fieldid)
  239. {
  240. Point ret_point = new Point(0, 0);
  241. for (int i = 0; i < in_list_dfield.Count; i++)
  242. {
  243. //这里TagID先代表的是底层返回的ID
  244. if (in_list_dfield[i].FieldID == in_fieldid.ToString())
  245. {
  246. ret_point = new Point(Convert.ToInt32(in_list_dfield[i].OTSCoordinatePos.X), Convert.ToInt32(in_list_dfield[i].OTSCoordinatePos.Y));
  247. }
  248. }
  249. return ret_point;
  250. }
  251. /// <summary>
  252. /// 将OTS坐标转换为Sem 坐标
  253. /// </summary>
  254. /// <param name="POTSCoord"></param>
  255. /// <returns></returns>
  256. public Point ChangeOTSToSemCoord(Point POTSCoord)
  257. {
  258. //first if m_semstagedata is null to get stage inforation
  259. Point ret_SEM_point = new Point();
  260. // get center point, um
  261. long xStart = resultFile.GetXAxisStart();
  262. long xEnd = resultFile.GetXAxisEnd();
  263. long xCenter = (xStart + xEnd) / 2;
  264. int yStart = resultFile.GetYAxisStart();
  265. long yEnd = resultFile.GetYAxisEnd();
  266. long yCenter = (yStart + yEnd) / 2;
  267. // delte = SEM - OTSa
  268. long deltex = xCenter - 0;
  269. long deltey = yCenter - 0;
  270. OTS_X_AXIS_DIRECTION xdir = resultFile.GetXAxisDirEnum();
  271. OTS_Y_AXIS_DIRECTION ydir = resultFile.GetYAxisDirEnum();
  272. if (xdir == OTS_X_AXIS_DIRECTION.LEFT_TOWARD)
  273. {
  274. ret_SEM_point.X = -1 * (POTSCoord.X - Convert.ToInt32(deltex));
  275. }
  276. else if (xdir == OTS_X_AXIS_DIRECTION.RIGHT_TOWARD)
  277. {
  278. ret_SEM_point.X = POTSCoord.X + Convert.ToInt32(deltex);
  279. }
  280. if (ydir == OTS_Y_AXIS_DIRECTION.UP_TOWARD)
  281. {
  282. ret_SEM_point.Y = POTSCoord.Y + Convert.ToInt32(deltey);
  283. }
  284. else if (ydir == OTS_Y_AXIS_DIRECTION.DOWN_TOWARD)
  285. {
  286. ret_SEM_point.Y = -1 * (POTSCoord.Y - Convert.ToInt32(deltey));
  287. }
  288. return ret_SEM_point;
  289. }
  290. #endregion
  291. public bool WhetherInRange(RectangleF rec, Point WhetherPoint)
  292. {
  293. var rect = rec;
  294. if ((rect.Left < WhetherPoint.X && WhetherPoint.X < rect.Right) && (rect.Top < WhetherPoint.Y && WhetherPoint.Y < rect.Bottom))
  295. {
  296. return true;
  297. }
  298. else
  299. {
  300. return false;
  301. }
  302. }
  303. #region 颗粒分割功能方法
  304. /// <summary>
  305. /// 获取线段与矩形的两个交点
  306. /// </summary>
  307. /// <param name="line1S"></param>
  308. /// <param name="line1E"></param>
  309. /// <param name="rectangle"></param>
  310. /// <param name="pointList"></param>
  311. /// <returns></returns>
  312. public bool GetInterBetweenLinesAndRect(Point line1S, Point line1E, RectangleF rectangle, ref List<Point> pointList)
  313. {
  314. try
  315. {
  316. PointF pointF = new PointF(-1, -1);
  317. Rect rect = new Rect();
  318. rect.X = m_ParticleSegmentation.ParticleData.RectLeft;
  319. rect.Y = m_ParticleSegmentation.ParticleData.RectTop;
  320. rect.Width = m_ParticleSegmentation.ParticleData.RectWidth;
  321. rect.Height = m_ParticleSegmentation.ParticleData.RectHeight;
  322. float border = 0;
  323. if (GetInterBetweenTwoLines(line1S, line1E, new PointF(rectangle.Left, rectangle.Top), new PointF(rectangle.Right, rectangle.Top), ref pointF))
  324. {
  325. border = (pointF.X - rectangle.Left) / (rectangle.Right - rectangle.Left);
  326. pointList.Add(new Point((int)(rect.X + rect.Width * border), rect.Y));
  327. }
  328. if (GetInterBetweenTwoLines(line1S, line1E,
  329. new PointF(rectangle.Left, rectangle.Bottom), new PointF(rectangle.Right, rectangle.Bottom), ref pointF))
  330. {
  331. border = (pointF.X - rectangle.Left) / (rectangle.Right - rectangle.Left);
  332. pointList.Add(new Point((int)(rect.X + rect.Width * border), rect.Y + rect.Height));
  333. }
  334. if (GetInterBetweenTwoLines(line1S, line1E,
  335. new PointF(rectangle.Left, rectangle.Top), new PointF(rectangle.Left, rectangle.Bottom), ref pointF))
  336. {
  337. border = (pointF.Y - rectangle.Top) / (rectangle.Bottom - rectangle.Top);
  338. pointList.Add(new Point(rect.X, (int)(rect.Y + rect.Height * border)));
  339. }
  340. if (GetInterBetweenTwoLines(line1S, line1E,
  341. new PointF(rectangle.Right, rectangle.Top), new PointF(rectangle.Right, rectangle.Bottom), ref pointF))
  342. {
  343. border = (pointF.Y - rectangle.Top) / (rectangle.Bottom - rectangle.Top);
  344. pointList.Add(new Point(rect.X + rect.Width, (int)(rect.Y + rect.Height * border)));
  345. }
  346. if (pointList.Count == 2)
  347. {
  348. return true;
  349. }
  350. else
  351. {
  352. return false;
  353. }
  354. }
  355. catch (Exception ex)
  356. {
  357. log.Trace("(GetSplitPartFun)" + ex);
  358. return false;
  359. }
  360. }
  361. /// <summary>
  362. /// 获取分离颗粒方法
  363. /// </summary>
  364. /// <param name="startPoint"></param>
  365. /// <param name="endPoint"></param>
  366. /// <returns></returns>
  367. public bool GetSplitPartFun(Point startPoint, Point endPoint, float m_pixelSize)
  368. {
  369. try
  370. {
  371. CImageHandler m_ImagePro = new CImageHandler();
  372. Particle particle1 = (Particle)CloneObject(m_ParticleSegmentation.ParticleData);
  373. Particle particle2 = (Particle)CloneObject(m_ParticleSegmentation.ParticleData);
  374. int width = resultFile.GetImageWidth();
  375. int height = resultFile.GetImageHeight();
  376. using (Mat mat = new Mat(height, width, MatType.CV_8UC1, Scalar.Black))//黑色底图
  377. using (Mat labelMat = new Mat())
  378. using (Mat stats = new Mat())
  379. using (Mat centroids = new Mat())
  380. using (Mat matBse = new Mat(resultFile.List_OTSField[particle1.FieldId].FieldImageName, ImreadModes.Grayscale))
  381. {
  382. foreach (Segment segment in m_ParticleSegmentation.ParticleData.SegmentList)
  383. {
  384. Cv2.Line(mat, new OpenCvSharp.Point(segment.Start, segment.Height), new OpenCvSharp.Point(segment.Start + segment.Length, segment.Height), Scalar.White, 1, LineTypes.AntiAlias);
  385. }
  386. //寻找坐标点
  387. List<Point> points1 = new List<Point>();
  388. List<Point> points2 = new List<Point>();
  389. List<int> aveGray1 = new List<int>();
  390. List<int> aveGray2 = new List<int>();
  391. for (int k = 0; k < mat.Height; k++)
  392. {
  393. for (int j = 0; j < mat.Width; j++)
  394. {
  395. if (mat.Get<int>(k, j) != 0)
  396. {
  397. int side = (startPoint.X - j) * (endPoint.Y - k) - (startPoint.Y - k) * (endPoint.X - j);
  398. if (side >= 0)//区分像素位置
  399. {
  400. points1.Add(new Point(j, k));
  401. aveGray1.Add(matBse.Get<byte>(k, j));
  402. }
  403. else
  404. {
  405. points2.Add(new Point(j, k));
  406. aveGray2.Add(matBse.Get<byte>(k, j));
  407. }
  408. }
  409. }
  410. }
  411. //处理Segment
  412. List<COTSSegmentClr> SegmentClrList1 = new List<COTSSegmentClr>();
  413. List<COTSSegmentClr> SegmentClrList2 = new List<COTSSegmentClr>();
  414. List<Segment> SegmentList1 = new List<Segment>();
  415. List<Segment> SegmentList2 = new List<Segment>();
  416. GetSegment(points1, m_ParticleSegmentation.ParticleData, SegmentClrList1, ref SegmentList1);
  417. GetSegment(points2, m_ParticleSegmentation.ParticleData, SegmentClrList2, ref SegmentList2);
  418. //颗粒一
  419. Cv2.Threshold(mat, mat, 0, 0, ThresholdTypes.Binary);
  420. foreach (Segment segment in SegmentList1)
  421. {
  422. Cv2.Line(mat, new OpenCvSharp.Point(segment.Start, segment.Height), new OpenCvSharp.Point(segment.Start + segment.Length, segment.Height), Scalar.White, 1, LineTypes.AntiAlias);
  423. }
  424. Cv2.ConnectedComponentsWithStats(mat, labelMat, stats, centroids, PixelConnectivity.Connectivity8);
  425. CopyToPart(particle1, SegmentList1, SegmentClrList1, aveGray1, stats, centroids, m_pixelSize);
  426. //颗粒二
  427. Cv2.Threshold(mat, mat, 0, 0, ThresholdTypes.Binary);
  428. foreach (Segment segment in SegmentList2)
  429. {
  430. Cv2.Line(mat, new OpenCvSharp.Point(segment.Start, segment.Height), new OpenCvSharp.Point(segment.Start + segment.Length, segment.Height), Scalar.White, 1, LineTypes.AntiAlias);
  431. }
  432. Cv2.ConnectedComponentsWithStats(mat, labelMat, stats, centroids, PixelConnectivity.Connectivity8);
  433. CopyToPart(particle2, SegmentList2, SegmentClrList2, aveGray2, stats, centroids, m_pixelSize);
  434. if (!SaveToDb(particle1, particle2))
  435. {
  436. log.Trace("(GetSplitPartFun) SaveToDb Faild");
  437. return false;
  438. }
  439. }
  440. return true;
  441. }
  442. catch (Exception ex)
  443. {
  444. log.Trace("(GetSplitPartFun)" + ex);
  445. return false;
  446. }
  447. }
  448. /// <summary>
  449. /// 拷贝颗粒数据
  450. /// </summary>
  451. /// <param name="particle"></param>
  452. /// <param name="segments"></param>
  453. /// <param name="SegmentClrList"></param>
  454. /// <param name="aveGray"></param>
  455. /// <param name="stats"></param>
  456. /// <param name="centroids"></param>
  457. /// <param name="dPixelSize"></param>
  458. private void CopyToPart(Particle particle, List<Segment> segments, List<COTSSegmentClr> SegmentClrList, List<int> aveGray, Mat stats, Mat centroids, double dPixelSize)
  459. {
  460. CImageHandler m_ImagePro = new CImageHandler();
  461. COTSParticleClr part = new COTSParticleClr();
  462. part.SetActualArea(stats.At<int>(1, 4) * dPixelSize * dPixelSize);
  463. part.SetParticleRect(new Rectangle(stats.At<int>(1, 0), stats.At<int>(1, 1), stats.At<int>(1, 2), stats.At<int>(1, 3)));
  464. part.GetFeature().SetSegmentsList(SegmentClrList, false);
  465. m_ImagePro.CalParticleImageProp(part, dPixelSize);
  466. particle.SegmentList = segments;
  467. particle.SegmentNum = segments.Count;
  468. particle.AveGray = (int)aveGray.Average();
  469. particle.RectLeft = stats.At<int>(1, 0);
  470. particle.RectTop = stats.At<int>(1, 1);
  471. particle.RectWidth = stats.At<int>(1, 2);
  472. particle.RectHeight = stats.At<int>(1, 3);
  473. particle.Area = stats.At<int>(1, 4) * dPixelSize * dPixelSize;
  474. particle.PosX = (int)centroids.At<double>(1, 0);
  475. particle.PosY = (int)centroids.At<double>(1, 1);
  476. particle.DFERET = part.GetFeretDiameter();
  477. particle.DMAX = part.GetDMAX();
  478. particle.DMIN = part.GetDMIN();
  479. particle.DPERP = part.GetDMPERP();
  480. particle.PERIMETER = part.GetDPRIMETER();
  481. particle.ORIENTATION = part.GetORIENTATION();
  482. particle.DINSCR = part.GetDINSCR();
  483. particle.DMEAN = part.GetDMEAN();
  484. particle.DELONG = part.GetDELONG();
  485. }
  486. /// <summary>
  487. /// 保存数据库
  488. /// </summary>
  489. /// <param name="particle1"></param>
  490. /// <param name="particle2"></param>
  491. /// <returns></returns>
  492. public bool SaveToDb(Particle particle1, Particle particle2)
  493. {
  494. //初始化
  495. SQLiteHelper sQLiteHelper = new SQLiteHelper(resultFile.FilePath + "\\FIELD_FILES\\Inclusion.db");
  496. sQLiteHelper.GetDBConnection();
  497. sQLiteHelper.BeginTransaction();
  498. //修改Segment表
  499. if (particle1.SegmentList.Count == 0 || particle2.SegmentList.Count == 0)
  500. {
  501. return false;
  502. }
  503. int SegmentCount = sQLiteHelper.ExecuteNewPartIdForTransaction() + 1;
  504. if (!sQLiteHelper.ExecuteSegmentForTransaction(particle1, particle2, SegmentCount))//修改Segment表
  505. {
  506. return false;
  507. }
  508. if (!sQLiteHelper.ExecuteXrayForTransaction(particle1, particle2, SegmentCount))//修改XRayData和PoxXrayInfo表
  509. {
  510. return false;
  511. }
  512. if (!sQLiteHelper.ExecuteIncAForTransaction(particle1, particle2, SegmentCount))//修改IncAData表
  513. {
  514. return false;
  515. }
  516. sQLiteHelper.CommitTransaction();
  517. return true;
  518. }
  519. /// <summary>
  520. /// 线段与矩形是否相交
  521. /// </summary>
  522. /// <param name="linePointX1"></param>
  523. /// <param name="linePointY1"></param>
  524. /// <param name="linePointX2"></param>
  525. /// <param name="linePointY2"></param>
  526. /// <param name="rectangleLeftTopX"></param>
  527. /// <param name="rectangleLeftTopY"></param>
  528. /// <param name="rectangleRightBottomX"></param>
  529. /// <param name="rectangleRightBottomY"></param>
  530. /// <returns></returns>
  531. public bool isLineIntersectRectangle(float linePointX1,
  532. float linePointY1,
  533. float linePointX2,
  534. float linePointY2,
  535. float rectangleLeftTopX,
  536. float rectangleLeftTopY,
  537. float rectangleRightBottomX,
  538. float rectangleRightBottomY)
  539. {
  540. float lineHeight = linePointY1 - linePointY2;
  541. float lineWidth = linePointX2 - linePointX1; // 计算叉乘
  542. float c = linePointX1 * linePointY2 - linePointX2 * linePointY1;
  543. if ((lineHeight * rectangleLeftTopX + lineWidth * rectangleLeftTopY + c >= 0 && lineHeight * rectangleRightBottomX + lineWidth * rectangleRightBottomY + c <= 0)
  544. || (lineHeight * rectangleLeftTopX + lineWidth * rectangleLeftTopY + c <= 0 && lineHeight * rectangleRightBottomX + lineWidth * rectangleRightBottomY + c >= 0)
  545. || (lineHeight * rectangleLeftTopX + lineWidth * rectangleRightBottomY + c >= 0 && lineHeight * rectangleRightBottomX + lineWidth * rectangleLeftTopY + c <= 0)
  546. || (lineHeight * rectangleLeftTopX + lineWidth * rectangleRightBottomY + c <= 0 && lineHeight * rectangleRightBottomX + lineWidth * rectangleLeftTopY + c >= 0))
  547. {
  548. if (rectangleLeftTopX > rectangleRightBottomX)
  549. {
  550. float temp = rectangleLeftTopX;
  551. rectangleLeftTopX = rectangleRightBottomX;
  552. rectangleRightBottomX = temp;
  553. }
  554. if (rectangleLeftTopY < rectangleRightBottomY)
  555. {
  556. float temp1 = rectangleLeftTopY;
  557. rectangleLeftTopY = rectangleRightBottomY;
  558. rectangleRightBottomY = temp1;
  559. }
  560. if ((linePointX1 < rectangleLeftTopX && linePointX2 < rectangleLeftTopX)
  561. || (linePointX1 > rectangleRightBottomX && linePointX2 > rectangleRightBottomX)
  562. || (linePointY1 > rectangleLeftTopY && linePointY2 > rectangleLeftTopY)
  563. || (linePointY1 < rectangleRightBottomY && linePointY2 < rectangleRightBottomY))
  564. {
  565. return false;
  566. }
  567. else
  568. {
  569. return true;
  570. }
  571. }
  572. else
  573. {
  574. return false;
  575. }
  576. }
  577. /// <summary>
  578. /// 求一个线段与另一个线段的交点
  579. /// </summary>
  580. /// <param name="line1S"></param>
  581. /// <param name="line1E"></param>
  582. /// <param name="line2S"></param>
  583. /// <param name="line2E"></param>
  584. /// <param name="interPoint"></param>
  585. /// <returns>-1:不存在交点;1:存在一个交点;2:存在无穷多个交点(重合或部分重合)</returns>
  586. public bool GetInterBetweenTwoLines(PointF line1S, PointF line1E,
  587. PointF line2S, PointF line2E, ref PointF interPoint)
  588. {
  589. int status = -1;
  590. // 判断两条直线各自的矩形包围盒的X与Y的值范围
  591. float line1Xmin = line1S.X < line1E.X ? line1S.X : line1E.X,
  592. line1Xmax = line1S.X > line1E.X ? line1S.X : line1E.X,
  593. line1Ymin = line1S.Y < line1E.Y ? line1S.Y : line1E.Y,
  594. line1Ymax = line1S.Y > line1E.Y ? line1S.Y : line1E.Y,
  595. line2Xmin = line2S.X < line2E.X ? line2S.X : line2E.X,
  596. line2Xmax = line2S.X > line2E.X ? line2S.X : line2E.X,
  597. line2Ymin = line2S.Y < line2E.Y ? line2S.Y : line2E.Y,
  598. line2Ymax = line2S.Y > line2E.Y ? line2S.Y : line2E.Y;
  599. if (line1S.X - line1E.X == 0)
  600. {
  601. // 两条线都垂直于X轴
  602. if (line2S.X - line2E.X == 0)
  603. {
  604. if (line1S.X != line2S.X) status = -1;
  605. else if ((line1Ymin > line2Ymax) || (line1Ymax < line2Ymin)) status = -1;
  606. else if (line1Ymin == line2Ymax)
  607. {
  608. interPoint = new PointF(line1S.X, line1Ymin); status = 1;
  609. }
  610. else if (line1Ymax == line2Ymin)
  611. {
  612. interPoint = new PointF(line1S.X, line1Ymax); status = 1;
  613. }
  614. else status = 2;
  615. }
  616. // line1垂直于X轴,line2不垂直于X轴
  617. else
  618. {
  619. float slope = (line2S.Y - line2E.Y) / (line2S.X - line2E.X);
  620. float offset = line2S.Y - slope * line2S.X;
  621. float newX = line1S.X, newY = slope * newX + offset;
  622. if (newX >= line2Xmin && newX <= line2Xmax && newY >= line1Ymin && newY <= line1Ymax
  623. && newY >= line2Ymin && newY <= line2Ymax)
  624. {
  625. interPoint = new PointF(newX, newY); status = 1;
  626. }
  627. }
  628. }
  629. else
  630. {
  631. // line1不垂直于X轴,line2垂直于X轴
  632. if (line2S.X - line2E.X == 0)
  633. {
  634. float slope = (line1S.Y - line1E.Y) / (line1S.X - line1E.X);
  635. float offset = line1S.Y - slope * line1S.X;
  636. float newX = line2S.X, newY = slope * newX + offset;
  637. if (newX >= line1Xmin && newX <= line1Xmax && newY >= line1Ymin && newY <= line1Ymax
  638. && newY >= line2Ymin && newY <= line2Ymax)
  639. {
  640. interPoint = new PointF(newX, newY); status = 1;
  641. }
  642. }
  643. // line1和line2都不垂直于X轴
  644. else
  645. {
  646. float slope1 = (line1S.Y - line1E.Y) / (line1S.X - line1E.X);
  647. float offset1 = line1S.Y - slope1 * line1S.X;
  648. float slope2 = (line2S.Y - line2E.Y) / (line2S.X - line2E.X);
  649. float offset2 = line2S.Y - slope2 * line2S.X;
  650. // 如果两条直线平行
  651. if (slope1 == slope2)
  652. {
  653. if (offset1 != offset2) status = -1;
  654. else if (line1Xmax == line2Xmin)
  655. {
  656. interPoint = new PointF(line1Xmax, line1Xmax * slope1 + offset1); status = 1;
  657. }
  658. else if (line1Xmin == line2Xmax)
  659. {
  660. interPoint = new PointF(line1Xmin, line1Xmin * slope1 + offset1); status = 1;
  661. }
  662. else if (line1Xmax < line2Xmin || line1Xmin > line2Xmax) status = -1;
  663. else status = 2;
  664. }
  665. else
  666. {
  667. float newX = (offset2 - offset1) / (slope1 - slope2), newY = newX * slope1 + offset1;
  668. if (newX >= line1Xmin && newX <= line1Xmax && newX >= line2Xmin && newX <= line2Xmax)
  669. {
  670. interPoint = new PointF(newX, newY); status = 1;
  671. }
  672. }
  673. }
  674. }
  675. if (status == 1)
  676. {
  677. return true;
  678. }
  679. else
  680. {
  681. return false;
  682. }
  683. }
  684. /// <summary>
  685. /// 获取Segment信息
  686. /// </summary>
  687. /// <param name="points"></param>
  688. /// <param name="SegmentClrList"></param>
  689. /// <param name="SegmentList"></param>
  690. public void GetSegment(List<Point> points, Particle ParticleData, List<COTSSegmentClr> SegmentClrList, ref List<Segment> SegmentList)
  691. {
  692. List<int> segmentStart = new List<int>();
  693. List<int> segmentHeight = new List<int>();
  694. List<int> segmentLength = new List<int>();
  695. FindSegment(points, ref segmentStart, ref segmentHeight, ref segmentLength);
  696. for (int i = 0; i < segmentLength.Count; i++)
  697. {
  698. COTSSegmentClr seg = new COTSSegmentClr();
  699. seg.SetStart(segmentStart[i]);
  700. seg.SetHeight(segmentHeight[i]);
  701. seg.SetLength(segmentLength[i]);
  702. SegmentClrList.Add(seg);
  703. Segment segment = new Segment();
  704. segment.Start = segmentStart[i];
  705. segment.Height = segmentHeight[i];
  706. segment.Length = segmentLength[i];
  707. segment.SegmentNum = segmentLength.Count;
  708. segment.SegmentId = i;
  709. segment.FieldId = ParticleData.FieldId;
  710. segment.XRayId = ParticleData.XrayId;
  711. segment.ParticleId = ParticleData.ParticleId;
  712. SegmentList.Add(segment);
  713. }
  714. }
  715. /// <summary>
  716. /// 寻找segment方法
  717. /// </summary>
  718. public void FindSegment(List<Point> point_list_final, ref List<int> segmentStart, ref List<int> segmentHeight, ref List<int> segmentLength)
  719. {
  720. int flag = 0;
  721. point_list_final.Sort(my_sort);//排序
  722. for (int k = 0; k < point_list_final.Count(); k++)
  723. {
  724. if (k == 0 && k != point_list_final.Count() - 1)//第一个颗粒
  725. {
  726. flag++;
  727. segmentStart.Add(point_list_final[k].X);
  728. segmentHeight.Add(point_list_final[k].Y);
  729. }
  730. else if (k == 0 && k == point_list_final.Count() - 1)//只有一个颗粒
  731. {
  732. segmentStart.Add(point_list_final[k].X);
  733. segmentHeight.Add(point_list_final[k].Y);
  734. segmentLength.Add(1);
  735. }
  736. else if (k == point_list_final.Count() - 1)//最后一个颗粒
  737. {
  738. flag++;
  739. segmentLength.Add(flag);
  740. }
  741. else if (point_list_final[k].Y == point_list_final[k - 1].Y && point_list_final[k].X == point_list_final[k - 1].X + 1)//同行连续
  742. {
  743. flag++;
  744. }
  745. else if (point_list_final[k].Y == point_list_final[k - 1].Y && point_list_final[k].X != point_list_final[k - 1].X + 1)//同行隔断
  746. {
  747. segmentLength.Add(flag);
  748. flag = 1;
  749. segmentStart.Add(point_list_final[k].X);
  750. segmentHeight.Add(point_list_final[k].Y);
  751. }
  752. else if (point_list_final[k].Y != point_list_final[k - 1].Y)//不同行
  753. {
  754. segmentLength.Add(flag);
  755. flag = 1;
  756. segmentStart.Add(point_list_final[k].X);
  757. segmentHeight.Add(point_list_final[k].Y);
  758. }
  759. }
  760. }
  761. /// <summary>
  762. /// 排序方法
  763. /// </summary>
  764. /// <param name="p1"></param>
  765. /// <param name="p2"></param>
  766. /// <returns></returns>
  767. private int my_sort(Point p1, Point p2)
  768. {
  769. return p1.Y == p2.Y ? p1.X.CompareTo(p2.X) : p1.Y.CompareTo(p2.Y); //升序排序
  770. }
  771. public object CloneObject(object obj)
  772. {
  773. using (MemoryStream stream = new MemoryStream())
  774. {
  775. BinaryFormatter formatter = new BinaryFormatter();
  776. stream.Position = 0;
  777. formatter.Serialize(stream, obj);
  778. stream.Position = 0;
  779. return formatter.Deserialize(stream);
  780. }
  781. }
  782. #endregion
  783. }
  784. }