check point

This commit is contained in:
meelstorm
2025-07-14 12:03:59 +02:00
commit d3cb790bd9
431 changed files with 44078 additions and 0 deletions

View File

@@ -0,0 +1,158 @@
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Origin;
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Poly;
using Hawkeye.VisionBuilder.Workflow.Links;
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
using OpenCvSharp;
using Point = OpenCvSharp.Point;
using RectangleElement = Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Rectangle.RectangleElement;
namespace Hawkeye.VisionBuilder.Workflow.Operations;
[Category("Simple/Blobs")]
public class FindBlobOperation : BaseOperation, IHaveOrigin, IHaveSearchArea
{
private readonly WorkflowList _workflowList;
public int MinArea { get; set; } = 100;
public int MaxArea { get; set; } = 99999;
public int MinRadius { get; set; } = 0;
public int MaxRadius { get; set; } = 99999;
public bool BadIfFound { get; set; }
public Guid ReferenceId { get; set; } = new Guid();
public RectangleElement SearchArea { get; set; }
//processing result of current element
[NotForTool] public OriginElement Origin { get; private set; } = OriginElement.Default;
public FindBlobOperation(WorkflowList workflowList)
{
_workflowList = workflowList;
SearchArea = new RectangleElement()
{
Editable = true,
Location = Vector2.One * 100,
Size = Vector2.One * 100,
IsGood = true
};
}
protected override void InterpretInternal(Context context)
{
if (!CheckImageExists(context)) return;
if (!CheckGrayscale(context)) return;
SearchArea.MovePivot(_workflowList.GetOriginById(ReferenceId).Origin.Location);
var img = context.ActiveImage.ImageData;
var mat = new Mat(img.Size(), img.Type(), Scalar.Black);
SearchArea.FillMat(mat);
var contoursMat = img.BitwiseAnd(mat).ToMat();
var contours = contoursMat.Threshold(128, 255, ThresholdTypes.Binary)
.FindContoursAsArray(RetrievalModes.List, ContourApproximationModes.ApproxSimple);
//case: no blobs found
if (contours.Length == 0)
{
Status = "No blobs found";
SearchArea.IsGood = false;
context.GraphicsElements.Add(SearchArea);
Result = BadIfFound;
return;
}
int GetContourRadius(Point[] contour)
{
Cv2.MinEnclosingCircle(contour, out var center, out var radius);
return (int)radius;
}
//case: blobs found but not matching criteria
var contour = contours.Select(x => new { c = x, area = (int)Cv2.ContourArea(x),radius= GetContourRadius(x) })
.OrderBy(x => Math.Min(Math.Abs(x.area - MinArea), Math.Abs(x.area - MaxArea)));
var match = contour.Where(x=>x.c.Length>1)
.Where(x => x.area >= MinArea && x.area <= MaxArea)
.FirstOrDefault(x => x.radius >= MinRadius && x.radius <= MaxRadius);
if (match == null)
{
var best = contour.First();
Status = $"Area: {best.area} Radius: {best.radius}";
PolyElement poly = new PolyElement() { IsGood = BadIfFound };
poly.Points = best.c.Select(x => new Vector2(x.X, x.Y)).ToArray();
context.GraphicsElements.Add(poly);
context.GraphicsElements.Add(SearchArea);
Result = BadIfFound;
return;
}
{
PolyElement poly = new PolyElement() { IsGood = !BadIfFound };
poly.Points = match.c.Select(x => new Vector2(x.X, x.Y)).ToArray();
context.GraphicsElements.Add(poly);
SearchArea.IsGood = true;
context.GraphicsElements.Add(SearchArea);
var center = poly.Points.Aggregate((v1, v2) => v1 + v2) / poly.Points.Length;
Origin = new OriginElement() { Location = center };
Status = $"Area: {match.area} Radius: {match.radius}";
Result = !BadIfFound;
}
}
public override void SetParameters(Dictionary<string, object> parameters)
{
base.SetParameters(parameters);
SearchArea.SetPivot(_workflowList.GetOriginById(ReferenceId).Origin.Location);
}
public override void Save(BinaryWriter bw)
{
bw.Write(Id.ToString());
bw.Write(Label);
bw.Write(MinArea);
bw.Write(MaxArea);
bw.Write(MinRadius);
bw.Write(MaxRadius);
bw.Write(BadIfFound);
bw.Write(ReferenceId.ToString());
SearchArea.Save(bw);
}
public override void Load(BinaryReader br)
{
Id = new Guid(br.ReadString());
Label = br.ReadString();
MinArea = br.ReadInt32();
MaxArea = br.ReadInt32();
MinRadius = br.ReadInt32();
MaxRadius = br.ReadInt32();
BadIfFound = br.ReadBoolean();
ReferenceId = new Guid(br.ReadString());
SearchArea.Load(br);
}
}

View File

@@ -0,0 +1,233 @@
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Origin;
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Poly;
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Rectangle;
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
using OpenCvSharp;
using Point = OpenCvSharp.Point;
namespace Hawkeye.VisionBuilder.Workflow.Operations;
[Category("Simple/Blobs")]
public class FindManyBlobsExtOperation : BaseOperation
{
private readonly WorkflowList _workflowList;
public int MinArea { get; set; } = 100;
public int MaxArea { get; set; } = 99999;
public int MinRadius { get; set; } = 0;
public int MaxRadius { get; set; } = 99999;
public bool CheckInternalRadius { get; set; } = false;
public int MinInternalRadius { get; set; } = 0;
public int MaxInternalRadius { get; set; } = 99999;
public bool CheckWidth { get; set; } = false;
public int MinWidth { get; set; } = 0;
public int MaxWidth { get; set; } = 99999;
public bool CheckHeight { get; set; } = false;
public int MinHeight { get; set; } = 0;
public int MaxHeight { get; set; } = 99999;
public Guid ReferenceId { get; set; } = new Guid();
public RectangleElement SearchArea { get; set; }
public FindManyBlobsExtOperation(WorkflowList workflowList)
{
_workflowList = workflowList;
SearchArea = new RectangleElement()
{
Editable = true,
Location = Vector2.One * 100,
Size = Vector2.One * 100,
IsGood = true
};
}
public record ContourMatch(Point[] c, int area, int radius, int internalRadius, int width, int height)
{
public override string ToString()
{
return $"{{area={area}, radius={radius}, internalRadius={internalRadius}, width={width}, height={height}}}";
}
}
protected override void InterpretInternal(Context context)
{
Result = true;
if (!CheckImageExists(context)) return;
if (!CheckGrayscale(context)) return;
if (SearchArea == null)
{
SearchArea = new RectangleElement()
{
Editable = true,
Location = Vector2.Zero,
Size = new Vector2(context.ActiveImage.ImageData.Width, context.ActiveImage.ImageData.Height),
IsGood = true
};
}
SearchArea.MovePivot(_workflowList.GetOriginById(ReferenceId).Origin.Location);
var img = context.ActiveImage.ImageData;
var mat = new Mat(img.Size(), img.Type(), Scalar.Black);
SearchArea.FillMat(mat);
var contoursMat = img.BitwiseAnd(mat).ToMat();
var contours = contoursMat.Threshold(128, 255, ThresholdTypes.Binary)
.FindContoursAsArray(RetrievalModes.List, ContourApproximationModes.ApproxSimple);
context.GraphicsElements.Add(SearchArea);
// Case: no blobs found
if (contours.Length == 0)
{
Status = "No blobs found";
SearchArea.IsGood = true;
return;
}
int GetContourRadius(Point[] contour)
{
Cv2.MinEnclosingCircle(contour, out _, out var radius);
return (int)radius;
}
int GetContourInternalRadius(Point[] contour)
{
// Create a mask for the contour with the size of the bounding rectangle.
Mat mask = new Mat(contoursMat.Height, contoursMat.Width, MatType.CV_8UC1, Scalar.All(0));
// Fill the mask with the polygon defined by the contour.
Cv2.FillPoly(mask, new Point[][] { contour }, Scalar.White);
// Compute the distance transform.
using Mat dist = new Mat();
Cv2.DistanceTransform(mask, dist, DistanceTypes.C, DistanceTransformMasks.Mask3);
Cv2.MinMaxLoc(dist, out double minVal, out var maxVal);
return (int)maxVal;
}
(int width, int height) GetContourDimensions(Point[] contour)
{
var rect = Cv2.BoundingRect(contour);
return (rect.Width, rect.Height);
}
var contourMatches = contours.Select(c =>
{
int area = (int)Cv2.ContourArea(c);
int radius =-1;
radius = GetContourRadius(c);
int innerCircle=-1;
if (CheckInternalRadius)
innerCircle = GetContourInternalRadius(c);
var (width, height) = GetContourDimensions(c);
return new ContourMatch(c, area, radius, innerCircle, width, height);
})
.OrderBy(cm => Math.Min(Math.Abs(cm.area - MinArea), Math.Abs(cm.area - MaxArea)))
.ToList();
var matches = contourMatches.Where(cm => cm.c.Length > 1)
.Where(cm => cm.area >= MinArea && cm.area <= MaxArea)
.Where(cm => cm.radius >= MinRadius && cm.radius <= MaxRadius);
if (CheckInternalRadius)
{
matches = matches.Where(cm => cm.internalRadius >= MinInternalRadius && cm.internalRadius <= MaxInternalRadius);
}
if (CheckWidth)
{
matches = matches.Where(cm => cm.width >= MinWidth && cm.width <= MaxWidth);
}
if (CheckHeight)
{
matches = matches.Where(cm => cm.height >= MinHeight && cm.height <= MaxHeight);
}
var finalMatches = matches.ToList();
int badCount = 0;
foreach (var contourMatch in finalMatches)
{
PolyElement poly = new PolyElement() { IsGood = false };
poly.Points = contourMatch.c.Select(p => new Vector2(p.X, p.Y)).ToArray();
context.GraphicsElements.Add(poly);
badCount++;
}
if (badCount > 0)
{
Status = $"Found: {badCount} First: {finalMatches.First()}";
Result = false;
SearchArea.IsGood = false;
return;
}
SearchArea.IsGood = true;
Status = $"Best: {contourMatches.First()}";
Result = true;
}
public override void SetParameters(Dictionary<string, object> parameters)
{
base.SetParameters(parameters);
SearchArea.SetPivot(_workflowList.GetOriginById(ReferenceId).Origin.Location);
}
public override void Save(BinaryWriter bw)
{
bw.Write(Id.ToString());
bw.Write(Label);
bw.Write(MinArea);
bw.Write(MaxArea);
bw.Write(MinRadius);
bw.Write(MaxRadius);
bw.Write(CheckInternalRadius);
bw.Write(MinInternalRadius);
bw.Write(MaxInternalRadius);
bw.Write(CheckWidth);
bw.Write(MinWidth);
bw.Write(MaxWidth);
bw.Write(CheckHeight);
bw.Write(MinHeight);
bw.Write(MaxHeight);
bw.Write(ReferenceId.ToString());
SearchArea.Save(bw);
}
public override void Load(BinaryReader br)
{
Id = new Guid(br.ReadString());
Label = br.ReadString();
MinArea = br.ReadInt32();
MaxArea = br.ReadInt32();
MinRadius = br.ReadInt32();
MaxRadius = br.ReadInt32();
CheckInternalRadius = br.ReadBoolean();
MinInternalRadius = br.ReadInt32();
MaxInternalRadius = br.ReadInt32();
CheckWidth = br.ReadBoolean();
MinWidth = br.ReadInt32();
MaxWidth = br.ReadInt32();
CheckHeight = br.ReadBoolean();
MinHeight = br.ReadInt32();
MaxHeight = br.ReadInt32();
ReferenceId = new Guid(br.ReadString());
SearchArea.Load(br);
}
}

View File

@@ -0,0 +1,175 @@
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Origin;
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Poly;
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Rectangle;
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
using OpenCvSharp;
using Point = OpenCvSharp.Point;
namespace Hawkeye.VisionBuilder.Workflow.Operations;
[Category("Simple/Blobs")]
public class FindManyBlobsOperation:BaseOperation
{
private readonly WorkflowList _workflowList;
public int MinArea { get; set; } = 100;
public int MaxArea { get; set; } = 99999;
public int MinRadius { get; set; } = 0;
public int MaxRadius { get; set; } = 99999;
public Guid ReferenceId { get; set; } = new Guid();
public RectangleElement SearchArea { get; set; }
//processing result of current element
public FindManyBlobsOperation(WorkflowList workflowList)
{
_workflowList = workflowList;
SearchArea = new RectangleElement()
{
Editable = true,
Location = Vector2.One * 100,
Size = Vector2.One * 100,
IsGood = true
};
}
public record ContourMatch(Point[] c, int area, int radius)
{
public override string ToString()
{
return $"{{area={area}, radius={radius}}}";
}
}
protected override void InterpretInternal(Context context)
{
Result = true;
if (!CheckImageExists(context)) return;
if (!CheckGrayscale(context)) return;
if (SearchArea==null)
{
SearchArea = new RectangleElement()
{
Editable = true,
Location = Vector2.Zero,
Size = new Vector2(context.ActiveImage.ImageData.Width, context.ActiveImage.ImageData.Height),
IsGood = true
};
}
SearchArea.MovePivot(_workflowList.GetOriginById(ReferenceId).Origin.Location);
var img = context.ActiveImage.ImageData;
var mat = new Mat(img.Size(), img.Type(), Scalar.Black);
SearchArea.FillMat(mat);
var contoursMat = img.BitwiseAnd(mat).ToMat();
var contours = contoursMat.Threshold(128, 255, ThresholdTypes.Binary)
.FindContoursAsArray(RetrievalModes.List, ContourApproximationModes.ApproxSimple);
context.GraphicsElements.Add(SearchArea);
//case: no blobs found
if (contours.Length == 0)
{
Status = "No blobs found";
SearchArea.IsGood = true;
return;
}
int GetContourRadius(Point[] contour)
{
Cv2.MinEnclosingCircle(contour, out var center, out var radius);
return (int)radius;
}
//case: blobs found but not matching criteria
var contour = contours.Select(x => new ContourMatch(x, (int)Cv2.ContourArea(x), GetContourRadius(x)))
.OrderBy(x => Math.Min(Math.Abs(x.area - MinArea), Math.Abs(x.area - MaxArea))).ToList();
var match = contour.Where(x => x.c.Length > 1)
.Where(x => x.area >= MinArea && x.area <= MaxArea)
.Where(x => x.radius >= MinRadius && x.radius <= MaxRadius).ToList();
int badCount = 0;
foreach (ContourMatch contourMatch in match)
{
PolyElement poly = new PolyElement() { IsGood = false };
poly.Points = contourMatch.c.Select(x => new Vector2(x.X, x.Y)).ToArray();
context.GraphicsElements.Add(poly);
badCount++;
}
if (badCount > 0)
{
Status = $"Found: {badCount} First: {match.First().ToString()}";
Result = false;
SearchArea.IsGood = false;
return;
}
SearchArea.IsGood = true;
Status = $"Best: {contour.First().ToString()}";
Result = true;
}
public override void SetParameters(Dictionary<string, object> parameters)
{
base.SetParameters(parameters);
SearchArea.SetPivot(_workflowList.GetOriginById(ReferenceId).Origin.Location);
}
public override void Save(BinaryWriter bw)
{
bw.Write(Id.ToString());
bw.Write(Label);
bw.Write(MinArea);
bw.Write(MaxArea);
bw.Write(MinRadius);
bw.Write(MaxRadius);
bw.Write(ReferenceId.ToString());
SearchArea.Save(bw);
}
public override void Load(BinaryReader br)
{
Id = new Guid(br.ReadString());
Label = br.ReadString();
MinArea = br.ReadInt32();
MaxArea = br.ReadInt32();
MinRadius = br.ReadInt32();
MaxRadius = br.ReadInt32();
ReferenceId = new Guid(br.ReadString());
SearchArea.Load(br);
}
}

View File

@@ -0,0 +1,112 @@
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Origin;
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Rectangle;
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
using OpenCvSharp;
using System;
using System.Security.Cryptography.Xml;
using Point = OpenCvSharp.Point;
namespace Hawkeye.VisionBuilder.Workflow.Operations;
[Category("Simple/Blobs")]
public class FindRectangleOperation:BaseOperation
{
private readonly WorkflowList _workflowList;
public int MinWidth { get; set; }
public int MaxWidth { get; set; } = 999999;
public int MinHeight { get; set; }
public int MaxHeight { get; set; } = 999999;
public Guid ReferenceId { get; set; } = new Guid();
public bool BadIfFound { get; set; }=false;
public FindRectangleOperation(WorkflowList workflowList)
{
_workflowList = workflowList;
SearchArea = new RectangleElement()
{
Editable = true,
Location = Vector2.One * 100,
Size = Vector2.One * 100,
IsGood = true
};
}
public RectangleElement SearchArea { get; set; }
//processing result of current element
[NotForTool] public OriginElement Origin { get; private set; } = OriginElement.Default;
protected override void InterpretInternal(Context context)
{
if(!CheckImageExists(context)) return;
SearchArea.MovePivot(_workflowList.GetOriginById(ReferenceId).Origin.Location);
var image = context.ActiveImage.ImageData;
var mat = new Mat(image.Size(), image.Type(), Scalar.Black);
SearchArea.FillMat(mat);
var contoursMat = image.BitwiseAnd(mat).ToMat();
context.GraphicsElements.Add(SearchArea);
// find rectangle on image
Cv2.FindContours(contoursMat, out var contours, out _, RetrievalModes.External, ContourApproximationModes.ApproxSimple);
foreach (Point[] contour in contours)
{
var rect = Cv2.BoundingRect(contour);
if (rect.Width >= MinWidth && rect.Width <= MaxWidth && rect.Height >= MinHeight &&
rect.Height <= MaxHeight)
{
Status = "Rectangle: " + rect;
Result = !BadIfFound;
RectangleElement re = new RectangleElement()
{
Editable = false,
IsGood = !BadIfFound,
Location = new Vector2(rect.X, rect.Y),
Size = new Vector2(rect.Width, rect.Height)
};
context.GraphicsElements.Add(re);
return;
}
}
Status = "No rectangle found";
Result = BadIfFound;
}
public override void SetParameters(Dictionary<string, object> parameters)
{
base.SetParameters(parameters);
SearchArea.SetPivot(_workflowList.GetOriginById(ReferenceId).Origin.Location);
}
public override void Save(BinaryWriter bw)
{
base.Save(bw);
bw.Write(ReferenceId.ToString());
bw.Write(MinWidth);
bw.Write(MaxWidth);
bw.Write(MinHeight);
bw.Write(MaxHeight);
bw.Write(BadIfFound);
SearchArea.Save(bw);
}
public override void Load(BinaryReader br)
{
base.Load(br);
ReferenceId = new Guid(br.ReadString());
MinWidth = br.ReadInt32();
MaxWidth = br.ReadInt32();
MinHeight = br.ReadInt32();
MaxHeight = br.ReadInt32();
BadIfFound = br.ReadBoolean();
SearchArea.Load(br);
}
}

View File

@@ -0,0 +1,106 @@
using Hawkeye.VisionBuilder.Workflow.Datatypes;
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Align;
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Origin;
using Hawkeye.VisionBuilder.Workflow.Links;
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
using OpenCvSharp;
namespace Hawkeye.VisionBuilder.Workflow.Operations;
[Category("Simple/Blobs")]
public class TwoBlobsAlign:BaseOperation,IHaveOrigin
{
private readonly WorkflowList _workflowList;
public TwoBlobsAlign(WorkflowList workflowList)
{
_workflowList = workflowList;
}
public Guid Reference1 { get; set; } = NoOrigin.Instance.Id;
public Guid Reference2 { get; set; } = NoOrigin.Instance.Id;
public int DesiredAngle { get; set; }
[NotForTool] public OriginElement Origin { get; private set; } = OriginElement.Default;
private string _status;
protected override void InterpretInternal(Context context)
{
var reference1 = _workflowList.GetOriginById(Reference1);
var reference2 = _workflowList.GetOriginById(Reference2);
if (reference1 == NoOrigin.Instance)
{
_status = $"{nameof(Reference1)} must be set";
Result = false;
return;
}
if (reference2 == NoOrigin.Instance)
{
_status = $"{nameof(Reference2)} must be set";
Result = false;
return;
}
if (!(reference1 as BaseOperation).Result)
{
_status = $"{nameof(Reference1)} detection failed";
Result = false;
return;
}
if (!(reference2 as BaseOperation).Result)
{
_status = $"{nameof(Reference2)} detection failed";
Result = false;
return;
}
if (context.ActiveImage == null)
{
_status = $"Image required";
Result = false;
return;
}
var center = (reference1.Origin.Location + reference2.Origin.Location) / 2;
var deltaX = reference2.Origin.Location.X-reference1.Origin.Location.X;
var deltaY = reference2.Origin.Location.Y-reference1.Origin.Location.Y;
var angle = ((float)Math.Atan2(deltaY, deltaX))/MathF.PI*180f;
var deltaAngle = DesiredAngle - angle;
var matrix=Cv2.GetRotationMatrix2D(new Point2f(center.X, center.Y), -deltaAngle, 1);
var destImage = new Mat(context.ActiveImage.ImageData.Size(), context.ActiveImage.ImageData.Type());
Cv2.WarpAffine(context.ActiveImage.ImageData, destImage,matrix,destImage.Size());
context.ActiveImage=new HawkeyeImage(){Filename = context.ActiveImage.Filename,ImageData = destImage };
Origin = new OriginElement() {Location = center };
_status = center.ToString();
Result = true;
context.GraphicsElements.Add(new AlignElement(){Location = center});
}
public override string ToString()
{
return _status;
}
public override void Save(BinaryWriter bw)
{
base.Save(bw);
bw.Write(Reference1.ToString());
bw.Write(Reference2.ToString());
bw.Write(DesiredAngle);
}
public override void Load(BinaryReader br)
{
base.Load(br);
Reference1 = new Guid(br.ReadString());
Reference2 = new Guid(br.ReadString());
DesiredAngle = br.ReadInt32();
}
}

View File

@@ -0,0 +1,104 @@
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Align;
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Edge;
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Origin;
using Hawkeye.VisionBuilder.Workflow.Links;
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
namespace Hawkeye.VisionBuilder.Workflow.Operations.PositionDetection.Edges;
[Category("Simple/Edges")]
public class EdgeIntersectionOperation : BaseOperation, IHaveOrigin
{
private readonly WorkflowList _workflowList;
private string _status;
public override string ToString()
{
return _status;
}
public EdgeIntersectionOperation(WorkflowList workflowList)
{
_workflowList = workflowList;
}
public Guid ReferenceEdge1 { get; set; }
public Guid ReferenceEdge2 { get; set; }
protected override void InterpretInternal(Context context)
{
var reference1 = _workflowList.GetById(ReferenceEdge1);
var reference2 = _workflowList.GetById(ReferenceEdge2);
if (reference1 == NoOrigin.Instance)
{
_status = $"{nameof(ReferenceEdge1)} must be set";
Result = false;
return;
}
if (reference2 == NoOrigin.Instance)
{
_status = $"{nameof(ReferenceEdge2)} must be set";
Result = false;
return;
}
if (!(reference1 as BaseOperation).Result)
{
_status = $"{nameof(ReferenceEdge1)} detection failed";
Result = false;
return;
}
if (!(reference2 as BaseOperation).Result)
{
_status = $"{nameof(ReferenceEdge2)} detection failed";
Result = false;
return;
}
var edge1 = reference1 as FindEdgeOperation;
var edge2 = reference2 as FindEdgeOperation;
//context.GraphicsElements.Add(new EdgeElement()
//{
// Location = edge1.EdgeOrigin.Location,
// Rotation = edge1.EdgeOrigin.Rotation
//});
//context.GraphicsElements.Add(new EdgeElement()
//{
// Location = edge2.EdgeOrigin.Location,
// Rotation = edge2.EdgeOrigin.Rotation
//});
if (edge1.EdgeOrigin.Intersection(edge2.EdgeOrigin, out var x, out var y))
{
Result = true;
Origin = new OriginElement()
{
Location = new Vector2(x, y)
};
_status = $"{Origin.Location}";
context.GraphicsElements.Add(new AlignElement() { Location = Origin.Location });
return;
}
_status = $"Edges are parallel";
Result = false;
}
public override void Save(BinaryWriter bw)
{
bw.Write(Id.ToString());
bw.Write(ReferenceEdge1.ToString());
bw.Write(ReferenceEdge2.ToString());
}
public override void Load(BinaryReader br)
{
Id = new Guid(br.ReadString());
ReferenceEdge1 = new Guid(br.ReadString());
ReferenceEdge2 = new Guid(br.ReadString());
}
public OriginElement Origin { get; set; } = OriginElement.Default;
}

View File

@@ -0,0 +1,130 @@
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Align;
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Edge;
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Origin;
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.RotatedRectangle;
using Hawkeye.VisionBuilder.Workflow.Links;
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
using OpenCvSharp;
namespace Hawkeye.VisionBuilder.Workflow.Operations.PositionDetection.Edges;
[Category("Simple/Edges")]
public class FindEdgeOperation : BaseOperation, IHaveEdge, IHaveOrigin
{
private readonly WorkflowList _workflowList;
public Guid ReferenceId { get; set; } = new Guid();
public EdgeOrigin EdgeOrigin { get; set; }
public FindEdgeOperation(WorkflowList workflowList)
{
_workflowList = workflowList;
SearchArea = new RotatedRectangleElement()
{
Location = new Vector2(100, 100),
Editable = true,
HalfWidth = 50,
Height = new Vector2(0, 100),
IsGood = true
};
}
private string _status;
public override string ToString()
{
return _status;
}
protected override void InterpretInternal(Context context)
{
if (context.ActiveImage == null)
{
Result = false;
_status = "No image provided";
return;
}
SearchArea.MovePivot(_workflowList.GetOriginById(ReferenceId).Origin.Location);
context.GraphicsElements.Add(SearchArea);
var img = context.ActiveImage.ImageData;
var mask = new Mat(img.Size(), img.Type(), Scalar.Black);
SearchArea.FillMat(mask);
var edges = img.Canny(100, 200);
var mat = edges.BitwiseAnd(mask).ToMat();
mat.MinMaxLoc(out _, out var maxVal, out _, out var maxLoc);
if (maxVal < 0.01)
{
_status = "No edges found";
SearchArea.IsGood = false;
context.GraphicsElements.Add(SearchArea);
Result = false;
return;
}
Cv2.FindContours(mat, out var contours, out var hierarchy, RetrievalModes.CComp, ContourApproximationModes.ApproxSimple);
//var closest = contours.OrderBy(c =>
//{
// return (c.Select(x => new Vector2(x.X, x.Y)).Aggregate((v1, v2) => v1 + v2) / c.Length - SearchArea.Location).Length();
//}).First();
var closest = contours.SelectMany(x => x);
var line = Cv2.FitLine(closest, DistanceTypes.L2, 0, 0.01, 0.01);
context.GraphicsElements.Add(new EdgeElement()
{
Location = new Vector2((float)line.X1, (float)line.Y1),
Rotation = (float)line.GetVectorRadian()
});
EdgeOrigin = new EdgeOrigin()
{
Found = true,
Location = new Vector2((float)line.X1, (float)line.Y1),
Rotation = (float)line.GetVectorRadian()
};
Origin = new OriginElement()
{
Location = new Vector2((float)line.X1, (float)line.Y1)
};
context.GraphicsElements.Add(new AlignElement() { Location = new Vector2((float)line.X1, (float)line.Y1) });
_status = EdgeOrigin.ToString();
SearchArea.IsGood = true;
Result = true;
}
public override void SetParameters(Dictionary<string, object> parameters)
{
base.SetParameters(parameters);
SearchArea.SetPivot(_workflowList.GetOriginById(ReferenceId).Origin.Location);
}
public override void Save(BinaryWriter bw)
{
bw.Write(Id.ToString());
bw.Write(Label);
bw.Write(ReferenceId.ToString());
SearchArea.Save(bw);
}
public override void Load(BinaryReader br)
{
Id = new Guid(br.ReadString());
Label = br.ReadString();
ReferenceId = new Guid(br.ReadString());
SearchArea = new RotatedRectangleElement();
SearchArea.Load(br);
}
public RotatedRectangleElement SearchArea { get; set; }
public OriginElement Origin { get; set; }
}