优化定位点查找

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

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

View File

@ -113,13 +113,14 @@ public partial class Temp_UserTemp : System.Web.UI.Page
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);
}
}
@ -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);
@ -234,7 +240,7 @@ public partial class Temp_UserTemp : System.Web.UI.Page
{
WebRequest imgRequest = WebRequest.Create(url);
HttpWebResponse res=null;
HttpWebResponse res = null;
try
{
@ -252,7 +258,8 @@ public partial class Temp_UserTemp : System.Web.UI.Page
{
return null;
}
finally {
finally
{
res.Dispose();
imgRequest.Abort();
res.Close();