Squirrels have fuzzy tails.
0 Members and 1 Guest are viewing this topic.
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using Emgu.CV;using Emgu.CV.Structure;using Emgu.Util;using Microsoft.Win32;namespace Camera{ public partial class Form1 : Form { // The path to the key where Windows looks for startup applications RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); private Capture _capture; private bool _captureInProgress; public Form1() { InitializeComponent(); // Check to see the current state (running at startup or not) if (rkApp.GetValue("MyApp") == null) { // The value doesn't exist, the application is not set to run at startup checkBox1.Checked = false; } else { // The value exists, the application is set to run at startup checkBox1.Checked = true; } } private void ProcessFrame(object sender, EventArgs arg) { Image<Bgr, Byte> frame = _capture.QueryFrame(); imageBox1.Image = frame; } private void button1_Click(object sender, EventArgs e) { #region if capture is not created, create it now if (_capture == null) { try { _capture = new Capture(); } catch (NullReferenceException excpt) { MessageBox.Show(excpt.Message); } } #endregion if (_capture != null) { if (_captureInProgress) { //stop the capture button1.Text = "Start Capture"; Application.Idle -= ProcessFrame; } else { //start the capture button1.Text = "Stop"; Application.Idle += ProcessFrame; } _captureInProgress = !_captureInProgress; } } private void button2_Click(object sender, EventArgs e) { if (checkBox1.Checked) { // Add the value in the registry so that the application runs at startup rkApp.SetValue("MyApp", Application.ExecutablePath.ToString()); } else { // Remove the value from the registry so that the application doesn't start rkApp.DeleteValue("MyApp", false); } } private void RefreshStatus() { PowerStatus power = SystemInformation.PowerStatus; // Show the main power status switch (power.PowerLineStatus) { case PowerLineStatus.Online: MainsPower.Checked = true; break; case PowerLineStatus.Offline: MainsPower.Checked = false; break; case PowerLineStatus.Unknown: MainsPower.CheckState = CheckState.Indeterminate; break; } // Power level int powerPercent = (int)(power.BatteryLifePercent * 100); if (powerPercent <= 100) BatteryIndicator.Value = powerPercent; else BatteryIndicator.Value = 0; label3.Text = Convert.ToString(powerPercent); // Battery Remaining int secondsRemaining = power.BatteryLifeRemaining; if (secondsRemaining >= 0) { BatteryLifeRemaining.Text = string.Format("{0} min", secondsRemaining / 60); } else { BatteryLifeRemaining.Text = string.Empty; } // Battery Status BatteryStatus.Text = power.BatteryChargeStatus.ToString(); } private void Form1_Load(object sender, EventArgs e) { RefreshStatus(); RefreshTimer.Enabled = true; } private void RefreshTimer_Tick(object sender, EventArgs e) { RefreshStatus(); PerformShapeDetection(); } public void PerformShapeDetection() { //Load the image from file and resize it for display Image<Bgr, Byte> img = _capture.QueryFrame(); //Convert the image to grayscale and filter out the noise Image<Gray, Byte> gray = img.Convert<Gray, Byte>().PyrDown().PyrUp(); Gray cannyThreshold = new Gray(180); Gray cannyThresholdLinking = new Gray(120); Gray circleAccumulatorThreshold = new Gray(500); CircleF[] circles = gray.HoughCircles( cannyThreshold, circleAccumulatorThreshold, 4.0, //Resolution of the accumulator used to detect centers of the circles 15.0, //min distance 5, //min radius 0 //max radius )[0]; //Get the circles from the first channel Image<Gray, Byte> cannyEdges = gray.Canny(cannyThreshold, cannyThresholdLinking); LineSegment2D[] lines = cannyEdges.HoughLinesBinary( 1, //Distance resolution in pixel-related units Math.PI / 45.0, //Angle resolution measured in radians. 20, //threshold 30, //min Line width 10 //gap between lines )[0]; //Get the lines from the first channel #region Find triangles and rectangles List<Triangle2DF> triangleList = new List<Triangle2DF>(); List<MCvBox2D> boxList = new List<MCvBox2D>(); //a box is a rotated rectangle using (MemStorage storage = new MemStorage()) //allocate storage for contour approximation for ( Contour<Point> contours = cannyEdges.FindContours( Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST, storage); contours != null; contours = contours.HNext) { Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, storage); if (currentContour.Area > 250) //only consider contours with area greater than 250 { if (currentContour.Total == 3) //The contour has 3 vertices, it is a triangle { Point[] pts = currentContour.ToArray(); triangleList.Add(new Triangle2DF( pts[0], pts[1], pts[2] )); } else if (currentContour.Total == 4) //The contour has 4 vertices. { #region determine if all the angles in the contour are within [80, 100] degree bool isRectangle = true; Point[] pts = currentContour.ToArray(); LineSegment2D[] edges = PointCollection.PolyLine(pts, true); for (int i = 0; i < edges.Length; i++) { double angle = Math.Abs( edges[(i + 1) % edges.Length].GetExteriorAngleDegree(edges[i])); if (angle < 80 || angle > 100) { isRectangle = false; break; } } #endregion if (isRectangle) boxList.Add(currentContour.GetMinAreaRect()); } } } #endregion imageBox3.Image = img; #region draw triangles and rectangles Image<Bgr, Byte> triangleRectangleImage = img.CopyBlank(); foreach (Triangle2DF triangle in triangleList) triangleRectangleImage.Draw(triangle, new Bgr(Color.DarkBlue), 2); foreach (MCvBox2D box in boxList) triangleRectangleImage.Draw(box, new Bgr(Color.DarkOrange), 2); imageBox3.Image = triangleRectangleImage; #endregion #region draw circles Image<Bgr, Byte> circleImage = img.CopyBlank(); foreach (CircleF circle in circles) circleImage.Draw(circle, new Bgr(Color.Brown), 2); imageBox2.Image = circleImage; #endregion #region draw lines Image<Bgr, Byte> lineImage = img.CopyBlank(); foreach (LineSegment2D line in lines) lineImage.Draw(line, new Bgr(Color.Green), 2); imageBox3.Image = lineImage; #endregion } } }
Here is how I did it, using C:http://www.societyofrobots.com/robot_ERP.shtmlSource code and designs are all included.Robot Grabs & Throws ERP test
errrr if this is your first robot, I'd suggest not building an expert level robot on your first try lol . . .And if this is exactly what you want, you could just copy my open source designs like this guy is doing:http://www.societyofrobots.com/robotforum/index.php?topic=13599.0Start off with just a robot arm, and after you finish, then work on the next thing. Basically do it bit by bit, project by project, and at the end then combine it all together. Don't bite off more than you can chew, etc.
ah, in that case . . . use RoboRealm + laptop + microcontroller.or with more programming:OpenCV + laptop + microcontroller