check point
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -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; }
|
||||
}
|
||||
Reference in New Issue
Block a user