%% Find Parabolic Lane Boundaries in Bird's-Eye-View Image
% Find lanes in an image by using parabolic lane boundary models. Overlay the identified lanes on the original image and on a bird's-eye-view transformation of the image.
% Load an image of a road with lanes. The image was obtained from a camera sensor mounted on the front of a vehicle.

I = imread('road.png');

%
% Transform the image into a bird's-eye-view image by using a preconfigured sensor object. This object models the sensor that captured the original image.

bevSensor = load('birdsEyeConfig');
birdsEyeImage = transformImage(bevSensor.birdsEyeConfig,I);
imshow(birdsEyeImage)

%
% Set the approximate lane marker width in world units (meters).

approxBoundaryWidth = 0.25;

%
% Detect lane features and display them as a black-and-white image.

birdsEyeBW = segmentLaneMarkerRidge(im2gray(birdsEyeImage), ...
    bevSensor.birdsEyeConfig,approxBoundaryWidth);
imshow(birdsEyeBW)

%
% Obtain lane candidate points in world coordinates.

[imageX,imageY] = find(birdsEyeBW);
xyBoundaryPoints = imageToVehicle(bevSensor.birdsEyeConfig,[imageY,imageX]);

%
% Find lane boundaries in the image by using the findParabolicLaneBoundaries function. By default, the function returns a maximum of two lane boundaries. The boundaries are stored in an array of parabolicLaneBoundary objects.

boundaries = findParabolicLaneBoundaries(xyBoundaryPoints,approxBoundaryWidth);

%
% Use insertLaneBoundary to overlay the lanes on the original image. The XPoints vector represents the lane points, in meters, that are within range of the ego vehicle's sensor. Specify the lanes in different colors. By default, lanes are yellow.

XPoints = 3:30;

figure
sensor = bevSensor.birdsEyeConfig.Sensor;
lanesI = insertLaneBoundary(I,boundaries(1),sensor,XPoints);
lanesI = insertLaneBoundary(lanesI,boundaries(2),sensor,XPoints,'Color','green');
imshow(lanesI)

%
% View the lanes in the bird's-eye-view image.

figure
BEconfig = bevSensor.birdsEyeConfig;
lanesBEI = insertLaneBoundary(birdsEyeImage,boundaries(1),BEconfig,XPoints);
lanesBEI = insertLaneBoundary(lanesBEI,boundaries(2),BEconfig,XPoints,'Color','green');
imshow(lanesBEI)

% Copyright 2012 The MathWorks, Inc.