private void GetProductPixelPosBtn_Click(object sender, EventArgs e) //计算示教产品的像素的坐标,并保存到配置文件 { this.GetProductPixelPosBtn.Enabled = false; this.ProductPixelPosTeachBtn.Enabled = true; try { LocalResult localResult = new LocalResult(); LocalManager myLocal = new LocalManager(); //1.0设置定位模式 myLocal.SetLocalModel(LocalPara0.localSetting.localModel); //2.0设置定位参数 myLocal.SetParam(CurImg, LocalPara0); //3.执行定位 myLocal.doLocal(); localResult = myLocal.GetResult(); this.LocalPara0.localSetting.TeachImgLocal = new St_VectorAngle(localResult.row, localResult.col, localResult.angle); view1.ResetWindow(); view1.AddImage(CurImg.CopyObj(1, -1)); view1.SetDraw("blue", "margin"); view1.AddViewObject(localResult.ShowContour.CopyObj(1, -1)); view1.Repaint(); this.ProductPixelPosTbx.Text = "Col:" + localResult.col.ToString("f2") + " Row:" + localResult.row.ToString("f2") + " Angle:" + localResult.angle.ToString("f2"); } catch { } }
/// <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); }
private void StartTeachBtn1_Click(object sender, EventArgs e) { HObject RoiContour = new HObject(); HTuple ArcType = new HTuple(); if (StartTeachBtn1.Text == "开始示教") { GroupBoxEnable(groupBox3, false); StartTeachBtn1.Enabled = true; StartTeachBtn1.Text = "保存"; view1.Refresh(); view1.AddImage(GrabedImg); ActionIsDrawingRoi(true); view1.roiController.reset(); view1.roiController.setROIShape(new ViewROI.ROICircularArc()); } else if (StartTeachBtn1.Text == "保存") { ActionIsDrawingRoi(false); GroupBoxEnable(groupBox3, true); ViewROI.ROICircularArc arc = new ViewROI.ROICircularArc(); if (view1.roiController.getActiveROI() is ViewROI.ROICircularArc) { StartTeachBtn1.Enabled = false; StartTeachBtn1.Text = "开始示教"; arc = (ViewROI.ROICircularArc)view1.roiController.getActiveROI(); arc.GetCircleArc(out TeachCenterRow, out TeachCenterCol, out TeachCircleR, out TeachStartPhi, out TeachEndPhi); if (ArcType.Length > 0) { TeachArcType = ArcType.S; } Thread TeachThd = new Thread(TeachProcess); TeachThd.IsBackground = true; TeachThd.Start(); } else { MessageBox.Show("请选中形状"); return; } } }
private void DrawLinesProcess() { ShowRoiFlag = true; HObject RoiContour = new HObject(); HObject DetectRegions = new HObject(); HTuple ResultRows = new HTuple(), ResultCols = new HTuple(); #region //直线参数示教 //2.1画出直线的检测区域 bool CurIsOk = true; this.myView.AddViewImage(GrabedImg); while (ShowRoiFlag) { try { //2.2显示直线的检测ROI,调整ROI的参数 Thread.Sleep(10); myView.ResetWindow(); myView.AddImage(GrabedImg); CurIsOk = MyVisionBase.gen_rake_ROI1(GrabedImg, out DetectRegions, out RoiContour, Element, DetectHeight, Threshold, LineRow1, LineCol1, LineRow2, LineCol2); myView.Repaint(); Thread.Sleep(100); myView.SetDraw("blue", "margin"); myView.AddViewObject(RoiContour); myView.Repaint(); Thread.Sleep(300); //2.5显示测量出每个示教小ROI的平均灰度,并保存到ListGrays变量中 HTuple MeanGrays = new HTuple(), GrayDivs = new HTuple(); HOperatorSet.Intensity(DetectRegions, GrabedImg, out MeanGrays, out GrayDivs); MyVisionBase.HTupleToList(MeanGrays, out ListGrays); } catch (Exception e0) { MessageBox.Show(e0.Message + e0.Source); StartTeachBtn1.Enabled = true; break; } } #endregion }
private void btnCreateTemplate_Click(object sender, EventArgs e) { if (btnCreateTemplate.Text == "新建") { btnCreateTemplate.Enabled = true; panelOperator.Enabled = true; groupBox1.Enabled = true; this.Enabled = true; btnCreateTemplate.Text = "保存"; view1.roiController.reset(); view1.roiController.NotifyRCObserver += UpdateViewData; view1.roiController.setROISign(ViewROI.ROIController.MODE_ROI_POS); if (CurrentImage == null) { Logger.PopError("请先采集图片!", true); return; } view1.SetDraw("red", "margin"); //1.0界面去使能 this.BeginInvoke(new Action(() => { ActionIsDrawingRoi(true); })); view1.Refresh(); view1.AddImage(CurrentImage); if (RoiStr.Equals("Rect1")) { view1.SetString(12, 12, "red", "绘制矩形区域作为模板区域"); Thread.Sleep(10); view1.roiController.setROIShape(new ViewROI.ROIRectangle1(200)); } #region //else if (RoiStr.Equals("Rect2")) //{ // HOperatorSet.ClearWindow(ShowWindow); // MyVisionBase.hDispObj(ShowWindow, CurrentImage); // MyVisionBase.disp_message(ShowWindow, "绘制矩形区域作为模板区域", "window", 12, 12, "red", "false"); // HTuple CenterRow, CenterCol, Rect2Phi, RectL1, RectL2; // HOperatorSet.DrawRectangle2(ShowWindow, out CenterRow, out CenterCol, out Rect2Phi, out RectL1, out RectL2); // ModelRoi = new HRegion(); // ModelRoi.GenRectangle2(CenterRow, CenterCol, Rect2Phi, RectL1, RectL2); //} //else if (RoiStr.Equals("Circle")) //{ // MyVisionBase.disp_message(ShowWindow, "绘制圆形区域作为模板区域", "window", 12, 12, "red", "false"); // HTuple CircleRow, CircleCol, CircleR; // HOperatorSet.DrawCircle(ShowWindow, out CircleRow, out CircleCol, out CircleR); // ModelRoi = new HRegion(); // ModelRoi.GenCircle(CircleRow, CircleCol, CircleR); //} //else if (RoiStr.Equals("CircleMod")) //{ // HTuple CircleRowIn, CircleColIn, CircleRIn, CircleRow, CircleCol, CircleR; // MyVisionBase.disp_message(ShowWindow, "绘制外圆区域作为圆环模板区域的外圆", "window", 12, 12, "red", "false"); // HOperatorSet.DrawCircle(ShowWindow, out CircleRowIn, out CircleColIn, out CircleRIn); // HOperatorSet.ClearWindow(ShowWindow); // MyVisionBase.hDispObj(ShowWindow, CurrentImage); // MyVisionBase.disp_message(ShowWindow, "绘制内圆区域作为圆环模板区域的内圆", "window", 12, 12, "red", "false"); // HOperatorSet.DrawCircleMod(ShowWindow, CircleRowIn, CircleColIn, CircleRIn, out CircleRow, out CircleCol, out CircleR); // HRegion InnerCirRegion = new HRegion(); // ModelRoi = new HRegion(); // InnerCirRegion.GenCircle(CircleRowIn, CircleColIn, CircleRIn); // ModelRoi.GenCircle(CircleRow, CircleCol, CircleR); // ModelRoi.Difference(InnerCirRegion); //} //else if (RoiStr.Equals("Rect2+Rect2")) //{ // HTuple CenterRow, CenterCol, Rect2Phi, RectL1, RectL2; // HRegion Rect2ROI = new HRegion(); // MyVisionBase.disp_message(ShowWindow, "绘制矩形1作为矩形联合区域的第一个矩形区域", "window", 12, 12, "red", "false"); // HOperatorSet.DrawRectangle2(ShowWindow, out CenterRow, out CenterCol, out Rect2Phi, out RectL1, out RectL2); // ModelRoi = new HRegion(); // ModelRoi.GenRectangle2(CenterRow, CenterCol, Rect2Phi, RectL1, RectL2); // HOperatorSet.ClearWindow(ShowWindow); // MyVisionBase.hDispObj(ShowWindow, CurrentImage); // MyVisionBase.disp_message(ShowWindow, "绘制矩形2作为矩形联合区域的第二个矩形区域", "window", 12, 12, "red", "false"); // HOperatorSet.DrawRectangle2(ShowWindow, out CenterRow, out CenterCol, out Rect2Phi, out RectL1, out RectL2); // Rect2ROI.GenRectangle2(CenterRow, CenterCol, Rect2Phi, RectL1, RectL2); // ModelRoi = ModelRoi.Union2(Rect2ROI); //} //else //{ // HOperatorSet.DrawRectangle1(ShowWindow, out row1, out column1, out row2, out column2); // ModelRoi = new HRegion(row1, column1, row2, column2); //} #endregion } else if (btnCreateTemplate.Text == "保存") { btnCreateTemplate.Text = "新建"; view1.roiController.NotifyRCObserver -= UpdateViewData; view1.roiController.reset(); this.BeginInvoke(new Action(() => { ActionIsDrawingRoi(false); })); string files = TemplateParam.TemplatePath; if (false) { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); if (saveFileDialog1.ShowDialog() == DialogResult.OK) { files = saveFileDialog1.FileName; TemplateParam.Save(files); } } else { TemplateParam.Save(files); TemplateParam.Load(files); } } }
/// <summary> /// 拟合参数的示教函数 /// </summary> public void TeachProcess() { ShowRoiFlag = true; HObject RoiContour = new HObject(); //1.0获取图像,调节曝光时间 HTuple ResultRows = new HTuple(), ResultCols = new HTuple(); //2.0示教参数 #region //直线参数示教 //2.1画出直线的检测区域 bool CurIsOk = true; view1.AddViewImage(GrabedImg); while (ShowRoiFlag) { //2.2显示直线的检测ROI,调整ROI的参数 Thread.Sleep(50); view1.Refresh(); view1.AddImage(GrabedImg); CurIsOk = MyVisionBase.gen_rake_ROI(GrabedImg, out RoiContour, Element, DetectHeight, 2, LineRow1, LineCol1, LineRow2, LineCol2); if (!CurIsOk) { break; } view1.SetDraw("blue", "margin"); view1.AddViewObject(RoiContour); //2.3利用卡尺工具找出边界点 CurIsOk = MyVisionBase.Rake(GrabedImg, out RoiContour, Element, DetectHeight, 2, 2, Threshold, "all", "first", LineRow1, LineCol1, LineRow2, LineCol2, out ResultRows, out ResultCols); //减去中间不要的点 int Count = ResultRows.Length; int ClipLength = (int )(Count * CenterClip); int StartClipNo = (int)((Count - ClipLength) * 0.5); if ((Count - ClipLength) > 4 && ClipLength > 0) { for (int i = 0; i < ClipLength; i++) { ResultRows = ResultRows.TupleRemove(StartClipNo); ResultCols = ResultCols.TupleRemove(StartClipNo); } } if (!CurIsOk) { break; } //2.4 HObject LinePtCross = new HObject(); //2.5显示拟合的点 HOperatorSet.GenCrossContourXld(out LinePtCross, ResultRows, ResultCols, 12, 0.7); view1.SetDraw("green", "margin"); view1.AddViewObject(LinePtCross); //2.6直线拟合 HObject FitLineCotour = new HObject(); HTuple FitLineRow1 = new HTuple(), FitLineCol1 = new HTuple(), FitLineRow2 = new HTuple(), FitLineCol2 = new HTuple(), PtToLineDist = new HTuple(); CurIsOk = MyVisionBase.PtsToBestLine(out FitLineCotour, ResultRows, ResultCols, 3, out FitLineRow1, out FitLineCol1, out FitLineRow2, out FitLineCol2, out PtToLineDist); if (CurIsOk) { HTuple AngleLx = new HTuple(); HOperatorSet.AngleLl(0, 0, 1, 0, FitLineCol1, FitLineRow1, FitLineCol2, FitLineRow2, out AngleLx); //直线1与X轴之间的夹角 HTuple DistP1L = new HTuple(), DistP2L = new HTuple(); HOperatorSet.DistancePl(LineRow10, LineCol10, FitLineRow1, FitLineCol1, FitLineRow2, FitLineCol2, out DistP1L); HOperatorSet.DistancePl(LineRow20, LineCol20, FitLineRow1, FitLineCol1, FitLineRow2, FitLineCol2, out DistP2L); AngleLx = AngleLx.D / Math.PI * 180.0; if (Math.Abs(AngleLx.D) % 90 < 10 && DistP1L.D < 10 && DistP2L.D < 10) { HOperatorSet.ProjectionPl(LineRow1, LineCol1, FitLineRow1, FitLineCol1, FitLineRow2, FitLineCol2, out LineRow1, out LineCol1); HOperatorSet.ProjectionPl(LineRow2, LineCol2, FitLineRow1, FitLineCol1, FitLineRow2, FitLineCol2, out LineRow2, out LineCol2); } } if (!CurIsOk) { break; } //2.7显示拟合直线 view1.SetDraw("red", "margin"); view1.AddViewObject(FitLineCotour); view1.Repaint(); Thread.Sleep(1500); } #endregion }