Commit a7bd6f52 贾鹏旭

添加修正的aoi检测

1 个父辈 e7461058
......@@ -36,7 +36,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="ConfigHelper">
<HintPath>..\..\ConfigHelper\ConfigHelper\bin\Debug\ConfigHelper.dll</HintPath>
<HintPath>..\..\..\..\类库\ConfigHelper.dll</HintPath>
</Reference>
<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>
......
......@@ -79,7 +79,7 @@ namespace AOI
}
else
{
ResultBean resultBean = method.Check(standardImage, image);
ResultBean resultBean = method.Check(standardImage,image);
resultBean.roiPath = method.RoiPath;
resultBean.labelKey = item.Key;
resultBeans.Add(resultBean);
......@@ -146,12 +146,14 @@ namespace AOI
imageFilePath = filePath.Replace( extension, ".bmp");
return imageFilePath;
}
public static String FilePath = null;
/// <summary>
/// 加载项目
/// </summary>
/// <param name="filePath"></param>
public static AoiProject Load(string filePath, out string msg)
{
FilePath = filePath;
Thread.Sleep(1);
GC.Collect();
msg = "";
......@@ -233,7 +235,6 @@ namespace AOI
}
public Bitmap ProcessTestImage(Bitmap targetimage, string name)
{
var markroi = BaseROI;
if (markroi == null || markroi.RoiPath == null || markroi.RoiPath.GetBounds() == RectangleF.Empty)
{
......@@ -406,8 +407,6 @@ namespace AOI
if (resultmat != null)
resultmat.Dispose();
}
}
public static bool Star_MatchTemplate(Bitmap basebmp, Bitmap targetbmp, RectangleF rectangleF, out Bitmap matchBitmap)
......
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
......@@ -9,6 +11,7 @@ using System.Text;
using System.Threading.Tasks;
using OpenCvSharp;
using OpenCvSharp.Blob;
using OpenCvSharp.ImgHash;
using OpenCvSharp.XFeatures2D;
using static AOI.Eyemlib;
......@@ -32,8 +35,13 @@ namespace AOI
bool needCut = true;
Image standardRoiImg = GetRoiImage(standardImage, needCut);
resultBean.standardRoiImage = standardRoiImg;
double percent, angle;
(percent,angle) = GetTemplateMatchPercent(standardImage, imageToCheck, out Image cutImg);
double percent=0d, angle=0d;
Image cutImg = null;
//将基准图切割成小块
if (CuttingAOIArea(standardImage, out Bitmap standardAOIImage, out RectangleF rect))
{
(percent, angle) = GetTemplateMatchPercent(standardAOIImage, imageToCheck,rect ,out cutImg);
}
bool result = false;
if(SamePercent > 100)
{
......@@ -44,9 +52,41 @@ namespace AOI
{
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.result = result;
resultBean.percentValue =Math.Round( percent,3);
resultBean.percentValue = Math.Round(percent, 3);
resultBean.checkData = angle;
return resultBean;
}
......@@ -58,19 +98,19 @@ namespace AOI
/// <param name="imageToCheck"></param>
/// <param name="cutImg"></param>
/// <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;
if (RoiPath == null)
return (0d, 0d);
var rect = RoiPath.GetBounds();
if (rect == RectangleF.Empty)
return (0d, 0d);
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);
var bitmap1 = AoiProject.CropBitmap((Bitmap)standardImage, newroi);
var template = Eyemlib.eyemCvtToEyemImage(bitmap1);
//if (RoiPath == null)
// return (0d, 0d);
//var rect = RoiPath.GetBounds();
//if (rect == RectangleF.Empty)
// return (0d, 0d);
//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);
//var bitmap1 = AoiProject.CropBitmap((Bitmap)standardImage, newroi);
var template = Eyemlib.eyemCvtToEyemImage((Bitmap)standardAOIImage);
var search = Eyemlib.eyemCvtToEyemImage((Bitmap)imageToCheck);
int iNumMatches = 5;//目标个数
......@@ -139,5 +179,28 @@ namespace AOI
Marshal.FreeHGlobal(ResultHandle);
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 @@
<Reference Include="Asa.Theme">
<HintPath>..\dll\Asa.Theme.dll</HintPath>
</Reference>
<Reference Include="ConfigHelper, Version=1.0.0.2, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\ConfigHelper\ConfigHelper\bin\Debug\ConfigHelper.dll</HintPath>
<Reference Include="ConfigHelper">
<HintPath>..\..\..\..\类库\ConfigHelper.dll</HintPath>
</Reference>
<Reference Include="MvCameraControl.Net">
<HintPath>..\dll\MvCameraControl.Net.dll</HintPath>
......@@ -56,7 +55,9 @@
<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>
</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">
<HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
</Reference>
......
......@@ -32,12 +32,12 @@
//
// 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.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.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
......
......@@ -31,7 +31,7 @@ namespace 园晶斑点分析测试
this.WindowState = FormWindowState.Minimized;
var src = System.Drawing.Image.FromFile("d:\\logs\\t10_2.jpg");
var src2 = System.Drawing.Image.FromFile("d:\\logs\\t10.jpg");
//var src = System.Drawing.Image.FromFile("d:\\hbyj.png");
//var bck = System.Drawing.Image.FromFile("d:\\blank.png");
Mat srcMat = ImageUtil.ToMat(src);
......@@ -42,10 +42,10 @@ namespace 园晶斑点分析测试
Mat threshMat = new Mat();
Cv2.CvtColor(srcMat, threshMat, ColorConversionCodes.RGB2GRAY);
ThresholdTypes threshType = ThresholdTypes.Binary;
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.Threshold(threshMat, threshMat, 60, 255, threshType);
//Cv2.ImShow("ddd", threshMat);
......@@ -97,19 +97,19 @@ namespace 园晶斑点分析测试
//cparams.FilterByInertia = true;
//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));
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);
var keypoints = simpleBlob.Detect(threshMat, mask);
////Mat im_with_keypoints = new Mat();
//Cv2.CvtColor(threshMat, im_with_keypoints, ColorConversionCodes.GRAY2RGB);
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.ImWrite("d:\\logs\\t10_result.jpg",srcMat2);
Cv2.ImWrite("d:\\logs\\t10_result.jpg", srcMat2);
//var dstCutImg = ImageUtil.ToImage(threshMat);
}
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!