public static bool isRegion(HObject obj) { #region 判断是否是真正的region bool ret = false; if (obj == null) { return(ret); } if (!obj.IsInitialized()) { return(ret); } HTuple area, col, row; try { HOperatorSet.AreaCenter(obj, out area, out row, out col); if (area.TupleLength() > 0 && row.TupleLength() > 0 && col.TupleLength() > 0) { ret = true; } } catch (Exception exc) { ret = false; } return(ret); #endregion }
void create_display_model() { HObject HImageReduced = null, HModelContours = null; HTuple hv_Area, Htemprow, Htempcol, hv_HomMat2D; HOperatorSet.GenEmptyObj(out HImageReduced); HOperatorSet.GenEmptyObj(out HModelContours); try { HImageReduced.Dispose(); HOperatorSet.ReduceDomain(image_show, Hdraw_roi, out HImageReduced); HOperatorSet.AreaCenter(Hdraw_roi, out hv_Area, out Htemprow, out Htempcol); HOperatorSet.CreateScaledShapeModel(HImageReduced, H_NumLevels, HangleStart * 3.1415926 / 180, HangleExtent * 3.1415926 / 180, H_AngleStep, HscaleMin, HscaleMax, H_ScaleStep, H_Optimization, H_Metric, Hcontrast, HminContrast, out HShapeModelID); HModelContours.Dispose(); HOperatorSet.GetShapeModelContours(out HModelContours, HShapeModelID, 1); HOperatorSet.VectorAngleToRigid(0, 0, 0, Htemprow, Htempcol, 0, out hv_HomMat2D); m_hModelXLD.Dispose(); HOperatorSet.AffineTransContourXld(HModelContours, out m_hModelXLD, hv_HomMat2D); //HOperatorSet.SetColor(Hwin, "blue"); //HOperatorSet.DispObj(m_hModelXLD, Hwin); show_pic_roi(); } catch (System.Exception ex) { MessageBox.Show("创建模板失败"); } }
public int FindModel(HObject ho_inImage, HObject ho_inRegion, double MinScore, HTuple hv_inModelID, out HObject ho_outRectifiedImage, out double Score) { int nRet = 1; Score = 0; HOperatorSet.AreaCenter(ho_inRegion, out _, out HTuple hv_RefRow, out HTuple hv_RefColumn); if (MinScore >= 1 || MinScore <= 0) { MinScore = 0.75; } HOperatorSet.FindShapeModel(ho_inImage, hv_inModelID, (new HTuple(-50)).TupleRad() , (new HTuple(100)).TupleRad(), 0.5, 1, 0, "least_squares", (new HTuple(3)).TupleConcat( 1), MinScore, out HTuple hv_Row, out HTuple hv_Column, out HTuple hv_Angle, out HTuple hv_Score); if (hv_Score.TupleLength() > 0) { HOperatorSet.HomMat2dIdentity(out HTuple hv_RectificationHomMat2D); HOperatorSet.HomMat2dTranslate(hv_RectificationHomMat2D, hv_RefRow - (hv_Row.TupleSelect(0)), hv_RefColumn - (hv_Column.TupleSelect(0)), out hv_RectificationHomMat2D); HOperatorSet.HomMat2dRotate(hv_RectificationHomMat2D, -(hv_Angle.TupleSelect(0)), hv_RefRow, hv_RefColumn, out hv_RectificationHomMat2D); HOperatorSet.AffineTransImage(ho_inImage, out ho_outRectifiedImage, hv_RectificationHomMat2D, "constant", "false"); Score = hv_Score.TupleSelect(0).D; nRet = 0; HOperatorSet.HomMat2dIdentity(out HTuple hv_AlignmentHomMat2D); HOperatorSet.HomMat2dTranslate(hv_AlignmentHomMat2D, -hv_RefRow, -hv_RefColumn, out hv_AlignmentHomMat2D); HOperatorSet.HomMat2dRotate(hv_AlignmentHomMat2D, hv_Angle.TupleSelect(0), 0, 0, out hv_AlignmentHomMat2D); HOperatorSet.HomMat2dTranslate(hv_AlignmentHomMat2D, hv_Row.TupleSelect(0), hv_Column.TupleSelect(0), out homMat2D); } else { HOperatorSet.HomMat2dIdentity(out homMat2D); HOperatorSet.CopyImage(ho_inImage, out ho_outRectifiedImage); } return(nRet); }
private bool FindCenterRegions(HObject ho_Image, HTuple hv_ROIR, HTuple hv_ROIC, HTuple hv_XField, HTuple hv_YField, HTuple hv_MinArea, HTuple hv_SFRThreshold, HTuple hv_Closing, ref HObject ho_SelectedRegions) { lock (obj) { //对心 try { HObject ho_Rectangle = null; HObject ho_ImageReduced = null; HObject ho_Region = null; HObject ho_RegionClosing = null; HObject ho_ConnectedRegion = null; HObject ho_GrayImage = null; // Local control variables HTuple hv_Area = new HTuple(); HTuple hv_Row0 = new HTuple(), hv_Column0 = new HTuple(); HTuple hv_Width = new HTuple(); HTuple hv_Height = new HTuple(), hv_del = new HTuple(); HTuple hv_delX = new HTuple(), hv_delY = new HTuple(); HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_RegionClosing); HOperatorSet.GenEmptyObj(out ho_ConnectedRegion); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_GrayImage); ho_GrayImage.Dispose(); HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage); HOperatorSet.GetImageSize(ho_GrayImage, out hv_Width, out hv_Height); hv_delX = hv_Width * hv_XField; hv_delY = hv_Height * hv_YField; //ho_Rectangle1.Dispose(); //HOperatorSet.GenRectangle2(out ho_Rectangle1, (hv_Height / 2) - (hv_delY / 2), (hv_Width / 2) - (hv_delX / 2), 0, hv_Corner1ROIR, hv_Corner1ROIC); //ho_Rectangle2.Dispose(); //HOperatorSet.GenRectangle2(out ho_Rectangle2, (hv_Height / 2) - (hv_delY / 2), (hv_Width / 2) + (hv_delX / 2), 0, hv_Corner1ROIR, hv_Corner1ROIC); //ho_Rectangle3.Dispose(); //HOperatorSet.GenRectangle2(out ho_Rectangle3, (hv_Height / 2) + (hv_delY / 2), (hv_Width / 2) - (hv_delX / 2), 0, hv_Corner1ROIR, hv_Corner1ROIC); ho_Rectangle.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle, (hv_Height / 2) + (hv_delY / 2), (hv_Width / 2) + (hv_delX / 2), 0, hv_ROIR / 2, hv_ROIC / 2); ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_GrayImage, ho_Rectangle, out ho_ImageReduced); ho_Region.Dispose(); HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, hv_SFRThreshold, 255); ho_RegionClosing.Dispose(); HOperatorSet.ClosingCircle(ho_Region, out ho_RegionClosing, hv_Closing); ho_ConnectedRegion.Dispose(); HOperatorSet.Connection(ho_RegionClosing, out ho_ConnectedRegion); HOperatorSet.AreaCenter(ho_ConnectedRegion, out hv_Area, out hv_Row0, out hv_Column0); ho_SelectedRegions.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegion, out ho_SelectedRegions, "area", "and", hv_Area.TupleMax(), 9999999); return(true); } catch { return(false); } } }
// Procedures public void FindCircleCenterUseHough(HObject ho_Image, out HObject ho_HoughCenterRegion, HTuple hv_MinGray, HTuple hv_MaxGray, HTuple hv_ExpectRadius, HTuple hv_Percent, out HTuple hv_CenterRegionRow, out HTuple hv_CenterRegionColumn) { // Local iconic variables HObject ho_Image2; // Local control variables HTuple hv_Area = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_HoughCenterRegion); HOperatorSet.GenEmptyObj(out ho_Image2); ho_Image2.Dispose(); HOperatorSet.Threshold(ho_Image, out ho_Image2, hv_MinGray, hv_MaxGray); ho_HoughCenterRegion.Dispose(); HOperatorSet.HoughCircles(ho_Image2, out ho_HoughCenterRegion, hv_ExpectRadius, hv_Percent, 0); HOperatorSet.AreaCenter(ho_HoughCenterRegion, out hv_Area, out hv_CenterRegionRow, out hv_CenterRegionColumn); //gen_circle (Circle, CenterRegionRow, CenterRegionColumn, ExpectRadius) ho_Image2.Dispose(); return; }
private void CalculateModelCenter(HObject Region, MatchingParameters Form2) { HTuple area, row, col; HOperatorSet.AreaCenter(Region, out area, out row, out col); Form2.CentroidRow = row; Form2.CentroidCol = col; Form2.ModelCenterRow = row; Form2.ModelCenterCol = col; DisplayCross(HGraphics, row, col); int x, y; x = Convert.ToInt16((double)row); y = Convert.ToInt16((double)col); Form2.hWindowControl1.HalconWindow.SetPart(x - 30, y - 30, x + 30, y + 30); //allows the window control in Form2 to display the same picture and other graphics Form2.HGraphics.allObj["pic"] = HGraphics.allObj["pic"]; Form2.HGraphics.allObj["green_cross"] = HGraphics.allObj["green_cross"]; Form2.HGraphics.allObj["green_matchingRegion"] = HGraphics.allObj["green_matchingRegion"]; Form2.HGraphics.allObj["colored_region"] = HGraphics.allObj["colored_region"]; Form2.HGraphics.display(); }
/// <summary> /// 创建圆形模板 /// </summary> /// <param name="window">绘图窗口</param> /// <param name="image">输入图像</param> public void DrawCircle(HWindow window, HObject image) { HObject ho_Circle, ho_targetROI, ho_coutours; HTuple hv_Row2 = null, hv_Column3 = null, hv_Radius3 = null, hv_ModelID = null;; HOperatorSet.GenEmptyObj(out ho_Circle); HOperatorSet.GenEmptyObj(out ho_targetROI); HOperatorSet.GenEmptyObj(out ho_coutours); HOperatorSet.SetColor(window, "red"); HOperatorSet.SetLineWidth(window, 2); HOperatorSet.SetDraw(window, "margin"); HOperatorSet.DrawCircle(window, out hv_Row2, out hv_Column3, out hv_Radius3); ho_Circle.Dispose(); HOperatorSet.GenCircle(out ho_Circle, hv_Row2, hv_Column3, hv_Radius3); ho_targetROI.Dispose(); HOperatorSet.DispObj(ho_Circle, window); HTuple hv_Area, hv_Row4, hv_Column4; HTuple hv_HomMat2DIdentity, hv_HomMat2DTranslate; HObject ho_ContoursAffinTrans, ho_Cross; HOperatorSet.GenEmptyObj(out ho_ContoursAffinTrans); HOperatorSet.GenEmptyObj(out ho_Cross); DialogResult Result = MessageBox.Show("是否保存模板!", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if (Result == DialogResult.OK) { HOperatorSet.ReduceDomain(image, ho_Circle, out ho_targetROI); HOperatorSet.AreaCenter(ho_targetROI, out hv_Area, out hv_Row4, out hv_Column4); HOperatorSet.SetSystem("border_shape_models", "false"); //HOperatorSet.CreateShapeModel(ho_targetROI, 4, -0.39, 0.79, "auto", "auto", "ignore_color_polarity", // "auto", "auto", out hv_ModelID); HOperatorSet.CreateShapeModel(ho_targetROI, 4, -0.39, 0.79, "auto", "point_reduction_high", "ignore_global_polarity", "auto", "auto", out hv_ModelID); //if (!Directory.Exists(_filePath)) //{ Directory.CreateDirectory(_filePath); } HOperatorSet.WriteShapeModel(hv_ModelID, /*_filePath + "//" + */ _fileName); ho_coutours.Dispose(); HOperatorSet.GetShapeModelContours(out ho_coutours, hv_ModelID, 1); HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity); HOperatorSet.HomMat2dTranslate(hv_HomMat2DIdentity, hv_Row4, hv_Column4, out hv_HomMat2DTranslate); ho_ContoursAffinTrans.Dispose(); HOperatorSet.AffineTransContourXld(ho_coutours, out ho_ContoursAffinTrans, hv_HomMat2DTranslate); HOperatorSet.DispObj(image, window); HOperatorSet.DispObj(ho_ContoursAffinTrans, window); HOperatorSet.SetColor(window, "green"); HOperatorSet.GenCrossContourXld(out ho_Cross, hv_Row4, hv_Column4, 180, 0.785398); HOperatorSet.DispObj(ho_Cross, window); MessageBox.Show("已保存模板", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { HOperatorSet.DispObj(image, window); return; } }
public override int Measure(HObject ho_Image) //测量 { base.Measure(ho_Image); //调用基类的测量方法 measureResult = Result.OK; color = "green"; ROI = parameter.rectangle2.GetShapePositioned() as Rectangle2; //获取ROI矩形 HObject ho_ROI = Func_HalconFunction.GenRectangle2(ROI); //创建ROI矩形形状 ho_Shape = Func_ImageProcessing.Threshold_SelectMaxRegion(ho_Image, ho_ROI, parameter.hv_MinGray, parameter.hv_MaxGray); //创建结果形状 HOperatorSet.AreaCenter(ho_Shape, out hv_Area, out centerPoint.hv_Row, out centerPoint.hv_Column); //求取区域面积 ho_ROI.Dispose(); if (minValue > hv_Area || hv_Area > maxValue) //?面积在设定范围内 { measureResult = Result.NG; if (Exist)//?无料 { measureResult = Result.无料; function = "产品有无"; } color = "red"; } MeasureDone = true;//已测量标志为true return(Convert.ToInt32(measureResult)); }
public void read_light_state(HObject ho_Image, HTuple hv_blue_num, HTuple hv_red_num, HTuple hv_yellow_num, out HTuple hv_outport_result) { // Local iconic variables HOperatorSet.WriteImage(ho_Image, "jpeg", 0, "light"); HObject ho_Rectangle, ho_lightImageReduced; HObject ho_ImageReducedChannel3, ho_lightRegions; // Local control variables HTuple hv_lightArea, hv_lightRow, hv_lightColumn; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_lightImageReduced); HOperatorSet.GenEmptyObj(out ho_ImageReducedChannel3); HOperatorSet.GenEmptyObj(out ho_lightRegions); hv_outport_result = new HTuple(); ho_Rectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle, 215, 395, 300, 435); ho_lightImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_Rectangle, out ho_lightImageReduced); //rgb1_to_gray (lightImageReduced, grayImage) ho_ImageReducedChannel3.Dispose(); HOperatorSet.AccessChannel(ho_lightImageReduced, out ho_ImageReducedChannel3, 3); ho_lightRegions.Dispose(); HOperatorSet.Threshold(ho_ImageReducedChannel3, out ho_lightRegions, 157, 255); HOperatorSet.AreaCenter(ho_lightRegions, out hv_lightArea, out hv_lightRow, out hv_lightColumn); hv_outport_result[1] = hv_lightArea.TupleInt(); if ((int)(new HTuple(hv_lightArea.TupleGreater(hv_blue_num))) != 0) { //blue light 1000 hv_outport_result[0] = 3; } else if ((int)(new HTuple(hv_lightArea.TupleGreater(hv_red_num))) != 0) { //red light 400 hv_outport_result[0] = 2; } else if ((int)(new HTuple(hv_lightArea.TupleGreater(hv_yellow_num))) != 0) { //yellow light 40 hv_outport_result[0] = 1; } else { //no light hv_outport_result[0] = 0; } ho_Rectangle.Dispose(); ho_lightImageReduced.Dispose(); ho_ImageReducedChannel3.Dispose(); ho_lightRegions.Dispose(); return; }
//public HObject InputImage //{ // get { return m_image; } // set { m_image = value; } //} private void btn_Decode_Click(object sender, EventArgs e) { List <HObject> region = new List <HObject>(); List <HObject> imageReduced = new List <HObject>(); region = hDisplay1.GetSearchRegions(); Time_Meter time_Meter = new Time_Meter(); foreach (var member in region) { HObject _iamge = new HObject(); HOperatorSet.ReduceDomain(m_image, member, out _iamge); HOperatorSet.CropDomain(_iamge, out _iamge); imageReduced.Add(_iamge); } TDCode tdcode; CodeResult sss = new CodeResult(); CodeResult[] results = new CodeResult[] { }; if (_form is FormDMCodeParam) { FormDMCodeParam form = (FormDMCodeParam)_form; tdcode = new DMCode(); tdcode.SetCodeParam(form.CodeParameter); results = tdcode.Decode(imageReduced); txt_TimeUse.Text = (time_Meter.TimePass() * 1000).ToString(); } else if (_form is FormQRCodeParam) { FormQRCodeParam form = (FormQRCodeParam)_form; tdcode = new QRCode(); tdcode.SetCodeParam(form.CodeParameter); results = tdcode.Decode(imageReduced); } List <StringX> ss = new List <StringX>(); List <HalWindow.RegionX> regions = new List <HalWindow.RegionX>(); foreach (var member in results) { if (member.code != string.Empty) { HTuple area = new HTuple(); HTuple row = new HTuple(); HTuple column = new HTuple(); HOperatorSet.AreaCenter(member.codexld, out area, out row, out column); StringX sx = new StringX(15, false, false); sx.SetString(member.code, (int)row.D, (int)column.D, System.Drawing.Color.Green); ss.Add(sx); HalWindow.RegionX regionX = new HalWindow.RegionX(member.codexld, "green"); regions.Add(regionX); } } DelegateUIControl.GetInstance().UpdateHDisplay("FormSetHDisplay", m_image, regions, ss); }
public override bool analyze_show(HWindow hwin, string Key, ref Dictionary <string, object> _dictionary_resulte) { bool ok = false; /***********************处理***************************************/ HObject ho_ImageReduced_; HOperatorSet.GenEmptyObj(out ho_ImageReduced_); HTuple hv_ModMat2D_; HObject ho_Rectangle_; HOperatorSet.GenEmptyObj(out ho_Rectangle_); ho_Rectangle_.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle_, IOutSide.Mid_row_y, IOutSide.Mid_col_x, -IOutSide.Phi, IOutSide.Len1, IOutSide.Len2); if (IrectShuJuPianYi != null)//判断有无定位 { HOperatorSet.VectorAngleToRigid(GenSuiDian_Y_Row, GeuSuiDian_X_Col, GenSuiDian_A, IrectShuJuPianYi.Row, IrectShuJuPianYi.Column, IrectShuJuPianYi.Angle, out hv_ModMat2D_); HOperatorSet.AffineTransRegion(ho_Rectangle_, out ho_Rectangle_, hv_ModMat2D_, "nearest_neighbor"); } HOperatorSet.ReduceDomain(this.ImageFather.Ho_image, ho_Rectangle_, out ho_ImageReduced_); this.GenSuiDianYuDingWeiDianDeBianHuanRegion.Dispose(); this.GenSuiDianYuDingWeiDianDeBianHuanRegion = ho_Rectangle_; HOperatorSet.Threshold(ho_ImageReduced_, out ho_ImageReduced_, this.MinGray, this.MaxGray); HOperatorSet.Connection(ho_ImageReduced_, out ho_ImageReduced_); HOperatorSet.SelectShape(ho_ImageReduced_, out ho_ImageReduced_, this.Features, this.Operation, this.Min, this.Max); this.SelectRegion.Dispose(); this.SelectRegion = ho_ImageReduced_; HOperatorSet.AreaCenter(ho_ImageReduced_, out this._result._area, out this._result._row_y, out this._result._col_x); /**********************************数据分析***************************/ Key = "blob_" + Key; this._result._tolatName = Key; if (this._result._area.Length > 0) { this._result._tolatResult = true; } else { this._result._tolatResult = false; } _dictionary_resulte.Add(Key, this._result); /**********显示******************/ this.show(hwin); ok = this._result._tolatResult; return(ok); }
private void getRectCenter() { // Local iconic variables HObject ho_image, ho_Region, ho_RegionTrans; HObject ho_RegionBorder; // Local control variables HTuple hv_Width = null, hv_Height = null, hv_WindowHandle = hWindowControl1.HalconWindow; HTuple hv_Area = null, hv_Row = null, hv_Column = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_image); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_RegionTrans); HOperatorSet.GenEmptyObj(out ho_RegionBorder); //if (HDevWindowStack.IsOpen()) //{ // HOperatorSet.CloseWindow(HDevWindowStack.Pop()); //} //ho_image.Dispose(); image_path = "G:/blazarlin/my_halcon_function/rect.png"; HOperatorSet.ReadImage(out ho_image, image_path); HOperatorSet.GetImageSize(ho_image, out hv_Width, out hv_Height); HOperatorSet.SetWindowAttr("background_color", "black"); //HOperatorSet.OpenWindow(0, 0, hv_Width / 1.2, hv_Height / 1.22, 0, "visible", "", out hv_WindowHandle); HDevWindowStack.Push(hv_WindowHandle); //阈值 //ho_Region.Dispose(); HOperatorSet.Threshold(ho_image, out ho_Region, 128, 255); // 外接矩形 // ho_RegionTrans.Dispose(); HOperatorSet.ShapeTrans(ho_Region, out ho_RegionTrans, "rectangle1"); // 获取中心 HOperatorSet.AreaCenter(ho_RegionTrans, out hv_Area, out hv_Row, out hv_Column); // 外边界 // ho_RegionBorder.Dispose(); HOperatorSet.Boundary(ho_RegionTrans, out ho_RegionBorder, "outer"); HOperatorSet.DispObj(ho_image, HDevWindowStack.GetActive()); // 显示边界 // 设置绘制器的颜色 HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red"); HOperatorSet.DispCross(hv_WindowHandle, hv_Row, hv_Column, 6, 0); HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green"); HOperatorSet.DispObj(ho_RegionBorder, HDevWindowStack.GetActive()); HOperatorSet.SetTposition(hv_WindowHandle, hv_Row, hv_Column); HOperatorSet.WriteString(hv_WindowHandle, (hv_Row + new HTuple(",")) + hv_Column); //HOperatorSet.DispObj(ho_RegionBorder, hWindowControl1.HalconWindow); ho_image.Dispose(); ho_Region.Dispose(); ho_RegionTrans.Dispose(); ho_RegionBorder.Dispose(); }
/// <summary> /// 拟合参数的示教函数 /// </summary> public void TeachProcess() { ShowRoiFlag = true; HObject RoiContour = new HObject(); HObject Roi = new HObject(); HObject RectContour = new HObject(); HObject ReduceImg = new HObject(); HObject ThresholdRegion = new HObject(); HObject ConnectRegion = new HObject(); HObject SelectRegion = new HObject(); HTuple Area = new HTuple(), Row = new HTuple(), Col = new HTuple(); HObject RegionContour = new HObject(); HTuple RectRow1 = new HTuple(), RectRow2 = new HTuple(), RectCol1 = new HTuple(), RectCol2 = new HTuple(); HObject MinRectContour = new HObject(); while (ShowRoiFlag) { try { #region view1.AddImage(GrabedImg); Thread.Sleep(50); HOperatorSet.GenRectangle1(out Roi, RectF.Y, RectF.X, RectF.Y + RectF.Height, RectF.X + RectF.Width);//生成检测区域 HOperatorSet.GenContourRegionXld(Roi, out RectContour, "border"); view1.SetDraw("green"); view1.AddViewObject(RectContour); view1.Repaint(); System.Threading.Thread.Sleep(100); HOperatorSet.ReduceDomain(GrabedImg, Roi, out ReduceImg); //裁剪图像 Roi.Dispose(); HOperatorSet.Threshold(ReduceImg, out ThresholdRegion, MinGray, MaxGray); //阈值分割 ReduceImg.Dispose(); HOperatorSet.Connection(ThresholdRegion, out ConnectRegion); //区域连接 ThresholdRegion.Dispose(); HOperatorSet.SelectShape(ConnectRegion, out SelectRegion, "area", "and", AreaMin, AreaMax); ConnectRegion.Dispose(); HOperatorSet.AreaCenter(SelectRegion, out Area, out Row, out Col); HOperatorSet.GenContourRegionXld(SelectRegion, out RegionContour, "border"); view1.SetDraw("red"); view1.AddViewObject(RegionContour); // HOperatorSet.SmallestRectangle2(SelectRegion,out Row ,out Col,out P) HOperatorSet.SmallestRectangle1(SelectRegion, out RectRow1, out RectCol1, out RectRow2, out RectCol2); HObject RectRegion = new HObject(); HOperatorSet.GenRectangle1(out RectRegion, RectRow1, RectCol1, RectRow2, RectCol2); HOperatorSet.GenContourRegionXld(RectRegion, out MinRectContour, "border"); view1.AddViewObject(MinRectContour); view1.Repaint(); System.Threading.Thread.Sleep(500); #endregion } catch { } } Thread.Sleep(500); }
public bool FindRegion(HObject srcImg, RectangleF serchArea, out int area, out int contLength, out double centerX, out double centerY) { contLength = 0; area = 0; centerX = 0; centerY = 0; System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); try { HTuple row1, row2, column1, column2, regionArea; HTuple mean, deviation, number, length; HObject imgReduced, targetRegion; row1 = serchArea.Y; row2 = serchArea.Y + serchArea.Height; column1 = serchArea.X; column2 = serchArea.X + serchArea.Width; HObject region, grayRegion, selectedRegion, connectedRegion, regionBorder; HOperatorSet.GenRectangle1(out region, row1, column1, row2, column2); HOperatorSet.ReduceDomain(srcImg, region, out imgReduced); HOperatorSet.Intensity(region, imgReduced, out mean, out deviation); mean = mean + 20; mean = mean < 200 ? mean : new HTuple(200); HOperatorSet.Threshold(imgReduced, out grayRegion, 0, mean); HOperatorSet.Connection(grayRegion, out connectedRegion); HOperatorSet.SelectShape(connectedRegion, out selectedRegion, "area", "and", 250000, 1000000); HOperatorSet.CountObj(selectedRegion, out number); connectedRegion.Dispose(); grayRegion.Dispose(); imgReduced.Dispose(); if (number.I == 1) { HOperatorSet.SelectObj(selectedRegion, out targetRegion, 1); HOperatorSet.AreaCenter(targetRegion, out regionArea, out row1, out column1); HOperatorSet.Boundary(targetRegion, out regionBorder, "inner"); HOperatorSet.Contlength(regionBorder, out length); area = (int)regionArea.D; contLength = (int)length.D; centerX = column1.D; centerY = row1.D; selectedRegion.Dispose(); targetRegion.Dispose(); regionBorder.Dispose(); } else { Logger.PopError("轮廓长度算法异常,轮廓数量=" + number.ToString()); return(false); } } catch (Exception ex) { Logger.PopError("轮廓长度算法异常:" + ex.Message.ToString()); throw; } return(true); }
private async Task <RobotPos> GetHolePoint(HObject image) { try { DelegateControls.Instance.DelegateHdisplay("FormMain_hDisplay1", image, null); HOperatorSet.Threshold(image, out HObject region1, 0, 100); HOperatorSet.Connection(region1, out HObject connectionregion); HOperatorSet.OpeningCircle(connectionregion, out HObject regionopening, 7.5); HOperatorSet.SelectShape(regionopening, out HObject selectedregion, "area", "and", 9000, 79999); HOperatorSet.SelectShape(selectedregion, out selectedregion, "circularity", "and", 0.4, 1); HOperatorSet.FillUp(selectedregion, out HObject regionfillup); HOperatorSet.Union1(regionfillup, out HObject regionunion); HOperatorSet.ReduceDomain(image, regionunion, out HObject imagereduced); HOperatorSet.MinMaxGray(regionunion, imagereduced, 0, out HTuple min, out HTuple max, out HTuple range);//20191128 HOperatorSet.Threshold(imagereduced, out HObject region2, 0, min + 5); HOperatorSet.ErosionCircle(region2, out HObject erosionregion, 2.5); HOperatorSet.Connection(erosionregion, out HObject connectionregion2); HOperatorSet.CountObj(connectionregion2, out HTuple number); HOperatorSet.SelectShape(connectionregion2, out HObject selectregiion2, "area", "and", 2000, 6500); HOperatorSet.CountObj(selectregiion2, out number); HOperatorSet.SelectShape(selectregiion2, out HObject selectregiion3, "circularity", "and", 0.3, 1); HOperatorSet.SelectShape(selectregiion3, out HObject selectregiion4, "outer_radius", "and", 25, 50); HOperatorSet.AreaCenter(selectregiion4, out HTuple area, out HTuple r, out HTuple c); HOperatorSet.ShapeTrans(selectregiion4, out HObject regiiontrans, "outer_circle"); RegionX regionX = new RegionX(regiiontrans, "green"); DelegateControls.Instance.DelegateHdisplay("FormMain_hDisplay1", image, new List <HalWindow.RegionX>() { regionX }); HTuple _x = new HTuple(); HTuple _y = new HTuple(); VisionManager.Instance.Calibrate.GetRobotpoint(r, c, ref _x, ref _y); lengthY = _x + Param.Instance.HMakeUp; lengthX = _y + Param.Instance.VMakeUp; RobotPos robotPos = await HardWareManager.Instance.YaskawaRobot.GetRobotCPoint(); // RobotPos robotPos = new RobotPos(); robotPos.Y += _x; robotPos.Z += _y; return(robotPos); } catch (Exception ex) { HOperatorSet.WriteImage(image, "bmp", 0, AppDomain.CurrentDomain.BaseDirectory + "Images\\Ng.bmp"); throw new Exception("GetHolePoint exception:第二次取像未找到螺丝孔位置"); } }
private static double getAreaPixel(double p) { HObject circle; HOperatorSet.GenEmptyObj(out circle); circle.Dispose(); HTuple area, row, column; HOperatorSet.GenCircle(out circle, 0, 0, p); HOperatorSet.AreaCenter(circle, out area, out row, out column); return(area.D); }
// Procedures public void image_judge_stop(HObject ho_ImageBefore, HObject ho_ImageLater, out HObject ho_Selected, out HTuple hv_juadge_stop) { // Local iconic variables // Local control variables HTuple hv_Area = new HTuple(), hv_Row = new HTuple(); HTuple hv_Column = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Selected); hv_juadge_stop = new HTuple(); //** 初始化图片 //* 测试traffic demo //read_image (ImageBefore, 'traffic1') //read_image (ImageLater, 'traffic2') //* 测试 高拍测试 //read_image (ImageBefore, 'C:/Users/iwake/MVS/Data/Hos0.bmp') //read_image (ImageLater, 'C:/Users/iwake/MVS/Data/Hos1.bmp') //read_image (ImageLater2, 'C:/Users/iwake/MVS/Data/Hos2.bmp') //* 对比图像 //difference //diff_of_gauss (ImageLater, DiffOfGauss, 3, 1.6) //traffic demo 参数 //check_difference (ImageBefore, ImageLater2, Selected, 'diff_outside', -15, 255, 0, 0, 0) //高拍测试 参数 ho_Selected.Dispose(); HOperatorSet.CheckDifference(ho_ImageBefore, ho_ImageLater, out ho_Selected, "diff_outside", -15, 255, 0, 0, 0); hv_Area.Dispose(); hv_Row.Dispose(); hv_Column.Dispose(); HOperatorSet.AreaCenter(ho_Selected, out hv_Area, out hv_Row, out hv_Column); //disp_obj (Selected, WindowHandle) //** 判断 hv_juadge_stop.Dispose(); hv_juadge_stop = 0; if ((int)(new HTuple(hv_Area.TupleLessEqual(10000))) != 0) { hv_juadge_stop.Dispose(); hv_juadge_stop = 1; } hv_Area.Dispose(); hv_Row.Dispose(); hv_Column.Dispose(); return; }
//对NG结果区域进行分析 /// <summary> /// /// </summary> /// <param name="ho_LNgRegions">NG区域</param> /// <param name="Area">面积</param> /// <param name="Row1">外包络行起始点</param> /// <param name="Col1">外包络列起始点</param> /// <param name="Row2">外包络行结束点</param> /// <param name="Col2">外包络列结束点</param> void GetNgRegionPar(HObject ho_NgRegions, out HTuple hv_Area, out HTuple hv_Row, out HTuple hv_Column, out HTuple hv_Row2, out HTuple hv_Column2) { hv_Area = null; HTuple hv_CenterRow = null; HTuple hv_CenterCol = null; hv_Row = null; hv_Column = null; hv_Row2 = null; hv_Column2 = null; HOperatorSet.AreaCenter(ho_NgRegions, out hv_Area, out hv_CenterRow, out hv_CenterCol); //求取面积和中心 HOperatorSet.SmallestRectangle1(ho_NgRegions, out hv_Row, out hv_Column, out hv_Row2, out hv_Column2); //包络区域 }
public void MeasureRoundness(HObject image, out HObject region, /*out HTuple area,*/ out HTuple roundness, out HTuple numHoles, out HTuple diameter) { //area = new HTuple(); roundness = new HTuple(); numHoles = new HTuple(); diameter = new HTuple(); region = new HObject(); try { HTuple absoluteHisto, relativeHisto, minThresh, maxThresh; HTuple distance, sigma, /*roundness,*/ sides, row, column; HOperatorSet.GrayHisto(image, image, out absoluteHisto, out relativeHisto); HOperatorSet.HistoToThresh(relativeHisto, 8, out minThresh, out maxThresh); HTuple threshMax = maxThresh[maxThresh.Length - 2]; for (int i = maxThresh.Length - 3; threshMax > 160 && i >= 0; i--) { threshMax = maxThresh[i]; } threshMax = threshMax.I < 140 ? 140 : threshMax.I; HTuple numConnected, /*numHoles,*/ row1, column1, row2, column2 /*, diameter*/, area, indices; HOperatorSet.Threshold(image, out region, minThresh[0], threshMax); HObject connectedRegions; HOperatorSet.Connection(region, out connectedRegions); HOperatorSet.AreaCenter(connectedRegions, out area, out row, out column); HOperatorSet.TupleSortIndex(area, out indices); HOperatorSet.TupleInverse(indices, out indices); HOperatorSet.SelectObj(connectedRegions, out region, indices[0] + 1); HOperatorSet.ConnectAndHoles(region, out numConnected, out numHoles); HOperatorSet.DiameterRegion(region, out row1, out column1, out row2, out column2, out diameter); HOperatorSet.Roundness(region, out distance, out sigma, out roundness, out sides); //HOperatorSet.AreaCenter(region, out area, out row, out column); } catch (Exception e) { Log.Show($"{e}"); } }
/// <summary> /// 让处理框跟着定位框走 /// </summary> public static void AffineModel(HObject RecRegion, out HObject TransContours) { HOperatorSet.GenEmptyObj(out TransContours); for (int i = 0; i < Row.TupleLength(); i++) { HObject hobject = new HObject(); HOperatorSet.GenEmptyObj(out hobject); hobject.Dispose(); HOperatorSet.AreaCenter(PublicData.CheckModel.VisualModelRegion, out HTuple Area, out HTuple row, out HTuple column); HOperatorSet.HomMat2dIdentity(out HTuple Mat2DIdentity); HOperatorSet.HomMat2dTranslate(Mat2DIdentity, -row, -column, out Mat2DIdentity); HOperatorSet.HomMat2dRotate(Mat2DIdentity, Angle[i], 0, 0, out HTuple Mat2DRotate); HOperatorSet.HomMat2dTranslate(Mat2DRotate, Row[i], Column[i], out HTuple Mat2DTranslate); HOperatorSet.AffineTransRegion(RecRegion, out TransContours, Mat2DTranslate, "nearest_neighbor"); } }
private void CreateVisualModel(HObject InImage, HObject ModelRegion, out HObject hModelXld, out HTuple ModelID) { HOperatorSet.ReduceDomain(InImage, ModelRegion, out HObject ho_TemplateImage); HOperatorSet.GenEmptyObj(out hModelXld); ModelID = new HTuple(); try { HTuple optimization = new HTuple("no_pregeneration"); HOperatorSet.TupleConcat(optimization, DetermineModelParamArr["optimization"].ToString(), out optimization); HTuple contrast = new HTuple(Convert.ToInt32(DetermineModelParamArr["contrast_low"])); HOperatorSet.TupleConcat(contrast, Convert.ToInt32(DetermineModelParamArr["contrast_high"]), out contrast); HOperatorSet.TupleConcat(contrast, Convert.ToInt32(DetermineModelParamArr["min_size"]), out contrast); HOperatorSet.CreateScaledShapeModel(ho_TemplateImage , new HTuple(Convert.ToInt32(DetermineModelParamArr["num_levels"])) , (new HTuple(Convert.ToDouble(DetermineModelParamArr["angle_start"]))).TupleRad() , (new HTuple(Convert.ToDouble(DetermineModelParamArr["angle_extent"]))).TupleRad() , (new HTuple(Convert.ToDouble(DetermineModelParamArr["angle_step"]))).TupleRad() , new HTuple(Convert.ToDouble(DetermineModelParamArr["scale_min"])) , new HTuple(Convert.ToDouble(DetermineModelParamArr["scale_max"])) , new HTuple(Convert.ToDouble(DetermineModelParamArr["scale_step"])) , optimization , new HTuple(DetermineModelParamArr["metric"].ToString()) , contrast , new HTuple(Convert.ToInt32(DetermineModelParamArr["min_contrast"])) , out ModelID ); FindModelParamArr["NumLevels"] = DetermineModelParamArr["num_levels"]; FindModelParamArr["AngleStart"] = DetermineModelParamArr["angle_start"]; FindModelParamArr["AngleExtent"] = DetermineModelParamArr["angle_extent"]; FindModelParamArr["ScaleMin"] = DetermineModelParamArr["scale_min"]; FindModelParamArr["ScaleMax"] = DetermineModelParamArr["scale_max"]; HOperatorSet.GenEmptyObj(out HObject ho_ModelContours); ho_ModelContours.Dispose(); HOperatorSet.GetShapeModelContours(out ho_ModelContours, ModelID, 1); // //Matching 01: Get the reference position HOperatorSet.AreaCenter(ModelRegion, out HTuple hv_ModelRegionArea, out HTuple hv_RefRow, out HTuple hv_RefColumn); HOperatorSet.VectorAngleToRigid(0, 0, 0, hv_RefRow, hv_RefColumn, 0, out HTuple hv_HomMat2D); hModelXld.Dispose(); HOperatorSet.AffineTransContourXld(ho_ModelContours, out hModelXld, hv_HomMat2D); } catch (HalconException e) { } }
/// <summary> /// Determines whether the calibration image is overexposed /// </summary> public void eval_caltab_overexposure(HObject ho_Image, HObject ho_Caltab, out HTuple hv_OverexposureScore) { // Local iconic variables HObject ho_ImageReduced, ho_Region; // Local control variables HTuple hv_AreaCaltab, hv_Row, hv_Column, hv_AreaOverExp; HTuple hv_Thresh, hv_Ratio; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_Region); //returns a measure of the amount of saturation of the plate hv_OverexposureScore = 0.0; HOperatorSet.AreaCenter(ho_Caltab, out hv_AreaCaltab, out hv_Row, out hv_Column); if ((int)((new HTuple(hv_AreaCaltab.TupleEqual(0))).TupleOr(new HTuple(hv_AreaCaltab.TupleEqual( new HTuple())))) != 0) { ho_ImageReduced.Dispose(); ho_Region.Dispose(); return; } ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_Caltab, out ho_ImageReduced); ho_Region.Dispose(); HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, 255, 255); HOperatorSet.AreaCenter(ho_Region, out hv_AreaOverExp, out hv_Row, out hv_Column); hv_Thresh = 0.15; hv_Ratio = (hv_AreaOverExp.TupleReal()) / hv_AreaCaltab; if ((int)(new HTuple(hv_Ratio.TupleLess(hv_Thresh))) != 0) { hv_OverexposureScore = (((new HTuple(1.0)).TupleConcat(1 - (hv_Ratio / hv_Thresh)))).TupleMin() ; } ho_ImageReduced.Dispose(); ho_Region.Dispose(); return; }
private void ReadModel(int index) { //if (System.IO.File.Exists(string.Format("./Model/Model{0}.bmp", index + 1))) //{ // HOperatorSet.ReadImage(out MyModel[index].h_img,string.Format("./Model/Model{0}.bmp", index + 1)); //} //if (System.IO.File.Exists(string.Format("./Model/Model{0}.reg", index + 1))) //{ // HOperatorSet.ReadRegion(out MyModel[index].h_roi, string.Format("./Model/Model{0}.reg", index + 1)); //} //if (System.IO.File.Exists(string.Format("./Model/Model{0}.tup", index + 1))) //{ // HOperatorSet.ReadShapeModel(string.Format("./Model/Model{0}.tup", index + 1),out MyModel[index].hv_ModelID); //} //if (System.IO.File.Exists(string.Format("./Model/Model{0}.dxf", index + 1))) //{ // HTuple hv_DxfStatus = -1; // HOperatorSet.ReadContourXldDxf(out MyModel[index].ho_ShapeModel, string.Format("./Model/Model{0}.dxf", index + 1), new HTuple(), new HTuple(), out hv_DxfStatus); //} //if (MyModel[index].h_img != null && MyModel[index].h_roi != null && MyModel[index].hv_ModelID != -1 && MyModel[index].ho_ShapeModel!=null) //{ // HOperatorSet.AreaCenter(MyModel[index].h_roi, out MyModel[index].hv_Orgin_Angle, out MyModel[index].hv_Orgin_Row, out MyModel[index].hv_Orgin_Column); // MyModel[index].EffectiveFlag = true; //} //序列化 if (System.IO.File.Exists(string.Format("./Model/Model{0}.dat", index + 1))) { FileStream fs = new FileStream(string.Format("./Model/Model{0}.dat", index + 1), FileMode.Open); BinaryFormatter bf = new BinaryFormatter(); SaveModelSerial tempRead = bf.Deserialize(fs) as SaveModelSerial; fs.Close(); MyModel[index].h_img = (HObject)tempRead.h_img; MyModel[index].h_roi = (HObject)tempRead.h_roi; MyModel[index].ho_ShapeModel = (HObject)tempRead.ho_ShapeModel; if (MyModel[index].h_img != null && MyModel[index].h_roi != null && MyModel[index].ho_ShapeModel != null) { HOperatorSet.AreaCenter(MyModel[index].h_roi, out MyModel[index].hv_Orgin_Angle, out MyModel[index].hv_Orgin_Row, out MyModel[index].hv_Orgin_Column); MyModel[index].EffectiveFlag = true; HObject hv_ImageReduced; HOperatorSet.ReduceDomain(MyModel[index].h_img, MyModel[index].h_roi, out hv_ImageReduced); HOperatorSet.CreateShapeModel(hv_ImageReduced, "auto", -0.39, 0.79, "auto", "auto", "use_polarity", "auto", "auto", out MyModel[index].hv_ModelID); } } }
///<summary> ///图像拼接 ///</summary> public void ImageStitching(HObject ho_ImageResult, HObject ho_Image, HObject ho_Rectangle2, HTuple hv_Phi, HTuple hv_Length1, HTuple hv_Length2, HTuple hv_CenterRow, HTuple hv_CenterCol, out HObject ho_ImageStich) { // Local iconic variables HObject ho_Rectangle2Scaled, ho_Image1, ho_Imagepaint; // Local control variables HTuple hv_Area = null, hv_PartRow = null, hv_PartCol = null; HTuple hv_ScaledRows = null, hv_ScaledColumns = null, hv_GrayvalScaled = null; HTuple hv_rows = null, hv_cols = null, hv_Width = null; HTuple hv_Height = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_ImageStich); HOperatorSet.GenEmptyObj(out ho_Rectangle2Scaled); HOperatorSet.GenEmptyObj(out ho_Image1); HOperatorSet.GenEmptyObj(out ho_Imagepaint); HOperatorSet.AreaCenter(ho_ImageResult, out hv_Area, out hv_PartRow, out hv_PartCol); ho_Rectangle2Scaled.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle2Scaled, hv_PartRow, hv_PartCol, hv_Phi, hv_Length1 + 10, hv_Length2 + 10); HOperatorSet.GetRegionPoints(ho_Rectangle2Scaled, out hv_ScaledRows, out hv_ScaledColumns); HOperatorSet.GetGrayval(ho_ImageResult, hv_ScaledRows, hv_ScaledColumns, out hv_GrayvalScaled); hv_rows = (hv_ScaledRows + hv_CenterRow) - hv_PartRow; hv_cols = (hv_ScaledColumns + hv_CenterCol) - hv_PartCol; HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); ho_Image1.Dispose(); HOperatorSet.GenImageConst(out ho_Image1, "byte", hv_Width, hv_Height); HOperatorSet.SetGrayval(ho_Image1, hv_rows, hv_cols, hv_GrayvalScaled); ho_Imagepaint.Dispose(); HOperatorSet.PaintRegion(ho_Rectangle2, ho_Image, out ho_Imagepaint, 0, "fill"); ho_ImageStich.Dispose(); HOperatorSet.AddImage(ho_Image1, ho_Imagepaint, out ho_ImageStich, 1, 0); ho_Rectangle2Scaled.Dispose(); ho_Image1.Dispose(); ho_Imagepaint.Dispose(); return; }
void CreateModel(HObject ho_inImage, HObject ho_inRegion, out HObject ho_outContour, out HTuple hv_outModelID) { HOperatorSet.GenEmptyObj(out ho_outContour); HOperatorSet.GenEmptyObj(out HObject ho_TemplateImage); HOperatorSet.SetSystem("border_shape_models", "false"); ho_TemplateImage.Dispose(); HOperatorSet.ReduceDomain(ho_inImage, ho_inRegion, out ho_TemplateImage); HOperatorSet.CreateShapeModel(ho_TemplateImage, 5, (new HTuple(-50)).TupleRad() , (new HTuple(100)).TupleRad(), (new HTuple(0.2733)).TupleRad(), "auto", "use_polarity", "auto", "auto", out hv_outModelID); HOperatorSet.GetShapeModelContours(out HObject ho_ModelContours, hv_outModelID, 1); HOperatorSet.AreaCenter(ho_inRegion, out _, out HTuple hv_RefRow, out HTuple hv_RefColumn); HOperatorSet.VectorAngleToRigid(0, 0, 0, hv_RefRow, hv_RefColumn, 0, out HTuple hv_HomMat2D); ho_outContour.Dispose(); HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_outContour, hv_HomMat2D); ho_TemplateImage.Dispose(); }
/*findModel takes a form object MatchingParameters as input, which contains matching model information, and uses this * information to find the matches in the image HGraphics.allObj["pic"]. The position, angle and score of the matches returned * Tip: Use try-catch block to enclose findModel when calling it in case of error*/ public void findModel(HObject Image, out HTuple Row, out HTuple Column, out HTuple Angle, out HTuple Score) { HObject ImageReduced, ReducedDomain; HTuple OriginRow, OriginCol, area; HTuple ModelID; HOperatorSet.FillUp(allObj["colored_region"], out ReducedDomain); HOperatorSet.Union1(ReducedDomain, out ReducedDomain); HOperatorSet.AreaCenter(ReducedDomain, out area, out OriginRow, out OriginCol); HOperatorSet.ReduceDomain(allObj["pic"], ReducedDomain, out ImageReduced); HOperatorSet.CreateShapeModel(ImageReduced, "auto", AngleStart, AngleExtent, "auto", Optimization, "ignore_local_polarity", Contrast, "auto", out ModelID); HOperatorSet.SetShapeModelOrigin(ModelID, ModelCenterRow - OriginRow, ModelCenterCol - OriginCol); HOperatorSet.FindShapeModel(Image, ModelID, AngleStart, AngleExtent, MinScore, NumMatches, Overlap, SubPix, NumLevels, Greediness, out Row, out Column, out Angle, out Score); }
public bool Run_Region(ExecuteBuffer _executeBuffer, out ExecuteBuffer outexecutebuffer, out string result_info, Dictionary <int, PointName> Point_temp_result, out Dictionary <int, PointName> Point_out_result) { outexecutebuffer = _executeBuffer; result_info = ""; Point_out_result = Point_temp_result; HObject regionconOutresult, regionOutresult; if (!_executeBuffer.imageBuffer.ContainsKey(this.cbb_regions.SelectedItem.ToString() + ".region")) { MessageBox.Show("二值化处理: 输入图像已经不存在,请重置设置输入图像"); result_info = " 二值化处理: 输入图像已经不存在,请重置设置输入图像"; return(false); } if (_executeBuffer.imageBuffer[this.cbb_regions.SelectedItem.ToString() + ".region"] == null) { MessageBox.Show("二值化处理: image参数为空或者未赋值"); result_info = " 二值化处理: 输入图像已经不存在,请重置设置输入图像"; return(false); } HTuple area1, row1, col1; HOperatorSet.Connection(_executeBuffer.imageBuffer[this.cbb_regions.SelectedItem.ToString() + ".region"], out regionconOutresult); HOperatorSet.SelectShape(regionconOutresult, out regionOutresult, this.cbb_features.SelectedItem.ToString(), this.cbb_operation.SelectedItem.ToString(), Convert.ToInt32(this.txt_min.Text.ToString()), Convert.ToInt32(this.txt_max.Text.ToString())); HOperatorSet.AreaCenter(regionOutresult, out area1, out row1, out col1); int out_number_list = Convert.ToInt32(this.tb_outnumber.Text.ToString()); if (Point_temp_result.ContainsKey(out_number_list)) { Point_temp_result[out_number_list].点X = col1; Point_temp_result[out_number_list].点Y = row1; } else { Point_temp_result.Add(out_number_list, new PointName(col1, row1)); } outexecutebuffer = _executeBuffer; Point_out_result = Point_temp_result; return(true); }
private void btn_sort_Click(object sender, EventArgs e) { try { sortType = cb_sort.SelectedItem.ToString(); //SortRegion的方式 HOperatorSet.GenEmptyObj(out ho_Characters); HOperatorSet.GenEmptyObj(out ho_Characters_C); ho_Characters.Dispose(); HOperatorSet.SortRegion(ho_RegionIntersection, out ho_Characters, "character", "true", sortType); //放大显示排序 ho_Characters_C.Dispose(); HOperatorSet.SortRegion(ho_RegionIntersection_C, out ho_Characters_C, "character", "true", sortType); HTuple number, Row_i, Column_i, hv_Area_i; HObject ho_ObjectSelected_i = null; HOperatorSet.ClearWindow(m_hWindowHandle3); HOperatorSet.GenEmptyObj(out ho_ObjectSelected_i); HOperatorSet.CountObj(ho_Characters_C, out number); for (int i = 1; i < number + 1; i++) { ho_ObjectSelected_i.Dispose(); HOperatorSet.SelectObj(ho_Characters_C, out ho_ObjectSelected_i, i); HOperatorSet.AreaCenter(ho_ObjectSelected_i, out hv_Area_i, out Row_i, out Column_i); m_Tools.RegionFitWindow(m_imageCroped, m_hWindowHandle3, ho_ObjectSelected_i); //display at HwindowControl3 m_Tools.disp_message(m_hWindowHandle3, i, "window", Row_i, Column_i, "black", "true"); m_Tools.RegionFitWindow(m_imageCroped, m_hWindowHandle3, ho_ObjectSelected_i); } } catch { } }
public static string[] GetBarcode2() { string[] res = new string[2]; DataCodeHandle2 = new HDataCode2D("Data Matrix ECC 200", new HTuple(), new HTuple()); HObject image1, image2; HObject Roi1, Roi2; HObject symbolXLD1, symbolXLD2; HOperatorSet.ReadObject(out Roi1, System.Environment.CurrentDirectory + "\\rectangle3.hobj"); HOperatorSet.ReadObject(out Roi2, System.Environment.CurrentDirectory + "\\rectangle4.hobj"); HOperatorSet.ReduceDomain(Camera2.CurrentImage, Roi1, out image1); HOperatorSet.ReduceDomain(Camera2.CurrentImage, Roi2, out image2); HOperatorSet.FindDataCode2d(new HImage(image1), out symbolXLD1, DataCodeHandle2, new HTuple(), new HTuple(), out ResultHandles2, out DecodedDataStrings2); res[0] = new HTuple((new HTuple(DecodedDataStrings2.TupleLength())).TupleEqual(1)) == 1 ? DecodedDataStrings2.TupleSelect(0).ToString().Replace("\"", "") : "error"; HOperatorSet.FindDataCode2d(new HImage(image2), out symbolXLD2, DataCodeHandle2, new HTuple(), new HTuple(), out ResultHandles2, out DecodedDataStrings2); res[1] = new HTuple((new HTuple(DecodedDataStrings2.TupleLength())).TupleEqual(1)) == 1 ? DecodedDataStrings2.TupleSelect(0).ToString().Replace("\"", "") : "error"; viewController2.addIconicVar(symbolXLD1); viewController2.addIconicVar(symbolXLD2); viewController2.addIconicVar(Roi1); viewController2.addIconicVar(Roi2); viewController2.viewPort.HalconWindow.SetColor("green"); viewController2.repaint(); HTuple area, rows, columns; HOperatorSet.AreaCenter(Roi1, out area, out rows, out columns); int[] co = GetCorinWindow(viewController2.viewPort.HalconWindow, Camera2.CurrentImage, (int)rows.D, (int)columns.D); HOperatorSet.DispText(viewController2.viewPort.HalconWindow, "① " + res[0], "window", co[0], co[1], "black", "box", "true"); HOperatorSet.AreaCenter(Roi2, out area, out rows, out columns); co = GetCorinWindow(viewController2.viewPort.HalconWindow, Camera2.CurrentImage, (int)rows.D, (int)columns.D); HOperatorSet.DispText(viewController2.viewPort.HalconWindow, "② " + res[1], "window", co[0], co[1], "black", "box", "true"); image1.Dispose(); image2.Dispose(); DataCodeHandle2.Dispose(); return(res); }
/// <summary> /// 创建模板 /// </summary> public static void CreateModel(HObject InImage, HObject hRegion, out HTuple hv_ModelID, out HObject ho_TransContours) { HOperatorSet.GenEmptyObj(out ho_TransContours); ho_TransContours.Dispose(); try { HOperatorSet.ReduceDomain(InImage, hRegion, out HObject ho_TemplateImage); HOperatorSet.CreateShapeModel(ho_TemplateImage, 7, new HTuple(0).TupleRad() , (new HTuple(360)).TupleRad(), (new HTuple(0.1919)).TupleRad(), (new HTuple("point_reduction_high")).TupleConcat( "no_pregeneration"), "use_polarity", ((new HTuple(16)).TupleConcat(30)).TupleConcat( 11), 3, out hv_ModelID); HOperatorSet.GetShapeModelContours(out HObject ho_ModelContours, hv_ModelID, 1); HOperatorSet.AreaCenter(hRegion, out HTuple hv_ModelRegionArea, out HTuple hv_RefRow, out HTuple hv_RefColumn); HOperatorSet.VectorAngleToRigid(0, 0, 0, hv_RefRow, hv_RefColumn, 0, out HTuple hv_HomMat2D); HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_TransContours, hv_HomMat2D); } catch (Exception e) { throw e; } }