0% found this document useful (0 votes)
367 views6 pages

Obstacle Avoidance Using Canny Edge Detector

This document contains code for controlling a Rovio robot using C# and Emgu CV. It defines classes and methods for connecting to the Rovio, getting images from its camera, and performing edge detection, color detection and collision avoidance to autonomously navigate the robot. Threads are used to continuously get images and process them to determine motor commands sent to the Rovio to move forward, turn, or stop based on the image analysis.

Uploaded by

Anurag Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
367 views6 pages

Obstacle Avoidance Using Canny Edge Detector

This document contains code for controlling a Rovio robot using C# and Emgu CV. It defines classes and methods for connecting to the Rovio, getting images from its camera, and performing edge detection, color detection and collision avoidance to autonomously navigate the robot. Threads are used to continuously get images and process them to determine motor commands sent to the Rovio to move forward, turn, or stop based on the image analysis.

Uploaded by

Anurag Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

using using using using using using using using using using using using using using using

using using using

System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; System.Diagnostics; System.Net; System.IO; System.Threading; System.Runtime.InteropServices; Emgu.CV; Emgu.CV.UI; Emgu.CV.Structure; Emgu.Util; RovioLib;

namespace rovio_1 { public partial class Form1 : Form { int processingChoice = 0; static string rovio1URL = "http://10.7.45.57:80"; //Rovio URL public delegate void MyDelegateMethod(string coordinates); public delegate void MyDelegateMethod1(); public delegate void MyDelegateMethod2(); public delegate void MyDelegateMethod3(int battery); public delegate void MyDelegateMethod4(int battery2); public delegate void MyDelegateMethod5(decimal distance); public delegate void MyDelegateMethod6(); public delegate void MyDelegateMethod7(string circlePosition); public delegate void MyDelegateMethod8(string color); int edparam1 = 70, edparam2 = 60; //edge detection parameters

//create rovio object for the first time RovioController rovio1 = new RovioController("username", "password", rovio1URL);

int firstCharacterIR; public static int speed = 8; //setting the rovio speed. 1=fastest 10=slowest

public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { /*Thread for taking images from Rovio */

Thread takeImagesThread1 = new Thread(new ThreadStart(takeImages1)); takeImagesThread1.IsBackground = true; takeImagesThread1.Start(); /*Main Thread*/ Thread roviomainThread = new Thread(new ThreadStart(roviomain)); roviomainThread.IsBackground= true; roviomainThread.Start();

} private void button1_Click(object sender, EventArgs e) { MessageBox.Show(rovio1.GetReport()); } private void trackBar1_Scroll(object sender, EventArgs e) //Selecting speed { trackBar1.SetRange(1, 10); speed = trackBar1.Value; } //setting the edge detection parameters private void trackBar2_Scroll(object sender, EventArgs e) { trackBar2.SetRange(0, 500); label1.Text = "Thresh=" + Convert.ToInt32(trackBar2.Value); edparam1 = trackBar2.Value; } private void trackBar3_Scroll(object sender, EventArgs e) { trackBar3.SetRange(0, 500); label1.Text = "Thresh=" + Convert.ToInt32(trackBar2.Value); edparam2 = trackBar3.Value; } private void button2_Click(object sender, EventArgs e) { rovio1.GoHomeAndDock(); }

public void takeImages1() { while (true) { /*string sourceURL = rovio1URL + "/Jpeg/CamImg[1111].jpg"; pictureBox1.Load(sourceURL); byte[] buffer = new byte[100000]; int read, total = 0; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sourceURL); WebResponse resp = req.GetResponse(); Stream stream = resp.GetResponseStream(); while ((read = stream.Read(buffer, total, 1000)) != 0) { total += read; }

Bitmap bmp = (Bitmap)Bitmap.FromStream(new MemoryStream(buffer, 0, total));*/ RovioController rovio1 = new RovioController("username", "password", rovio1URL); Bitmap bmp1= rovio1.GetImage(); Image<Bgr, Byte> frame = new Image<Bgr, Byte>((Bitmap)bmp1); Image<Gray, Byte> frameoutput = frame.Convert<Gray,Byte>(); if (processingChoice == 3) { imageBox1.Image = ColorDetection(frame); } else { imageBox1.Image = frame; }

} }

public void roviomain() { while (true) { RovioController rovio1 = new RovioController("username", "password", rovio1URL); string strRovio1 = rovio1.GetReport(); firstCharacterIR = strRovio1.IndexOf("flags"); strRovio1 = strRovio1.Substring(firstCharacterIR + 9, 1); if (strRovio1 == "5") { rovio1.ManualDrive(1, speed); //move forward } else { processingChoice = 3; } } } private void button4_Click(object sender, EventArgs e) { roviomain(); }

public Image<Gray,Byte> EdgeDetection(Image<Bgr,Byte> image) { Image<Gray, Byte> grayFrame = image.Convert<Gray, Byte>(); Image<Gray, Byte> smallGrayFrame = grayFrame.PyrDown();

Image<Gray, Byte> smoothedGrayFrame = smallGrayFrame.PyrUp(); //Image<Gray, Byte> cannyFrame = smoothedGrayFrame.Canny(new Gray(100), new Gray(60)); //100,60 Image<Gray, Byte> cannyFrame = smoothedGrayFrame.Canny(edparam1, edparam2);

return cannyFrame;

} public Image<Gray, Byte> CollisionAvoidance(Image<Bgr, Byte> image) { Image<Gray, Byte> cannyFrame = EdgeDetection(image); Image<Gray, Byte> mask = new Image<Gray, Byte>(cannyFrame.Width + 2, cannyFrame.Height + 2); MCvConnectedComp comp = new MCvConnectedComp(); CvInvoke.cvFloodFill(cannyFrame.Ptr, new System.Drawing.Point(400, 400), new MCvScalar(160), new MCvScalar(5), new MCvScalar(5), out comp, Emgu.CV.CvEnum.CONNECTIVITY.EIGHT_CONNECTED, Emgu.CV.CvEnum.FLOODFILL_FLAG.DEFAULT, mask.Ptr); CvInvoke.cvErode(mask, mask, IntPtr.Zero, 5); CvInvoke.cvSmooth(mask, mask, Emgu.CV.CvEnum.SMOOTH_TYPE.CV_BLUR, 9, 9, 0, 0); int x; int y; Matrix<float> mtrx = new Matrix<float>(mask.Width, mask.Height); CvInvoke.cvConvert(mask, mtrx); Point pos = new Point(0, 0); for (x = 0; x <= 320; x++) { for (y = 0; y <= 480; y++) { if (mtrx[x, y] == 255) { if (x > pos.X) { pos.X = x; pos.Y = y; } if (y > pos.Y) pos.Y = y; } } } if (pos.X > 400) { rovio1.ManualDrive(8, speed); //forward right } else if (pos.X > 200 || pos.X < 400) { rovio1.ManualDrive(1, speed); //forward } else if (pos.X <= 200) {

rovio1.ManualDrive(7, speed); //forward left } else { rovio1.ManualDrive(17, speed); //rotate left rovio1.ManualDrive(0, speed); //stop } return mask; } public Image<Bgr, Byte> ColorDetection(Image<Bgr, Byte> image) { MCvMoments moments = new MCvMoments(); MCvScalar hsv_min = new MCvScalar(38, 238, 116); MCvScalar hsv_max = new MCvScalar(42, 243, 122); Image<Gray, Byte> thresholded = image.Convert<Gray, Byte>(); Image<Gray, Byte> thresholded2 = image.Convert<Gray, Byte>(); Image<Hsv, Byte> hsv_image = image.Convert<Hsv, Byte>(); CvInvoke.cvCvtColor(image, hsv_image, Emgu.CV.CvEnum.COLOR_CONVERSION.CV_BGR2HSV); CvInvoke.cvInRangeS(hsv_image, hsv_min, hsv_max, thresholded); CvInvoke.cvSmooth(thresholded, thresholded, Emgu.CV.CvEnum.SMOOTH_TYPE.CV_BLUR, 9, 9, 0, 0); CvInvoke.cvSmooth(thresholded, thresholded, Emgu.CV.CvEnum.SMOOTH_TYPE.CV_BLUR, 9, 9, 0, 0); CvInvoke.cvSmooth(thresholded, thresholded, Emgu.CV.CvEnum.SMOOTH_TYPE.CV_BLUR, 9, 9, 0, 0); CvInvoke.cvThreshold(thresholded, thresholded, 12, 256, Emgu.CV.CvEnum.THRESH.CV_THRESH_BINARY); int iterations = 5; CvInvoke.cvErode(thresholded, thresholded, IntPtr.Zero, iterations); CvInvoke.cvDilate(thresholded, thresholded, IntPtr.Zero, iterations); CvInvoke.cvDilate(thresholded, thresholded, IntPtr.Zero, iterations); CvInvoke.cvErode(thresholded, thresholded, IntPtr.Zero, iterations); CvInvoke.cvMoments(thresholded, ref moments, 1); double moment10 = CvInvoke.cvGetSpatialMoment(ref moments, 1, 0); double moment01 = CvInvoke.cvGetSpatialMoment(ref moments, 0, 1); double areaYellow = CvInvoke.cvGetCentralMoment(ref moments, 0, 0); int posX = 0; int posY = 0; try { posX = Convert.ToInt32(moment10 / areaYellow); posY = Convert.ToInt32(moment01 / areaYellow); } catch { //do nothing }

if(posX > 0 && posY > 0) { rovio1.ManualDrive(0,speed); //stop } else {

CollisionAvoidance(image); } return image; } private void button3_Click(object sender, EventArgs e) { rovio1.ManualDrive(1, speed); } private void button46_Click(object sender, EventArgs e) { rovio1URL = "http://107.45.57:80"; rovio1 = new RovioController("username", "password", rovio1URL); }

} }

You might also like