From 00f4b9c95dc6b844d63dcd9a9d63d3fc87ec7bcc Mon Sep 17 00:00:00 2001
From: "xhm\\HP" <1173131411@qq.com>
Date: Thu, 27 Jun 2024 17:54:35 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=89=80=E6=9C=89=E5=8C=BA?=
=?UTF-8?q?=E5=9F=9F=E5=8D=B7=E7=A7=AF?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
HisenceYoloDetection/CheckDiffSciHelper.cs | 53 +++++++++--------
HisenceYoloDetection/MainForm.cs | 68 +++++++++++++++-------
2 files changed, 74 insertions(+), 47 deletions(-)
diff --git a/HisenceYoloDetection/CheckDiffSciHelper.cs b/HisenceYoloDetection/CheckDiffSciHelper.cs
index 3dc368f..adbad78 100644
--- a/HisenceYoloDetection/CheckDiffSciHelper.cs
+++ b/HisenceYoloDetection/CheckDiffSciHelper.cs
@@ -1,4 +1,5 @@
using OpenCvSharp;
+using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -14,7 +15,6 @@ namespace HisenceYoloDetection
{
public static class CheckDiffSciHelper
{
-
///
///
///
@@ -22,7 +22,7 @@ namespace HisenceYoloDetection
/// 要对比的图像
/// 白板黑字为true
/// 存储路径
- public static bool CheckDiffSci(string path1, Mat MatDet,Rect sqlrect,Rect detrect, bool IfWhiteWord, string saveDir)
+ public static bool CheckDiffSci(string path1, Mat MatDet, Rect sqlrect, Rect detrect, bool IfWhiteWord, string saveDir)
{
// 读取和处理第一张图片
Mat img1 = Cv2.ImRead(path1, ImreadModes.Color);
@@ -31,31 +31,31 @@ namespace HisenceYoloDetection
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();
Cv2.CvtColor(img1, gimg1, ColorConversionCodes.BGR2GRAY);
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
{
Cv2.Threshold(gimg1, thr1, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);
}
-
-
+
+
// 读取和处理第二张图片
Mat img2 = MatDet.Clone();
if (img2.Empty())
{
- // Console.WriteLine($"Error loading image {path2}");
+ // Console.WriteLine($"Error loading image {path2}");
return false;
}
- // Cv2.Resize(img2, img2, new Size(550, 270));
+ // Cv2.Resize(img2, img2, new Size(550, 270));
Mat gimg2 = new Mat();
Cv2.CvtColor(img2, gimg2, ColorConversionCodes.BGR2GRAY);
Mat thr2 = new Mat();
@@ -68,9 +68,7 @@ namespace HisenceYoloDetection
{
Cv2.Threshold(gimg2, thr2, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);
}
- // Cv2.Threshold(gimg2, thr2, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);
-
-
+
//Rect area2 = new Rect(148,30,229,222);
sqlrect.Width += 20;
@@ -95,22 +93,22 @@ namespace HisenceYoloDetection
}
Cv2.Resize(thr1, thr1, new Size(550, 270));
Cv2.Resize(thr2, thr2, new Size(550, 270));
- 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");
+ 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");
+ 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 filter1 = new Mat(5, 5, MatType.CV_32F, new Scalar(0));
+ filter1.Row(2).SetTo(new Scalar(0.025));
+ filter1.Col(2).SetTo(new Scalar(0.025));
// 应用卷积
Mat final_result1 = new Mat();
@@ -160,7 +158,7 @@ namespace HisenceYoloDetection
Cv2.Dilate(sumIMG, blackhatImg, kernelCL);
// 处理轮廓和保存结果
- Point[][] contours = new Point[10000][];
+ Point[][] contours = new Point[10000][];
Cv2.FindContours(blackhatImg, out contours, out _, RetrievalModes.Tree, ContourApproximationModes.ApproxSimple);
bool isMatch = true;
foreach (var contour in contours)
@@ -169,27 +167,28 @@ namespace HisenceYoloDetection
{
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;
+ isMatch = false;
}
}
- 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);
- 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);
return isMatch;
}
+
public static Rect strChangeRect(string strrect)
{
if (!string.IsNullOrEmpty(strrect))
diff --git a/HisenceYoloDetection/MainForm.cs b/HisenceYoloDetection/MainForm.cs
index ebe060b..50bcf23 100644
--- a/HisenceYoloDetection/MainForm.cs
+++ b/HisenceYoloDetection/MainForm.cs
@@ -195,10 +195,11 @@ namespace HisenceYoloDetection
bool st = ManagerModelHelper.StrMatch2(s1, s2);
- //Rect rect = new Rect(0, 0, 0, 0);
- // string path2 = "D:\\Hisence\\SQLImages\\BatchW81XBModelWNHA62SASUA\\3\\BatchW81XBModelWNHA62SASUAresult.jpg";
- //Mat cameraMat = Cv2.ImRead(path2);
- //bool ifg = CheckDiffSciHelper.CheckDiffSci(path2, cameraMat, rect, rect, false, "D://Hisence//Test");
+ Rect rect = new Rect(0, 0, 0, 0);
+ string path2 = "D:\\Hisence\\SQLImages\\LR1006G4SILVERWJ060480U0SW901N120105\\4\\LR1006G4SILVERWJ060480U0SW901N120105result.jpg";
+ string path23 = "D:\\Hisence\\SQLImages\\LR1006G4SILVERWJ060480U0SW901N120105\\4\\L1.jpg";
+ Mat cameraMat = Cv2.ImRead(path23);
+ bool ifg = CheckDiffSciHelper.CheckDiffSci(path2, cameraMat, rect, rect, false, "D://Hisence//Test");
string vBarPath = "D:\\Hisence\\\\Lp\\202452414231.jpg";
string vCam1OnePath = "D:\\Hisence\\\\Lp\\001.jpg";
@@ -2927,8 +2928,13 @@ namespace HisenceYoloDetection
if (!saveimage)
{
SQLStr = SQlxK_HisenceWord.ThreeblockMainWord;
- OneIF = ManagerModelHelper.StrMatch2(xK_HisenceWord.ThreeblockMainWord, SQlxK_HisenceWord.ThreeblockMainWord);
- if (!OneIF)
+ bool OneIF1 = ManagerModelHelper.StrMatch2(xK_HisenceWord.ThreeblockMainWord, SQlxK_HisenceWord.ThreeblockMainWord);
+
+ Rect rect = new Rect(0, 0, 0, 0);
+ string PathSql = SQlxK_HisenceWord.ThreeblockPath;
+
+ juanjiMatch = CheckDiffSciHelper.CheckDiffSci(PathSql, CutBlockMat, rect, rect, false, "D://Hisence//Test");
+ if (!OneIF1 || !juanjiMatch)
{
DisplayResult(mLcut);
//ʾİ
@@ -2951,8 +2957,13 @@ namespace HisenceYoloDetection
if (!saveimage)
{
SQLStr = SQlxK_HisenceWord.FourblockMainWord;
- OneIF = ManagerModelHelper.StrMatch2(xK_HisenceWord.FourblockMainWord, SQlxK_HisenceWord.FourblockMainWord);
- if (!OneIF)
+ bool OneIF1 = ManagerModelHelper.StrMatch2(xK_HisenceWord.FourblockMainWord, SQlxK_HisenceWord.FourblockMainWord);
+
+ 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)
{
DisplayResult(mLcut);
//ʾİ
@@ -2975,8 +2986,12 @@ namespace HisenceYoloDetection
if (!saveimage)
{
SQLStr = SQlxK_HisenceWord.FiveblockMainWord;
- OneIF = ManagerModelHelper.StrMatch2(xK_HisenceWord.FiveblockMainWord, SQlxK_HisenceWord.FiveblockMainWord);
- if (!OneIF)
+ bool OneIF1 = ManagerModelHelper.StrMatch2(xK_HisenceWord.FiveblockMainWord, SQlxK_HisenceWord.FiveblockMainWord);
+ 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)
{
DisplayResult(mLcut);
//ʾİ
@@ -2998,8 +3013,14 @@ namespace HisenceYoloDetection
if (!saveimage)
{
SQLStr = SQlxK_HisenceWord.SixblockMainWord;
- OneIF = ManagerModelHelper.StrMatch2(xK_HisenceWord.SixblockMainWord, SQlxK_HisenceWord.SixblockMainWord);
- if (!OneIF)
+ 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)
{
// DisplayResult(mLcut);
//ʾİ
@@ -3022,8 +3043,12 @@ namespace HisenceYoloDetection
if (!saveimage)
{
SQLStr = SQlxK_HisenceWord.SevenblockMainWord;
- OneIF = ManagerModelHelper.StrMatch2(xK_HisenceWord.SevenblockMainWord, SQlxK_HisenceWord.SevenblockMainWord);
- if (!OneIF)
+ bool OneIF1 = ManagerModelHelper.StrMatch2(xK_HisenceWord.SevenblockMainWord, SQlxK_HisenceWord.SevenblockMainWord);
+ 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)
{
//DisplayResult(mLcut);
//ʾİ
@@ -3045,8 +3070,12 @@ namespace HisenceYoloDetection
if (!saveimage)
{
SQLStr = SQlxK_HisenceWord.EightblockMainWord;
- OneIF = ManagerModelHelper.StrMatch2(xK_HisenceWord.EightblockMainWord, SQlxK_HisenceWord.EightblockMainWord);
- if (!OneIF)
+ bool OneIF1 = ManagerModelHelper.StrMatch2(xK_HisenceWord.EightblockMainWord, SQlxK_HisenceWord.EightblockMainWord);
+ 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)
{
// DisplayResult(mLcut);
//ʾİ
@@ -3067,10 +3096,9 @@ namespace HisenceYoloDetection
using (StreamWriter sw = new StreamWriter(txtLog, true))
{
- if (blockIndex == "2")
- {
- sw.WriteLine("ƥ" + juanjiMatch);
- }
+
+ sw.WriteLine("" + blockIndex + "ƥ" + juanjiMatch);
+
sw.WriteLine("" + blockIndex + "ݿ" + SQLStr + "ؼ" + MainWoidStr + "" + OneIF + "\n");
sw.WriteLine("" + blockIndex + " " + TextWoidStr + "\n");