From 8af89537715e0b37b762f4c747075e0bf0fdf70e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=BA=20=E6=9D=A8?= Date: Thu, 2 Nov 2023 17:19:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AE=9A=E4=BD=8D=E7=82=B9?= =?UTF-8?q?=E6=9F=A5=E6=89=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App_Code/CommonUse.cs | 47 ++++++++++++++++++++++++++++++++++++++--- Temp/Default.aspx.cs | 16 +++++++------- Temp/UserTemp.aspx.cs | 49 ++++++++++++++++++++++++------------------- 3 files changed, 80 insertions(+), 32 deletions(-) diff --git a/App_Code/CommonUse.cs b/App_Code/CommonUse.cs index 5b993e9..9320b94 100644 --- a/App_Code/CommonUse.cs +++ b/App_Code/CommonUse.cs @@ -399,7 +399,7 @@ public class CommonUse { VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();//所有的轮廓 VectorOfVectorOfPoint selected_contours = new VectorOfVectorOfPoint();//用于存储筛选过后的轮廓 - CvInvoke.FindContours(mat, contours, null, Emgu.CV.CvEnum.RetrType.List, + CvInvoke.FindContours(mat, contours, null, Emgu.CV.CvEnum.RetrType.External, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);//提取所有轮廓,操作过程中会对输入图像进行修改 //筛选轮廓。筛选条件:长宽比大于给定值 @@ -419,12 +419,13 @@ public class CommonUse double length = CvInvoke.ArcLength(contours[i], false); //计算连通轮廓的周长 VectorOfPoint approx_curve = new VectorOfPoint();//用于存放逼近的结果 - CvInvoke.ApproxPolyDP(contours[i], approx_curve, length * 0.02, true); + CvInvoke.ApproxPolyDP(contours[i], approx_curve, length * 0.05, true); //外接矩形宽、高需在给定范围内 // bool bo = (rect.Width / rect.Height >= ratio && (rect.Width > 3 && rect.Height > 2) && (area > 200 && area <= 4500)); - bool bo = ((rect.Width > 3 && rect.Height > 2) && (area > 80 && area <= 15000)); + //bool bo = ((rect.Width > 3 && rect.Height > 2) && (area > 80 && area <= 15000)); //bool bo = pxNums > 130 && pxNums < 300; + bool bo = (approx_curve.Size == 4 && CvInvoke.IsContourConvex(approx_curve) && (rect.Width > 15 && rect.Height > 15) && (area > 550 && area < 2000) && (rect.Width < 50 && rect.Height < 50) && pxNums > 550); if (bo) { selected_contours.Push(contours[i]); @@ -1502,6 +1503,46 @@ public class CommonUse return bitmap; } + + /// + /// 过滤红色 + /// + /// + /// + public Bitmap FilterRed(Bitmap bitmap) + { + CommonUse commonUse = new CommonUse(); + + Image src = new Image(bitmap); + + Mat gray = new Mat(); + CvInvoke.CvtColor(src, gray, ColorConversion.Bgr2Gray); + + + VectorOfMat channels = new VectorOfMat(); + CvInvoke.Split(src, channels); + if (channels.Size < 3) + { + return bitmap; + } + Mat red = channels[2]; + //commonUse.ShowMatWaitKey("red", red, 0.5); + + Mat redBinary = new Mat(); + CvInvoke.Threshold(red, redBinary, 150, 255, ThresholdType.Binary); + //commonUse.ShowMatWaitKey("red+binary", redBinary, 0.5); + + //Mat redDilate = new Mat(); + //Mat kernel = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(2, 2), new Point(-1, -1)); + //CvInvoke.Dilate(redBinary, redDilate, kernel, new Point(-1, -1), 1, BorderType.Default, new MCvScalar(0, 0, 0)); + + //kernel = CvInvoke.GetStructuringElement(ElementShape.Ellipse, new Size(2, 2), new Point(-1, -1)); + //CvInvoke.MorphologyEx(redBinary, redDilate, MorphOp.Open, kernel, new Point(-1, -1), 1, BorderType.Default, new MCvScalar(0, 0, 0)); + //commonUse.ShowMatWaitKey("redDilate", redDilate, 0.5); + + return redBinary.Bitmap; + } + } diff --git a/Temp/Default.aspx.cs b/Temp/Default.aspx.cs index 21a7c58..63a077d 100644 --- a/Temp/Default.aspx.cs +++ b/Temp/Default.aspx.cs @@ -22,7 +22,7 @@ using MySql.Data.MySqlClient; using Emgu.CV.Flann; using System.Data.SqlTypes; using System.Security.AccessControl; -using System.IO; +using System.IO; public partial class 外部答题卡_Default : System.Web.UI.Page { @@ -334,16 +334,16 @@ public partial class 外部答题卡_Default : System.Web.UI.Page PictureBoxBitMap = ScaleToSize(PictureBoxBitMap, width, height); Image currentFramext = new Image(PictureBoxBitMap); - Image myImage = new Image(PictureBoxBitMap); + Image myImage = new Image(commonUse.FilterRed(PictureBoxBitMap)); // 进行中值滤波 - CvInvoke.MedianBlur(myImage, myImage, 11); + CvInvoke.MedianBlur(myImage, myImage, 5); // 进行高斯滤波 - CvInvoke.GaussianBlur(myImage, myImage, new Size(0, 0), 3); + CvInvoke.GaussianBlur(myImage, myImage, new Size(5, 5), 0); // 去除定位点周围噪点 - Image blur = myImage.AddWeighted(myImage, 1.9, -0.5, 0); + //Image blur = myImage.AddWeighted(myImage, 1.9, -0.5, 0); - Bitmap bm_dest = blur.Bitmap; + Bitmap bm_dest = myImage.Bitmap; Image EmguImagex1 = new Image(bm_dest); Mat mat_threshold1 = new Mat(); CvInvoke.Threshold(EmguImagex1, mat_threshold1, 180, 255, Emgu.CV.CvEnum.ThresholdType.BinaryInv); @@ -513,7 +513,7 @@ public partial class 外部答题卡_Default : System.Web.UI.Page int hL = Convert.ToInt32(H / tiliang); Image bytimg = GetMatCutImg(color_mat.Bitmap, X, Y, W, H); - + X = 0; Y = 0; Mat src = bytimg.Mat; Mat mat_threshold = new Mat(); @@ -551,7 +551,7 @@ public partial class 外部答题卡_Default : System.Web.UI.Page Image bytimg = GetMatCutImg(srcBimap, X, Y, W, H); - + //commonUse.ShowMatWaitKey("maodingdian", bytimg.Mat, 2); diff --git a/Temp/UserTemp.aspx.cs b/Temp/UserTemp.aspx.cs index 19615d4..85ec8df 100644 --- a/Temp/UserTemp.aspx.cs +++ b/Temp/UserTemp.aspx.cs @@ -18,7 +18,7 @@ using MySql.Data.MySqlClient; public partial class Temp_UserTemp : System.Web.UI.Page { - + public long TempID = 0; public string Bindlist = ""; @@ -29,16 +29,16 @@ public partial class Temp_UserTemp : System.Web.UI.Page public string islocked = ""; public string pager = ""; public long tenant = 0; - + protected void Page_Load(object sender, EventArgs e) { - + int action = Globals.Request("action") == "" ? 0 : Convert.ToInt32(Globals.Request("action")); - tenant = Globals.Request("tenant") == "" ? 0 : Convert.ToInt64(Globals.Request("tenant")); + tenant = Globals.Request("tenant") == "" ? 0 : Convert.ToInt64(Globals.Request("tenant")); if (action != 0) { - + Response.Clear(); switch (action) { @@ -53,7 +53,7 @@ public partial class Temp_UserTemp : System.Web.UI.Page } else { - + UserID = Globals.Decrypt(Globals.Request("UserID")); pager = Globals.Request("pager"); if (string.IsNullOrEmpty(UserID)) @@ -64,7 +64,7 @@ public partial class Temp_UserTemp : System.Web.UI.Page else { long UID = Convert.ToInt64(UserID); - + TempID = Globals.Request("TempID") == "" ? 0 : Convert.ToInt32(Globals.Request("TempID"));//获取小模板ID string sql = "select * from MK_TempleteDataUser where DetaID_bigint=" + TempID + " AND UserID_bigint=" + UID; //string sql = "select * from MK_TempleteDataUser where DetaID_bigint=" + TempID ; @@ -83,11 +83,11 @@ public partial class Temp_UserTemp : System.Web.UI.Page Response.Write(""); } } - + } } - public void AnsyTemplete() + public void AnsyTemplete() { UserID = Globals.Request("UserID"); TempID = Globals.Request("TempID") == "" ? 0 : Convert.ToInt32(Globals.Request("TempID"));//获取小模板ID @@ -107,19 +107,20 @@ public partial class Temp_UserTemp : System.Web.UI.Page private void UpdateTempData() { - - UserID = Globals.Request("UserID"); + + UserID = Globals.Request("UserID"); TempID = Globals.Request("TempID") == "" ? 0 : Convert.ToInt32(Globals.Request("TempID"));//获取小模板ID string totalHtml = Server.UrlDecode(Globals.Request("totalHtml")); string Rotate_float = Globals.Request("roteDY"); - string sql = "Update MK_TempleteDataUser SET TempValue_nvarchar=@TempValue_nvarchar,Rotate_float=@Rotate_float where DetaID_bigint=" + TempID + " AND UserID_bigint="+ Convert.ToInt64(UserID); + string sql = "Update MK_TempleteDataUser SET TempValue_nvarchar=@TempValue_nvarchar,Rotate_float=@Rotate_float where DetaID_bigint=" + TempID + " AND UserID_bigint=" + Convert.ToInt64(UserID); MySqlParameter[] sp = new MySqlParameter[] { new MySqlParameter("@TempValue_nvarchar",totalHtml), new MySqlParameter("@Rotate_float",Rotate_float), }; - if (new MysqlDBHelper(tenant).ExecuteNoQuery(sql, sp) == 1) { + if (new MysqlDBHelper(tenant).ExecuteNoQuery(sql, sp) == 1) + { AnsyTemp(Convert.ToInt64(UserID), TempID); } } @@ -149,7 +150,7 @@ public partial class Temp_UserTemp : System.Web.UI.Page Response.Write("NODATA"); return; } - Bitmap temp = ImgGet(dt.Rows[0]["TempPicture64_nvarchar"].ToString()); + Bitmap temp = ImgGet(dt.Rows[0]["TempPicture64_nvarchar"].ToString()); var PictureBoxBitMap = new CommonUse().RotateByOrientation(temp); if (PictureBoxBitMap == null) { @@ -171,10 +172,15 @@ public partial class Temp_UserTemp : System.Web.UI.Page CommonUse commonUse = new CommonUse(); Mat mat_threshold1 = new Mat(); - + Image zaodianPic = new Image(commonUse.FilterRed(PictureBoxBitMap)); + // 去除定位点周围噪点 + CvInvoke.MedianBlur(zaodianPic, zaodianPic, 5); + //commonUse.ShowMatWaitKey("zaodianPic zaodianPic", zaodianPic.Mat, 0.6); + CvInvoke.GaussianBlur(zaodianPic, zaodianPic, new Size(5, 5), 0); - CvInvoke.Threshold(imagex, mat_threshold1, 180, 255, Emgu.CV.CvEnum.ThresholdType.BinaryInv); + + CvInvoke.Threshold(zaodianPic, mat_threshold1, 180, 255, Emgu.CV.CvEnum.ThresholdType.BinaryInv); Mat mat_dilate1 = commonUse.MyDilate(mat_threshold1); VectorOfVectorOfPoint selected_contours1; selected_contours1 = commonUse.GetUsefulContoursDingWei(mat_dilate1, 1); @@ -197,7 +203,7 @@ public partial class Temp_UserTemp : System.Web.UI.Page return; } Response.Write("ok"); - + } catch (Exception ex) { @@ -232,9 +238,9 @@ public partial class Temp_UserTemp : System.Web.UI.Page /// 路径 public Bitmap ImgGet(string url) { - + WebRequest imgRequest = WebRequest.Create(url); - HttpWebResponse res=null; + HttpWebResponse res = null; try { @@ -252,13 +258,14 @@ public partial class Temp_UserTemp : System.Web.UI.Page { return null; } - finally { + finally + { res.Dispose(); imgRequest.Abort(); res.Close(); } - + GC.Collect();