测试一二块基本没问题版本

This commit is contained in:
YZJ 2024-06-28 17:50:44 +08:00
parent d3a08b35f7
commit a6d2f87095
4 changed files with 517 additions and 267 deletions

View File

@ -1,4 +1,5 @@
using OpenCvSharp; using OpenCvSharp;
using Sunny.UI.Win32;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -14,7 +15,6 @@ namespace HisenceYoloDetection
{ {
public static class CheckDiffSciHelper public static class CheckDiffSciHelper
{ {
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
@ -31,6 +31,7 @@ namespace HisenceYoloDetection
Console.WriteLine($"Error loading image {path1}"); Console.WriteLine($"Error loading image {path1}");
return false; return false;
} }
// Cv2.Resize(img1, img1, new Size(550, 270));
Mat gimg1 = new Mat(); Mat gimg1 = new Mat();
Cv2.CvtColor(img1, gimg1, ColorConversionCodes.BGR2GRAY); Cv2.CvtColor(img1, gimg1, ColorConversionCodes.BGR2GRAY);
Mat thr1 = new Mat(); Mat thr1 = new Mat();
@ -44,16 +45,21 @@ namespace HisenceYoloDetection
Cv2.Threshold(gimg1, thr1, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu); Cv2.Threshold(gimg1, thr1, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);
} }
// 读取和处理第二张图片 // 读取和处理第二张图片
Mat img2 = MatDet.Clone(); Mat img2 = MatDet.Clone();
if (img2.Empty()) if (img2.Empty())
{ {
// Console.WriteLine($"Error loading image {path2}");
return false; return false;
} }
// Cv2.Resize(img2, img2, new Size(550, 270));
Mat gimg2 = new Mat(); Mat gimg2 = new Mat();
Cv2.CvtColor(img2, gimg2, ColorConversionCodes.BGR2GRAY); Cv2.CvtColor(img2, gimg2, ColorConversionCodes.BGR2GRAY);
Mat thr2 = new Mat(); Mat thr2 = new Mat();
//Cv2.Threshold(gimg2, thr2, 0, 255, ThresholdTypes.BinaryInv | ThresholdTypes.Otsu);
if (IfWhiteWord) if (IfWhiteWord)
{ {
Cv2.Threshold(gimg2, thr2, 0, 255, ThresholdTypes.BinaryInv | ThresholdTypes.Otsu); Cv2.Threshold(gimg2, thr2, 0, 255, ThresholdTypes.BinaryInv | ThresholdTypes.Otsu);
@ -63,61 +69,114 @@ namespace HisenceYoloDetection
Cv2.Threshold(gimg2, thr2, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu); Cv2.Threshold(gimg2, thr2, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);
} }
// 裁剪和设置为黑色
sqlrect.Width += 20;
detrect.Width += 20;
Mat matCutblack1 = new Mat(thr1, sqlrect);
matCutblack1.SetTo(Scalar.Black);
Mat matCutblack2 = new Mat(thr2, detrect);
matCutblack2.SetTo(Scalar.Black);
//Rect area2 = new Rect(148,30,229,222);
sqlrect.Width += 20;
sqlrect.Height += 60;
detrect.Width += 20;
detrect.Height += 60;
Mat matCutblack1 = new Mat(thr1, sqlrect);
if (IfWhiteWord)
{
matCutblack1.SetTo(Scalar.Black);
}
else
{
matCutblack1.SetTo(Scalar.Black);
}
Mat matCutblack2 = new Mat(thr2, detrect);
if (IfWhiteWord)
{
matCutblack2.SetTo(Scalar.Black);
}
else
{
matCutblack2.SetTo(Scalar.Black);
}
Cv2.Resize(thr1, thr1, new Size(550, 270)); Cv2.Resize(thr1, thr1, new Size(550, 270));
Cv2.Resize(thr2, thr2, new Size(550, 270)); Cv2.Resize(thr2, thr2, new Size(550, 270));
DateTime dt = DateTime.Now; DateTime dt = DateTime.Now;
string filename = dt.ToString("yyyyMMddHHmmssfff"); string filename = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Millisecond.ToString();
string savePath4 = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "_thr1.png"); string savePath4 = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "_thr1.png");
// 保存结果
Cv2.ImWrite(savePath4, thr1); Cv2.ImWrite(savePath4, thr1);
string savePath3 = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "_thr2.png"); string savePath3 = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "_thr2.png");
// 保存结果
Cv2.ImWrite(savePath3, thr2); Cv2.ImWrite(savePath3, thr2);
// 创建和应用卷积核 // 创建卷积核
Mat filter1 = new Mat(7, 7, MatType.CV_32F, new Scalar(0.025)); Mat filter1 = new Mat(15, 15, MatType.CV_32F, new Scalar(0));
filter1.Row(7).SetTo(new Scalar(0.025));
filter1.Col(7).SetTo(new Scalar(0.025));
// 应用卷积
Mat final_result1 = new Mat(); Mat final_result1 = new Mat();
Cv2.Filter2D(thr1, final_result1, -1, filter1, anchor: new Point(-1, -1), 0, BorderTypes.Reflect);
Cv2.Filter2D(final_result1, final_result1, -1, filter1, anchor: new Point(-1, -1), 0, BorderTypes.Reflect);
Cv2.Filter2D(final_result1, final_result1, -1, filter1, anchor: new Point(-1, -1), 0, BorderTypes.Reflect);
Mat final_result2 = new Mat(); Mat final_result2 = new Mat();
Cv2.Filter2D(thr1, final_result1, -1, filter1, new Point(-1, -1), 0, BorderTypes.Reflect); Cv2.Filter2D(thr2, final_result2, -1, filter1, anchor: new Point(-1, -1), 0, BorderTypes.Reflect);
Cv2.Filter2D(thr2, final_result2, -1, filter1, new Point(-1, -1), 0, BorderTypes.Reflect); Cv2.Filter2D(final_result2, final_result2, -1, filter1, anchor: new Point(-1, -1), 0, BorderTypes.Reflect);
Cv2.Filter2D(final_result2, final_result2, -1, filter1, anchor: new Point(-1, -1), 0, BorderTypes.Reflect);
//裁剪才行
//string savePath2 = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + "_final_result1.png");
//// 保存结果
////string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png");
//Cv2.ImWrite(savePath2, final_result1);
//string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + "_final_result2.png");
//// 保存结果
////string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png");
//Cv2.ImWrite(savePath, final_result2);
// 计算图像差异 // 计算图像差异
Mat devIMG = new Mat(); Mat devIMG = new Mat();
Mat devIMG_ = new Mat(); Mat devIMG_ = new Mat();
Cv2.Subtract(final_result1, final_result2, devIMG); Cv2.Subtract(final_result1, final_result2, devIMG);
Cv2.Subtract(final_result2, final_result1, devIMG_); Cv2.Subtract(final_result2, final_result1, devIMG_);
string savePathd = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "devIMG.png");
// 保存结果
Cv2.ImWrite(savePathd, devIMG);
string savePathd1 = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "devIMG_.png");
// 保存结果
Cv2.ImWrite(savePathd1, devIMG_);
// 对差异图像应用阈值 // 对差异图像应用阈值
double threshold = 20.0; // 调低阈值 Cv2.Threshold(devIMG, devIMG, 20, 255, ThresholdTypes.Binary);
Cv2.Threshold(devIMG, devIMG, threshold, 255, ThresholdTypes.Binary); Cv2.Threshold(devIMG_, devIMG_, 20, 255, ThresholdTypes.Binary);
Cv2.Threshold(devIMG_, devIMG_, threshold, 255, ThresholdTypes.Binary);
// 结合差异 // 结合差异
Mat sumIMG = new Mat(); Mat sumIMG = new Mat();
Cv2.Add(devIMG, devIMG_, sumIMG); Cv2.Add(devIMG, devIMG_, sumIMG);
// 形态学操作 // 应用形态学操作
Mat kernelCL = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3)); Mat kernelCL = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
Mat blackhatImg = new Mat(); Mat blackhatImg = new Mat();
Cv2.Dilate(sumIMG, blackhatImg, kernelCL); // 使用膨胀操作 Cv2.Dilate(sumIMG, blackhatImg, kernelCL);
// 检测和绘制轮廓 // 处理轮廓和保存结果
Point[][] contours; Point[][] contours = new Point[10000][];
Cv2.FindContours(blackhatImg, out contours, out _, RetrievalModes.Tree, ContourApproximationModes.ApproxSimple); Cv2.FindContours(blackhatImg, out contours, out _, RetrievalModes.Tree, ContourApproximationModes.ApproxSimple);
bool isMatch = true; bool isMatch = true;
foreach (var contour in contours) foreach (var contour in contours)
{ {
if (Cv2.ContourArea(contour) <= 100) if (Cv2.ContourArea(contour) <= 500)
{ {
Cv2.DrawContours(blackhatImg, new Point[][] { contour }, -1, Scalar.Black, thickness: Cv2.FILLED); Cv2.DrawContours(blackhatImg, new Point[][] { contour }, -1, Scalar.Black, thickness: Cv2.FILLED);
// 框选轮廓
} }
else else
{ {
@ -127,16 +186,19 @@ namespace HisenceYoloDetection
} }
} }
// 保存最终结果
string savePath2 = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "_Rect.png"); string savePath2 = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "_Rect.png");
// 保存结果
//string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png");
Cv2.ImWrite(savePath2, img2); Cv2.ImWrite(savePath2, img2);
string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "_diff.png"); string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "_diff.png");
// 保存结果
//string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png");
Cv2.ImWrite(savePath, blackhatImg); Cv2.ImWrite(savePath, blackhatImg);
return isMatch; return isMatch;
} }
public static Rect strChangeRect(string strrect) public static Rect strChangeRect(string strrect)
{ {
if (!string.IsNullOrEmpty(strrect)) if (!string.IsNullOrEmpty(strrect))
@ -149,7 +211,8 @@ namespace HisenceYoloDetection
Rect rect = new Rect(areaX, areaY, areaWidth, areaHeight); Rect rect = new Rect(areaX, areaY, areaWidth, areaHeight);
return rect; return rect;
}else }
else
{ {
return new Rect(0, 0, 0, 0); return new Rect(0, 0, 0, 0);
} }
@ -167,123 +230,198 @@ namespace HisenceYoloDetection
string strrect = rectsql.Join(","); string strrect = rectsql.Join(",");
return strrect; return strrect;
} }
//public static void CheckDiffSci(string path1, string path2, bool IfWhiteWord, string saveDir) public static class CheckDiffSciHelper1
//{ {
// // 读取和处理第一张图片 /// <summary>
// Mat img1 = Cv2.ImRead(path1, ImreadModes.Color); ///
// if (img1.Empty()) /// </summary>
// { /// <param name="path1">标准图像</param>
// Console.WriteLine($"Error loading image {path1}"); /// <param name="path2">要对比的图像</param>
// return; /// <param name="IfWhiteWord"> 白板黑字为true </param>
// } /// <param name="saveDir">存储路径</param>
public static bool CheckDiffSci(string path1, Mat MatDet, Rect sqlrect, Rect detrect, bool IfWhiteWord, string saveDir)
{
// 读取和处理第一张图片
Mat img1 = Cv2.ImRead(path1, ImreadModes.Color);
if (img1.Empty())
{
Console.WriteLine($"Error loading image {path1}");
return false;
}
// Cv2.Resize(img1, img1, new Size(550, 270)); // Cv2.Resize(img1, img1, new Size(550, 270));
// Mat gimg1 = new Mat(); Mat gimg1 = new Mat();
// Cv2.CvtColor(img1, gimg1, ColorConversionCodes.BGR2GRAY); Cv2.CvtColor(img1, gimg1, ColorConversionCodes.BGR2GRAY);
// Mat thr1 = new Mat(); Mat thr1 = new Mat();
// if (IfWhiteWord) if (IfWhiteWord)
// { {
// Cv2.Threshold(gimg1, thr1, 0, 255, ThresholdTypes.BinaryInv | ThresholdTypes.Otsu); Cv2.Threshold(gimg1, thr1, 0, 255, ThresholdTypes.BinaryInv | ThresholdTypes.Otsu);
// } }
// else else
// { {
// Cv2.Threshold(gimg1, thr1, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu); Cv2.Threshold(gimg1, thr1, 100, 255, ThresholdTypes.Binary );//| ThresholdTypes.Otsu
// } }
// string savePath4 = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_thr1.png");
// // 保存结果
// //string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png");
// Cv2.ImWrite(savePath4, thr1);
// // 读取和处理第二张图片
// Mat img2 = Cv2.ImRead(path2, ImreadModes.Color); // 读取和处理第二张图片
// if (img2.Empty()) Mat img2 = MatDet.Clone();
// { if (img2.Empty())
{
// Console.WriteLine($"Error loading image {path2}"); // Console.WriteLine($"Error loading image {path2}");
// return; return false;
// } }
// Cv2.Resize(img2, img2, new Size(550, 270)); // Cv2.Resize(img2, img2, new Size(550, 270));
// Mat gimg2 = new Mat(); Mat gimg2 = new Mat();
// Cv2.CvtColor(img2, gimg2, ColorConversionCodes.BGR2GRAY); Cv2.CvtColor(img2, gimg2, ColorConversionCodes.BGR2GRAY);
// Mat thr2 = new Mat(); Mat thr2 = new Mat();
// //Cv2.Threshold(gimg2, thr2, 0, 255, ThresholdTypes.BinaryInv | ThresholdTypes.Otsu);
// if (IfWhiteWord)
// {
//Cv2.Threshold(gimg2, thr2, 0, 255, ThresholdTypes.BinaryInv | ThresholdTypes.Otsu); //Cv2.Threshold(gimg2, thr2, 0, 255, ThresholdTypes.BinaryInv | ThresholdTypes.Otsu);
// } if (IfWhiteWord)
// else {
// { Cv2.Threshold(gimg2, thr2, 0, 255, ThresholdTypes.BinaryInv | ThresholdTypes.Otsu);
// Cv2.Threshold(gimg2, thr2, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu); }
// } else
// // Cv2.Threshold(gimg2, thr2, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu); {
Cv2.Threshold(gimg2, thr2, 100, 255, ThresholdTypes.Binary );//| ThresholdTypes.Otsu
}
// string savePath3 = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_thr2.png");
//Rect area2 = new Rect(148,30,229,222);
sqlrect.Width += 20;
sqlrect.Height += 20;
detrect.Width += 20;
detrect.Height += 20;
Mat matCutblack1 = new Mat(thr1, sqlrect);
if (IfWhiteWord)
{
matCutblack1.SetTo(Scalar.Black);
}
else
{
matCutblack1.SetTo(Scalar.Black);
}
Mat matCutblack2 = new Mat(thr2, detrect);
if (IfWhiteWord)
{
matCutblack2.SetTo(Scalar.Black);
}
else
{
matCutblack2.SetTo(Scalar.Black);
}
Cv2.Resize(thr1, thr1, new Size(845, 498));
Cv2.Resize(thr2, thr2, new Size(845, 498));
DateTime dt = DateTime.Now;
string filename = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Millisecond.ToString();
string savePath4 = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "_thr1.png");
// 保存结果
Cv2.ImWrite(savePath4, thr1);
string savePath3 = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "_thr2.png");
// 保存结果
Cv2.ImWrite(savePath3, thr2);
// 创建卷积核
Mat filter1 = new Mat(15,15, MatType.CV_32F, new Scalar(0));
filter1.Row(7).SetTo(new Scalar(0.025));
filter1.Col(7).SetTo(new Scalar(0.025));
// 应用卷积
Mat final_result1 = new Mat();
Cv2.Filter2D(thr1, final_result1, -1, filter1, anchor: new Point(-1, -1), 0, BorderTypes.Reflect);
Cv2.Filter2D(final_result1, final_result1, -1, filter1, anchor: new Point(-1, -1), 0, BorderTypes.Reflect);
Cv2.Filter2D(final_result1, final_result1, -1, filter1, anchor: new Point(-1, -1), 0, BorderTypes.Reflect);
//Cv2.Filter2D(final_result1, final_result1, -1, filter2, anchor: new Point(-1, -1), 0, BorderTypes.Reflect);
Mat final_result2 = new Mat();
Cv2.Filter2D(thr2, final_result2, -1, filter1, anchor: new Point(-1, -1), 0, BorderTypes.Reflect);
Cv2.Filter2D(final_result2, final_result2, -1, filter1, anchor: new Point(-1, -1), 0, BorderTypes.Reflect);
Cv2.Filter2D(final_result2, final_result2, -1, filter1, anchor: new Point(-1, -1), 0, BorderTypes.Reflect);
//Cv2.Filter2D(final_result2, final_result2, -1, filter2, anchor: new Point(-1, -1), 0, BorderTypes.Reflect);
//裁剪才行
//string savePath2 = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + "_final_result1.png");
//// 保存结果 //// 保存结果
////string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png"); ////string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png");
// Cv2.ImWrite(savePath3, thr2); //Cv2.ImWrite(savePath2, final_result1);
//string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + "_final_result2.png");
// // 创建卷积核
// Mat filter1 = new Mat(17, 17, MatType.CV_32F, new Scalar(0));
// filter1.Row(8).SetTo(new Scalar(0.025));
// filter1.Col(8).SetTo(new Scalar(0.025));
// // 应用卷积
// Mat final_result1 = new Mat();
// Cv2.Filter2D(thr1, final_result1, -1, filter1, anchor: new Point(-1, -1), 0, BorderTypes.Reflect);
// Cv2.Filter2D(final_result1, final_result1, -1, filter1, anchor: new Point(-1, -1), 0, BorderTypes.Reflect);
// Cv2.Filter2D(final_result1, final_result1, -1, filter1, anchor: new Point(-1, -1), 0, BorderTypes.Reflect);
// Mat final_result2 = new Mat();
// Cv2.Filter2D(thr2, final_result2, -1, filter1, anchor: new Point(-1, -1), 0, BorderTypes.Reflect);
// Cv2.Filter2D(final_result2, final_result2, -1, filter1, anchor: new Point(-1, -1), 0, BorderTypes.Reflect);
// Cv2.Filter2D(final_result2, final_result2, -1, filter1, anchor: new Point(-1, -1), 0, BorderTypes.Reflect);
// // 计算图像差异
// Mat devIMG = new Mat();
// Mat devIMG_ = new Mat();
// Cv2.Subtract(final_result1, final_result2, devIMG);
// Cv2.Subtract(final_result2, final_result1, devIMG_);
// // 对差异图像应用阈值
// Cv2.Threshold(devIMG, devIMG, 50, 255, ThresholdTypes.Binary);
// Cv2.Threshold(devIMG_, devIMG_, 50, 255, ThresholdTypes.Binary);
// // 结合差异
// Mat sumIMG = new Mat();
// Cv2.Add(devIMG, devIMG_, sumIMG);
// // 应用形态学操作
// Mat kernelCL = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
// Mat blackhatImg = new Mat();
// Cv2.Dilate(sumIMG, blackhatImg, kernelCL);
// // 处理轮廓和保存结果
// Point[][] contours = new Point[10000][];
// Cv2.FindContours(blackhatImg, out contours, out _, RetrievalModes.Tree, ContourApproximationModes.ApproxSimple);
// foreach (var contour in contours)
// {
// if (Cv2.ContourArea(contour) <= 100)
// {
// Cv2.DrawContours(blackhatImg, new Point[][] { contour }, -1, Scalar.Black, thickness: Cv2.FILLED);
// // 框选轮廓
// }
// else
// {
// Rect boundingRect = Cv2.BoundingRect(contour);
// Cv2.Rectangle(img2, boundingRect, Scalar.Red, thickness: 2);
// }
// }
// string savePath2 = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_Rect.png");
//// 保存结果 //// 保存结果
////string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png"); ////string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png");
// Cv2.ImWrite(savePath2, img2); //Cv2.ImWrite(savePath, final_result2);
// 计算图像差异
Mat devIMG = new Mat();
Mat devIMG_ = new Mat();
Cv2.Subtract(final_result1, final_result2, devIMG);
Cv2.Subtract(final_result2, final_result1, devIMG_);
string savePathd = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "devIMG.png");
// 保存结果
Cv2.ImWrite(savePathd, devIMG);
string savePathd1 = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "devIMG_.png");
// 保存结果
Cv2.ImWrite(savePathd1, devIMG_);
// 对差异图像应用阈值
Cv2.Threshold(devIMG, devIMG, 45, 255, ThresholdTypes.Binary);
Cv2.Threshold(devIMG_, devIMG_, 45, 255, ThresholdTypes.Binary);
// 结合差异
Mat sumIMG = new Mat();
Cv2.Add(devIMG, devIMG_, sumIMG);
string savePaths = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "sumIMG.png");
// 保存结果
Cv2.ImWrite(savePaths, sumIMG);
// 应用形态学操作
Mat kernelCL = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
Mat blackhatImg = new Mat();
Cv2.Dilate(sumIMG, blackhatImg, kernelCL);
// 处理轮廓和保存结果
Point[][] contours = new Point[10000][];
Cv2.FindContours(blackhatImg, out contours, out _, RetrievalModes.Tree, ContourApproximationModes.ApproxSimple);
bool isMatch = true;
foreach (var contour in contours)
{
if (Cv2.ContourArea(contour) <= 100)
{
Cv2.DrawContours(blackhatImg, new Point[][] { contour }, -1, Scalar.Black, thickness: Cv2.FILLED);
// 框选轮廓
}
else
{
Rect boundingRect = Cv2.BoundingRect(contour);
Cv2.Rectangle(img2, boundingRect, Scalar.Red, thickness: 2);
isMatch = false;
}
}
string savePath2 = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "_Rect.png");
// 保存结果
//string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png"); //string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png");
// // 保存结果 Cv2.ImWrite(savePath2, img2);
// //string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png"); string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "_diff.png");
// Cv2.ImWrite(savePath, blackhatImg); // 保存结果
//} //string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png");
Cv2.ImWrite(savePath, blackhatImg);
return isMatch;
}
}
} }
} }

View File

@ -31,13 +31,13 @@
components = new System.ComponentModel.Container(); components = new System.ComponentModel.Container();
OpenCvSharp.Mat mat1 = new OpenCvSharp.Mat(); OpenCvSharp.Mat mat1 = new OpenCvSharp.Mat();
OpenCvSharp.Mat mat2 = new OpenCvSharp.Mat(); OpenCvSharp.Mat mat2 = new OpenCvSharp.Mat();
OpenCvSharp.Mat mat3 = new OpenCvSharp.Mat();
OpenCvSharp.Mat mat4 = new OpenCvSharp.Mat();
OpenCvSharp.Mat mat5 = new OpenCvSharp.Mat();
OpenCvSharp.Mat mat6 = new OpenCvSharp.Mat(); OpenCvSharp.Mat mat6 = new OpenCvSharp.Mat();
OpenCvSharp.Mat mat7 = new OpenCvSharp.Mat(); OpenCvSharp.Mat mat7 = new OpenCvSharp.Mat();
OpenCvSharp.Mat mat8 = new OpenCvSharp.Mat(); OpenCvSharp.Mat mat8 = new OpenCvSharp.Mat();
OpenCvSharp.Mat mat5 = new OpenCvSharp.Mat();
OpenCvSharp.Mat mat9 = new OpenCvSharp.Mat(); OpenCvSharp.Mat mat9 = new OpenCvSharp.Mat();
OpenCvSharp.Mat mat4 = new OpenCvSharp.Mat();
OpenCvSharp.Mat mat3 = new OpenCvSharp.Mat();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm)); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
tabPage2 = new TabPage(); tabPage2 = new TabPage();
panel1 = new Panel(); panel1 = new Panel();
@ -75,6 +75,8 @@
label12 = new Label(); label12 = new Label();
label11 = new Label(); label11 = new Label();
ModeleShow = new GroupBox(); ModeleShow = new GroupBox();
handStopBtn = new Button();
HandRuningBtn = new Button();
ZBackwardrbx = new RadioButton(); ZBackwardrbx = new RadioButton();
YBackwardrbx = new RadioButton(); YBackwardrbx = new RadioButton();
XBackwardrbx = new RadioButton(); XBackwardrbx = new RadioButton();
@ -128,7 +130,8 @@
tabPage5 = new TabPage(); tabPage5 = new TabPage();
panel4 = new Panel(); panel4 = new Panel();
panel5 = new Panel(); panel5 = new Panel();
DefetShow5 = new XKRS.UI.Canvas(); DefetShow7 = new XKRS.UI.Canvas();
DefetShow6 = new XKRS.UI.Canvas();
panel8 = new Panel(); panel8 = new Panel();
= new Label(); = new Label();
OKOrNGShow = new PictureBox(); OKOrNGShow = new PictureBox();
@ -142,11 +145,12 @@
ScanDevList = new Button(); ScanDevList = new Button();
DevNameCombo = new ComboBox(); DevNameCombo = new ComboBox();
InitMachine = new Button(); InitMachine = new Button();
DefetShow2 = new XKRS.UI.Canvas();
panel6 = new Panel(); panel6 = new Panel();
groupBox2 = new GroupBox(); groupBox2 = new GroupBox();
DefetShow5 = new XKRS.UI.Canvas();
DefetShow4 = new XKRS.UI.Canvas(); DefetShow4 = new XKRS.UI.Canvas();
DefetShow3 = new XKRS.UI.Canvas(); DefetShow3 = new XKRS.UI.Canvas();
DefetShow2 = new XKRS.UI.Canvas();
originMat2Show = new PictureBox(); originMat2Show = new PictureBox();
DefetShow1 = new XKRS.UI.Canvas(); DefetShow1 = new XKRS.UI.Canvas();
ResultMatShow2 = new PictureBox(); ResultMatShow2 = new PictureBox();
@ -198,8 +202,6 @@
richTextBox1 = new RichTextBox(); richTextBox1 = new RichTextBox();
contextMenuStrip1 = new ContextMenuStrip(components); contextMenuStrip1 = new ContextMenuStrip(components);
backgroundWorker1 = new System.ComponentModel.BackgroundWorker(); backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
DefetShow6 = new XKRS.UI.Canvas();
DefetShow7 = new XKRS.UI.Canvas();
tabPage2.SuspendLayout(); tabPage2.SuspendLayout();
panel1.SuspendLayout(); panel1.SuspendLayout();
groupBox5.SuspendLayout(); groupBox5.SuspendLayout();
@ -563,6 +565,8 @@
// //
// ModeleShow // ModeleShow
// //
ModeleShow.Controls.Add(handStopBtn);
ModeleShow.Controls.Add(HandRuningBtn);
ModeleShow.Controls.Add(ZBackwardrbx); ModeleShow.Controls.Add(ZBackwardrbx);
ModeleShow.Controls.Add(YBackwardrbx); ModeleShow.Controls.Add(YBackwardrbx);
ModeleShow.Controls.Add(XBackwardrbx); ModeleShow.Controls.Add(XBackwardrbx);
@ -582,6 +586,26 @@
ModeleShow.TabStop = false; ModeleShow.TabStop = false;
ModeleShow.Text = "模组显示"; ModeleShow.Text = "模组显示";
// //
// handStopBtn
//
handStopBtn.Location = new Point(266, 266);
handStopBtn.Name = "handStopBtn";
handStopBtn.Size = new Size(125, 82);
handStopBtn.TabIndex = 23;
handStopBtn.Text = "手动阻塞";
handStopBtn.UseVisualStyleBackColor = true;
handStopBtn.Click += handStopBtn_Click;
//
// HandRuningBtn
//
HandRuningBtn.Location = new Point(410, 266);
HandRuningBtn.Name = "HandRuningBtn";
HandRuningBtn.Size = new Size(125, 82);
HandRuningBtn.TabIndex = 22;
HandRuningBtn.Text = "手动放行";
HandRuningBtn.UseVisualStyleBackColor = true;
HandRuningBtn.Click += HandRuningBtn_Click;
//
// ZBackwardrbx // ZBackwardrbx
// //
ZBackwardrbx.Appearance = Appearance.Button; ZBackwardrbx.Appearance = Appearance.Button;
@ -1142,26 +1166,47 @@
panel5.Size = new Size(1276, 762); panel5.Size = new Size(1276, 762);
panel5.TabIndex = 0; panel5.TabIndex = 0;
// //
// DefetShow5 // DefetShow7
// //
DefetShow5.AllowSelectDefect = true; DefetShow7.AllowSelectDefect = true;
DefetShow5.BrushColor = null; DefetShow7.BrushColor = null;
DefetShow5.ImagePath = ""; DefetShow7.ImagePath = "";
mat6.IsEnabledDispose = true; mat3.IsEnabledDispose = true;
DefetShow5.ImaMAt = mat6; DefetShow7.ImaMAt = mat3;
DefetShow5.ImgData = null; DefetShow7.ImgData = null;
DefetShow5.Location = new Point(537, 460); DefetShow7.Location = new Point(1021, 464);
DefetShow5.MoveStep = 5F; DefetShow7.MoveStep = 5F;
DefetShow5.Name = "DefetShow5"; DefetShow7.Name = "DefetShow7";
DefetShow5.Route = 0D; DefetShow7.Route = 0D;
DefetShow5.Scale = 1D; DefetShow7.Scale = 1D;
DefetShow5.Size = new Size(209, 196); DefetShow7.Size = new Size(209, 196);
DefetShow5.TabIndex = 10; DefetShow7.TabIndex = 12;
DefetShow5.WholeScale = 0F; DefetShow7.WholeScale = 0F;
DefetShow5.XMove = 0F; DefetShow7.XMove = 0F;
DefetShow5.XScale = 0F; DefetShow7.XScale = 0F;
DefetShow5.YMove = 0F; DefetShow7.YMove = 0F;
DefetShow5.YScale = 0F; DefetShow7.YScale = 0F;
//
// DefetShow6
//
DefetShow6.AllowSelectDefect = true;
DefetShow6.BrushColor = null;
DefetShow6.ImagePath = "";
mat4.IsEnabledDispose = true;
DefetShow6.ImaMAt = mat4;
DefetShow6.ImgData = null;
DefetShow6.Location = new Point(751, 464);
DefetShow6.MoveStep = 5F;
DefetShow6.Name = "DefetShow6";
DefetShow6.Route = 0D;
DefetShow6.Scale = 1D;
DefetShow6.Size = new Size(209, 196);
DefetShow6.TabIndex = 11;
DefetShow6.WholeScale = 0F;
DefetShow6.XMove = 0F;
DefetShow6.XScale = 0F;
DefetShow6.YMove = 0F;
DefetShow6.YScale = 0F;
// //
// panel8 // panel8
// //
@ -1290,6 +1335,27 @@
InitMachine.UseVisualStyleBackColor = true; InitMachine.UseVisualStyleBackColor = true;
InitMachine.Click += InitMachine_Click; InitMachine.Click += InitMachine_Click;
// //
// DefetShow2
//
DefetShow2.AllowSelectDefect = true;
DefetShow2.BrushColor = null;
DefetShow2.ImagePath = "";
mat5.IsEnabledDispose = true;
DefetShow2.ImaMAt = mat5;
DefetShow2.ImgData = null;
DefetShow2.Location = new Point(850, 249);
DefetShow2.MoveStep = 5F;
DefetShow2.Name = "DefetShow2";
DefetShow2.Route = 0D;
DefetShow2.Scale = 1D;
DefetShow2.Size = new Size(285, 196);
DefetShow2.TabIndex = 9;
DefetShow2.WholeScale = 0F;
DefetShow2.XMove = 0F;
DefetShow2.XScale = 0F;
DefetShow2.YMove = 0F;
DefetShow2.YScale = 0F;
//
// panel6 // panel6
// //
panel6.Controls.Add(groupBox2); panel6.Controls.Add(groupBox2);
@ -1318,6 +1384,27 @@
groupBox2.Text = "图片"; groupBox2.Text = "图片";
groupBox2.Enter += groupBox2_Enter; groupBox2.Enter += groupBox2_Enter;
// //
// DefetShow5
//
DefetShow5.AllowSelectDefect = true;
DefetShow5.BrushColor = null;
DefetShow5.ImagePath = "";
mat6.IsEnabledDispose = true;
DefetShow5.ImaMAt = mat6;
DefetShow5.ImgData = null;
DefetShow5.Location = new Point(537, 460);
DefetShow5.MoveStep = 5F;
DefetShow5.Name = "DefetShow5";
DefetShow5.Route = 0D;
DefetShow5.Scale = 1D;
DefetShow5.Size = new Size(209, 196);
DefetShow5.TabIndex = 10;
DefetShow5.WholeScale = 0F;
DefetShow5.XMove = 0F;
DefetShow5.XScale = 0F;
DefetShow5.YMove = 0F;
DefetShow5.YScale = 0F;
//
// DefetShow4 // DefetShow4
// //
DefetShow4.AllowSelectDefect = true; DefetShow4.AllowSelectDefect = true;
@ -1360,27 +1447,6 @@
DefetShow3.YMove = 0F; DefetShow3.YMove = 0F;
DefetShow3.YScale = 0F; DefetShow3.YScale = 0F;
// //
// DefetShow2
//
DefetShow2.AllowSelectDefect = true;
DefetShow2.BrushColor = null;
DefetShow2.ImagePath = "";
mat5.IsEnabledDispose = true;
DefetShow2.ImaMAt = mat5;
DefetShow2.ImgData = null;
DefetShow2.Location = new Point(850, 249);
DefetShow2.MoveStep = 5F;
DefetShow2.Name = "DefetShow2";
DefetShow2.Route = 0D;
DefetShow2.Scale = 1D;
DefetShow2.Size = new Size(285, 196);
DefetShow2.TabIndex = 9;
DefetShow2.WholeScale = 0F;
DefetShow2.XMove = 0F;
DefetShow2.XScale = 0F;
DefetShow2.YMove = 0F;
DefetShow2.YScale = 0F;
//
// originMat2Show // originMat2Show
// //
originMat2Show.Location = new Point(574, 21); originMat2Show.Location = new Point(574, 21);
@ -1817,48 +1883,6 @@
contextMenuStrip1.Name = "contextMenuStrip1"; contextMenuStrip1.Name = "contextMenuStrip1";
contextMenuStrip1.Size = new Size(61, 4); contextMenuStrip1.Size = new Size(61, 4);
// //
// DefetShow6
//
DefetShow6.AllowSelectDefect = true;
DefetShow6.BrushColor = null;
DefetShow6.ImagePath = "";
mat4.IsEnabledDispose = true;
DefetShow6.ImaMAt = mat4;
DefetShow6.ImgData = null;
DefetShow6.Location = new Point(751, 464);
DefetShow6.MoveStep = 5F;
DefetShow6.Name = "DefetShow6";
DefetShow6.Route = 0D;
DefetShow6.Scale = 1D;
DefetShow6.Size = new Size(209, 196);
DefetShow6.TabIndex = 11;
DefetShow6.WholeScale = 0F;
DefetShow6.XMove = 0F;
DefetShow6.XScale = 0F;
DefetShow6.YMove = 0F;
DefetShow6.YScale = 0F;
//
// DefetShow7
//
DefetShow7.AllowSelectDefect = true;
DefetShow7.BrushColor = null;
DefetShow7.ImagePath = "";
mat3.IsEnabledDispose = true;
DefetShow7.ImaMAt = mat3;
DefetShow7.ImgData = null;
DefetShow7.Location = new Point(1021, 464);
DefetShow7.MoveStep = 5F;
DefetShow7.Name = "DefetShow7";
DefetShow7.Route = 0D;
DefetShow7.Scale = 1D;
DefetShow7.Size = new Size(209, 196);
DefetShow7.TabIndex = 12;
DefetShow7.WholeScale = 0F;
DefetShow7.XMove = 0F;
DefetShow7.XScale = 0F;
DefetShow7.YMove = 0F;
DefetShow7.YScale = 0F;
//
// MainForm // MainForm
// //
AutoScaleDimensions = new SizeF(7F, 17F); AutoScaleDimensions = new SizeF(7F, 17F);
@ -2056,7 +2080,7 @@
private Button openModelBtn; private Button openModelBtn;
private TextBox modelChangeCbx; private TextBox modelChangeCbx;
private Label label34; private Label label34;
private Button button1; private Button HandRuningBtn;
private Label label33; private Label label33;
private CheckBox WhiteBanCbx; private CheckBox WhiteBanCbx;
private XKRS.UI.Canvas DefetShow1; private XKRS.UI.Canvas DefetShow1;
@ -2069,5 +2093,6 @@
private PictureBox originMat2Show; private PictureBox originMat2Show;
private XKRS.UI.Canvas DefetShow7; private XKRS.UI.Canvas DefetShow7;
private XKRS.UI.Canvas DefetShow6; private XKRS.UI.Canvas DefetShow6;
private Button handStopBtn;
} }
} }

View File

@ -6,6 +6,7 @@ using OpenCvSharp.Dnn;
using OpenCvSharp.Extensions; using OpenCvSharp.Extensions;
using OpenCvSharp.XFeatures2D; using OpenCvSharp.XFeatures2D;
using Sunny.UI; using Sunny.UI;
using Sunny.UI.Win32;
using System; using System;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
@ -24,6 +25,7 @@ using System.Text.RegularExpressions;
using System.Windows.Forms; using System.Windows.Forms;
using XKRS.Device.SimboVision.SimboHelper; using XKRS.Device.SimboVision.SimboHelper;
using XKRS.UI; using XKRS.UI;
using static HisenceYoloDetection.CheckDiffSciHelper;
using static HisenceYoloDetection.MainForm; using static HisenceYoloDetection.MainForm;
using static System.Runtime.InteropServices.JavaScript.JSType; using static System.Runtime.InteropServices.JavaScript.JSType;
using static System.Windows.Forms.VisualStyles.VisualStyleElement; using static System.Windows.Forms.VisualStyles.VisualStyleElement;
@ -190,10 +192,11 @@ namespace HisenceYoloDetection
private void MainForm_Load(object sender, EventArgs e) private void MainForm_Load(object sender, EventArgs e)
{ {
string s1 = "0##Ec04060##20¡æ##Rapide15mns"; string s1 = "gorenjeSTEAMTECHSNOWFLAKEDRUM65kg";
string s2 = "ECOmns"; string s2 = "STEAMTECHSNOWFLAKEDRUM65kg";
bool st = ManagerModelHelper.StrMatch(s1, s2); bool st = ManagerModelHelper.StrMatch(s1, s2);
bool st1 = ManagerModelHelper.StrMatch2(s1, s2);
//Rect rect = new Rect(0, 0, 0, 0); //Rect rect = new Rect(0, 0, 0, 0);
//string path2 = "D:\\Hisence\\SQLImages\\W80WXWJ060539V0WW80WX120293\\2\\W80WXWJ060539V0WW80WX120293result.jpg"; //string path2 = "D:\\Hisence\\SQLImages\\W80WXWJ060539V0WW80WX120293\\2\\W80WXWJ060539V0WW80WX120293result.jpg";
@ -594,10 +597,10 @@ namespace HisenceYoloDetection
melsecPLCTCPDriver.WriteInt("514", 0);//定位松开 melsecPLCTCPDriver.WriteInt("514", 0);//定位松开
melsecPLCTCPDriver.WriteInt("524", 0);//定位松开 melsecPLCTCPDriver.WriteInt("524", 0);//定位松开
melsecPLCTCPDriver.WriteInt("406", 60000); //写入手动速度 melsecPLCTCPDriver.WriteInt("406", 60000); //写入手动速度
melsecPLCTCPDriver.WriteInt("416", 8000); melsecPLCTCPDriver.WriteInt("416", 10000);
melsecPLCTCPDriver.WriteInt("426", 10000); melsecPLCTCPDriver.WriteInt("426", 10000);
melsecPLCTCPDriver.WriteInt("404", 100000); //写入定位速度 melsecPLCTCPDriver.WriteInt("404", 100000); //写入定位速度
melsecPLCTCPDriver.WriteInt("414", 8000); melsecPLCTCPDriver.WriteInt("414", 10000);
melsecPLCTCPDriver.WriteInt("424", 10000); melsecPLCTCPDriver.WriteInt("424", 10000);
@ -612,6 +615,7 @@ namespace HisenceYoloDetection
Thread.Sleep(200); Thread.Sleep(200);
melsecPLCTCPDriver.WriteInt("580", 0); melsecPLCTCPDriver.WriteInt("580", 0);
Thread.Sleep(200); Thread.Sleep(200);
melsecPLCTCPDriver.WriteInt("550", 0);
while (true) while (true)
{ {
int IfXZeros = melsecPLCTCPDriver.ReadInt("102"); int IfXZeros = melsecPLCTCPDriver.ReadInt("102");
@ -1039,6 +1043,7 @@ namespace HisenceYoloDetection
#endregion #endregion
int YPos = 8715; int YPos = 8715;
int ZPos = 25000; int ZPos = 25000;
bool StopM = false;//第一次用信号挡
/// <summary> /// <summary>
/// 监听洗衣机544是否触发后面对射传感器 相机 /// 监听洗衣机544是否触发后面对射传感器 相机
/// </summary> /// </summary>
@ -1081,6 +1086,7 @@ namespace HisenceYoloDetection
else else
{ {
iNum1 = 0; iNum1 = 0;
melsecPLCTCPDriver.WriteInt("550", 1);//阻塞
} }
if (iNum1 >= 2) if (iNum1 >= 2)
{ {
@ -1088,6 +1094,13 @@ namespace HisenceYoloDetection
{ {
#if true #if true
if(!StopM)
{
melsecPLCTCPDriver.WriteInt("550", 1);//阻塞
StopM = true;
}
myLog("第一次采集图像", DateTime.Now); myLog("第一次采集图像", DateTime.Now);
//移动模组 读三次测距的地址 看是否往前移动 //移动模组 读三次测距的地址 看是否往前移动
MoveToP(5815, YPos, ZPos); MoveToP(5815, YPos, ZPos);
@ -1129,6 +1142,9 @@ namespace HisenceYoloDetection
MoveToP(51043, YPos, ZPos + (int)(dismove)); MoveToP(51043, YPos, ZPos + (int)(dismove));
Cam1.Snapshot(); Cam1.Snapshot();
MoveToP(5815, YPos, ZPos); MoveToP(5815, YPos, ZPos);
melsecPLCTCPDriver.WriteInt("550", 0);//方行
//Thread.Sleep(300);//洗衣机可能过不去
//if (xKNow != null) //if (xKNow != null)
//{ //{
// MoveToP(xKNow.MoveX, xKNow.MoveY, xKNow.MoveZ); // MoveToP(xKNow.MoveX, xKNow.MoveY, xKNow.MoveZ);
@ -1342,7 +1358,7 @@ namespace HisenceYoloDetection
sw.WriteLine(IOcrBAr + "\n"); sw.WriteLine(IOcrBAr + "\n");
sw.Flush(); sw.Flush();
} }
IOcrBAr = "W821PWMS27106WD2"; IOcrBAr = "BatchW96DHModelWF3S7021BWPl";
//根据条码数据库比对 //根据条码数据库比对
xKNow = GetModeFromBar(IOcrBAr);//从数据库中查询到这个条码的四轴的值 xKNow = GetModeFromBar(IOcrBAr);//从数据库中查询到这个条码的四轴的值
List<XK_HisenceWord> xkWordList = ManagerModelHelper.GetModeWordFromBar(IOcrBAr); List<XK_HisenceWord> xkWordList = ManagerModelHelper.GetModeWordFromBar(IOcrBAr);
@ -1503,7 +1519,7 @@ namespace HisenceYoloDetection
bBarTriger = false; bBarTriger = false;
melsecPLCTCPDriver.WriteInt("550", 0);//方行
if (MatchStr) if (MatchStr)
{ {
OKDsums++; OKDsums++;
@ -1795,6 +1811,24 @@ namespace HisenceYoloDetection
private void StartDecBtn_Click(object sender, EventArgs e) private void StartDecBtn_Click(object sender, EventArgs e)
{ {
/*
string path22 = "D:\\Hisence\\Test2\\BatchW96c0ModelWNHB6X2SDSPresult2024628924614_Rect.png";
Mat pathmat2 = Cv2.ImRead(path22);
string path222 = "D:\\Hisence\\Test2\\BatchW96c0ModelWNHB6X2SDSPresult2024628923102_Rect.png";
Rect rect1 = new Rect(1929, 745, 396, 377);
Rect rect = new Rect(368, 53, 396, 377);
CheckDiffSciHelper.CheckDiffSci(path222, pathmat2, rect, rect, true, "D://Hisence//Test2");
string path = "D:\\Hisence\\Test1\\1.png";
Mat pathmat =Cv2.ImRead(path);
string path2 = "D:\\Hisence\\Test1\\2.png";
Rect rect2 = new Rect(0, 0, 0, 0);
CheckDiffSciHelper1.CheckDiffSci(path2, pathmat, rect2, rect2, false, "D://Hisence//Test1");
*/
//OnGetBar(); //OnGetBar();
TriggerCameral2(); TriggerCameral2();
ReadyDetect(); ReadyDetect();
@ -2865,11 +2899,14 @@ namespace HisenceYoloDetection
xK_HisenceWord.OneblockPath = CutSavePath; xK_HisenceWord.OneblockPath = CutSavePath;
xK_HisenceWord.OneblockMainWord = MainWoidStr; xK_HisenceWord.OneblockMainWord = MainWoidStr;
xK_HisenceWord.OneblockText = TextWoidStr; xK_HisenceWord.OneblockText = TextWoidStr;
SQLStr = SQlxK_HisenceWord.OneblockMainWord; SQLStr = SQlxK_HisenceWord.OneblockText;
string SQLStr2 = SQlxK_HisenceWord.OneblockMainWord;
if (!saveimage) if (!saveimage)
{ {
bool ssD= ManagerModelHelper.StrMatch(xK_HisenceWord.OneblockMainWord, SQLStr); bool ssD = ManagerModelHelper.StrMatch(SQLStr, xK_HisenceWord.OneblockText);
bool OneIF1 = ManagerModelHelper.StrMatch2(xK_HisenceWord.OneblockMainWord, SQLStr); Console.Write(SQLStr);
bool OneIF1 = ManagerModelHelper.StrMatch2(xK_HisenceWord.OneblockMainWord, SQLStr2);
if (!OneIF1 || !ssD) if (!OneIF1 || !ssD)
{ {
OneIF = false; OneIF = false;
@ -2930,16 +2967,26 @@ namespace HisenceYoloDetection
if (!saveimage) if (!saveimage)
{ {
SQLStr = SQlxK_HisenceWord.ThreeblockMainWord; SQLStr = SQlxK_HisenceWord.ThreeblockMainWord;
OneIF = ManagerModelHelper.StrMatch2(xK_HisenceWord.ThreeblockMainWord, SQlxK_HisenceWord.ThreeblockMainWord); bool OneIF1 = ManagerModelHelper.StrMatch2(xK_HisenceWord.ThreeblockMainWord, SQlxK_HisenceWord.ThreeblockMainWord);
if (!OneIF)
Rect rect = new Rect(0, 0, 0, 0);
string PathSql = SQlxK_HisenceWord.ThreeblockPath;
juanjiMatch = CheckDiffSciHelper1.CheckDiffSci(PathSql, CutBlockMat, rect, rect, false, "D://Hisence//Test1");
if (!OneIF1 || !juanjiMatch)
{ {
OneIF = true;//待修改6.28
/*
DisplayResult(mLcut); DisplayResult(mLcut);
//显示错误的板 //显示错误的板
// mLcut.ResultMap; // mLcut.ResultMap;
Defet_OnDetectionDone(mLcut.ResultMap.ToMat(), 3); Defet_OnDetectionDone(mLcut.ResultMap.ToMat(), 3);
*/
Defet_OnDetectionDone(OKMat, 3);
} }
else else
{ {
OneIF = true;
Defet_OnDetectionDone(OKMat, 3); Defet_OnDetectionDone(OKMat, 3);
} }
@ -2954,9 +3001,15 @@ namespace HisenceYoloDetection
if (!saveimage) if (!saveimage)
{ {
SQLStr = SQlxK_HisenceWord.FourblockMainWord; SQLStr = SQlxK_HisenceWord.FourblockMainWord;
OneIF = ManagerModelHelper.StrMatch2(xK_HisenceWord.FourblockMainWord, SQlxK_HisenceWord.FourblockMainWord); bool OneIF1 = ManagerModelHelper.StrMatch2(xK_HisenceWord.FourblockMainWord, SQlxK_HisenceWord.FourblockMainWord);
if (!OneIF)
Rect rect = new Rect(0, 0, 0, 0);
string PathSql = SQlxK_HisenceWord.FourblockPath;
juanjiMatch = CheckDiffSciHelper.CheckDiffSci(PathSql, CutBlockMat, rect, rect, false, "D://Hisence//Test");
if (!OneIF1 || !juanjiMatch)
{ {
OneIF = false;
DisplayResult(mLcut); DisplayResult(mLcut);
//显示错误的板 //显示错误的板
// mLcut.ResultMap; // mLcut.ResultMap;
@ -2964,6 +3017,7 @@ namespace HisenceYoloDetection
} }
else else
{ {
OneIF = true;
Defet_OnDetectionDone(OKMat, 4); Defet_OnDetectionDone(OKMat, 4);
} }
@ -2978,9 +3032,14 @@ namespace HisenceYoloDetection
if (!saveimage) if (!saveimage)
{ {
SQLStr = SQlxK_HisenceWord.FiveblockMainWord; SQLStr = SQlxK_HisenceWord.FiveblockMainWord;
OneIF = ManagerModelHelper.StrMatch2(xK_HisenceWord.FiveblockMainWord, SQlxK_HisenceWord.FiveblockMainWord); bool OneIF1 = ManagerModelHelper.StrMatch2(xK_HisenceWord.FiveblockMainWord, SQlxK_HisenceWord.FiveblockMainWord);
if (!OneIF) Rect rect = new Rect(0, 0, 0, 0);
string PathSql = SQlxK_HisenceWord.FiveblockMainWord;
juanjiMatch = CheckDiffSciHelper.CheckDiffSci(PathSql, CutBlockMat, rect, rect, false, "D://Hisence//Test");
if (!OneIF1 || !juanjiMatch)
{ {
OneIF = false;
DisplayResult(mLcut); DisplayResult(mLcut);
//显示错误的板 //显示错误的板
// mLcut.ResultMap; // mLcut.ResultMap;
@ -2988,6 +3047,7 @@ namespace HisenceYoloDetection
} }
else else
{ {
OneIF = true;
Defet_OnDetectionDone(OKMat, 5); Defet_OnDetectionDone(OKMat, 5);
} }
} }
@ -3001,9 +3061,16 @@ namespace HisenceYoloDetection
if (!saveimage) if (!saveimage)
{ {
SQLStr = SQlxK_HisenceWord.SixblockMainWord; SQLStr = SQlxK_HisenceWord.SixblockMainWord;
OneIF = ManagerModelHelper.StrMatch2(xK_HisenceWord.SixblockMainWord, SQlxK_HisenceWord.SixblockMainWord); bool OneIF1 = ManagerModelHelper.StrMatch2(xK_HisenceWord.SixblockMainWord, SQlxK_HisenceWord.SixblockMainWord);
Rect rect = new Rect(0, 0, 0, 0);
string PathSql = SQlxK_HisenceWord.SixblockPath;
juanjiMatch = CheckDiffSciHelper.CheckDiffSci(PathSql, CutBlockMat, rect, rect, false, "D://Hisence//Test");
if (!OneIF1 || !juanjiMatch)
if (!OneIF) if (!OneIF)
{ {
OneIF = false;
// DisplayResult(mLcut); // DisplayResult(mLcut);
//显示错误的板 //显示错误的板
// mLcut.ResultMap; // mLcut.ResultMap;
@ -3011,6 +3078,7 @@ namespace HisenceYoloDetection
} }
else else
{ {
OneIF = true;
Defet_OnDetectionDone(OKMat, 6); Defet_OnDetectionDone(OKMat, 6);
} }
@ -3025,9 +3093,14 @@ namespace HisenceYoloDetection
if (!saveimage) if (!saveimage)
{ {
SQLStr = SQlxK_HisenceWord.SevenblockMainWord; SQLStr = SQlxK_HisenceWord.SevenblockMainWord;
OneIF = ManagerModelHelper.StrMatch2(xK_HisenceWord.SevenblockMainWord, SQlxK_HisenceWord.SevenblockMainWord); bool OneIF1 = ManagerModelHelper.StrMatch2(xK_HisenceWord.SevenblockMainWord, SQlxK_HisenceWord.SevenblockMainWord);
if (!OneIF) Rect rect = new Rect(0, 0, 0, 0);
string PathSql = SQlxK_HisenceWord.SevenblockPath;
juanjiMatch = CheckDiffSciHelper.CheckDiffSci(PathSql, CutBlockMat, rect, rect, false, "D://Hisence//Test");
if (!OneIF1 || !juanjiMatch)
{ {
OneIF = false;
//DisplayResult(mLcut); //DisplayResult(mLcut);
//显示错误的板 //显示错误的板
// mLcut.ResultMap; // mLcut.ResultMap;
@ -3035,6 +3108,7 @@ namespace HisenceYoloDetection
} }
else else
{ {
OneIF = true;
Defet_OnDetectionDone(OKMat, 7); Defet_OnDetectionDone(OKMat, 7);
} }
} }
@ -3048,9 +3122,14 @@ namespace HisenceYoloDetection
if (!saveimage) if (!saveimage)
{ {
SQLStr = SQlxK_HisenceWord.EightblockMainWord; SQLStr = SQlxK_HisenceWord.EightblockMainWord;
OneIF = ManagerModelHelper.StrMatch2(xK_HisenceWord.EightblockMainWord, SQlxK_HisenceWord.EightblockMainWord); bool OneIF1 = ManagerModelHelper.StrMatch2(xK_HisenceWord.EightblockMainWord, SQlxK_HisenceWord.EightblockMainWord);
if (!OneIF) Rect rect = new Rect(0, 0, 0, 0);
string PathSql = SQlxK_HisenceWord.EightblockPath;
juanjiMatch = CheckDiffSciHelper.CheckDiffSci(PathSql, CutBlockMat, rect, rect, false, "D://Hisence//Test");
if (!OneIF1 || !juanjiMatch)
{ {
OneIF = false;
// DisplayResult(mLcut); // DisplayResult(mLcut);
//显示错误的板 //显示错误的板
// mLcut.ResultMap; // mLcut.ResultMap;
@ -3058,6 +3137,7 @@ namespace HisenceYoloDetection
} }
else else
{ {
OneIF = true;
Defet_OnDetectionDone(OKMat, 8); Defet_OnDetectionDone(OKMat, 8);
} }
} }
@ -3070,10 +3150,7 @@ namespace HisenceYoloDetection
using (StreamWriter sw = new StreamWriter(txtLog, true)) using (StreamWriter sw = new StreamWriter(txtLog, true))
{ {
if (blockIndex == "2") sw.WriteLine("块" + blockIndex + "卷积匹配" + juanjiMatch);
{
sw.WriteLine("¾í»ýÆ¥Åä" + juanjiMatch);
}
sw.WriteLine("块" + blockIndex + "数据库" + SQLStr + "关键字" + MainWoidStr + "结果" + OneIF + "\n"); sw.WriteLine("块" + blockIndex + "数据库" + SQLStr + "关键字" + MainWoidStr + "结果" + OneIF + "\n");
sw.WriteLine("块" + blockIndex + " 所有字 " + TextWoidStr + "\n"); sw.WriteLine("块" + blockIndex + " 所有字 " + TextWoidStr + "\n");
@ -3125,5 +3202,15 @@ namespace HisenceYoloDetection
myLog("X轴停止归零", DateTime.Now); myLog("X轴停止归零", DateTime.Now);
} }
} }
private void handStopBtn_Click(object sender, EventArgs e)
{
melsecPLCTCPDriver.WriteInt("550", 1);
}
private void HandRuningBtn_Click(object sender, EventArgs e)
{
melsecPLCTCPDriver.WriteInt("550", 0);
}
} }
} }

View File

@ -189,7 +189,7 @@ namespace HisenceYoloDetection
// 计算相似度相似度等于1减去标准化的Levenshtein距离 // 计算相似度相似度等于1减去标准化的Levenshtein距离
double similarity = 1 - ((double)distance / Math.Max(SqlText.Length, DetText.Length)); double similarity = 1 - ((double)distance / Math.Max(SqlText.Length, DetText.Length));
bool areEqual = false; bool areEqual = false;
if (similarity < 0.5) if (similarity < 0.9)
{ {
areEqual = false; areEqual = false;
} }