修改图像大小

This commit is contained in:
820689062 2024-07-30 09:17:32 +08:00
parent df358a4e3a
commit 33fd534579
3 changed files with 71 additions and 56 deletions

View File

@ -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)
{
// 将图像转换为灰度图

View File

@ -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);
}

View File

@ -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);
}
/// <summary>
@ -1566,24 +1567,7 @@ namespace HisenceYoloDetection
/// <param name="dt"></param>
/// <param name="cameraMat"></param>
/// <param name="SnapshotCount"></param>
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