优化定位点查找

This commit is contained in:
强 杨 2023-11-02 17:19:07 +08:00
parent 3c6e080dd7
commit 8af8953771
3 changed files with 80 additions and 32 deletions

View File

@ -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;
}
/// <summary>
/// 过滤红色
/// </summary>
/// <param name="bitmap"></param>
/// <returns></returns>
public Bitmap FilterRed(Bitmap bitmap)
{
CommonUse commonUse = new CommonUse();
Image<Bgr, byte> src = new Image<Bgr, byte>(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;
}
}

View File

@ -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<Gray, byte> currentFramext = new Image<Gray, byte>(PictureBoxBitMap);
Image<Bgr, byte> myImage = new Image<Bgr, byte>(PictureBoxBitMap);
Image<Gray, byte> myImage = new Image<Gray, byte>(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<Bgr, byte> blur = myImage.AddWeighted(myImage, 1.9, -0.5, 0);
//Image<Bgr, byte> blur = myImage.AddWeighted(myImage, 1.9, -0.5, 0);
Bitmap bm_dest = blur.Bitmap;
Bitmap bm_dest = myImage.Bitmap;
Image<Gray, byte> EmguImagex1 = new Image<Gray, byte>(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<Gray, byte> 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<Gray, byte> bytimg = GetMatCutImg(srcBimap, X, Y, W, H);
//commonUse.ShowMatWaitKey("maodingdian", bytimg.Mat, 2);

View File

@ -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("<script>alert('无有效数据');</script>");
}
}
}
}
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<Gray, byte> zaodianPic = new Image<Gray, byte>(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
/// <param name="url">路径</param>
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();