diff --git a/HisenceYoloDetection/MainForm.Designer.cs b/HisenceYoloDetection/MainForm.Designer.cs index 127e624..8f6e326 100644 --- a/HisenceYoloDetection/MainForm.Designer.cs +++ b/HisenceYoloDetection/MainForm.Designer.cs @@ -194,6 +194,7 @@ timer6 = new System.Windows.Forms.Timer(components); richTextBox1 = new RichTextBox(); backgroundWorker1 = new System.ComponentModel.BackgroundWorker(); + pictureBox1 = new PictureBox(); tabPage2.SuspendLayout(); panel1.SuspendLayout(); groupBox5.SuspendLayout(); @@ -218,6 +219,7 @@ tabPage3.SuspendLayout(); panel2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)InsertDataDgv).BeginInit(); + ((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit(); SuspendLayout(); // // tabPage2 @@ -1223,15 +1225,15 @@ // DevNameCombo // DevNameCombo.FormattingEnabled = true; - DevNameCombo.Location = new Point(7, 33); + DevNameCombo.Location = new Point(8, 33); DevNameCombo.Margin = new Padding(4, 2, 4, 2); DevNameCombo.Name = "DevNameCombo"; - DevNameCombo.Size = new Size(386, 33); + DevNameCombo.Size = new Size(524, 33); DevNameCombo.TabIndex = 1; // // ScanDevList // - ScanDevList.Location = new Point(424, 32); + ScanDevList.Location = new Point(557, 33); ScanDevList.Margin = new Padding(4, 2, 4, 2); ScanDevList.Name = "ScanDevList"; ScanDevList.Size = new Size(94, 35); @@ -1243,6 +1245,7 @@ // groupBox2 // groupBox2.AutoSize = true; + groupBox2.Controls.Add(pictureBox1); groupBox2.Controls.Add(canvas3); groupBox2.Controls.Add(canvas2); groupBox2.Controls.Add(canvas1); @@ -1376,6 +1379,7 @@ groupBox8.Controls.Add(label35); groupBox8.Location = new Point(11, 326); groupBox8.Name = "groupBox8"; + groupBox8.RightToLeft = RightToLeft.No; groupBox8.Size = new Size(596, 462); groupBox8.TabIndex = 24; groupBox8.TabStop = false; @@ -1627,10 +1631,10 @@ // tabPage3 // tabPage3.Controls.Add(panel2); - tabPage3.Location = new Point(4, 34); + tabPage3.Location = new Point(4, 33); tabPage3.Margin = new Padding(4, 2, 4, 2); tabPage3.Name = "tabPage3"; - tabPage3.Size = new Size(1276, 815); + tabPage3.Size = new Size(1276, 816); tabPage3.TabIndex = 4; tabPage3.Text = "录入新型号"; tabPage3.UseVisualStyleBackColor = true; @@ -1950,6 +1954,16 @@ 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 +2013,7 @@ panel2.ResumeLayout(false); panel2.PerformLayout(); ((System.ComponentModel.ISupportInitialize)InsertDataDgv).EndInit(); + ((System.ComponentModel.ISupportInitialize)pictureBox1).EndInit(); ResumeLayout(false); } @@ -2184,5 +2199,6 @@ private TextBox textBox4; private Label label36; private Label label37; + private PictureBox pictureBox1; } } diff --git a/HisenceYoloDetection/MainForm.cs b/HisenceYoloDetection/MainForm.cs index 042d51d..8ab00af 100644 --- a/HisenceYoloDetection/MainForm.cs +++ b/HisenceYoloDetection/MainForm.cs @@ -3415,24 +3415,19 @@ namespace HisenceYoloDetection Rect rectsql = CheckDiffSciHelper.strChangeRect(SQlxK_HisenceWord.TwoRect); Rect rectDet = CheckDiffSciHelper.strChangeRect(xK_HisenceWord.TwoRect); - bool iswhite = IsMostlyWhite(PathSql); - int centerX = rectsql.X + rectsql.Width / 2; - int centerY = rectsql.Y + rectsql.Height / 2; - - // ȷµľοϽͿ - int rectSize = 60; // 30չ - int rectX = Math.Max(0, centerX - rectSize / 2); - int rectY = Math.Max(0, centerY - rectSize / 2); - int rectWidth = Math.Min(rectSize, rectsql.Width - rectX); - int rectHeight = Math.Min(rectSize, rectsql.Height - rectY); - - // µľο - Rect newRect = new Rect(rectX, rectY, rectWidth, rectHeight); - string color = BtnColor(CutBlockMat, newRect); + bool iswhite = IsMostlyWhite(PathSql);//жݿģ鱳ɫ + + Mat SqlImg= Cv2.ImRead(PathSql); + Mat SqlBtn = GetCroppedImage(SqlImg, rectsql); + Mat BtnImg = GetCroppedImage(CutBlockMat, rectDet);//ȡʵʱͼťͼƬ + Bitmap bitmap = MatToBitmap(BtnImg); + pictureBox1.Image = bitmap;//չʾʵʱťͼƬ + Rect newRect = GetCenterCroppedRect(SqlBtn, 30); + string color = BtnColor(SqlBtn, newRect); this.Invoke(new Action(() => { - textBox2.Text = color; + textBox2.Text = color;//ݿťɫ })); Mat matresult = CutBlockMat.Clone(); @@ -3694,7 +3689,32 @@ namespace HisenceYoloDetection // ɫڵ0.6false򷵻true return blackRatio < 0.6; } + //Mat תBitmap + private Bitmap MatToBitmap(Mat mat) + { + // Convert OpenCV Mat to Bitmap + Bitmap bitmap; + using (var ms = new System.IO.MemoryStream(mat.ToBytes())) + { + bitmap = new Bitmap(ms); + } + return bitmap; + } + static Mat GetCroppedImage(Mat picture, Rect rectDet) + { + // ȷ rectDet picture Χ + Rect validRect = new Rect( + Math.Max(rectDet.X, 0), + Math.Max(rectDet.Y, 0), + Math.Min(rectDet.Width, picture.Width - rectDet.X), + Math.Min(rectDet.Height, picture.Height - rectDet.Y) + ); + // ȡָͼ + Mat croppedImage = new Mat(picture, validRect); + + return croppedImage; + } private void bnGetParam2_Click_1(object sender, EventArgs e) { // tbExposure2.Text = Cam2.dvpGetExposure().ToString(); @@ -3736,7 +3756,26 @@ namespace HisenceYoloDetection myLog("Xֹͣ", DateTime.Now); } } + //ȡĵܵľο򣨷ֹťܲͬɫӰ죩 + private Rect GetCenterCroppedRect(Mat picture, int borderSize) + { + // ȡͼߴ + int width = picture.Width; + int height = picture.Height; + // üľο + int newWidth = width - 2 * borderSize; + int newHeight = height - 2 * borderSize; + int x = borderSize; + int y = borderSize; + + // ȷüͼΧ + Rect validRect = new Rect(x, y, newWidth, newHeight); + validRect = validRect & new Rect(0, 0, width, height); + + return validRect; + } + //ȡťɫ static string BtnColor(Mat img, Rect rect) { // ȡָ