From 33fd534579815804c585e8d8af0da4ac7f8d2538 Mon Sep 17 00:00:00 2001 From: 820689062 <820689062@qq.com> Date: Tue, 30 Jul 2024 09:17:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=9B=BE=E5=83=8F=E5=A4=A7?= =?UTF-8?q?=E5=B0=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HisenceYoloDetection/CheckDiffSciHelper.cs | 74 +++++++++++++++------- HisenceYoloDetection/MainForm.Designer.cs | 26 ++++---- HisenceYoloDetection/MainForm.cs | 27 ++------ 3 files changed, 71 insertions(+), 56 deletions(-) diff --git a/HisenceYoloDetection/CheckDiffSciHelper.cs b/HisenceYoloDetection/CheckDiffSciHelper.cs index 41d1e64..be8911a 100644 --- a/HisenceYoloDetection/CheckDiffSciHelper.cs +++ b/HisenceYoloDetection/CheckDiffSciHelper.cs @@ -154,14 +154,14 @@ namespace HisenceYoloDetection 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 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(savePath4, thr1); + //string savePath3 = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "_thr2.png"); // 保存结果 - Cv2.ImWrite(savePath3, thr2); + //Cv2.ImWrite(savePath3, thr2); // 创建卷积核 Mat filter1 = new Mat(15, 15, MatType.CV_32F, new Scalar(0)); @@ -232,14 +232,14 @@ namespace HisenceYoloDetection { Cv2.DrawContours(blackhatImg, new Point[][] { contour }, -1, Scalar.Black, thickness: Cv2.FILLED); // 框选轮廓 - string savePath2 = Path.Combine("D:\\Hisence\\Test\\2\\ok", Path.GetFileNameWithoutExtension(path1) + filename + "_Rect.png"); + //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"); - Cv2.ImWrite(savePath2, img2); - string savePath = Path.Combine("D:\\Hisence\\Test\\2\\ok", Path.GetFileNameWithoutExtension(path1) + filename + "_diff.png"); + //Cv2.ImWrite(savePath2, img2); + //string savePath = Path.Combine("D:\\Hisence\\Test\\2\\ok", Path.GetFileNameWithoutExtension(path1) + filename + "_diff.png"); // 保存结果 //string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png"); - Cv2.ImWrite(savePath, blackhatImg); + // Cv2.ImWrite(savePath, blackhatImg); } else { @@ -251,11 +251,13 @@ namespace HisenceYoloDetection // 保存结果 //string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png"); Cv2.ImWrite(savePath2, img2); + CheckDiffSciHelper1.ResizeImage(savePath2, savePath2, 640, 480, 75); + ResultMat = img2.Clone(); - string savePath = Path.Combine("D:\\Hisence\\Test\\2\\ng", Path.GetFileNameWithoutExtension(path1) + filename + "_diff.png"); + //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); + //Cv2.ImWrite(savePath, blackhatImg); } } @@ -394,14 +396,14 @@ namespace HisenceYoloDetection 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 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(savePath4, thr1); + // string savePath3 = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "_thr2.png"); // 保存结果 - Cv2.ImWrite(savePath3, thr2); + // Cv2.ImWrite(savePath3, thr2); // 创建卷积核 @@ -448,14 +450,14 @@ namespace HisenceYoloDetection 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"); + //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(savePathd, devIMG); + //string savePathd1 = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "devIMG_.png"); // 保存结果 - Cv2.ImWrite(savePathd1, devIMG_); + //Cv2.ImWrite(savePathd1, devIMG_); // 对差异图像应用阈值 Cv2.Threshold(devIMG, devIMG, 8, 255, ThresholdTypes.Binary); Cv2.Threshold(devIMG_, devIMG_, 8, 255, ThresholdTypes.Binary); @@ -463,10 +465,10 @@ namespace HisenceYoloDetection // 结合差异 Mat sumIMG = new Mat(); Cv2.Add(devIMG, devIMG_, sumIMG); - string savePaths = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "sumIMG.png"); + //string savePaths = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "sumIMG.png"); // 保存结果 - Cv2.ImWrite(savePaths, sumIMG); + //Cv2.ImWrite(savePaths, sumIMG); // 应用形态学操作 Mat kernelCL = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3)); Mat blackhatImg = new Mat(); @@ -496,12 +498,40 @@ namespace HisenceYoloDetection // 保存结果 //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"); + ResizeImage(savePath2, savePath2, 640, 480, 75); + //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; } + + public static void ResizeImage(string inputPath, string outputPath, int newWidth, int newHeight, int quality) + { + // 加载原始图像 + using (Mat originalImage = Cv2.ImRead(inputPath)) + { + // 创建一个Mat对象用于存储缩放后的图像 + using (Mat resizedImage = new Mat()) + { + // 缩放图像 + Cv2.Resize(originalImage, resizedImage, new OpenCvSharp.Size(newWidth, newHeight)); + + // 保存图像为JPEG格式,并设置压缩质量 + SaveJpeg(outputPath, resizedImage, quality); + Console.WriteLine($"Image saved to {outputPath}"); + } + } + } + + static void SaveJpeg(string path, Mat image, int quality) + { + // 设置JPEG编码参数 + var encodeParams = new[] { new ImageEncodingParam(ImwriteFlags.JpegQuality, quality) }; + + // 保存图像 + Cv2.ImWrite(path, image, encodeParams); + } static Mat RemoveBorders(Mat image) { // 将图像转换为灰度图 diff --git a/HisenceYoloDetection/MainForm.Designer.cs b/HisenceYoloDetection/MainForm.Designer.cs index 8f6e326..c6f9581 100644 --- a/HisenceYoloDetection/MainForm.Designer.cs +++ b/HisenceYoloDetection/MainForm.Designer.cs @@ -126,6 +126,7 @@ DevNameCombo = new ComboBox(); ScanDevList = new Button(); groupBox2 = new GroupBox(); + pictureBox1 = new PictureBox(); canvas3 = new XKRS.UI.Canvas(); canvas2 = new XKRS.UI.Canvas(); canvas1 = new XKRS.UI.Canvas(); @@ -194,7 +195,6 @@ timer6 = new System.Windows.Forms.Timer(components); richTextBox1 = new RichTextBox(); backgroundWorker1 = new System.ComponentModel.BackgroundWorker(); - pictureBox1 = new PictureBox(); tabPage2.SuspendLayout(); panel1.SuspendLayout(); groupBox5.SuspendLayout(); @@ -209,6 +209,7 @@ panel5.SuspendLayout(); groupBox9.SuspendLayout(); groupBox2.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit(); ((System.ComponentModel.ISupportInitialize)originMatShow).BeginInit(); ((System.ComponentModel.ISupportInitialize)ResultMatShow).BeginInit(); panel8.SuspendLayout(); @@ -219,7 +220,6 @@ tabPage3.SuspendLayout(); panel2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)InsertDataDgv).BeginInit(); - ((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit(); SuspendLayout(); // // tabPage2 @@ -1261,6 +1261,16 @@ groupBox2.Text = "结果展示"; groupBox2.Enter += groupBox2_Enter; // + // pictureBox1 + // + pictureBox1.Location = new Point(367, 454); + pictureBox1.Margin = new Padding(4, 2, 4, 2); + pictureBox1.Name = "pictureBox1"; + pictureBox1.Size = new Size(291, 202); + pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; + pictureBox1.TabIndex = 12; + pictureBox1.TabStop = false; + // // canvas3 // canvas3.AllowSelectDefect = true; @@ -1954,16 +1964,6 @@ richTextBox1.TabIndex = 1; richTextBox1.Text = ""; // - // pictureBox1 - // - pictureBox1.Location = new Point(367, 454); - pictureBox1.Margin = new Padding(4, 2, 4, 2); - pictureBox1.Name = "pictureBox1"; - pictureBox1.Size = new Size(284, 202); - pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; - pictureBox1.TabIndex = 12; - pictureBox1.TabStop = false; - // // MainForm // AutoScaleDimensions = new SizeF(12F, 25F); @@ -1999,6 +1999,7 @@ panel5.PerformLayout(); groupBox9.ResumeLayout(false); groupBox2.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)pictureBox1).EndInit(); ((System.ComponentModel.ISupportInitialize)originMatShow).EndInit(); ((System.ComponentModel.ISupportInitialize)ResultMatShow).EndInit(); panel8.ResumeLayout(false); @@ -2013,7 +2014,6 @@ panel2.ResumeLayout(false); panel2.PerformLayout(); ((System.ComponentModel.ISupportInitialize)InsertDataDgv).EndInit(); - ((System.ComponentModel.ISupportInitialize)pictureBox1).EndInit(); ResumeLayout(false); } diff --git a/HisenceYoloDetection/MainForm.cs b/HisenceYoloDetection/MainForm.cs index 8ab00af..bc56670 100644 --- a/HisenceYoloDetection/MainForm.cs +++ b/HisenceYoloDetection/MainForm.cs @@ -578,6 +578,7 @@ namespace HisenceYoloDetection MLRequest req = new MLRequest(); string path = "D:\\Hisence\\\\12\\202461417146451.jpg"; req.currentMat = Cv2.ImRead(path); + string BarT = DetMachineBar(ref req); // @@ -1554,10 +1555,10 @@ namespace HisenceYoloDetection } - + string pathfile = fitImageFolder + dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Millisecond.ToString() + "1.jpg"; //CamShow1.ImaMAt = cameraMat; - cameraMat.ImWrite(fitImageFolder + dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Millisecond.ToString() + "1.jpg"); - + cameraMat.ImWrite(pathfile); + CheckDiffSciHelper1.ResizeImage(pathfile,pathfile,640,480,75); } /// @@ -1566,24 +1567,7 @@ namespace HisenceYoloDetection /// /// /// - public void OnCameraMatOutPut2(DateTime dt, Mat cameraMat, int SnapshotCount) - { - string fitImageFolder = "D://cam2//"; - if (!Directory.Exists(fitImageFolder)) - { - Directory.CreateDirectory(fitImageFolder); - } - - // Mat mat = Cv2.ImRead("F:\\ϴ»\\cam1\\2024517161641.jpg"); - Cam2ImgShowBar = cameraMat; - IfCam2Triger = true; - - //originMat2Show.Image = cameraMat.ToBitmap(); - //_runHandleAfter.Set(); - // CamShow2.ImaMAt = cameraMat; - cameraMat.ImWrite(fitImageFolder + dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Millisecond.ToString() + "1.jpg"); - - } + public volatile int AllDsums = 0; public volatile int NGDsums = 0; public volatile int OKDsums = 0; @@ -3385,6 +3369,7 @@ namespace HisenceYoloDetection string Path_1 = Path.Combine("D:\\Hisence\\Test\\1\\ng", filenameone + "_res.png"); Cv2.ImWrite(Path_1, mLcut.ResultMap.ToMat()); + CheckDiffSciHelper1.ResizeImage(Path_1, Path_1, 640, 480, 75); } else