From 64969498bc75884dbfa12ece919d02995d394449 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=BA=20=E6=9D=A8?= Date: Tue, 7 Nov 2023 16:59:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E4=BD=8D=E7=82=B9=E8=AF=86=E5=88=AB?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App_Code/CommonUse.cs | 66 ++++++++++++++++++++++++++++++++++--------- Temp/Default.aspx.cs | 6 ++-- Temp/TempDraw.aspx.cs | 6 ++-- Temp/UserTemp.aspx.cs | 6 ++-- 4 files changed, 62 insertions(+), 22 deletions(-) diff --git a/App_Code/CommonUse.cs b/App_Code/CommonUse.cs index 9320b94..12813ab 100644 --- a/App_Code/CommonUse.cs +++ b/App_Code/CommonUse.cs @@ -166,10 +166,9 @@ public class CommonUse /// /// /// - public Mat GetPostionXYByFindPostionAndXieLv(Mat color_mat, VectorOfVectorOfPoint selected_contours, string postionCut, out string PYPostion, Image currentFrame, out double xielv) + public Mat GetPostionXYByFindPostionAndXieLv(Mat color_mat, VectorOfVectorOfPoint selected_contours, string postionCut, out string PYPostion, Image currentFrame, out double xielv, out string message) { - - + message = string.Empty; CommonUse commonUse = new CommonUse(); string[] postionc = postionCut.Split(new string[] { "^" }, StringSplitOptions.RemoveEmptyEntries); PYPostion = ""; @@ -179,6 +178,7 @@ public class CommonUse int Y2 = 0, Y1 = 0, X2 = 0, X1 = 0; + xielv = -100; for (int i = 0; i < postionc.Length; i++) { string[] postCutList = postionc[i].Split(','); @@ -193,21 +193,27 @@ public class CommonUse string mx = ""; try { - mx = commonUse.GetValueAndDrawGrid_Find(color_mat.Bitmap, selected_contours, X, W, 1, Y, H, 1, "", out GrXY); + mx = commonUse.GetValueAndDrawGrid_Find(color_mat.Bitmap, selected_contours, X, W, 1, Y, H, 1, "", out GrXY, out message); } catch { + message = GetPostionString(i)+"未找到定位点"; xielv = -100; return null; - mx = "null"; - GrXY = new Point(0, 0); } if (mx.Trim() != "A") { + message = GetPostionString(i) + "未找到定位点"; xielv = -100; return null; - break; + } + + if (!string.IsNullOrWhiteSpace(message)) + { + var posStr = GetPostionString(i); + message = posStr + "识别到多个定位点"; + return null; } Point Mx = GrXY; if (i == 0)//左上角 @@ -243,6 +249,19 @@ public class CommonUse } + private string GetPostionString(int i) + { + switch (i) + { + case 0: return "左上角"; + case 1: return "右上角"; + case 2: return "左下角"; + case 3: return "右下角"; + default: + return "未知位置"; + } + } + /// /// 将给定点集顺时针排序 /// @@ -535,8 +554,9 @@ public class CommonUse /// /// public int[] GetTargetGravityFind(VectorOfVectorOfPoint contours, int x_begin, int x_interval, int x_num, - int y_begin, int y_interval, int y_num, out Point gravityXY) + int y_begin, int y_interval, int y_num, out Point gravityXY, out string messag) { + messag = string.Empty; int[] result = new int[x_num];//结果数组 //数组初值默认为-1 for (int i = 0; i < x_num; i++) @@ -561,9 +581,10 @@ public class CommonUse if (result[x_id] != -1) { - string str = string.Format("第{0}列存在多个答案!请擦拭干净后再扫描", x_id); - result[x_id] = Convert.ToInt32(result[x_id].ToString() + value.ToString()); - gravityXY = gravity[i]; + messag = "识别到多个定位点"; + //string str = string.Format("第{0}列存在多个答案!请擦拭干净后再扫描", x_id); + //result[x_id] = Convert.ToInt32(result[x_id].ToString() + value.ToString()); + //gravityXY = gravity[i]; } else @@ -574,8 +595,25 @@ public class CommonUse gravityXY = gravity[i]; gravityXY.X = rect.X; gravityXY.Y = rect.Y; - break; } + + //if (result[x_id] != -1) + //{ + // string str = string.Format("第{0}列存在多个答案!请擦拭干净后再扫描", x_id); + // result[x_id] = Convert.ToInt32(result[x_id].ToString() + value.ToString()); + // gravityXY = gravity[i]; + + //} + //else + //{ + // Rectangle rect = CvInvoke.BoundingRectangle(contour); + // //这里有修改 + // result[x_id] = value; + // gravityXY = gravity[i]; + // gravityXY.X = rect.X; + // gravityXY.Y = rect.Y; + // break; + //} } return result; } @@ -1010,11 +1048,11 @@ public class CommonUse /// 输出文字 /// public string GetValueAndDrawGrid_Find(Bitmap img, VectorOfVectorOfPoint contours, - int x_begin, int x_interval, int x_num, int y_begin, int y_interval, int y_num, string strText, out Point pFindX) + int x_begin, int x_interval, int x_num, int y_begin, int y_interval, int y_num, string strText, out Point pFindX, out string message) { //画网格 Point pFind; - int[] intArray = GetTargetGravityFind(contours, x_begin, x_interval, x_num, y_begin, y_interval, y_num, out pFind); + int[] intArray = GetTargetGravityFind(contours, x_begin, x_interval, x_num, y_begin, y_interval, y_num, out pFind, out message); int maxValue = GetMaxValueOfArray(intArray);//数组最大值 pFindX = pFind; string str = ""; diff --git a/Temp/Default.aspx.cs b/Temp/Default.aspx.cs index 63a077d..c49a03a 100644 --- a/Temp/Default.aspx.cs +++ b/Temp/Default.aspx.cs @@ -364,11 +364,11 @@ public partial class 外部答题卡_Default : System.Web.UI.Page //Mat mat_dilate1 = commonUse.MyDilate(mat_threshold1); //VectorOfVectorOfPoint selected_contours1; //selected_contours1 = commonUse.GetUsefulContoursDingWei(mat_dilate1, 1); - - Mat color_mxxx = commonUse.GetPostionXYByFindPostionAndXieLv(currentFramext.Mat, selected_contours1, GetData[0], out PYPostionList, currentFramext, out xielv); + string message = string.Empty; + Mat color_mxxx = commonUse.GetPostionXYByFindPostionAndXieLv(currentFramext.Mat, selected_contours1, GetData[0], out PYPostionList, currentFramext, out xielv,out message); if (xielv == -100) { - Response.Write("模板配置错误,斜率无法计算"); + Response.Write(message); return false; } int oldWidth = color_mxxx.Bitmap.Width; diff --git a/Temp/TempDraw.aspx.cs b/Temp/TempDraw.aspx.cs index 08174df..4316fc9 100644 --- a/Temp/TempDraw.aspx.cs +++ b/Temp/TempDraw.aspx.cs @@ -679,7 +679,8 @@ public class MK_Operater string mx = ""; try { - mx = commonUse.GetValueAndDrawGrid_Find(color_mat.Bitmap, selected_contours, X, W, 1, Y, H, 1, "", out GrXY); + string message = string.Empty; + mx = commonUse.GetValueAndDrawGrid_Find(color_mat.Bitmap, selected_contours, X, W, 1, Y, H, 1, "", out GrXY,out message); } catch { @@ -747,7 +748,8 @@ public class MK_Operater string mx = ""; try { - mx = commonUse.GetValueAndDrawGrid_Find(color_mat.Bitmap, selected_contours, X, W, 1, Y, H, 1, "", out GrXY); + string message = string.Empty; + mx = commonUse.GetValueAndDrawGrid_Find(color_mat.Bitmap, selected_contours, X, W, 1, Y, H, 1, "", out GrXY,out message); } catch { diff --git a/Temp/UserTemp.aspx.cs b/Temp/UserTemp.aspx.cs index 85ec8df..1c65c30 100644 --- a/Temp/UserTemp.aspx.cs +++ b/Temp/UserTemp.aspx.cs @@ -189,11 +189,11 @@ public partial class Temp_UserTemp : System.Web.UI.Page //Mat color_mat1 = commonUse.DrawContours(currentFramext.Mat, selected_contours1); //commonUse.ShowMatWaitKey("shijuan bianjie", color_mat1, 0.6); - - Mat color_mxxx = commonUse.GetPostionXYByFindPostionAndXieLv(imagex.Mat, selected_contours1, GetData[0], out PYPostionList, currentFramext, out xielv); + string message = string.Empty; + Mat color_mxxx = commonUse.GetPostionXYByFindPostionAndXieLv(imagex.Mat, selected_contours1, GetData[0], out PYPostionList, currentFramext, out xielv,out message); if (xielv == -100) { - Response.Write("TEMPERRO"); + Response.Write(message); return; } imagex.Dispose();