/// <summary> /// Called when videoPlayer receives a new frame. /// </summary> /// <param name="sender"></param> /// <param name="image"></param> private void videoPlayer_NewFrame(object sender, ref Bitmap image) { // convert image to grayscale var gray = new GrayscaleBT709(); var frame = gray.Apply(image); // threshold image to only keep light pixels var threshold = new Threshold(125); threshold.ApplyInPlace(frame); // blank out everything but the road var horizonY = (int)(image.Height * 0.65); var fill = new CanvasFill(new Rectangle(0, 0, image.Width, horizonY), Color.Red); fill.ApplyInPlace(frame); // detect edges var edgeDetector = new CannyEdgeDetector(); edgeDetector.ApplyInPlace(frame); // do a hough line transformation, which will search for straight lines in the frame var transform = new HoughLineTransformation(); transform.ProcessImage(frame); var rawLines = transform.GetMostIntensiveLines(50); // only keep non-horizontal lines that cross the horizon at the vanishing point var lines = from l in rawLines let range = new Range(-75, -65) where range.IsInside(l.Radius) && (l.Theta <= 85 || l.Theta >= 95) select l; // show the edge detection view in the bottom left box edgeBox.Image = frame; // show the lane detection view in the bottom right box var laneImg = new Bitmap(image.Width, image.Height); Utility.DrawHoughLines(lines, laneImg, Color.White, 1); laneBox.Image = laneImg; // draw the lanes on the main camera image too Utility.DrawHoughLines(lines, image, Color.LightGreen, 2); }
/// <summary> /// Detect the highway lane boundaries. /// </summary> /// <param name="image">The camera frame to process</param> /// <returns>The detected lane lines in the frame</returns> private HoughLine[] DetectLaneLines(Bitmap image) { // convert image to grayscale var frame = Grayscale.CommonAlgorithms.BT709.Apply(image); // threshold image to only keep light pixels var threshold = new Threshold(125); threshold.ApplyInPlace(frame); // blank out everything but the road var horizonY = (int)(image.Height * 0.65); var fill = new CanvasFill(new Rectangle(0, 0, image.Width, horizonY), Color.Red); fill.ApplyInPlace(frame); // detect edges var edgeDetector = new CannyEdgeDetector(); edgeDetector.ApplyInPlace(frame); // do a hough line transformation, which will search for straight lines in the frame var transform = new HoughLineTransformation(); transform.ProcessImage(frame); var rawLines = transform.GetMostIntensiveLines(50); // only keep non-horizontal lines that cross the horizon at the vanishing point var lines = from l in rawLines let range = new Range(-75, -65) where range.IsInside(l.Radius) && (l.Theta <= 85 || l.Theta >= 95) select l; // show the edge detection view in the bottom left box edgeBox.Image = (Bitmap)frame.Clone(); // return lines return(lines.ToArray()); }