public void FindLine(HImage image, double Row, double Column, double Phi, double Length1, double Length2, ushort pointNum, ushort EdgeWidth, ushort EdgeThresold, EdgePolarEnum edgePolar, EdgeTypeEnum edgeType, out PointF firstPoint, out PointF endPoint, out double angle, out HXLDCont[] cross, out HXLDCont line) { double[] row = new double[pointNum]; double[] column = new double[pointNum]; HTuple _rowEdges = new HTuple(); HTuple _columnEdges = new HTuple(); image.GetImageSize(out int width, out int height); for (int i = 0; i <= (pointNum - 1) / 2; i++)//遍历搜索点数的一半 { row[i] = Row - (Length2 - 2.0 * Length2 * (i + 1) / (pointNum + 1)) * Math.Cos(Phi); column[i] = Column - (Length2 - 2.0 * Length2 * (i + 1) / (pointNum + 1)) * Math.Sin(Phi); row[pointNum - 1 - i] = 2 * Row - row[i]; column[pointNum - 1 - i] = 2 * Column - column[i]; } for (int i = 0; i < pointNum - 1; i++) { HMeasure measure = new HMeasure(row[i], column[i], Phi, Length1, EdgeWidth, width, height, "nearest_neighbor"); //获得测量矩形 measure.MeasurePos(image, 1, EdgeThresold, edgePolar.ToString(), edgeType.ToString(), out HTuple rowEdge, out HTuple columnEdge, out HTuple Amplitude, out HTuple distance); //提取垂直于矩形或环形弧的直边 if (rowEdge.Length > 0 && columnEdge.Length > 0) { _rowEdges = _rowEdges.TupleConcat(rowEdge); _columnEdges = _columnEdges.TupleConcat(columnEdge); } } if (_rowEdges.Length > 5) { HXLDCont Contour = new HXLDCont(_rowEdges, _columnEdges); Contour.SmoothContoursXld(5); Contour.FitLineContourXld("tukey", -1, 0, 5, 2, out double rowBegin, out double columnBegin, out double rowEnd, out double columnEnd, out double nr, out double nc, out double dist); Contour.GenContourPolygonXld(new HTuple(rowBegin, rowEnd), new HTuple(columnBegin, columnEnd)); cross = new HXLDCont[_rowEdges.Length]; for (int i = 0; i < _rowEdges.Length; i++) { HXLDCont _cross = new HXLDCont(); _cross.GenCrossContourXld(_rowEdges[i], _columnEdges[i], 30d, 0); cross[i] = _cross; } firstPoint = new PointF((float)columnBegin, (float)rowBegin); endPoint = new PointF((float)columnEnd, (float)rowEnd); angle = HMisc.AngleLx(rowBegin, columnBegin, rowEnd, columnEnd) * 180 / Math.PI; line = Contour; } else { firstPoint = new PointF(0, 0); endPoint = new PointF(0, 0); cross = null; line = null; angle = 0; } }
private double FindPositionBottle(HImage Image, DataType.AlgoritmoControlloLivelloParam param, bool useIconic, ref ArrayList iconicList) { double columnCenter = 0; HMeasure MeasureHandle = null; HImage ImgEmfatize = null; try { double Rowstart, Rowend, Colstart, Colend; int Width, Height; Image.GetImageSize(out Width, out Height); Rowstart = 0.0; Rowend = Height; Colstart = Width / 2; Colend = Width / 2; if (useIconic) { iconicList.Add(new Utilities.ObjectToDisplay("Line", new HTuple(Rowstart, Colstart, Rowend, Colend), "red", 1)); } param.RectCentraggio.Column = Width / 2; if (useIconic) { // genero i due rettangoli per la misura dell'inclinazione bottiglia // solo per visualizzazione HRegion rectangleCentraggio = new HRegion(); rectangleCentraggio.GenRectangle2(param.RectCentraggio.Row, Width / 2, 0, param.RectCentraggio.Length1, param.RectCentraggio.Length2); iconicList.Add(new Utilities.ObjectToDisplay(rectangleCentraggio, "red", 1)); } // Determine all edge pairs that have a negative transition, i.e., edge pairs that enclose dark regions. const string Interpolation = "nearest_neighbor"; HTuple RowEdge_first, ColumnEdge_first, Amplitude_first, Distance_first; HTuple RowEdge_last, ColumnEdge_last, Amplitude_last, Distance_last; ImgEmfatize = Image.Emphasize(7, 7, param.EmphasizeFactorx10 / 10.0); // Misura degli edge sul primo rettangolo MeasureHandle = new HMeasure(param.RectCentraggio.Row, Width / 2, 0, param.RectCentraggio.Length1, param.RectCentraggio.Length2, Width, Height, Interpolation); MeasureHandle.MeasurePos(ImgEmfatize, param.Sigmax10 / 10.0, param.Thresholdx10 / 10.0, "all", "first", out RowEdge_first, out ColumnEdge_first, out Amplitude_first, out Distance_first); MeasureHandle.Dispose(); // Libera lo spazio associato a 'MeasureHandle' MeasureHandle = new HMeasure(param.RectCentraggio.Row, Width / 2, Math.PI, param.RectCentraggio.Length1, param.RectCentraggio.Length2, Width, Height, Interpolation); MeasureHandle.MeasurePos(ImgEmfatize, param.Sigmax10 / 10.0, param.Thresholdx10 / 10.0, "all", "first", out RowEdge_last, out ColumnEdge_last, out Amplitude_last, out Distance_last); MeasureHandle.Dispose(); // Libera lo spazio associato a 'MeasureHandle' if (RowEdge_first.Length == 1 && RowEdge_last.Length == 1 && ColumnEdge_first.Length == 1 && ColumnEdge_last.Length == 1) { columnCenter = (ColumnEdge_last.D + ColumnEdge_first.D) / 2; if (useIconic) { iconicList.Add(new Utilities.ObjectToDisplay("Cross", new HTuple(RowEdge_first.D, ColumnEdge_first.D, 15, Math.PI / 4), "magenta", 1)); iconicList.Add(new Utilities.ObjectToDisplay("Cross", new HTuple(RowEdge_last.D, ColumnEdge_last.D, 15, Math.PI / 4), "magenta", 1)); iconicList.Add(new Utilities.ObjectToDisplay("Cross", new HTuple(RowEdge_last.D, columnCenter, 15, Math.PI / 4), "green", 1)); } } else { columnCenter = Width / 2; } } catch (Exception) { columnCenter = 0; } finally { if (MeasureHandle != null) { MeasureHandle.Dispose(); } if (ImgEmfatize != null) { ImgEmfatize.Dispose(); } } return(columnCenter); }