Commit a7bd6f52 贾鹏旭

添加修正的aoi检测

1 个父辈 e7461058
...@@ -36,7 +36,7 @@ ...@@ -36,7 +36,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="ConfigHelper"> <Reference Include="ConfigHelper">
<HintPath>..\..\ConfigHelper\ConfigHelper\bin\Debug\ConfigHelper.dll</HintPath> <HintPath>..\..\..\..\类库\ConfigHelper.dll</HintPath>
</Reference> </Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> <Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath> <HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
......
...@@ -79,7 +79,7 @@ namespace AOI ...@@ -79,7 +79,7 @@ namespace AOI
} }
else else
{ {
ResultBean resultBean = method.Check(standardImage, image); ResultBean resultBean = method.Check(standardImage,image);
resultBean.roiPath = method.RoiPath; resultBean.roiPath = method.RoiPath;
resultBean.labelKey = item.Key; resultBean.labelKey = item.Key;
resultBeans.Add(resultBean); resultBeans.Add(resultBean);
...@@ -146,12 +146,14 @@ namespace AOI ...@@ -146,12 +146,14 @@ namespace AOI
imageFilePath = filePath.Replace( extension, ".bmp"); imageFilePath = filePath.Replace( extension, ".bmp");
return imageFilePath; return imageFilePath;
} }
public static String FilePath = null;
/// <summary> /// <summary>
/// 加载项目 /// 加载项目
/// </summary> /// </summary>
/// <param name="filePath"></param> /// <param name="filePath"></param>
public static AoiProject Load(string filePath, out string msg) public static AoiProject Load(string filePath, out string msg)
{ {
FilePath = filePath;
Thread.Sleep(1); Thread.Sleep(1);
GC.Collect(); GC.Collect();
msg = ""; msg = "";
...@@ -233,7 +235,6 @@ namespace AOI ...@@ -233,7 +235,6 @@ namespace AOI
} }
public Bitmap ProcessTestImage(Bitmap targetimage, string name) public Bitmap ProcessTestImage(Bitmap targetimage, string name)
{ {
var markroi = BaseROI; var markroi = BaseROI;
if (markroi == null || markroi.RoiPath == null || markroi.RoiPath.GetBounds() == RectangleF.Empty) if (markroi == null || markroi.RoiPath == null || markroi.RoiPath.GetBounds() == RectangleF.Empty)
{ {
...@@ -406,8 +407,6 @@ namespace AOI ...@@ -406,8 +407,6 @@ namespace AOI
if (resultmat != null) if (resultmat != null)
resultmat.Dispose(); resultmat.Dispose();
} }
} }
public static bool Star_MatchTemplate(Bitmap basebmp, Bitmap targetbmp, RectangleF rectangleF, out Bitmap matchBitmap) public static bool Star_MatchTemplate(Bitmap basebmp, Bitmap targetbmp, RectangleF rectangleF, out Bitmap matchBitmap)
......
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing; using System.Drawing;
using System.Drawing.Drawing2D; using System.Drawing.Drawing2D;
using System.IO;
using System.Linq; using System.Linq;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
...@@ -9,6 +11,7 @@ using System.Text; ...@@ -9,6 +11,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using OpenCvSharp; using OpenCvSharp;
using OpenCvSharp.Blob; using OpenCvSharp.Blob;
using OpenCvSharp.ImgHash;
using OpenCvSharp.XFeatures2D; using OpenCvSharp.XFeatures2D;
using static AOI.Eyemlib; using static AOI.Eyemlib;
...@@ -32,8 +35,13 @@ namespace AOI ...@@ -32,8 +35,13 @@ namespace AOI
bool needCut = true; bool needCut = true;
Image standardRoiImg = GetRoiImage(standardImage, needCut); Image standardRoiImg = GetRoiImage(standardImage, needCut);
resultBean.standardRoiImage = standardRoiImg; resultBean.standardRoiImage = standardRoiImg;
double percent, angle; double percent=0d, angle=0d;
(percent,angle) = GetTemplateMatchPercent(standardImage, imageToCheck, out Image cutImg); Image cutImg = null;
//将基准图切割成小块
if (CuttingAOIArea(standardImage, out Bitmap standardAOIImage, out RectangleF rect))
{
(percent, angle) = GetTemplateMatchPercent(standardAOIImage, imageToCheck,rect ,out cutImg);
}
bool result = false; bool result = false;
if(SamePercent > 100) if(SamePercent > 100)
{ {
...@@ -44,9 +52,41 @@ namespace AOI ...@@ -44,9 +52,41 @@ namespace AOI
{ {
result = true; result = true;
} }
if (!resultBean.result)
{
string aoipath = AoiProject.FilePath;
string xypath= Path.GetFileNameWithoutExtension(aoipath);
if (Directory.Exists(xypath))
{
string pst = null;
PointF[] points = this.RoiPath.PathData.Points;
if (points.Length >= 4)
{
for (int i = 0; i < points.Length; i++)
{
pst += (int)points[i].X + "-" + (int)points[i].Y + "_";
}
string path = Path.Combine(xypath,pst);
if (Directory.Exists(xypath))
{
string[] files = Directory.GetFiles(xypath);
foreach (string file in files)
{
if (file.EndsWith(".jpg") || file.EndsWith(".png") || file.EndsWith(".bmp") || file.EndsWith(".gif"))
{
Bitmap bmp = new Bitmap(file);
RectangleF rects = new RectangleF(points[0],new System.Drawing.Size(bmp.Width,bmp.Height));
(percent, angle) = GetTemplateMatchPercent(bmp, imageToCheck, rects, out cutImg);
}
}
}
}
}
}
resultBean.currentRoiImage = cutImg; resultBean.currentRoiImage = cutImg;
resultBean.result = result; resultBean.result = result;
resultBean.percentValue =Math.Round( percent,3); resultBean.percentValue = Math.Round(percent, 3);
resultBean.checkData = angle; resultBean.checkData = angle;
return resultBean; return resultBean;
} }
...@@ -58,19 +98,19 @@ namespace AOI ...@@ -58,19 +98,19 @@ namespace AOI
/// <param name="imageToCheck"></param> /// <param name="imageToCheck"></param>
/// <param name="cutImg"></param> /// <param name="cutImg"></param>
/// <returns></returns> /// <returns></returns>
public (double,double) GetTemplateMatchPercent(Image standardImage, Image imageToCheck, out Image cutImg) public (double,double) GetTemplateMatchPercent(Image standardAOIImage, Image imageToCheck, RectangleF rect, out Image cutImg)
{ {
cutImg = null; cutImg = null;
if (RoiPath == null) //if (RoiPath == null)
return (0d, 0d); // return (0d, 0d);
var rect = RoiPath.GetBounds(); //var rect = RoiPath.GetBounds();
if (rect == RectangleF.Empty) //if (rect == RectangleF.Empty)
return (0d, 0d); // return (0d, 0d);
float MatchRoi_Inflate = ConfigHelper.Config.Get("SURF_MatchRect_Inflate", 0.3f); //float MatchRoi_Inflate = ConfigHelper.Config.Get("SURF_MatchRect_Inflate", 0.3f);
var newroi = new RectangleF(rect.Location,rect.Size); //var newroi = new RectangleF(rect.Location,rect.Size);
newroi.Inflate(rect.Width * MatchRoi_Inflate, rect.Height * MatchRoi_Inflate); //newroi.Inflate(rect.Width * MatchRoi_Inflate, rect.Height * MatchRoi_Inflate);
var bitmap1 = AoiProject.CropBitmap((Bitmap)standardImage, newroi); //var bitmap1 = AoiProject.CropBitmap((Bitmap)standardImage, newroi);
var template = Eyemlib.eyemCvtToEyemImage(bitmap1); var template = Eyemlib.eyemCvtToEyemImage((Bitmap)standardAOIImage);
var search = Eyemlib.eyemCvtToEyemImage((Bitmap)imageToCheck); var search = Eyemlib.eyemCvtToEyemImage((Bitmap)imageToCheck);
int iNumMatches = 5;//目标个数 int iNumMatches = 5;//目标个数
...@@ -139,5 +179,28 @@ namespace AOI ...@@ -139,5 +179,28 @@ namespace AOI
Marshal.FreeHGlobal(ResultHandle); Marshal.FreeHGlobal(ResultHandle);
return (matchscore,anglescore); return (matchscore,anglescore);
} }
public bool CuttingAOIArea(Image standardImage,out Bitmap standardAOIImage,out RectangleF rect)
{
standardAOIImage = null;
rect = new RectangleF();
try
{
if (RoiPath == null)
return false;
rect = RoiPath.GetBounds();
if (rect == RectangleF.Empty)
return false;
float MatchRoi_Inflate = ConfigHelper.Config.Get("SURF_MatchRect_Inflate", 0.3f);
var newroi = new RectangleF(rect.Location, rect.Size);
newroi.Inflate(rect.Width * MatchRoi_Inflate, rect.Height * MatchRoi_Inflate);
standardAOIImage= AoiProject.CropBitmap((Bitmap)standardImage, newroi);
}
catch (Exception)
{
return false;
}
return true;
}
} }
} }
...@@ -42,9 +42,8 @@ ...@@ -42,9 +42,8 @@
<Reference Include="Asa.Theme"> <Reference Include="Asa.Theme">
<HintPath>..\dll\Asa.Theme.dll</HintPath> <HintPath>..\dll\Asa.Theme.dll</HintPath>
</Reference> </Reference>
<Reference Include="ConfigHelper, Version=1.0.0.2, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="ConfigHelper">
<SpecificVersion>False</SpecificVersion> <HintPath>..\..\..\..\类库\ConfigHelper.dll</HintPath>
<HintPath>..\..\ConfigHelper\ConfigHelper\bin\Debug\ConfigHelper.dll</HintPath>
</Reference> </Reference>
<Reference Include="MvCameraControl.Net"> <Reference Include="MvCameraControl.Net">
<HintPath>..\dll\MvCameraControl.Net.dll</HintPath> <HintPath>..\dll\MvCameraControl.Net.dll</HintPath>
...@@ -56,7 +55,9 @@ ...@@ -56,7 +55,9 @@
<Reference Include="OpenCvSharp.Extensions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL"> <Reference Include="OpenCvSharp.Extensions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL">
<HintPath>..\packages\OpenCvSharp4.Extensions.4.8.0.20230708\lib\netstandard2.0\OpenCvSharp.Extensions.dll</HintPath> <HintPath>..\packages\OpenCvSharp4.Extensions.4.8.0.20230708\lib\netstandard2.0\OpenCvSharp.Extensions.dll</HintPath>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System">
<HintPath>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.dll</HintPath>
</Reference>
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> <Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath> <HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
</Reference> </Reference>
......
...@@ -32,12 +32,12 @@ ...@@ -32,12 +32,12 @@
// //
// Form1 // Form1
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450); this.ClientSize = new System.Drawing.Size(1067, 562);
this.Margin = new System.Windows.Forms.Padding(4);
this.Name = "Form1"; this.Name = "Form1";
this.Text = "Form1"; this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); this.ResumeLayout(false);
} }
......
...@@ -45,7 +45,7 @@ namespace 园晶斑点分析测试 ...@@ -45,7 +45,7 @@ namespace 园晶斑点分析测试
Cv2.Threshold(threshMat, threshMat, 120, 255, threshType); Cv2.Threshold(threshMat, threshMat, 120, 255, threshType);
Cv2.GaussianBlur(threshMat, threshMat, new OpenCvSharp.Size(5, 5),0); Cv2.GaussianBlur(threshMat, threshMat, new OpenCvSharp.Size(5, 5), 0);
Cv2.AddWeighted(threshMat, 1.5, threshMat, -0.5, 0, threshMat); Cv2.AddWeighted(threshMat, 1.5, threshMat, -0.5, 0, threshMat);
//Cv2.Threshold(threshMat, threshMat, 60, 255, threshType); //Cv2.Threshold(threshMat, threshMat, 60, 255, threshType);
//Cv2.ImShow("ddd", threshMat); //Cv2.ImShow("ddd", threshMat);
...@@ -97,9 +97,9 @@ namespace 园晶斑点分析测试 ...@@ -97,9 +97,9 @@ namespace 园晶斑点分析测试
//cparams.FilterByInertia = true; //cparams.FilterByInertia = true;
//cparams.MinInertiaRatio = 0.01f; //cparams.MinInertiaRatio = 0.01f;
Mat mask = new Mat(threshMat.Size(),MatType.CV_8U,1); Mat mask = new Mat(threshMat.Size(), MatType.CV_8U, 1);
mask.SetTo(new Scalar(0)); mask.SetTo(new Scalar(0));
Cv2.Rectangle(mask,new Rect(1064,152,1356,2424), new Scalar(255), -1); Cv2.Rectangle(mask, new Rect(1064, 152, 1356, 2424), new Scalar(255), -1);
SimpleBlobDetector simpleBlob = SimpleBlobDetector.Create(cparams); SimpleBlobDetector simpleBlob = SimpleBlobDetector.Create(cparams);
var keypoints = simpleBlob.Detect(threshMat, mask); var keypoints = simpleBlob.Detect(threshMat, mask);
...@@ -107,9 +107,9 @@ namespace 园晶斑点分析测试 ...@@ -107,9 +107,9 @@ namespace 园晶斑点分析测试
//Cv2.CvtColor(threshMat, im_with_keypoints, ColorConversionCodes.GRAY2RGB); //Cv2.CvtColor(threshMat, im_with_keypoints, ColorConversionCodes.GRAY2RGB);
Cv2.DrawKeypoints(threshMat, keypoints, srcMat2, Scalar.FromRgb(255, 0, 0), DrawMatchesFlags.DrawOverOutImg); Cv2.DrawKeypoints(threshMat, keypoints, srcMat2, Scalar.FromRgb(255, 0, 0), DrawMatchesFlags.DrawOverOutImg);
Cv2.PutText(srcMat2, $"{keypoints.Length}", new OpenCvSharp.Point(1064, 152), HersheyFonts.HersheySimplex, 4, Scalar.Red,4); Cv2.PutText(srcMat2, $"{keypoints.Length}", new OpenCvSharp.Point(1064, 152), HersheyFonts.HersheySimplex, 4, Scalar.Red, 4);
Cv2.ImShow("bbb", srcMat2); Cv2.ImShow("bbb", srcMat2);
Cv2.ImWrite("d:\\logs\\t10_result.jpg",srcMat2); Cv2.ImWrite("d:\\logs\\t10_result.jpg", srcMat2);
//var dstCutImg = ImageUtil.ToImage(threshMat); //var dstCutImg = ImageUtil.ToImage(threshMat);
} }
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!