# matplotlib and numpy are the library for ploting
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

# Read in the image and print out some stats
image = mpimg.imread('test.jpg')
print('This image is: ',type(image),
         'with dimensions:', image.shape)

# Grab the x and y size and make two copies of the image
ysize = image.shape[0]
xsize = image.shape[1]
# Note: always make a copy rather than simply using "=".
# With one copy we'll extract only the pixels that meet our selection,
# then we'll paint those pixels red in the original image to see our selection
# overlaid on the original.
color_select = np.copy(image)
line_image = np.copy(image)

# # Define our RGB color selection criteria
red_threshold = 200
green_threshold = 200
blue_threshold = 200
rgb_threshold = [red_threshold, green_threshold, blue_threshold]

# Define a triangle region of interest.
# The origin (x=0, y=0) is in the upper left in image processing
left_bottom = [0, 539]
right_bottom = [900, 539]
apex = [475, 320]

# Fit lines (y=Ax+B) to identify the  3 sided region of interest
# np.polyfit() returns the coefficients [A, B] of the fit
fit_left = np.polyfit((left_bottom[0], apex[0]), (left_bottom[1], apex[1]), 1)
fit_right = np.polyfit((right_bottom[0], apex[0]), (right_bottom[1], apex[1]), 1)
fit_bottom = np.polyfit((left_bottom[0], right_bottom[0]), (left_bottom[1], right_bottom[1]), 1)

# Mask pixels below the threshold. What is the meaning of |\?
color_thresholds = (image[:,:,0] < rgb_threshold[0]) | \
             (image[:,:,1] < rgb_threshold[1]) | \
             (image[:,:,2] < rgb_threshold[2])

# Find the region inside the lines. Arange function is to distribute the numbers uniformly.
XX, YY = np.meshgrid(np.arange(0, xsize), np.arange(0, ysize))
region_thresholds = (YY > (XX*fit_left[0] + fit_left[1])) & \
                    (YY > (XX*fit_right[0] + fit_right[1])) & \
                    (YY < (XX*fit_bottom[0] + fit_bottom[1]))

# Mask color selection
color_select[color_thresholds] = [0, 0, 0]

# Find where image is both colored right and in the region
# ~color_thresholds means the color range that we want.
line_image[~color_thresholds & region_thresholds] = [255,0,0]

# Display our two output images
# plt.imshow(color_select)
# plt.show()
# plt.imshow(line_image)
# plt.show()
# mpimg.imsave("red-line.jpg", color_select)

plt.imshow(image)
x = [left_bottom[0], right_bottom[0], apex[0], left_bottom[0]]
y = [left_bottom[1], right_bottom[1], apex[1], left_bottom[1]]
plt.plot(x,y,'b--', lw=4)
plt.imshow(line_image)
plt.show()
mpimg.imsave("red-line.jpg", line_image)


493b4fb5ebc79ad7e60cca85a3c0186507227499
<mask token>

<mask token>
print('This image is: ', type(image), 'with dimensions:', image.shape)
<mask token>
plt.imshow(image)
<mask token>
plt.plot(x, y, 'b--', lw=4)
plt.imshow(line_image)
plt.show()
mpimg.imsave('red-line.jpg', line_image)

<mask token>
image = mpimg.imread('test.jpg')
print('This image is: ', type(image), 'with dimensions:', image.shape)
ysize = image.shape[0]
xsize = image.shape[1]
color_select = np.copy(image)
line_image = np.copy(image)
red_threshold = 200
green_threshold = 200
blue_threshold = 200
rgb_threshold = [red_threshold, green_threshold, blue_threshold]
left_bottom = [0, 539]
right_bottom = [900, 539]
apex = [475, 320]
fit_left = np.polyfit((left_bottom[0], apex[0]), (left_bottom[1], apex[1]), 1)
fit_right = np.polyfit((right_bottom[0], apex[0]), (right_bottom[1], apex[1
    ]), 1)
fit_bottom = np.polyfit((left_bottom[0], right_bottom[0]), (left_bottom[1],
    right_bottom[1]), 1)
color_thresholds = (image[:, :, 0] < rgb_threshold[0]) | (image[:, :, 1] <
    rgb_threshold[1]) | (image[:, :, 2] < rgb_threshold[2])
XX, YY = np.meshgrid(np.arange(0, xsize), np.arange(0, ysize))
region_thresholds = (YY > XX * fit_left[0] + fit_left[1]) & (YY > XX *
    fit_right[0] + fit_right[1]) & (YY < XX * fit_bottom[0] + fit_bottom[1])
color_select[color_thresholds] = [0, 0, 0]
line_image[~color_thresholds & region_thresholds] = [255, 0, 0]
plt.imshow(image)
x = [left_bottom[0], right_bottom[0], apex[0], left_bottom[0]]
y = [left_bottom[1], right_bottom[1], apex[1], left_bottom[1]]
plt.plot(x, y, 'b--', lw=4)
plt.imshow(line_image)
plt.show()
mpimg.imsave('red-line.jpg', line_image)

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
image = mpimg.imread('test.jpg')
print('This image is: ', type(image), 'with dimensions:', image.shape)
ysize = image.shape[0]
xsize = image.shape[1]
color_select = np.copy(image)
line_image = np.copy(image)
red_threshold = 200
green_threshold = 200
blue_threshold = 200
rgb_threshold = [red_threshold, green_threshold, blue_threshold]
left_bottom = [0, 539]
right_bottom = [900, 539]
apex = [475, 320]
fit_left = np.polyfit((left_bottom[0], apex[0]), (left_bottom[1], apex[1]), 1)
fit_right = np.polyfit((right_bottom[0], apex[0]), (right_bottom[1], apex[1
    ]), 1)
fit_bottom = np.polyfit((left_bottom[0], right_bottom[0]), (left_bottom[1],
    right_bottom[1]), 1)
color_thresholds = (image[:, :, 0] < rgb_threshold[0]) | (image[:, :, 1] <
    rgb_threshold[1]) | (image[:, :, 2] < rgb_threshold[2])
XX, YY = np.meshgrid(np.arange(0, xsize), np.arange(0, ysize))
region_thresholds = (YY > XX * fit_left[0] + fit_left[1]) & (YY > XX *
    fit_right[0] + fit_right[1]) & (YY < XX * fit_bottom[0] + fit_bottom[1])
color_select[color_thresholds] = [0, 0, 0]
line_image[~color_thresholds & region_thresholds] = [255, 0, 0]
plt.imshow(image)
x = [left_bottom[0], right_bottom[0], apex[0], left_bottom[0]]
y = [left_bottom[1], right_bottom[1], apex[1], left_bottom[1]]
plt.plot(x, y, 'b--', lw=4)
plt.imshow(line_image)
plt.show()
mpimg.imsave('red-line.jpg', line_image)

# matplotlib and numpy are the library for ploting
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

# Read in the image and print out some stats
image = mpimg.imread('test.jpg')
print('This image is: ',type(image),
         'with dimensions:', image.shape)

# Grab the x and y size and make two copies of the image
ysize = image.shape[0]
xsize = image.shape[1]
# Note: always make a copy rather than simply using "=".
# With one copy we'll extract only the pixels that meet our selection,
# then we'll paint those pixels red in the original image to see our selection
# overlaid on the original.
color_select = np.copy(image)
line_image = np.copy(image)

# # Define our RGB color selection criteria
red_threshold = 200
green_threshold = 200
blue_threshold = 200
rgb_threshold = [red_threshold, green_threshold, blue_threshold]

# Define a triangle region of interest.
# The origin (x=0, y=0) is in the upper left in image processing
left_bottom = [0, 539]
right_bottom = [900, 539]
apex = [475, 320]

# Fit lines (y=Ax+B) to identify the  3 sided region of interest
# np.polyfit() returns the coefficients [A, B] of the fit
fit_left = np.polyfit((left_bottom[0], apex[0]), (left_bottom[1], apex[1]), 1)
fit_right = np.polyfit((right_bottom[0], apex[0]), (right_bottom[1], apex[1]), 1)
fit_bottom = np.polyfit((left_bottom[0], right_bottom[0]), (left_bottom[1], right_bottom[1]), 1)

# Mask pixels below the threshold. What is the meaning of |\?
color_thresholds = (image[:,:,0] < rgb_threshold[0]) | \
             (image[:,:,1] < rgb_threshold[1]) | \
             (image[:,:,2] < rgb_threshold[2])

# Find the region inside the lines. Arange function is to distribute the numbers uniformly.
XX, YY = np.meshgrid(np.arange(0, xsize), np.arange(0, ysize))
region_thresholds = (YY > (XX*fit_left[0] + fit_left[1])) & \
                    (YY > (XX*fit_right[0] + fit_right[1])) & \
                    (YY < (XX*fit_bottom[0] + fit_bottom[1]))

# Mask color selection
color_select[color_thresholds] = [0, 0, 0]

# Find where image is both colored right and in the region
# ~color_thresholds means the color range that we want.
line_image[~color_thresholds & region_thresholds] = [255,0,0]

# Display our two output images
# plt.imshow(color_select)
# plt.show()
# plt.imshow(line_image)
# plt.show()
# mpimg.imsave("red-line.jpg", color_select)

plt.imshow(image)
x = [left_bottom[0], right_bottom[0], apex[0], left_bottom[0]]
y = [left_bottom[1], right_bottom[1], apex[1], left_bottom[1]]
plt.plot(x,y,'b--', lw=4)
plt.imshow(line_image)
plt.show()
mpimg.imsave("red-line.jpg", line_image)

