Пример #1
0
        public static void DrawCircle(PictureBox whereToDraw, CircleParam circles, bool save, string label = "")
        {
            whereToDraw.Image = (Image)Registry.FixedImage.Clone();

            Size      circleSize   = new Size(2 * circles.Radius, 2 * circles.Radius);
            Image     currentImage = (Image)whereToDraw.Image.Clone();
            Graphics  g            = Graphics.FromImage(currentImage);
            Rectangle boundRect    = new Rectangle(circles.ImageLocation, circleSize);

            g.FillEllipse(circles.FillColor, boundRect);
            g.DrawEllipse(circles.OutlineColor, new Rectangle(circles.ImageLocation, circleSize));
            if (!String.IsNullOrEmpty(label))
            {
                g.DrawString(label, new Font("Arial", 22), new SolidBrush(Color.Black), boundRect, new StringFormat {
                    LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center
                });
            }
            g.Dispose();

            whereToDraw.Image = (Image)currentImage.Clone();
            if (save)
            {
                Registry.FixedImage = (Image)whereToDraw.Image.Clone();
            }
        }
Пример #2
0
        public static void UnDrawCircle(PictureBox whereToDraw, CircleParam circles)
        {
            whereToDraw.Image = (Image)Registry.FixedImage.Clone();
            Size     circleSize   = new Size(0, 0);
            Image    currentImage = (Image)whereToDraw.Image.Clone();
            Graphics g            = Graphics.FromImage(currentImage);

            g.FillEllipse(circles.FillColor, new Rectangle(circles.ImageLocation, circleSize));
            g.DrawEllipse(circles.OutlineColor, new Rectangle(circles.ImageLocation, circleSize));
            g.Dispose();
            whereToDraw.Image = (Image)currentImage.Clone();
        }
Пример #3
0
        public ToolResult GetResult()
        {
            string  str;
            int     width, height;
            HRegion findRegion = null;

            IsSuccess = false;
            HiPerfTimer timer = new HiPerfTimer();

            timer.Start();
            if (Image == null)
            {
                return(new ToolResult()
                {
                    ResultName = info.ToolName, Errormessage = "图像采集失败", IsSuccess = false, GetResultTime = DateTime.Now
                });
            }
            else if (NCCModel == null)
            {
                return(new ToolResult()
                {
                    ResultName = info.ToolName, Errormessage = "模板文件没有找到", IsSuccess = false, GetResultTime = DateTime.Now
                });
            }
            Image.GetImagePointer1(out str, out width, out height);
            HImage image2 = new HImage(str, width, height);

            image2 = Image.CopyImage();
            try
            {
                if (info.FindModelROIParam != null)
                {
                    if (info.FindModelROIParam.GetType() == typeof(CircleParam))
                    {
                        CircleParam circle = info.ModelROIParam as CircleParam;
                        if (circle != null)
                        {
                            findRegion = GenRegionHelper.GenCircleRegion(circle.CircleRow, circle.CircleColumn, circle.Radius);
                        }
                    }
                    else if (info.FindModelROIParam.GetType() == typeof(Rectangle1Param))
                    {
                        Rectangle1Param rectangle1 = info.ModelROIParam as Rectangle1Param;
                        if (rectangle1 != null)
                        {
                            findRegion = GenRegionHelper.GenRectangle1Region(rectangle1.RectangleStartRow, rectangle1.RectangleStartColumn, rectangle1.RectangleEndRow, rectangle1.RectangleEndColumn);
                        }
                    }
                    else if (info.FindModelROIParam.GetType() == typeof(Rectangle2Param))
                    {
                        Rectangle2Param rectangle2 = info.ModelROIParam as Rectangle2Param;
                        if (rectangle2 != null)
                        {
                            findRegion = GenRegionHelper.GenRectangle2Region(rectangle2.Rectangle2CenterRow, rectangle2.Retangle2CenterColumn, rectangle2.Retangle2Angle, rectangle2.Rectangle2Length1, rectangle2.Rectangle2Length2);
                        }
                    }
                    else if (info.FindModelROIParam.GetType() == typeof(EllipseParam))
                    {
                        EllipseParam ellipse = info.ModelROIParam as EllipseParam;
                        if (ellipse != null)
                        {
                            findRegion = GenRegionHelper.GenEllipseRegion(ellipse.EllipseCenterRow, ellipse.EllipseCenterColumn, ellipse.EllipseAngle, ellipse.EllipseRadius1, ellipse.EllipseRadius2);
                        }
                    }
                }
                if (findRegion != null)
                {
                    image2 = image2.ReduceDomain(findRegion);
                }
                HTuple modelRow, modelCol, modelAngle, modelScore;
                if (this.NCCModel != null)
                {
                    this.NCCModel.FindNccModel(image2, TransAngle.AngleToHu(info.AngleStart), TransAngle.AngleToHu(info.AngleExtent), info.MinScore, info.NumberMacths, info.MaxOverlap, info.SubPixel, info.NumLevels, out modelRow, out modelCol, out modelAngle, out modelScore
                                               );
                    if (modelScore.Length > info.NumberMacths)
                    {
                        IsSuccess = true;
                        ToolResult result = new ToolResult();
                        result.ImageModelX     = modelCol.D;
                        result.ImageModelY     = modelRow.D;
                        result.ImageModelAngle = modelAngle.D;
                        result.ResultScore     = modelScore.D;
                        result.IsSuccess       = true;
                        result.GetResultTime   = DateTime.Now;
                        this.info.ResultX      = result.ImageModelX;
                        this.info.ResultY      = result.ImageModelY;
                        this.info.ResultAngle  = result.ImageAngle;
                        result.ElapsedTime     = timer.Duration;
                        return(result);
                    }
                    else
                    {
                        return(new ToolResult()
                        {
                            Errormessage = "模板查找失败", IsSuccess = false, GetResultTime = DateTime.Now
                        });
                    }
                }
                else
                {
                    return(new ToolResult()
                    {
                        Errormessage = "模板尚未创建", IsSuccess = false, GetResultTime = DateTime.Now
                    });
                }
            }
            catch (Exception e)
            {
                WriteErrorLog("VisionTool", e.ToString());
                return(new ToolResult()
                {
                    Errormessage = "模板查找出现错误", IsSuccess = false, GetResultTime = DateTime.Now
                });
            }
        }
Пример #4
0
        public bool CreateMatchTool()
        {
            HImage  modelImage  = null;
            HRegion modelRegion = null;

            if (info.ModelROIParam.GetType() == typeof(CircleParam))
            {
                CircleParam circle = info.ModelROIParam as CircleParam;
                if (circle != null)
                {
                    modelImage = GetModelImageByCircle(Image.CopyImage(), circle.CircleRow, circle.CircleColumn, circle.Radius, out modelRegion);
                }
            }
            else if (info.ModelROIParam.GetType() == typeof(Rectangle1Param))
            {
                Rectangle1Param rectangle1 = info.ModelROIParam as Rectangle1Param;
                if (rectangle1 != null)
                {
                    modelImage = GetModelImageByRectangle1(Image.CopyImage(), rectangle1.RectangleStartRow, rectangle1.RectangleStartColumn, rectangle1.RectangleEndRow, rectangle1.RectangleEndColumn, out modelRegion);
                }
            }
            else if (info.ModelROIParam.GetType() == typeof(Rectangle2Param))
            {
                Rectangle2Param rectangle2 = info.ModelROIParam as Rectangle2Param;
                if (rectangle2 != null)
                {
                    modelImage = GetModelImageByRectangle2(Image.CopyImage(), rectangle2.Rectangle2CenterRow, rectangle2.Retangle2CenterColumn, rectangle2.Retangle2Angle, rectangle2.Rectangle2Length1, rectangle2.Rectangle2Length2, out modelRegion);
                }
            }
            else if (info.ModelROIParam.GetType() == typeof(EllipseParam))
            {
                EllipseParam ellipse = info.ModelROIParam as EllipseParam;
                if (ellipse != null)
                {
                    modelImage = GetModelImageByEllipse(Image.CopyImage(), ellipse.EllipseCenterRow, ellipse.EllipseCenterColumn, ellipse.EllipseAngle, ellipse.EllipseRadius1, ellipse.EllipseRadius2, out modelRegion);
                }
            }
            else
            {
                WriteErrorLog("VisionTool", info.ToolName + "模板创建失败,原因是截取图像ROI参数类型不正确");
                return(false);
            }
            HNCCModel nccModel = new HNCCModel();
            double    modelRow, modelCol;

            if (modelImage != null)
            {
                try
                {
                    nccModel.CreateNccModel(modelImage, new HTuple(info.NumLevels), TransAngle.AngleToHu(info.AngleStart), TransAngle.AngleToHu(info.AngleExtent), new HTuple(TransAngle.AngleToHu(info.AngleStep)), info._Metric.ToString());
                    modelRegion.AreaCenter(out modelRow, out modelCol);
                    info.ModelRegionRow   = modelRow;
                    info.ModelRegionCol   = modelCol;
                    info.ModelRegionAngle = 0.0;
                    this.NCCModel         = nccModel;
                    SaveModelFile(info.ToolName);
                }
                catch
                {
                    WriteErrorLog("VisionTool", info.ToolName + "模板创建失败,原因是调用Halcon对象创建函数出错");
                    return(false);
                }
            }
            else
            {
                WriteErrorLog("VisionTool", info.ToolName + "模板创建失败,原因是模板图像对象为空");
                return(false);
            }
            return(true);
        }
Пример #5
0
        public bool CreateMatchTool()
        {
            HImage  modelImage  = null;
            HRegion modelRegion = null;

            if (info.ModelROIParam.GetType() == typeof(CircleParam))
            {
                CircleParam circle = info.ModelROIParam as CircleParam;
                if (circle != null)
                {
                    modelImage = GetModelImageByCircle(Image.CopyImage(), circle.CircleRow, circle.CircleColumn, circle.Radius, out modelRegion);
                }
            }
            else if (info.ModelROIParam.GetType() == typeof(Rectangle1Param))
            {
                Rectangle1Param rectangle1 = info.ModelROIParam as Rectangle1Param;
                if (rectangle1 != null)
                {
                    modelImage = GetModelImageByRectangle1(Image.CopyImage(), rectangle1.RectangleStartRow, rectangle1.RectangleStartColumn, rectangle1.RectangleEndRow, rectangle1.RectangleEndColumn, out modelRegion);
                }
            }
            else if (info.ModelROIParam.GetType() == typeof(Rectangle2Param))
            {
                Rectangle2Param rectangle2 = info.ModelROIParam as Rectangle2Param;
                if (rectangle2 != null)
                {
                    modelImage = GetModelImageByRectangle2(Image.CopyImage(), rectangle2.Rectangle2CenterRow, rectangle2.Retangle2CenterColumn, rectangle2.Retangle2Angle, rectangle2.Rectangle2Length1, rectangle2.Rectangle2Length2, out modelRegion);
                }
            }
            else if (info.ModelROIParam.GetType() == typeof(EllipseParam))
            {
                EllipseParam ellipse = info.ModelROIParam as EllipseParam;
                if (ellipse != null)
                {
                    modelImage = GetModelImageByEllipse(Image.CopyImage(), ellipse.EllipseCenterRow, ellipse.EllipseCenterColumn, ellipse.EllipseAngle, ellipse.EllipseRadius1, ellipse.EllipseRadius2, out modelRegion);
                }
            }
            else
            {
                WriteErrorLog("VisionTool", info.ToolName + "模板创建失败");
                return(false);
            }
            HShapeModel shapeScaleModel = new HShapeModel();
            double      modelRow, modelCol;

            if (modelImage != null)
            {
                try
                {
                    shapeScaleModel.CreateScaledShapeModel(modelImage, new HTuple(info.NumLevels), TransAngle.AngleToHu(info.AngleStart), TransAngle.AngleToHu(info.AngleExtent), new HTuple(info.AngleStep), info.ScaleMin, info.ScaleMax, new HTuple(info.ScaleStep), new HTuple(info._Optimization.ToString()), info._Metric.ToString(), new HTuple(info.Contrast), new HTuple(info.MinContrast));
                    AffineTransModelContour(shapeScaleModel, modelRegion);
                    //record the model coordinate to the setting info.
                    modelRegion.AreaCenter(out modelRow, out modelCol);
                    info.ModelRegionRow   = modelRow;
                    info.ModelRegionCol   = modelCol;
                    info.ModelRegionAngle = 0.0;

                    this.ShapeModel = shapeScaleModel;
                    this.ModelXLD   = shapeScaleModel.GetShapeModelContours(1);
                    SaveModelFile(info.ToolName);
                }
                catch
                {
                    WriteErrorLog("VisionTool", info.ToolName + "模板创建失败");
                    return(false);
                }
            }
            else
            {
                WriteErrorLog("VisionTool", info.ToolName + "模板创建失败");
                return(false);
            }
            return(true);
        }
Пример #6
0
        private void modifyFindRegionBtn_Click(object sender, EventArgs e)
        {
            if (currentImage == null)
            {
                MessageHelper.ShowWarning("请获取图像后再修改搜索框");
                return;
            }
            if (currentFindRegionComb.SelectedItem == null)
            {
                MessageHelper.ShowWarning("请选择一个模板进行修改搜索框");
                return;
            }
            GrayMatchToolInfo info = Infos.Find(p => p.ToolName == currentFindRegionComb.SelectedItem.ToString());

            if (info == null)
            {
                MessageHelper.ShowWarning("无法修改模板搜索框,请删除该模板后重新创建!");
                return;
            }
            if (info.FindModelROIParam != null)
            {
                switch (info.FindModelROIParam.type)
                {
                case ROI.Line:
                    break;

                case ROI.Circle:
                    CircleParam circle = info.FindModelROIParam as CircleParam;
                    if (circle != null)
                    {
                        bDrawFindROI   = false;
                        bModifyFindROI = true;
                        displayForm.DrawCircleROI(Convert.ToInt32(circle.CircleRow), Convert.ToInt32(circle.CircleColumn), circle.Radius);
                    }
                    else
                    {
                        MessageHelper.ShowError("创建搜索框失败,请删除后再重新创建!");
                    }
                    break;

                case ROI.Ellipse:
                    EllipseParam ellipse = info.FindModelROIParam as EllipseParam;
                    if (ellipse != null)
                    {
                        bDrawFindROI   = false;
                        bModifyFindROI = true;
                        displayForm.DrawEllipseROI(ellipse.EllipseCenterRow, ellipse.EllipseCenterColumn, ellipse.EllipseAngle, ellipse.EllipseRadius1, ellipse.EllipseRadius2);
                    }
                    else
                    {
                        MessageHelper.ShowError("修改模板出现错误,请删除当前模板后重修创建");
                    }
                    break;

                case ROI.Rectangle1:
                    Rectangle1Param rectangle1 = info.FindModelROIParam as Rectangle1Param;
                    if (rectangle1 != null)
                    {
                        bDrawFindROI   = false;
                        bModifyFindROI = true;
                        displayForm.DrawRectangle1ROI(rectangle1.RectangleStartRow, rectangle1.RectangleStartColumn, rectangle1.RectangleEndRow, rectangle1.RectangleEndColumn);
                    }
                    else
                    {
                        MessageHelper.ShowError("修改模板出现错误,请删除当前模板后重修创建");
                    }
                    break;

                case ROI.Rectangle2:
                    Rectangle2Param rectangle2 = info.FindModelROIParam as Rectangle2Param;
                    if (rectangle2 != null)
                    {
                        bDrawFindROI   = false;
                        bModifyFindROI = true;
                        displayForm.DrawRectangle2ROI(rectangle2.Rectangle2CenterRow, rectangle2.Retangle2CenterColumn, rectangle2.Retangle2Angle, rectangle2.Rectangle2Length1, rectangle2.Rectangle2Length2);
                    }
                    else
                    {
                        MessageHelper.ShowError("修改模板出现错误,请删除当前模板后重修创建");
                    }
                    break;

                default:
                    break;
                }
            }
        }
Пример #7
0
        private void ModifyBtn_Click(object sender, EventArgs e)
        {
            if (CurrentModelComb.SelectedItem != null)
            {
                if (Info != null && Info.ToolName == CurrentModelComb.SelectedItem.ToString())
                {
                    switch (Info.ModelROIParam.type)
                    {
                    case ROI.Line:
                        break;

                    case ROI.Circle:
                        CircleParam circle = Info.ModelROIParam as CircleParam;
                        if (circle != null)
                        {
                            bModifyModelROI = true;
                            displayForm.DrawCircleROI(Convert.ToInt32(circle.CircleRow), Convert.ToInt32(circle.CircleColumn), circle.Radius);
                        }
                        else
                        {
                            MessageHelper.ShowError("修改模板出现错误,请删除当前模板后重修创建");
                        }
                        break;

                    case ROI.Ellipse:
                        EllipseParam ellipse = Info.ModelROIParam as EllipseParam;
                        if (ellipse != null)
                        {
                            bModifyModelROI = true;
                            displayForm.DrawEllipseROI(ellipse.EllipseCenterRow, ellipse.EllipseCenterColumn, ellipse.EllipseAngle, ellipse.EllipseRadius1, ellipse.EllipseRadius2);
                        }
                        else
                        {
                            MessageHelper.ShowError("修改模板出现错误,请删除当前模板后重修创建");
                        }
                        break;

                    case ROI.Rectangle1:
                        Rectangle1Param rectangle1 = Info.ModelROIParam as Rectangle1Param;
                        if (rectangle1 != null)
                        {
                            bModifyModelROI = true;
                            displayForm.DrawRectangle1ROI(rectangle1.RectangleStartRow, rectangle1.RectangleStartColumn, rectangle1.RectangleEndRow, rectangle1.RectangleEndColumn);
                        }
                        else
                        {
                            MessageHelper.ShowError("修改模板出现错误,请删除当前模板后重修创建");
                        }
                        break;

                    case ROI.Rectangle2:
                        Rectangle2Param rectangle2 = Info.ModelROIParam as Rectangle2Param;
                        if (rectangle2 != null)
                        {
                            bModifyModelROI = true;
                            displayForm.DrawRectangle2ROI(rectangle2.Rectangle2CenterRow, rectangle2.Retangle2CenterColumn, rectangle2.Retangle2Angle, rectangle2.Rectangle2Length1, rectangle2.Rectangle2Length2);
                        }
                        else
                        {
                            MessageHelper.ShowError("修改模板出现错误,请删除当前模板后重修创建");
                        }
                        break;

                    default:
                        break;
                    }
                }
            }
        }
Пример #8
0
 private void DisplayForm_CircleROIEvent(int t1, int t2, double t3, object t4, object t5)
 {
     if (!bModifyModelROI && !bModifyFindROI && !bDrawFindROI)
     {
         if (!string.IsNullOrEmpty(newModelName))
         {
             //Create a new setting info
             GrayMatchToolInfo info = new GrayMatchToolInfo();
             info.ToolName      = newModelName;
             info.ModelROIParam = new CircleParam()
             {
                 CircleRow = t1, CircleColumn = t2, Radius = t3, GraphName = newModelName
             };
             //Create a new match tool
             GrayMatchTool tool = new GrayMatchTool(info, displayForm);
             if (!tool.CreateMatchTool())
             {
                 MessageHelper.ShowError("模板创建失败,请重新创建");
                 return;
             }
             Infos.Add(info);
             Tools.Add(info.ToolName, tool);
             CurrentModelComb.Items.Add(info.ToolName);
             currentFindRegionComb.Items.Add(info.ToolName);
         }
     }
     else if (bModifyModelROI && !bModifyFindROI && !bDrawFindROI)
     {
         bModifyModelROI = false;
         if (Info != null)
         {
             CircleParam circle = Info.ModelROIParam as CircleParam;
             if (circle != null)
             {
                 circle.CircleRow    = t1;
                 circle.CircleColumn = t2;
                 circle.Radius       = t3;
             }
         }
     }
     else if (!bModifyModelROI && !bModifyFindROI && bDrawFindROI)
     {
         bDrawFindROI = false;
         GrayMatchToolInfo info = Infos.Find(p => p.ToolName == currentFindRegionComb.SelectedItem.ToString());
         if (info != null)
         {
             info.FindModelROIParam = new CircleParam()
             {
                 GraphName    = currentFindRegionComb.SelectedItem.ToString(),
                 CircleRow    = t1,
                 CircleColumn = t2,
                 Radius       = t3
             };
         }
     }
     else if (!bModifyModelROI && bModifyFindROI && bDrawFindROI)
     {
         bDrawFindROI   = false;
         bModifyFindROI = false;
         GrayMatchToolInfo info = Infos.Find(p => p.ToolName == currentFindRegionComb.SelectedItem.ToString());
         if (info != null)
         {
             CircleParam circle = info.FindModelROIParam as CircleParam;
             if (circle != null)
             {
                 circle.CircleRow    = t1;
                 circle.CircleColumn = t2;
                 circle.Radius       = t3;
             }
         }
     }
 }