opencvsharp5
This commit is contained in:
parent
03d3250fa3
commit
890d27712a
@ -19,10 +19,9 @@
|
||||
<PackageReference Include="DotNetCore.Natasha.CSharp.Compiler" Version="10.1.2.2" />
|
||||
<PackageReference Include="DotNetCore.Natasha.CSharp.Compiler.Domain" Version="10.0.0.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.17" />
|
||||
<PackageReference Include="MiniExcel" Version="1.44.1" />
|
||||
<PackageReference Include="MiniExcel" Version="1.45.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
|
||||
<PackageReference Include="OpenCvSharp4.Windows" Version="4.13.0.20260602" />
|
||||
<PackageReference Include="OpenCvSharp4.WpfExtensions" Version="4.13.0.20260602" />
|
||||
<PackageReference Include="OpenCvSharp5.Windows" Version="5.0.0.20260702" />
|
||||
<PackageReference Include="Prism.DryIoc" Version="9.0.537" />
|
||||
<PackageReference Include="RoslynPad.Editor.Windows" Version="5.0.0" />
|
||||
<!--<PackageReference Include="SourceGear.sqlite3" Version="3.50.4.5" />-->
|
||||
|
||||
@ -1163,7 +1163,16 @@ namespace PLCSharp.VVMs.Vision
|
||||
double spacing = SelectVisionFlow.DoubleParams.TryGetValue("CalibSpacing", out double sp) ? Math.Max(sp, 0.1) : 5;
|
||||
string matName = SelectVisionFlow.StringParams.TryGetValue("CalibMatName", out var mn) && !string.IsNullOrEmpty(mn) ? mn : "标定矩阵";
|
||||
|
||||
var gray = src.Channels() == 3 ? src.CvtColor(ColorConversionCodes.BGR2GRAY) : src;
|
||||
Mat gray;
|
||||
if (src.Channels() == 3)
|
||||
{
|
||||
gray = new Mat();
|
||||
Cv2.CvtColor(src, gray, ColorConversionCodes.BGR2GRAY);
|
||||
}
|
||||
else
|
||||
{
|
||||
gray = src;
|
||||
}
|
||||
int x = Math.Clamp((int)roi.Left, 0, gray.Width - 1);
|
||||
int y = Math.Clamp((int)roi.Top, 0, gray.Height - 1);
|
||||
int w = Math.Min((int)roi.Width, gray.Width - x);
|
||||
@ -1197,7 +1206,7 @@ namespace PLCSharp.VVMs.Vision
|
||||
rows.Add(sorted.Skip(i).Take(3).OrderBy(d => d.cx).ToList());
|
||||
|
||||
// 画圆
|
||||
if (src.Channels() == 1) src = src.CvtColor(ColorConversionCodes.GRAY2BGR);
|
||||
if (src.Channels() == 1) { Cv2.CvtColor(src, src, ColorConversionCodes.GRAY2BGR); }
|
||||
int idx = 0;
|
||||
foreach (var row in rows)
|
||||
foreach (var (cx, cy, r) in row)
|
||||
@ -1222,7 +1231,8 @@ namespace PLCSharp.VVMs.Vision
|
||||
worldPts.Add(new Point2f((float)(colBase + c * spacing), (float)(rowBase + r * spacing)));
|
||||
}
|
||||
|
||||
var affine = Cv2.EstimateAffine2D(InputArray.Create(imgPts), InputArray.Create(worldPts), null, RobustEstimationAlgorithms.RANSAC, 3.0);
|
||||
using Mat noInliers = new Mat();
|
||||
var affine = Cv2.EstimateAffine2D(InputArray.Create(imgPts), InputArray.Create(worldPts), noInliers, RobustEstimationAlgorithms.RANSAC, 3.0);
|
||||
if (affine == null || affine.Empty()) { SendInfoDialog("计算变换矩阵失败!"); return; }
|
||||
|
||||
string json = MatExtension.SerializeAffineMat(affine);
|
||||
|
||||
@ -40,7 +40,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
|
||||
? rv : "卡尺找圆_Circle";
|
||||
|
||||
if (src.Channels() == 3)
|
||||
src = src.CvtColor(ColorConversionCodes.BGR2GRAY);
|
||||
Cv2.CvtColor(src, src, ColorConversionCodes.BGR2GRAY);
|
||||
|
||||
double halfLen = caliperLength / 2.0;
|
||||
int scanLen = caliperLength;
|
||||
|
||||
@ -27,7 +27,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
|
||||
? en : "卡尺寻边_Edge";
|
||||
|
||||
if (src.Channels() == 3)
|
||||
src = src.CvtColor(ColorConversionCodes.BGR2GRAY);
|
||||
Cv2.CvtColor(src, src, ColorConversionCodes.BGR2GRAY);
|
||||
|
||||
double lineDx = ex - sx, lineDy = ey - sy;
|
||||
double lineLen = Math.Sqrt(lineDx * lineDx + lineDy * lineDy);
|
||||
|
||||
@ -34,7 +34,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
|
||||
? rv : "卡尺找旋转矩形_Rect";
|
||||
|
||||
if (src.Channels() == 3)
|
||||
src = src.CvtColor(ColorConversionCodes.BGR2GRAY);
|
||||
Cv2.CvtColor(src, src, ColorConversionCodes.BGR2GRAY);
|
||||
|
||||
double halfLen = caliperLength / 2.0;
|
||||
int scanLen = caliperLength;
|
||||
|
||||
@ -78,7 +78,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
|
||||
Mat mask = null;
|
||||
Mat homography = null;
|
||||
Mat template = null;
|
||||
Mat templateMask = null; // 圆形模板遮罩
|
||||
Mat templateMask = new Mat(); // 圆形模板遮罩(空 Mat = 无掩码)
|
||||
|
||||
try
|
||||
{
|
||||
@ -115,8 +115,25 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
|
||||
}
|
||||
|
||||
// 灰度化
|
||||
graySrc = src.Channels() == 3 ? src.CvtColor(ColorConversionCodes.BGR2GRAY) : src.Clone();
|
||||
grayTemplate = template.Channels() == 3 ? template.CvtColor(ColorConversionCodes.BGR2GRAY) : template.Clone();
|
||||
if (src.Channels() == 3)
|
||||
{
|
||||
graySrc = new Mat();
|
||||
Cv2.CvtColor(src, graySrc, ColorConversionCodes.BGR2GRAY);
|
||||
}
|
||||
else
|
||||
{
|
||||
graySrc = src.Clone();
|
||||
}
|
||||
|
||||
if (template.Channels() == 3)
|
||||
{
|
||||
grayTemplate = new Mat();
|
||||
Cv2.CvtColor(template, grayTemplate, ColorConversionCodes.BGR2GRAY);
|
||||
}
|
||||
else
|
||||
{
|
||||
grayTemplate = template.Clone();
|
||||
}
|
||||
|
||||
// 圆形模板创建遮罩(仅圆形区域参与特征检测)
|
||||
if (roiShape == "圆形")
|
||||
@ -131,7 +148,8 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
|
||||
var orb = ORB.Create(nfeatures);
|
||||
// 源图特征
|
||||
srcDescriptors = new Mat();
|
||||
orb.DetectAndCompute(graySrc, null, out KeyPoint[] srcKps, srcDescriptors);
|
||||
using Mat noMask = new Mat();
|
||||
orb.DetectAndCompute(graySrc, noMask, out KeyPoint[] srcKps, srcDescriptors);
|
||||
// 模板特征(圆形模板使用遮罩)
|
||||
templateDescriptors = new Mat();
|
||||
orb.DetectAndCompute(grayTemplate, templateMask, out KeyPoint[] templateKps, templateDescriptors);
|
||||
@ -241,7 +259,8 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
|
||||
Mat colorSrc = null;
|
||||
if (func.Src.Channels() == 1)
|
||||
{
|
||||
colorSrc = func.Src.CvtColor(ColorConversionCodes.GRAY2BGR);
|
||||
colorSrc = new Mat();
|
||||
Cv2.CvtColor(func.Src, colorSrc, ColorConversionCodes.GRAY2BGR);
|
||||
func.Src = colorSrc;
|
||||
}
|
||||
|
||||
|
||||
@ -366,7 +366,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler
|
||||
|
||||
if (src.Type() == MatType.CV_8UC3)
|
||||
{
|
||||
gray = src.CvtColor(ColorConversionCodes.BGR2GRAY);
|
||||
Cv2.CvtColor(src, gray, ColorConversionCodes.BGR2GRAY);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -395,7 +395,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler
|
||||
}
|
||||
else
|
||||
{
|
||||
bgr = src.CvtColor(ColorConversionCodes.GRAY2BGR);
|
||||
Cv2.CvtColor(src, bgr, ColorConversionCodes.GRAY2BGR);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -601,7 +601,8 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler
|
||||
|
||||
if (src.Type() != MatType.CV_8UC3) return false;
|
||||
|
||||
var hsv = src.CvtColor(ColorConversionCodes.BGR2HSV);
|
||||
Mat hsv = new Mat();
|
||||
Cv2.CvtColor(src, hsv, ColorConversionCodes.BGR2HSV);
|
||||
Scalar low = new(hLow, sLow, vLow);
|
||||
Scalar high = new(hHigh, sHigh, vHigh);
|
||||
Cv2.InRange(hsv, low, high, dst);
|
||||
|
||||
@ -21,7 +21,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Processing
|
||||
{
|
||||
if (func.Src.Channels() == 3)
|
||||
{
|
||||
Mat[] channels = func.Src.Split();
|
||||
Mat[] channels = Cv2.Split(func.Src);
|
||||
Mat maxMat = new Mat();
|
||||
Cv2.Max(channels[0], channels[1], maxMat);
|
||||
Cv2.Max(maxMat, channels[2], maxMat);
|
||||
|
||||
@ -21,7 +21,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Processing
|
||||
{
|
||||
if (func.Src.Channels() == 3)
|
||||
{
|
||||
Mat[] channels = func.Src.Split();
|
||||
Mat[] channels = Cv2.Split(func.Src);
|
||||
Mat minMat = new Mat();
|
||||
Cv2.Min(channels[0], channels[1], minMat);
|
||||
Cv2.Min(minMat, channels[2], minMat);
|
||||
|
||||
@ -23,7 +23,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Processing
|
||||
{
|
||||
if (item.IntParams.TryGetValue("ChannelIndex", out int _channelIndex))
|
||||
{
|
||||
Mat[] channels = func.Src.Split();
|
||||
Mat[] channels = Cv2.Split(func.Src);
|
||||
func.Src = channels[_channelIndex];
|
||||
|
||||
}
|
||||
|
||||
@ -33,7 +33,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Processing
|
||||
{
|
||||
|
||||
if (func.Src.Channels() == 3)
|
||||
func.Src = func.Src.CvtColor(ColorConversionCodes.BGR2GRAY);
|
||||
Cv2.CvtColor(func.Src, func.Src, ColorConversionCodes.BGR2GRAY);
|
||||
|
||||
|
||||
ThresholdTypes type = (ThresholdTypes)thresholdType;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user