private void button6_Click(object sender, EventArgs e) { int point_count = 0; for (int i = 0; i < point.Length; i++) // 边缘检测 { if ((point[i].X > 0) && point[i].Y > 0) { point_count++; } } Point[] point_select = new Point[point_count]; int j = 0; for (int i = 0; i < point.Length; i++) // 边缘检测 { if ((point[i].X > 0) && point[i].Y > 0) { point_select[j].X = point[i].X; point_select[j].Y = point[i].Y; j++; } } line_struct a = lineCalculate(point_select); richTextBox1.Text += string.Format("RCA:{0}\n", a.RCA); richTextBox1.Text += string.Format("RCB:{0}\n", a.RCB); richTextBox1.Text += string.Format("Angle:{0}\n", a.angle); //richTextBox1.Text += string.Format("getDistanceFormPointToLine:{0}\n", (int)getDistanceFormPointToLine(1,1,1,1,0)); Point startPoint = new Point(); Point endPoint = new Point(); startPoint = getFootOfPerpendicular((int)point[0].X, (int)point[0].Y, a.RCB, -1, a.RCA); endPoint = getFootOfPerpendicular((int)point[point_count - 1].X, (int)point[point_count - 1].Y, a.RCB, -1, a.RCA); g.DrawLine(laser, halfWidth + startPoint.X, halfHeight - startPoint.Y, halfWidth + endPoint.X, halfHeight - endPoint.Y); g.DrawLine(bluePen, halfWidth + point[0].X, halfHeight - (point[0].X * a.RCB + a.RCA), halfWidth + point[point_count - 1].X, halfHeight - (point[point_count - 1].X * a.RCB + a.RCA)); for (int i = 0; i < point_count; i++) { double distance = getDistanceFormPointToLine((int)point[i].X, (int)point[i].Y, a.RCB, -1, a.RCA); richTextBox1.Text += string.Format("Point:{0}的直线距离是:{1}\n", i, distance); } }
/// <summary> /// 尝试5度一个区间 360度共6300个数据,每一度的数据是17.5个 /// </summary> /// <param name="Angledelta"></param> public void angelSplit(int StartAngle, int EndAngle) { int startCount; int endCount; float theta_temp; Point[] selectPoint = new Point[100]; List <int> index_ = new List <int>(); //数据的范围角度换计数值 selectPoint.Initialize(); index_.Clear(); startCount = (int)Math.Ceiling(StartAngle * 17.5); endCount = (int)Math.Ceiling(EndAngle * 17.5); for (int i = startCount; i < endCount; i++) { if (LaserAngleAndLength[i] > 25000) { return; } } int count_temp = 0; int index; for (int i = StartAngle; i < EndAngle; i++) // 数据抽选 { index = (int)Math.Ceiling(i * 17.5); selectPoint[count_temp].X = (int)lengthX[index]; selectPoint[count_temp].Y = (int)lengthY[index]; count_temp++; } Point[] point_select = new Point[count_temp]; for (int i = 0; i < count_temp; i++) // 边缘检测 { point_select[i].X = selectPoint[i].X; point_select[i].Y = selectPoint[i].Y; } line_struct a = lineCalculate(point_select); richTextBox1.Text += string.Format("RCA:{0}\n", a.RCA); richTextBox1.Text += string.Format("RCB:{0}\n", a.RCB); richTextBox1.Text += string.Format("Angle:{0}\n", a.angle); Point startPoint = new Point(); Point endPoint = new Point(); startPoint = getFootOfPerpendicular((int)point_select[0].X, (int)point_select[0].Y, a.RCB, -1, a.RCA); endPoint = getFootOfPerpendicular((int)point_select[count_temp - 1].X, (int)point_select[count_temp - 1].Y, a.RCB, -1, a.RCA); success_count++; g.DrawLine(outLine, halfWidth + startPoint.X * addsd, halfHeight - startPoint.Y * addsd, halfWidth + endPoint.X * addsd, halfHeight - endPoint.Y * addsd); for (int i = 0; i < count_temp; i++) { double distance = getDistanceFormPointToLine((int)point_select[i].X, (int)point_select[i].Y, a.RCB, -1, a.RCA); richTextBox1.Text += string.Format("point_select:{0}的直线距离是:{1}\n", i, distance); } }
/// <summary> /// 尝试5度一个区间 360度共6300个数据,每一度的数据是17.5个 /// </summary> /// <param name="Angledelta"></param> public int angelSplit_explane(int StartAngle, int EndAngle) { int startCount; int endCount; float theta_temp; Point[] selectPoint = new Point[100]; List <int> index_ = new List <int>(); int next_startAngle = EndAngle; //数据的范围角度换计数值 selectPoint.Initialize(); index_.Clear(); startCount = (int)Math.Ceiling(StartAngle * 17.5); endCount = (int)Math.Ceiling(EndAngle * 17.5); for (int i = startCount; i < endCount; i++) { if (LaserAngleAndLength[i] == 30000) { return(next_startAngle); } } //************************************// int count_temp = 0; int index; for (int i = StartAngle; i < EndAngle; i++) // 数据抽选 { index = (int)Math.Ceiling(i * 17.5); selectPoint[count_temp].X = (int)lengthX[index]; selectPoint[count_temp].Y = (int)lengthY[index]; count_temp++; } Point[] point_select = new Point[count_temp]; for (int i = 0; i < count_temp; i++) // 边缘检测 { point_select[i].X = selectPoint[i].X; point_select[i].Y = selectPoint[i].Y; } line_struct a = lineCalculate(point_select); richTextBox1.Text += string.Format("RCA:{0}\n", a.RCA); richTextBox1.Text += string.Format("RCB:{0}\n", a.RCB); richTextBox1.Text += string.Format("Angle:{0}\n", a.angle); //*****************************************// int detect_nextAngle = EndAngle; while (detect_nextAngle < 359) { detect_nextAngle++; count_temp++; index = (int)Math.Ceiling(detect_nextAngle * 17.5); selectPoint[count_temp].X = (int)lengthX[index]; selectPoint[count_temp].Y = (int)lengthY[index]; double distance = getDistanceFormPointToLine(selectPoint[count_temp].X, selectPoint[count_temp].Y, a.RCB, -1, a.RCA); if (distance > 100) { detect_nextAngle--; count_temp--; break; } } next_startAngle = detect_nextAngle; //************************************// count_temp = 0; point_select.Initialize(); for (int i = StartAngle; i < next_startAngle; i++) // 数据抽选 { index = (int)Math.Ceiling(i * 17.5); selectPoint[count_temp].X = (int)lengthX[index]; selectPoint[count_temp].Y = (int)lengthY[index]; count_temp++; } point_select = new Point[count_temp]; for (int i = 0; i < count_temp; i++) // 边缘检测 { point_select[i].X = selectPoint[i].X; point_select[i].Y = selectPoint[i].Y; } a = lineCalculate(point_select); richTextBox1.Text += string.Format("RCA:{0}\n", a.RCA); richTextBox1.Text += string.Format("RCB:{0}\n", a.RCB); richTextBox1.Text += string.Format("Angle:{0}\n", a.angle); //*****************************************// Point startPoint = new Point(); Point endPoint = new Point(); startPoint = getFootOfPerpendicular((int)point_select[0].X, (int)point_select[0].Y, a.RCB, -1, a.RCA); endPoint = getFootOfPerpendicular((int)point_select[count_temp - 1].X, (int)point_select[count_temp - 1].Y, a.RCB, -1, a.RCA); success_count++; g.DrawLine(outLine, halfWidth + startPoint.X * addsd, halfHeight - startPoint.Y * addsd, halfWidth + endPoint.X * addsd, halfHeight - endPoint.Y * addsd); for (int i = 0; i < count_temp; i++) { double distance = getDistanceFormPointToLine((int)point_select[i].X, (int)point_select[i].Y, a.RCB, -1, a.RCA); richTextBox1.Text += string.Format("point_select:{0}的直线距离是:{1}\n", i, distance); } if (success_count == 10) { ; } Thread.Sleep(300); return(next_startAngle); }
public line_struct lineCalculate(Point[] point_select) { ArrayList pointX = new ArrayList(); ArrayList pointY = new ArrayList(); float[] ArrPoint_X = new float[400]; float[] ArrPoint_Y = new float[400]; int ArrPoint_Count = 0; pointX.Clear(); pointY.Clear(); try { for (int i = 0; i < point_select.Length; i++) // 边缘检测 { pointX.Add(point_select[i].X); pointY.Add(point_select[i].Y); ArrPoint_X[ArrPoint_Count] = point_select[i].X; ArrPoint_Y[ArrPoint_Count] = point_select[i].Y; ArrPoint_Count++; } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } float averagex = 0, averagey = 0; foreach (int i in pointX) { averagex += i; } foreach (int j in pointY) { averagey += j; } averagex /= pointX.Count; // 取X坐标的平均数 averagey /= pointY.Count; // 取Y坐标的平均数 //经验回归系数的分子与分母 float numerator = 0; float denominator = 0; for (int i = 0; i < pointX.Count; i++) { numerator += (ArrPoint_X[i] - averagex) * (ArrPoint_Y[i] - averagey); denominator += (ArrPoint_X[i] - averagex) * (ArrPoint_X[i] - averagex); } //回归系数b(Regression Coefficient) y = bx + a ; ==> bx-y+a =0 float RCB = numerator / denominator; //回归系数a float RCA = averagey - RCB * averagex; double angle = 180 * Math.Atan(RCB) / Math.PI; line_struct param = new line_struct(); param.RCB = RCB; param.RCA = RCA; param.angle = angle; return(param); }