图片闪烁,关闭阻塞问题未解决

This commit is contained in:
YZJ 2024-07-01 11:39:19 +08:00
parent 33b32a6c92
commit a018a8f54d
3 changed files with 62 additions and 37 deletions

View File

@ -6,6 +6,7 @@ using System.Linq;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
using Point = OpenCvSharp.Point; using Point = OpenCvSharp.Point;
using Size = OpenCvSharp.Size; using Size = OpenCvSharp.Size;
@ -98,14 +99,14 @@ namespace HisenceYoloDetection
DateTime dt = DateTime.Now; 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 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(15, 15, MatType.CV_32F, new Scalar(0)); Mat filter1 = new Mat(15, 15, MatType.CV_32F, new Scalar(0));
@ -145,14 +146,14 @@ namespace HisenceYoloDetection
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"); //string savePathd = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "devIMG.png");
// 保存结果 //// 保存结果
Cv2.ImWrite(savePathd, devIMG); //Cv2.ImWrite(savePathd, devIMG);
string savePathd1 = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "devIMG_.png"); //string savePathd1 = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "devIMG_.png");
// 保存结果 //// 保存结果
Cv2.ImWrite(savePathd1, devIMG_); //Cv2.ImWrite(savePathd1, devIMG_);
// 对差异图像应用阈值 // 对差异图像应用阈值
Cv2.Threshold(devIMG, devIMG, 20, 255, ThresholdTypes.Binary); Cv2.Threshold(devIMG, devIMG, 20, 255, ThresholdTypes.Binary);
Cv2.Threshold(devIMG_, devIMG_, 20, 255, ThresholdTypes.Binary); Cv2.Threshold(devIMG_, devIMG_, 20, 255, ThresholdTypes.Binary);
@ -176,24 +177,32 @@ namespace HisenceYoloDetection
{ {
Cv2.DrawContours(blackhatImg, new Point[][] { contour }, -1, Scalar.Black, thickness: Cv2.FILLED); 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 else
{ {
Rect boundingRect = Cv2.BoundingRect(contour); Rect boundingRect = Cv2.BoundingRect(contour);
Cv2.Rectangle(img2, boundingRect, Scalar.Red, thickness: 2); Cv2.Rectangle(img2, boundingRect, Scalar.Red, thickness: 2);
isMatch = false; isMatch = false;
} string savePath2 = Path.Combine("D:\\Hisence\\Test\\2\\ok", 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"); //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("D:\\Hisence\\Test\\2\\ok", Path.GetFileNameWithoutExtension(path1) + filename + "_diff.png");
// 保存结果 // 保存结果
//string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png"); //string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png");
Cv2.ImWrite(savePath, blackhatImg); Cv2.ImWrite(savePath, blackhatImg);
}
}
return isMatch; return isMatch;
} }
@ -315,14 +324,14 @@ namespace HisenceYoloDetection
DateTime dt = DateTime.Now; 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 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);
// 创建卷积核 // 创建卷积核
@ -368,14 +377,14 @@ namespace HisenceYoloDetection
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"); //string savePathd = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "devIMG.png");
// 保存结果 //// 保存结果
Cv2.ImWrite(savePathd, devIMG); //Cv2.ImWrite(savePathd, devIMG);
string savePathd1 = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "devIMG_.png"); //string savePathd1 = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path1) + filename + "devIMG_.png");
// 保存结果 //// 保存结果
Cv2.ImWrite(savePathd1, devIMG_); //Cv2.ImWrite(savePathd1, devIMG_);
// 对差异图像应用阈值 // 对差异图像应用阈值
Cv2.Threshold(devIMG, devIMG, 45, 255, ThresholdTypes.Binary); Cv2.Threshold(devIMG, devIMG, 45, 255, ThresholdTypes.Binary);
Cv2.Threshold(devIMG_, devIMG_, 45, 255, ThresholdTypes.Binary); Cv2.Threshold(devIMG_, devIMG_, 45, 255, ThresholdTypes.Binary);
@ -383,10 +392,10 @@ namespace HisenceYoloDetection
// 结合差异 // 结合差异
Mat sumIMG = new Mat(); Mat sumIMG = new Mat();
Cv2.Add(devIMG, devIMG_, sumIMG); 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 kernelCL = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
Mat blackhatImg = new Mat(); Mat blackhatImg = new Mat();

View File

@ -277,6 +277,7 @@ namespace HisenceYoloDetection
} }
public void Execute(object source, System.Timers.ElapsedEventArgs e) public void Execute(object source, System.Timers.ElapsedEventArgs e)
{ {
t.Stop(); //先关闭定时器 t.Stop(); //先关闭定时器
//melsecPLCTCPDriver.WriteInt("548", 1); //melsecPLCTCPDriver.WriteInt("548", 1);
//Thread.Sleep(1000); //Thread.Sleep(1000);
@ -524,6 +525,7 @@ namespace HisenceYoloDetection
// tbFrameRate.Enabled = false; // tbFrameRate.Enabled = false;
bnGetParam.Enabled = false; bnGetParam.Enabled = false;
bnSetParam.Enabled = false; bnSetParam.Enabled = false;
} }
private void button3_Click(object sender, EventArgs e) private void button3_Click(object sender, EventArgs e)
{ {
@ -1449,6 +1451,12 @@ namespace HisenceYoloDetection
ResultMatShow.Image = mL.ResultMap; ResultMatShow.Image = mL.ResultMap;
} }
else
{
Mat res_yolo = Cv2.ImRead("ng.jpg");
ResultMatShow.Image = res_yolo.ToBitmap();
}//无法框定导致图片闪烁加else无法解决
//进行推理 //进行推理
MLRequest req2 = new MLRequest(); MLRequest req2 = new MLRequest();
req2.currentMat = Cam1ImgTwo; req2.currentMat = Cam1ImgTwo;
@ -2811,6 +2819,7 @@ namespace HisenceYoloDetection
string blockIndex = cam1TwoML.ResultDetails[i].LabelDisplay; string blockIndex = cam1TwoML.ResultDetails[i].LabelDisplay;
Rect area = new Rect(rectsx, rectsy, rectsWidth, rectsHeight); Rect area = new Rect(rectsx, rectsy, rectsWidth, rectsHeight);
Mat matCut = new Mat(mResultCuti, area); Mat matCut = new Mat(mResultCuti, area);
Stopwatch sw = new Stopwatch(); Stopwatch sw = new Stopwatch();
sw.Start(); sw.Start();
//OCR识别裁剪图片 //OCR识别裁剪图片
@ -2892,6 +2901,7 @@ namespace HisenceYoloDetection
string TextWoidStr = OcrFuzzyTextInsert.Join("##"); //实际的检测字 string TextWoidStr = OcrFuzzyTextInsert.Join("##"); //实际的检测字
string SQLStr = "";//日志显示数据库的关键字 string SQLStr = "";//日志显示数据库的关键字
bool juanjiMatch = false; bool juanjiMatch = false;
string filenameone = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Millisecond.ToString();
switch (blockIndex) switch (blockIndex)
{ {
case "1"://完全匹配 重量信息 case "1"://完全匹配 重量信息
@ -2913,11 +2923,17 @@ namespace HisenceYoloDetection
//显示错误的板 //显示错误的板
DisplayResult(mLcut); DisplayResult(mLcut);
Defet_OnDetectionDone(mLcut.ResultMap.ToMat(), 1); Defet_OnDetectionDone(mLcut.ResultMap.ToMat(), 1);
string Path_1 = Path.Combine("D:\\Hisence\\Test\\1\\ng", filenameone + "_res.png");
Cv2.ImWrite(Path_1, mLcut.ResultMap.ToMat());
} }
else else
{ {
OneIF = true; OneIF = true;
Defet_OnDetectionDone(OKMat, 1); Defet_OnDetectionDone(OKMat, 1);
string Path_2 = Path.Combine("D:\\Hisence\\Test\\1\\ok", filenameone + "_res.png");
Cv2.ImWrite(Path_2, OKMat);
} }
} }

View File

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