public MainWindow() { InitializeComponent(); //ViewModel からのスクリプト実行用のdelegate DataContextChanged += (o, e) => { ViewModel vm = DataContext as ViewModel; if (vm != null) { vm._ExecuteScript += (sender, arg) => { Dispatcher.Invoke(new Action(() => { pythonConsole.Pad.Console.RunStatements(arg.cmd); })); }; vm._DrawCameraBitmap += (sender, arg) => { Dispatcher.BeginInvoke(new Action(() => { IplImage img = vm.VisionControl.GetCameraImage(); DrawCameraViewEventArgs a = arg as DrawCameraViewEventArgs; if (a._draw == 1) { CvRect rect = new CvRect(a._x1, a._y1, a._x2, a._y2); img.DrawRect(rect, new CvScalar(255, 0, 0), 2); } else if (a._draw == 2) { int x1 = a._x1 - a._x2 / 2; int x2 = a._x1 + a._x2 / 2; int y1 = a._y1 - a._y2 / 2; int y2 = a._y1 + a._y2 / 2; img.DrawLine(x1, a._y1, x2, a._y1, new CvScalar(255, 0, 0), 2); img.DrawLine(a._x1, y1, a._x1, y2, new CvScalar(255, 0, 0), 2); } if (VM.CenterLine == true) { img.DrawLine(0, 320, 640, 320, new CvScalar(255, 0, 0, 0), 2); img.DrawLine(320, 0, 320, 640, new CvScalar(255, 0, 0, 0), 2); } WriteableBitmapConverter.ToWriteableBitmap(img, _col_wb); cameraImage.Source = _col_wb; img.Dispose(); //cameraImage.Source = vm.VisionControl.GetCameraBitmap(); })); }; } }; pythonConsole.Pad.Host.ConsoleCreated += new PythonConsoleControl.ConsoleCreatedEventHandler(Host_ConsoleCreated); }
/// <summary> /// ConvexityDefectsの描画 /// </summary> /// <param name="img"></param> /// <param name="defect"></param> private void DrawDefects(IplImage img, CvSeq <CvConvexityDefect> defect) { int count = 0; foreach (CvConvexityDefect item in defect) { CvPoint p1 = item.Start, p2 = item.End; double dist = GetDistance(p1, p2); CvPoint2D64f mid = GetMidpoint(p1, p2); img.DrawLine(p1, p2, CvColor.White, 3); img.DrawCircle(item.DepthPoint, 10, CvColor.Green, -1); img.DrawLine(mid, item.DepthPoint, CvColor.White, 1); Console.WriteLine("No:{0} Depth:{1} Dist:{2}", count, item.Depth, dist); count++; } }
public IplImage DrawShapeClips(IplImage source, ShapeClip[] clips) { IplImage result = source;// new IplImage(source.Size, BitDepth.U8, 3); //Cv.CvtColor(source, result, ColorConversion.GrayToRgb); foreach (var clip in clips) { CvScalar color = CvColor.Red; if (clip.DetectionState == DetectionState.SemiOriented) { color = CvColor.Yellow; } else if (clip.DetectionState == DetectionState.FullyOriented) { color = CvColor.Green; } result.DrawCircle((int)clip.Position.X, (int)clip.Position.Y, (int)clip.BoundingBox.Length, color, 1); result.DrawLine( (int)clip.Position.X, (int)clip.Position.Y, (int)(clip.Position.X + (clip.BoundingBox.Length * Math.Cos((Math.PI / 180.0) * clip.Angle))), (int)(clip.Position.Y + (clip.BoundingBox.Length * Math.Sin((Math.PI / 180.0) * clip.Angle))), color, 1); } return(result); }
}//求平面交点,输入交点的顺利有讲究(前两个点是是第一副图的轮廓点,后两个是极线上的点) List <double[]> Compute_epiline_contour_intersection(CvMat right_epiline_point, double[,] ref_img_contour, int n)//n表示划分bin的个数 { //计时 //Stopwatch sw = new Stopwatch(); //sw.Start(); double[] epipole = new double[2]; //计算极点 double X1 = right_epiline_point[2, 0]; double Y1 = right_epiline_point[1, 0]; double X2 = right_epiline_point[2, 1]; double Y2 = right_epiline_point[1, 1]; epipole[0] = (Y2 - Y1) / (Y2 / X2 - Y1 / X1); epipole[1] = (X2 - X1) / (X2 / Y2 - X1 / Y1); double temp; double radius; double x, y; double min_radius = 200.0; double max_radius = -200.0; //double min_radius_contour_point; //double max_radius_contour_point; double[] temp_radius = new double[ref_img_contour.GetLength(1)]; for (int i = 0; i < ref_img_contour.GetLength(1); i++)//不用最后一个轮廓点 { //求角度 tan -> 角度,将其分到一个盒子中,一共有n=360/m个盒子 x = ref_img_contour[0, i] - epipole[0]; y = ref_img_contour[1, i] - epipole[1]; //cacu_angle temp_radius[i] = Math.Atan2(y, x) + Math.PI;//x轴负方向为0,2pi,x轴正方向为pi if (temp_radius[i] < min_radius) { min_radius = temp_radius[i]; //min_radius_contour_point = i; } else if (temp_radius[i] > max_radius) { max_radius = temp_radius[i]; //max_radius_contour_point = i; } } List <int>[] bins = new List <int> [n + 1]; for (int i = 0; i < bins.GetLength(0); i++) { bins[i] = new List <int>(); } for (int i = 0; i < ref_img_contour.GetLength(1); i++)//不用最后一个轮廓点 { //put_in_bins temp = (temp_radius[i] - min_radius) / ((max_radius - min_radius) / n); bins[(int)((temp_radius[i] - min_radius) / ((max_radius - min_radius) / n))].Add(i);//将第i个点放入盒子中 } IplImage img = Cv.CreateImage(new CvSize(2000, 1500), BitDepth.U8, 3); Cv.Set(img, new CvScalar(255, 255, 255)); for (int i = 0; i < bins.Length; i++) { Random rand = new Random(); int r = rand.Next() % 255; int g = rand.Next() % 255; int b = rand.Next() % 255; if (i == 0) { r = 255; g = 0; b = 0; } else if (i == 1) { r = 0; g = 255; b = 0; } else if (i == 2) { r = 0; g = 0; b = 255; } else if (i == 3) { r = 0; g = 255; b = 255; } else if (i == 4) { r = 255; g = 0; b = 255; } else if (i == 5) { r = 255; g = 255; b = 0; } for (int j = 0; j < bins[i].Count; j++) { int k = bins[i][j]; img.DrawLine(new CvPoint((int)ref_img_contour[0, k], (int)ref_img_contour[1, k]), new CvPoint((int)(ref_img_contour[0, k] + 1), (int)(ref_img_contour[1, k]) + 1), Cv.RGB(r, g, b), 2); } } Cv.SaveImage("bins.jpg", img); ////将最后一个点和第一个点连起来 //y = ref_img_contour[0, 1] - ref_img_contour[0, ref_img_contour_num-1]; //x = ref_img_contour[0, 0] - ref_img_contour[0, ref_img_contour_num-2]; //radius = Math.Atan2(y, x)+Math.PI; //bins[(int)(radius / (2*Math.PI / n))].Add(ref_img_contour_num/2-1);//将第i个点放入盒子中 double[] intersection_point; List <double[]> intersection_point_list = new List <double[]>(); CvPoint epiline_point_start; CvPoint epiline_end_point_end; CvPoint contour_line_point_start; CvPoint contour_line_point_end; //计算极线所在的bin int current_bin; for (int i = 0; i < right_epiline_point.GetDimSize(1); i++) { y = right_epiline_point[1, i] - right_epiline_point[3, i]; x = right_epiline_point[0, i] - right_epiline_point[2, i]; radius = Math.Atan2(y, x) + Math.PI; if (radius >= min_radius && radius <= max_radius) { current_bin = (int)((radius - min_radius) / ((max_radius - min_radius) / n)); for (int j = 0; j < bins[current_bin].Count - 1; j++) { epiline_point_start.X = (int)(right_epiline_point[0, i]); epiline_point_start.Y = (int)(right_epiline_point[1, i]); epiline_end_point_end.X = (int)(right_epiline_point[2, i]); epiline_end_point_end.Y = (int)(right_epiline_point[3, i]); contour_line_point_start.X = (int)(ref_img_contour[0, bins[current_bin][j]]); contour_line_point_start.Y = (int)(ref_img_contour[1, bins[current_bin][j]]); contour_line_point_end.X = (int)(ref_img_contour[0, bins[current_bin][j] + 1]); contour_line_point_end.Y = (int)(ref_img_contour[1, bins[current_bin][j] + 1]); intersection_point = SL_Cross_Intersection(contour_line_point_start, contour_line_point_end, epiline_point_start, epiline_end_point_end); if (intersection_point[0] > 0.0) { intersection_point[0] = i; intersection_point_list.Add(intersection_point); } } } } ////显示计时时间 //sw.Stop(); //MessageBox.Show(sw.ElapsedMilliseconds.ToString()); return(intersection_point_list); }
/// <summary> /// ConvexityDefectsの描画 /// </summary> /// <param name="img"></param> /// <param name="defect"></param> private void DrawDefects(IplImage img, CvSeq<CvConvexityDefect> defect) { int count = 0; foreach (CvConvexityDefect item in defect) { CvPoint p1 = item.Start, p2 = item.End; double dist = GetDistance(p1, p2); CvPoint2D64f mid = GetMidpoint(p1, p2); img.DrawLine(p1, p2, CvColor.White, 3); img.DrawCircle(item.DepthPoint, 10, CvColor.Green, -1); img.DrawLine(mid, item.DepthPoint, CvColor.White, 1); Console.WriteLine("No:{0} Depth:{1} Dist:{2}", count, item.Depth, dist); count++; } }