修改关闭检测

This commit is contained in:
YZJ 2024-07-22 16:50:01 +08:00
parent f1a9e75917
commit 267332bad5
4 changed files with 701 additions and 356 deletions

View File

@ -1,4 +1,5 @@
using OpenCvSharp;
using OpenCvSharp.Flann;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
@ -10,7 +11,10 @@ using static System.Net.Mime.MediaTypeNames;
using Point = OpenCvSharp.Point;
using Size = OpenCvSharp.Size;
using OpenCvSharp;
using OpenCvSharp.Features2D;
using OpenCvSharp.Flann;
using System.Drawing;
namespace HisenceYoloDetection
{
@ -57,6 +61,9 @@ namespace HisenceYoloDetection
return false;
}
// Cv2.Resize(img2, img2, new Size(550, 270));
Rect bottomleftRect= new Rect(0,img1.Height-30,30,30);
Scalar avgColor1 = Cv2.Mean(new Mat(img1,bottomleftRect));
Mat gimg2 = new Mat();
Cv2.CvtColor(img2, gimg2, ColorConversionCodes.BGR2GRAY);
Mat thr2 = new Mat();
@ -73,9 +80,9 @@ namespace HisenceYoloDetection
//Rect area2 = new Rect(148,30,229,222);
sqlrect.Width += 20;
sqlrect.Height += 60;
sqlrect.Height+= 100;
detrect.Width += 20;
detrect.Height += 60;
detrect.Height+=100;
Mat matCutblack1 = new Mat(thr1, sqlrect);
if (IfWhiteWord)
@ -100,14 +107,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));
@ -128,12 +135,12 @@ namespace HisenceYoloDetection
//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(savePath2, final_result1);
//string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + "_final_result2.png");
//// 保存结果
////string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png");
////保存结果
//string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png");
//Cv2.ImWrite(savePath, final_result2);
@ -253,12 +260,14 @@ namespace HisenceYoloDetection
{
// 读取和处理第一张图片
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));
img1 = RemoveBorders(img1);
Mat gimg1 = new Mat();
Cv2.CvtColor(img1, gimg1, ColorConversionCodes.BGR2GRAY);
Mat thr1 = new Mat();
@ -283,6 +292,7 @@ namespace HisenceYoloDetection
return false;
}
// Cv2.Resize(img2, img2, new Size(550, 270));
img2 = RemoveBorders(img2);
Mat gimg2 = new Mat();
Cv2.CvtColor(img2, gimg2, ColorConversionCodes.BGR2GRAY);
Mat thr2 = new Mat();
@ -338,7 +348,7 @@ namespace HisenceYoloDetection
Mat filter1 = new Mat(15, 15, MatType.CV_32F, new Scalar(0));
filter1.Row(7).SetTo(new Scalar(0.025));
filter1.Row(7).SetTo(new Scalar(0.025));
filter1.Col(7).SetTo(new Scalar(0.025));
@ -388,8 +398,8 @@ namespace HisenceYoloDetection
Cv2.ImWrite(savePathd1, devIMG_);
// 对差异图像应用阈值
Cv2.Threshold(devIMG, devIMG, 9, 255, ThresholdTypes.Binary);
Cv2.Threshold(devIMG_, devIMG_, 9, 255, ThresholdTypes.Binary);
Cv2.Threshold(devIMG, devIMG, 8, 255, ThresholdTypes.Binary);
Cv2.Threshold(devIMG_, devIMG_, 8, 255, ThresholdTypes.Binary);
// 结合差异
Mat sumIMG = new Mat();
@ -433,7 +443,100 @@ namespace HisenceYoloDetection
Cv2.ImWrite(savePath, blackhatImg);
return isMatch;
}
static Mat RemoveBorders(Mat image)
{
// 将图像转换为灰度图
Mat grayImage = new Mat();
Cv2.CvtColor(image, grayImage, ColorConversionCodes.BGR2GRAY);
// 使用自适应二值化将图像变为黑白图
Mat binaryImage = new Mat();
Cv2.AdaptiveThreshold(grayImage, binaryImage, 255, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary, 11, 2);
// 反转颜色
Mat invertedBinaryImage = new Mat();
Cv2.BitwiseNot(binaryImage, invertedBinaryImage);
// 查找轮廓
Point[][] contours;
HierarchyIndex[] hierarchy;
Cv2.FindContours(invertedBinaryImage, out contours, out hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxSimple);
// 找到包含最大面积的轮廓
double maxArea = 0;
Point[] maxContour = null;
foreach (var contour in contours)
{
double area = Cv2.ContourArea(contour);
if (area > maxArea)
{
maxArea = area;
maxContour = contour;
}
}
if (maxContour == null)
{
Console.WriteLine("未找到有效轮廓!");
return image;
}
// 找到平行四边形的四个顶点
Point[] approx = Cv2.ApproxPolyDP(maxContour, Cv2.ArcLength(maxContour, true) * 0.02, true);
Point2f[] srcPoints = approx.Select(p => new Point2f(p.X, p.Y)).ToArray();
if (srcPoints.Length != 4)
{
Console.WriteLine("未找到平行四边形的四个顶点!");
return image;
}
// 按顺时针顺序对顶点进行排序
srcPoints = OrderPoints(srcPoints);
// 确定目标图像的四个顶点
Point2f[] dstPoints = new Point2f[]
{
new Point2f(0, 0),
new Point2f(image.Width - 1, 0),
new Point2f(image.Width - 1, image.Height - 1),
new Point2f(0, image.Height - 1)
};
// 计算透视变换矩阵
Mat transformMatrix = Cv2.GetPerspectiveTransform(srcPoints, dstPoints);
// 应用透视变换
Mat warpedImage = new Mat();
Cv2.WarpPerspective(image, warpedImage, transformMatrix, new Size(image.Width, image.Height));
return warpedImage;
}
private static Point2f[] OrderPoints(Point2f[] points)
{
// 对顶点进行排序,顺时针顺序
Point2f[] orderedPoints = new Point2f[4];
// 计算质心
Point2f center = new Point2f(points.Average(p => p.X), points.Average(p => p.Y));
foreach (var point in points)
{
if (point.X < center.X && point.Y < center.Y)
orderedPoints[0] = point; // 左上
else if (point.X > center.X && point.Y < center.Y)
orderedPoints[1] = point; // 右上
else if (point.X > center.X && point.Y > center.Y)
orderedPoints[2] = point; // 右下
else if (point.X < center.X && point.Y > center.Y)
orderedPoints[3] = point; // 左下
}
return orderedPoints;
}
}
}
}
}

View File

@ -29,6 +29,9 @@
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
OpenCvSharp.Mat mat1 = new OpenCvSharp.Mat();
OpenCvSharp.Mat mat2 = new OpenCvSharp.Mat();
OpenCvSharp.Mat mat3 = new OpenCvSharp.Mat();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
tabPage2 = new TabPage();
panel1 = new Panel();
@ -120,6 +123,9 @@
panel4 = new Panel();
panel5 = new Panel();
groupBox2 = new GroupBox();
canvas3 = new XKRS.UI.Canvas();
canvas2 = new XKRS.UI.Canvas();
canvas1 = new XKRS.UI.Canvas();
originMatShow = new PictureBox();
ResultMatShow = new PictureBox();
panel8 = new Panel();
@ -178,6 +184,7 @@
timer6 = new System.Windows.Forms.Timer(components);
richTextBox1 = new RichTextBox();
backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
CloseDecBtn = new Button();
tabPage2.SuspendLayout();
panel1.SuspendLayout();
groupBox5.SuspendLayout();
@ -1188,6 +1195,9 @@
// groupBox2
//
groupBox2.AutoSize = true;
groupBox2.Controls.Add(canvas3);
groupBox2.Controls.Add(canvas2);
groupBox2.Controls.Add(canvas1);
groupBox2.Controls.Add(originMatShow);
groupBox2.Controls.Add(ResultMatShow);
groupBox2.Location = new Point(0, 0);
@ -1200,6 +1210,70 @@
groupBox2.Text = "图片";
groupBox2.Enter += groupBox2_Enter;
//
// canvas3
//
canvas3.AllowSelectDefect = true;
canvas3.BrushColor = null;
canvas3.ImagePath = "";
mat1.IsEnabledDispose = true;
canvas3.ImaMAt = mat1;
canvas3.ImgData = null;
canvas3.Location = new Point(8, 467);
canvas3.MoveStep = 5F;
canvas3.Name = "canvas3";
canvas3.Route = 0D;
canvas3.Scale = 1D;
canvas3.Size = new Size(351, 223);
canvas3.TabIndex = 6;
canvas3.WholeScale = 0F;
canvas3.XMove = 0F;
canvas3.XScale = 0F;
canvas3.YMove = 0F;
canvas3.YScale = 0F;
//
// canvas2
//
canvas2.AllowSelectDefect = true;
canvas2.BrushColor = null;
canvas2.ImagePath = "";
mat2.IsEnabledDispose = true;
canvas2.ImaMAt = mat2;
canvas2.ImgData = null;
canvas2.Location = new Point(367, 240);
canvas2.MoveStep = 5F;
canvas2.Name = "canvas2";
canvas2.Route = 0D;
canvas2.Scale = 1D;
canvas2.Size = new Size(363, 205);
canvas2.TabIndex = 5;
canvas2.WholeScale = 0F;
canvas2.XMove = 0F;
canvas2.XScale = 0F;
canvas2.YMove = 0F;
canvas2.YScale = 0F;
//
// canvas1
//
canvas1.AllowSelectDefect = true;
canvas1.BrushColor = null;
canvas1.ImagePath = "";
mat3.IsEnabledDispose = true;
canvas1.ImaMAt = mat3;
canvas1.ImgData = null;
canvas1.Location = new Point(8, 237);
canvas1.MoveStep = 5F;
canvas1.Name = "canvas1";
canvas1.Route = 0D;
canvas1.Scale = 1D;
canvas1.Size = new Size(351, 208);
canvas1.TabIndex = 4;
canvas1.WholeScale = 0F;
canvas1.XMove = 0F;
canvas1.XScale = 0F;
canvas1.YMove = 0F;
canvas1.YScale = 0F;
canvas1.Load += canvas1_Load;
//
// originMatShow
//
originMatShow.Location = new Point(8, 21);
@ -1222,6 +1296,7 @@
//
// panel8
//
panel8.Controls.Add(CloseDecBtn);
panel8.Controls.Add(label24);
panel8.Controls.Add(ScannerBox);
panel8.Controls.Add(textBox2);
@ -1324,7 +1399,7 @@
//
// OKDNum
//
OKDNum.Location = new Point(130, 196);
OKDNum.Location = new Point(129, 249);
OKDNum.Margin = new Padding(4, 2, 4, 2);
OKDNum.Name = "OKDNum";
OKDNum.ReadOnly = true;
@ -1334,7 +1409,7 @@
// label20
//
label20.AutoSize = true;
label20.Location = new Point(32, 201);
label20.Location = new Point(31, 254);
label20.Margin = new Padding(4, 0, 4, 0);
label20.Name = "label20";
label20.Size = new Size(56, 17);
@ -1343,7 +1418,7 @@
//
// NGDNum
//
NGDNum.Location = new Point(130, 153);
NGDNum.Location = new Point(129, 206);
NGDNum.Margin = new Padding(4, 2, 4, 2);
NGDNum.Name = "NGDNum";
NGDNum.ReadOnly = true;
@ -1353,7 +1428,7 @@
// label19
//
label19.AutoSize = true;
label19.Location = new Point(32, 156);
label19.Location = new Point(31, 209);
label19.Margin = new Padding(4, 0, 4, 0);
label19.Name = "label19";
label19.Size = new Size(57, 17);
@ -1362,7 +1437,7 @@
//
// AllDSum
//
AllDSum.Location = new Point(130, 111);
AllDSum.Location = new Point(129, 164);
AllDSum.Margin = new Padding(4, 2, 4, 2);
AllDSum.Name = "AllDSum";
AllDSum.ReadOnly = true;
@ -1372,7 +1447,7 @@
// label18
//
label18.AutoSize = true;
label18.Location = new Point(32, 114);
label18.Location = new Point(31, 167);
label18.Margin = new Padding(4, 0, 4, 0);
label18.Name = "label18";
label18.Size = new Size(50, 17);
@ -1747,6 +1822,17 @@
richTextBox1.TabIndex = 1;
richTextBox1.Text = "";
//
// CloseDecBtn
//
CloseDecBtn.Location = new Point(170, 109);
CloseDecBtn.Margin = new Padding(4, 2, 4, 2);
CloseDecBtn.Name = "CloseDecBtn";
CloseDecBtn.Size = new Size(121, 40);
CloseDecBtn.TabIndex = 20;
CloseDecBtn.Text = "关闭检测";
CloseDecBtn.UseVisualStyleBackColor = true;
CloseDecBtn.Click += CloseDecBtn_Click;
//
// MainForm
//
AutoScaleDimensions = new SizeF(8F, 17F);
@ -1955,8 +2041,8 @@
private Label label35;
private TextBox textBox2;
private Label label36;
private XKRS.UI.Canvas canvas5;
private XKRS.UI.Canvas canvas4;
//private XKRS.UI.Canvas canvas5;
//private XKRS.UI.Canvas canvas4;
private XKRS.UI.Canvas canvas3;
private XKRS.UI.Canvas canvas1;
private XKRS.UI.Canvas canvas6;
@ -1964,5 +2050,6 @@
private Button button1;
private Label label24;
private TextBox ScannerBox;
private Button CloseDecBtn;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -142,7 +142,7 @@
<value>733, 17</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>52</value>
<value>25</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">