From 618c7998187421b7571785a88553a2b822f6a958 Mon Sep 17 00:00:00 2001 From: 820689062 <820689062@qq.com> Date: Wed, 24 Jul 2024 10:48:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=97=8B=E9=92=AE=E8=AF=86?= =?UTF-8?q?=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HisenceYoloDetection/CheckDiffSciHelper.cs | 44 +++-- HisenceYoloDetection/MainForm.cs | 185 +++++++++++++-------- 2 files changed, 140 insertions(+), 89 deletions(-) diff --git a/HisenceYoloDetection/CheckDiffSciHelper.cs b/HisenceYoloDetection/CheckDiffSciHelper.cs index e4f0827..aa1b589 100644 --- a/HisenceYoloDetection/CheckDiffSciHelper.cs +++ b/HisenceYoloDetection/CheckDiffSciHelper.cs @@ -10,7 +10,7 @@ using System.Threading.Tasks; using static System.Net.Mime.MediaTypeNames; using Point = OpenCvSharp.Point; using Size = OpenCvSharp.Size; - +using System; using OpenCvSharp; using OpenCvSharp.Features2D; using OpenCvSharp.Flann; @@ -20,6 +20,8 @@ namespace HisenceYoloDetection { public static class CheckDiffSciHelper { + + public static Mat ProcessImage(Mat image, Rect fillRect) { // 获取图像尺寸 @@ -229,20 +231,6 @@ namespace HisenceYoloDetection { Cv2.DrawContours(blackhatImg, new Point[][] { contour }, -1, Scalar.Black, thickness: Cv2.FILLED); // 框选轮廓 - string savePath2 = Path.Combine("D:\\Hisence\\Test\\2\\ng", Path.GetFileNameWithoutExtension(path1) + filename + "_Rect.png"); - // 保存结果 - //string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png"); - Cv2.ImWrite(savePath2, img2); - string savePath = Path.Combine("D:\\Hisence\\Test\\2\\ng", Path.GetFileNameWithoutExtension(path1) + filename + "_diff.png"); - // 保存结果 - //string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png"); - Cv2.ImWrite(savePath, blackhatImg); - } - else - { - Rect boundingRect = Cv2.BoundingRect(contour); - Cv2.Rectangle(img2, boundingRect, Scalar.Red, thickness: 2); - isMatch = false; string savePath2 = Path.Combine("D:\\Hisence\\Test\\2\\ok", Path.GetFileNameWithoutExtension(path1) + filename + "_Rect.png"); // 保存结果 //string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png"); @@ -252,9 +240,33 @@ namespace HisenceYoloDetection //string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png"); Cv2.ImWrite(savePath, blackhatImg); } + else + { + Rect boundingRect = Cv2.BoundingRect(contour); + Cv2.Rectangle(img2, boundingRect, Scalar.Red, thickness: 2); + isMatch = false; + string savePath2 = Path.Combine("D:\\Hisence\\Test\\2\\ng", Path.GetFileNameWithoutExtension(path1) + filename + "_Rect.png"); + // 保存结果 + //string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png"); + Cv2.ImWrite(savePath2, img2); + string savePath = Path.Combine("D:\\Hisence\\Test\\2\\ng", Path.GetFileNameWithoutExtension(path1) + filename + "_diff.png"); + // 保存结果 + //string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png"); + Cv2.ImWrite(savePath, blackhatImg); + } + } + + // 新增的白色面积占比判断 + double whiteArea1 = Cv2.CountNonZero(thr1); + double whiteArea2 = Cv2.CountNonZero(thr2); + double ratio1 = whiteArea1 / (thr1.Rows * thr1.Cols); + double ratio2 = whiteArea2 / (thr2.Rows * thr2.Cols); + + if (Math.Abs(ratio1 - ratio2) >= 0.9) + { + isMatch = true; } - return isMatch; } diff --git a/HisenceYoloDetection/MainForm.cs b/HisenceYoloDetection/MainForm.cs index 1d17cd9..987bcca 100644 --- a/HisenceYoloDetection/MainForm.cs +++ b/HisenceYoloDetection/MainForm.cs @@ -1605,25 +1605,7 @@ namespace HisenceYoloDetection //2һ //if (IfCam2Triger) { - // IfCam2Triger = false; - //OCRʶ - //MLRequest req = new MLRequest(); - //req.currentMat = Cam2ImgShowBar; - ////req.currentMat = Cv2.ImRead("D:\\Hisence\\\\1\\bar.jpg"); - ////ʶַ - //string IOcrBAr = DetMachineBar(ref req); - //DateTime dt = DateTime.Now; - //using (StreamWriter sw = new StreamWriter("D://Hisence//logsBar.log", true)) - //{ - // string filename = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Millisecond.ToString(); - // sw.WriteLine(filename + "\n"); - // sw.WriteLine(IOcrBAr + "\n"); - // sw.Flush(); - //} - // IOcrBAr = "BatchW9659ModelWNHPI74SCPSDE"; - // IOcrBAr = "W821PWMS27106WD2"; - // IOcrBAr= //ݿȶ _runHandleAfter.Reset(); if (xKNow == null) @@ -1654,22 +1636,7 @@ namespace HisenceYoloDetection if (xKNow.Detect != "") { - ////ʱеϴ»Ǻ֮ǰһģ - //if (IfChangeLanguage == IOcrBAr) - //{ - //} - //else - //{ - // //شOCR.josn - // if (File.Exists(xKNow.OcrParm)) - // { - // paddleOcrModel.Load(xKNow.OcrParm, "CPU"); - - // IfChangeLanguage = IOcrBAr; - // } - - //} myLog("ͺƥɹ" + IOcrBAr, DateTime.Now); } @@ -1689,10 +1656,10 @@ namespace HisenceYoloDetection Defet_OnDetectionDone(whiteMat, 1); Defet_OnDetectionDone(whiteMat, 2); Defet_OnDetectionDone(whiteMat, 3); - Defet_OnDetectionDone(whiteMat, 4); - Defet_OnDetectionDone(whiteMat, 5); - Defet_OnDetectionDone(whiteMat, 6); - Defet_OnDetectionDone(whiteMat, 7); + //Defet_OnDetectionDone(whiteMat, 4); + //Defet_OnDetectionDone(whiteMat, 5); + //Defet_OnDetectionDone(whiteMat, 6); + //Defet_OnDetectionDone(whiteMat, 7); XK_HisenceWord xK_MatchDet = new XK_HisenceWord(); xK_MatchDet.TwoIFWhile = xK_HisenceSQLWord.TwoIFWhile; @@ -1818,48 +1785,48 @@ namespace HisenceYoloDetection OKOrNGShow.Image = NGbitmap; })); - //melsecPLCTCPDriver.WriteInt(RedLightingAdress, 1);// - //melsecPLCTCPDriver.WriteInt(YellowLightingAdress, 0);//Ƶ - //melsecPLCTCPDriver.WriteInt(GreenLightingAdress, 0);//̵ - //melsecPLCTCPDriver.WriteInt(WaringAdress, 1);// + melsecPLCTCPDriver.WriteInt(RedLightingAdress, 1);// + melsecPLCTCPDriver.WriteInt(YellowLightingAdress, 0);//Ƶ + melsecPLCTCPDriver.WriteInt(GreenLightingAdress, 0);//̵ + melsecPLCTCPDriver.WriteInt(WaringAdress, 1);// - ////ΪжǷNGϴ» - //DialogResult dr = MessageBox.Show("Ƿ죿", "Ƿ", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); - //if (dr == DialogResult.OK) - //{ + //ΪжǷNGϴ» + DialogResult dr = MessageBox.Show("Ƿ죿", "Ƿ", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); + if (dr == DialogResult.OK) + { - // melsecPLCTCPDriver.WriteInt(RedLightingAdress, 0);// - // melsecPLCTCPDriver.WriteInt(GreenLightingAdress, 1);//̵ - // melsecPLCTCPDriver.WriteInt(WaringAdress, 0);// + melsecPLCTCPDriver.WriteInt(RedLightingAdress, 0);// + melsecPLCTCPDriver.WriteInt(GreenLightingAdress, 1);//̵ + melsecPLCTCPDriver.WriteInt(WaringAdress, 0);// - // OKDsums++; - // WUsums++; - // this.Invoke(new Action(() => - // { - // double percent = (double)WUsums / AllDsums; - // string percentText = percent.ToString("0.0%");//percentTextֵΪ10.0% - // textBox1.Text = percentText; - // OKOrNGShow.Image = OKbitmap; - // })); - // myLog("ƥʧ", DateTime.Now); - //} - //else - //{ - // NGDsums++; + OKDsums++; + WUsums++; + this.Invoke(new Action(() => + { + double percent = (double)WUsums / AllDsums; + string percentText = percent.ToString("0.0%");//percentTextֵΪ10.0% + textBox1.Text = percentText; + OKOrNGShow.Image = OKbitmap; + })); + myLog("ƥʧ", DateTime.Now); + } + else + { + NGDsums++; - // melsecPLCTCPDriver.WriteInt(WaringAdress, 0);// - // melsecPLCTCPDriver.WriteInt(RedLightingAdress, 0);// - // melsecPLCTCPDriver.WriteInt(GreenLightingAdress, 1);//̵ + melsecPLCTCPDriver.WriteInt(WaringAdress, 0);// + melsecPLCTCPDriver.WriteInt(RedLightingAdress, 0);// + melsecPLCTCPDriver.WriteInt(GreenLightingAdress, 1);//̵ - // this.Invoke(new Action(() => - // { - // OKOrNGShow.Image = NGbitmap; - // })); - // myLog("ƥɹ", DateTime.Now); - //} + this.Invoke(new Action(() => + { + OKOrNGShow.Image = NGbitmap; + })); + myLog("ƥɹ", DateTime.Now); + } @@ -3423,6 +3390,7 @@ namespace HisenceYoloDetection Rect rectsql = CheckDiffSciHelper.strChangeRect(SQlxK_HisenceWord.TwoRect); Rect rectDet = CheckDiffSciHelper.strChangeRect(xK_HisenceWord.TwoRect); + juanjiMatch = CheckDiffSciHelper.CheckDiffSci(PathSql, CutBlockMat, rectsql, rectDet, (bool)SQlxK_HisenceWord.TwoIFWhile, "D://Hisence//Test"); // juanjiMatch = true; @@ -3454,8 +3422,8 @@ namespace HisenceYoloDetection Rect rect = new Rect(0, 0, 0, 0); string PathSql = SQlxK_HisenceWord.ThreeblockPath; - - juanjiMatch = CheckDiffSciHelper1.CheckDiffSci(PathSql, CutBlockMat, rect, rect, false, "D://Hisence//Test1"); + bool iswhite = IsMostlyWhite(PathSql); + juanjiMatch = CheckDiffSciHelper1.CheckDiffSci(PathSql, CutBlockMat, rect, rect, iswhite, "D://Hisence//Test1"); if (!OneIF1 || !juanjiMatch) { OneIF = true;//޸6.28 @@ -3644,6 +3612,72 @@ namespace HisenceYoloDetection } + static bool IsMostlyWhite(string imagePath) + { + Bitmap bitmap = new Bitmap(imagePath); + int width = bitmap.Width; + int height = bitmap.Height; + + int blackCount = 0; + int whiteCount = 0; + + for (int y = 0; y < height; y++) + { + for (int x = 0; x < width; x++) + { + Color pixelColor = bitmap.GetPixel(x, y); + int r = pixelColor.R; + int g = pixelColor.G; + int b = pixelColor.B; + + // жǷΪɫ + if (r <= 70 && g <= 70 && b <= 70) + { + blackCount++; + } + else + { + whiteCount++; + } + } + } + + int totalPixels = width * height; + double blackRatio = (double)blackCount / totalPixels; + + // ɫڵ0.6false򷵻true + return blackRatio < 0.6; + } + static string matchBtnColor(Mat img, Rect rect) + { + // ȡָ + Mat roi = new Mat(img, rect); + + // ƽɫ + Scalar mean = Cv2.Mean(roi); + + // ȡƽɫRGBֵ + double r = mean.Val2; + double g = mean.Val1; + double b = mean.Val0; + + // ƽҶֵ + double averageGray = (r + g + b) / 3; + + // жɫ + if (averageGray < 60) + { + return "ɫ"; + } + else if (averageGray >= 60 && averageGray <= 150) + { + return "ɫ"; + } + else + { + return "ɫ"; + } + } private void bnGetParam2_Click_1(object sender, EventArgs e) { @@ -3765,5 +3799,10 @@ namespace HisenceYoloDetection { } + + private void label27_Click(object sender, EventArgs e) + { + + } } }