} //end of method /// <summary> /// Auxiliary method to recalculate the contour points of /// the rectangle by transforming the initial row and /// column coordinates (rowsInit, colsInit) by the updated /// homography hom2D /// </summary> private void updateHandlePos() { hom2D.HomMat2dIdentity(); hom2D = hom2D.HomMat2dTranslate(midC, midR); hom2D = hom2D.HomMat2dRotateLocal(phi); tmp = hom2D.HomMat2dScaleLocal(length1, length2); cols = tmp.AffineTransPoint2d(colsInit, rowsInit, out rows); }
/// <summary> ///更新操作点 /// </summary> private void updateHandlePos() { _hom2D.HomMat2dIdentity(); _hom2D = _hom2D.HomMat2dTranslate(this._mid_col_x, this._mid_row_y); _hom2D = _hom2D.HomMat2dRotateLocal(this._phi); _tmp = _hom2D.HomMat2dScaleLocal(this._len1, this._len2); _cols_x = _tmp.AffineTransPoint2d(_colsInit_x, _rowsInit_y, out _rows_y); updateInterface(); }
public ToolResult GetResult() { try { int imageWidt, imageHeight; HMetrologyModel metrologyModel = new HMetrologyModel(); HTuple startRow, startCol, endRow, endCol; HHomMat2D matd = new HHomMat2D(); HHomMat2D matd3 = matd.HomMat2dRotate(info.Rectangle2Angle, 0.0, 0.0); HHomMat2D matd4 = matd3.HomMat2dTranslate(info.Rectangle2Row, info.Rectangle2Col); HOperatorSet.AffineTransPoint2d(matd4, new HTuple(0.0), new HTuple(-info.Rectangle2Length1), out startRow, out startCol); HOperatorSet.AffineTransPoint2d(matd4, new HTuple(0.0), new HTuple(info.Rectangle2Length1), out endRow, out endCol); //创建找线工具 Image.GetImageSize(out imageWidt, out imageHeight); int[] value = new int[] { Convert.ToInt32(startRow.D), Convert.ToInt32(startCol.D), Convert.ToInt32(endRow.D), Convert.ToInt32(endCol.D) }; HTuple values = new HTuple(value); metrologyModel.CreateMetrologyModel(); metrologyModel.SetMetrologyModelImageSize(imageWidt, imageHeight); int index = metrologyModel.AddMetrologyObjectGeneric("line", values, 20, 5, 1, 30, "min_score", 0.6); #region 设置参数 metrologyModel.SetMetrologyObjectParam(new HTuple(index), new HTuple("measure_length1"), new HTuple(Convert.ToInt32(info.measure_length1))); metrologyModel.SetMetrologyObjectParam(new HTuple(index), new HTuple("measure_length2"), new HTuple(Convert.ToInt32(info.measure_length2))); metrologyModel.SetMetrologyObjectParam(new HTuple(index), new HTuple("measure_distance"), new HTuple(info.measure_distance)); metrologyModel.SetMetrologyObjectParam(new HTuple(index), new HTuple("num_measures"), new HTuple(info.num_measures)); metrologyModel.SetMetrologyObjectParam(new HTuple(index), new HTuple("num_instances"), new HTuple(info.num_instances)); metrologyModel.SetMetrologyObjectParam(new HTuple(index), new HTuple("measure_sigma"), new HTuple(info.measure_sigma)); metrologyModel.SetMetrologyObjectParam(new HTuple(index), new HTuple("measure_threshold"), new HTuple(info.measure_threshold)); metrologyModel.SetMetrologyObjectParam(new HTuple(index), new HTuple("measure_transition"), new HTuple(info.measure_transition.ToString())); #endregion HTuple Row; HTuple Col; HXLDCont modelXLD = metrologyModel.GetMetrologyObjectMeasures(index, "all", out Row, out Col); if (info.IsShowLineMeasureDetail) { Window.SetColor("red"); modelXLD.DispXld(Window.Window); } return(ApplyMetrology(metrologyModel, index, info.ToolName)); } catch (Exception e) { return(new ToolResult() { ResultName = info.ToolName, Errormessage = e.ToString(), GetResultTime = DateTime.Now, IsSuccess = false }); } }
public HXLDCont GetMatchModelCont(int index) { HXLDCont hXLD = GetModelCont(); if (hXLD == null) { return(null); } HXLDCont cont = null; if (index < OutputResult.Count) { HHomMat2D mat2d = new HHomMat2D(); mat2d = mat2d.HomMat2dScale(OutputResult.Scale[index], OutputResult.Scale[index], 0d, 0d); mat2d = mat2d.HomMat2dRotate(OutputResult.Angle[index], 0d, 0d); mat2d = mat2d.HomMat2dTranslate(OutputResult.Row[index].D, OutputResult.Col[index].D); cont = hXLD.AffineTransContourXld(mat2d); } return(cont); }
/// <summary> /// Gets the model supplied by a loaded shapebased model file (.shm) /// </summary> public HXLD getLoadedModelContour() { HTuple row1, col1, row2, col2, row, col; HHomMat2D homMat2D = new HHomMat2D(); try { tResult.mContour.SmallestRectangle1Xld(out row1, out col1, out row2, out col2); row2 = row1.TupleMin(); col2 = col1.TupleMin(); row = row2.TupleFloor() - 5; col = col2.TupleFloor() - 5; homMat2D.HomMat2dIdentity(); homMat2D = homMat2D.HomMat2dTranslate(-row, -col); return(homMat2D.AffineTransContourXld(tResult.mContour)); } catch (HOperatorException e) { exceptionText = e.Message; NotifyIconObserver(MatchingAssistant.ERR_READ_SHAPEMODEL); return(null); } }
public bool FindDataCode(HImage img, HRegion region, bool train = false) { if (!region.IsInitialized()) { region = img.GetDomain(); } if (!train) { Display(0, region); } HImage reduceImg = img.ReduceDomain(region); HImage searchImg = reduceImg.CropDomain(); reduceImg.Dispose(); int width, height; searchImg.GetImageSize(out width, out height); HImage zoomImg = searchImg.ZoomImageFactor(DateCodeZoomImg, DateCodeZoomImg, "constant"); DataCodeContour.Dispose(); searchImg.Dispose(); HTuple ResultHandles, DecodedDataStrings; if (train) { DataCodeContour = CodeReaderHand.FindDataCode2d(zoomImg, "train", "all", out ResultHandles, out DecodedDataStrings); } else { DataCodeContour = CodeReaderHand.FindDataCode2d(zoomImg, "stop_after_result_num", 1, out ResultHandles, out DecodedDataStrings); } if (DecodedDataStrings.Length > 0) { DataCodeString = DecodedDataStrings[0].S; HHomMat2D mat = new HHomMat2D(); HHomMat2D scalMat = mat.HomMat2dScale(1 / DateCodeZoomImg, 1 / DateCodeZoomImg, 0.0, 0.0); double row, col; region.AreaCenter(out row, out col); HHomMat2D tranMat = scalMat.HomMat2dTranslate(row - height / 2.0, col - width / 2.0); HXLDCont tranDataCodeContour = tranMat.AffineTransContourXld(DataCodeContour); DataCodeContour.Dispose(); DataCodeContour = tranDataCodeContour; if (!train) { Display(2); Display(3); } } else { if (UseBrighten) { HImage brightenImg = zoomImg.ScaleImage(UseBrightenValue, 0.0); DataCodeContour = CodeReaderHand.FindDataCode2d(brightenImg, "stop_after_result_num", 1, out ResultHandles, out DecodedDataStrings); if (DecodedDataStrings.Length > 0) { DataCodeString = DecodedDataStrings[0].S; HHomMat2D mat = new HHomMat2D(); HHomMat2D scalMat = mat.HomMat2dScale(1 / DateCodeZoomImg, 1 / DateCodeZoomImg, 0.0, 0.0); double row, col; region.AreaCenter(out row, out col); HHomMat2D tranMat = scalMat.HomMat2dTranslate(row - height / 2.0, col - width / 2.0); HXLDCont tranDataCodeContour = tranMat.AffineTransContourXld(DataCodeContour); DataCodeContour.Dispose(); DataCodeContour = tranDataCodeContour; if (!train) { Display(2); Display(3); } return(true); } } DataCodeString = string.Empty; if (!train) { Display(4); } return(false); } return(true); }
/// <summary> /// 在当前图像上按单元配置查找直线 /// </summary> /// <param name="img"></param> /// <param name="hFixTool"></param> /// <returns></returns> public bool FindLine(HObject img) { m_rowBegin = new HTuple(); m_colBegin = new HTuple(); m_rowEnd = new HTuple(); m_colEnd = new HTuple(); m_hLine.Dispose(); m_hCross.Dispose(); m_hCrossDrop.Dispose(); if (img == null) { return(false); } string strSelect = string.Empty; if (Hselect == "strongest") { strSelect = "all"; } else { strSelect = Hselect; } HTuple imgWidth, imgHeight; HOperatorSet.GetImageSize(img, out imgWidth, out imgHeight); m_imageWidth = imgWidth; m_imageHeight = imgHeight; double x1, y1, x2, y2; HTuple in_phi_t = in_phi + data_from_phi + data_phi * 3.1415926 / 180; double angle = in_phi_t[0].D + 3.1415926 / 2; x1 = (in_col + data_col)[0].D + L2 * Math.Cos(angle); x2 = (in_col + data_col)[0].D - L2 * Math.Cos(angle); y1 = (in_row + data_row)[0].D - L2 * Math.Sin(angle); y2 = (in_row + data_row)[0].D + L2 * Math.Sin(angle); HHomMat2D hom = new HHomMat2D(); hom.HomMat2dIdentity(); hom = hom.HomMat2dTranslate((y2 - y1) / (Hnum - 1), (x2 - x1) / (Hnum - 1)); double rect_row = y1; double rect_col = x1; double rect_phi = in_phi_t[0].D; double rect_len1 = L1; double rect_len2 = (double)Hwid / 2.0; HTuple hms = null; HOperatorSet.GenMeasureRectangle2(rect_row, rect_col, rect_phi, rect_len1, rect_len2, imgWidth, imgHeight, "nearest_neighbor", out hms); HTuple Row = new HTuple(); HTuple Col = new HTuple(); HTuple Distance = new HTuple(); HTuple outRow = new HTuple(); HTuple outCol = new HTuple(); HTuple outAmp = new HTuple(); HTuple outDistance = new HTuple(); HRegion regionCount = new HRegion(); regionCount.GenEmptyRegion(); HXLDCont contour = new HXLDCont(); for (int i = 0; i < Hnum[0].I; ++i) { if (m_bShowRectang2) { contour.Dispose(); contour.GenRectangle2ContourXld(rect_row, rect_col, rect_phi, rect_len1, rect_len2); HRegion region; region = contour.GenRegionContourXld("margin"); regionCount = regionCount.Union2(region); } try { HOperatorSet.MeasurePos(img, hms, Hsomth, Hamp, Htransition, strSelect, out outRow, out outCol, out outAmp, out outDistance); } catch { continue; } if (strSelect == "all" && outRow.Length > 1) { HTuple hIndex = outAmp.TupleSortIndex(); int nMax = hIndex[hIndex.Length - 1]; Row.Append(outRow[nMax]); Col.Append(outCol[nMax]); } else { Row.Append(outRow); Col.Append(outCol); } hom.AffineTransPixel(rect_row, rect_col, out rect_row, out rect_col); HOperatorSet.TranslateMeasure(hms, rect_row, rect_col); } HOperatorSet.CloseMeasure(hms); if (m_bShowRectang2) { m_hRectang2 = regionCount.GenContoursSkeletonXld(1, "filter"); } if (Row.Length > 1) { HXLDCont counter = new HXLDCont(); counter.GenContourPolygonXld(Row, Col); HTuple nr, nc, dist; counter.FitLineContourXld("tukey", -1, 0, 5, 2, out m_rowBegin, out m_colBegin, out m_rowEnd, out m_colEnd, out nr, out nc, out dist); HTuple rowDrop = new HTuple(); HTuple colDrop = new HTuple(); if (Row.Length - 3 > m_nNumDropPoints && m_nNumDropPoints > 0) { HTuple distance = new HTuple(); for (int i = 0; i < Row.Length; ++i) { double dis = HMisc.DistancePl(Row[i], Col[i], m_rowBegin, m_colBegin, m_rowEnd, m_colEnd); distance.Append(dis); } HTuple index = distance.TupleSortIndex(); index = index.TupleInverse(); for (int i = 0; i < m_nNumDropPoints; ++i) { int n = index[i]; rowDrop.Append(Row[n]); colDrop.Append(Col[n]); } index = index.TupleFirstN(m_nNumDropPoints - 1); Row = Row.TupleRemove(index); Col = Col.TupleRemove(index); counter.GenContourPolygonXld(Row, Col); counter.FitLineContourXld("tukey", -1, 0, 5, 2, out m_rowBegin, out m_colBegin, out m_rowEnd, out m_colEnd, out nr, out nc, out dist); } if (m_bShowLine) { HTuple rrow = new HTuple(m_rowBegin); HTuple ccol = new HTuple(m_colBegin); rrow.Append(m_rowEnd); ccol.Append(m_colEnd); m_hLine.GenContourPolygonXld(rrow, ccol); } if (m_bShowCross) { m_hCross.GenEmptyObj(); m_hCross.GenCrossContourXld(Row, Col, 17, (new HTuple(45)).TupleRad()); } if (m_bShowCrossDrop) { m_hCrossDrop.GenEmptyObj(); m_hCrossDrop.GenCrossContourXld(rowDrop, colDrop, 17, (new HTuple(45)).TupleRad()); } Result_Array[0] = m_rowBegin; Result_Array[1] = m_colBegin; Result_Array[2] = m_rowEnd; Result_Array[3] = m_colEnd; return(true); } return(false); }
/// <summary> /// Gets the model supplied by a loaded shapebased model file (.shm) /// </summary> public HXLD getLoadedModelContour() { HTuple row1, col1, row2, col2, row, col; HHomMat2D homMat2D = new HHomMat2D(); try { tResult.mContour.SmallestRectangle1Xld(out row1, out col1, out row2, out col2); row2 = row1.TupleMin(); col2 = col1.TupleMin(); row = row2.TupleFloor()-5; col = col2.TupleFloor()-5; homMat2D.HomMat2dIdentity(); homMat2D = homMat2D.HomMat2dTranslate(-row,-col); return homMat2D.AffineTransContourXld(tResult.mContour); } catch(HOperatorException e) { exceptionText = e.Message; NotifyIconObserver(MatchingAssistant.ERR_READ_SHAPEMODEL); return null; } }
/// <summary> /// Adds visual effects (shadows) /// to the measure results. /// </summary> /// <param name="edges"> /// Add shadows along the edges provided /// </param> public void UpdateShadowContours(HObject edges) { double shift; HXLDCont shadow1, shadow2; HHomMat2D hom2D = new HHomMat2D(); mShadow.Dispose(); mShadow.GenEmptyObj(); shift = Math.Min(0.5 * lineWidth, 2.0); hom2D.HomMat2dIdentity(); hom2D = hom2D.HomMat2dTranslate(shift, 1); shadow1 = ((HXLDCont)edges).AffineTransContourXld(hom2D); hom2D.HomMat2dIdentity(); hom2D = hom2D.HomMat2dTranslate(1, shift); shadow2 = ((HXLDCont)edges).AffineTransContourXld(hom2D); mShadow = mShadow.ConcatObj(shadow1); mShadow = mShadow.ConcatObj(shadow2); shadow1.Dispose(); shadow2.Dispose(); }