Skip to content

Instantly share code, notes, and snippets.

@hristo-vrigazov
Created November 29, 2016 05:43
Show Gist options
  • Save hristo-vrigazov/a859364e97714360bd04d8a5e6989776 to your computer and use it in GitHub Desktop.
Save hristo-vrigazov/a859364e97714360bd04d8a5e6989776 to your computer and use it in GitHub Desktop.
Lane detection using simple thresholding
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
# Read in the image and print out some stats
# Note: in the previous example we were reading a .jpg
# Here we read a .png and convert to 0,255 bytescale
image = (mpimg.imread('test.png')*255).astype('uint8')
print('This image is: ',type(image),
'with dimensions:', image.shape)
# Grab the x and y size and make a copy of the image
ysize = image.shape[0]
xsize = image.shape[1]
color_select = np.copy(image)
# Define color selection criteria
###### MODIFY THESE VARIABLES TO MAKE YOUR COLOR SELECTION
red_threshold = 200
green_threshold = 100
blue_threshold = 10
######
rgb_threshold = [red_threshold, green_threshold, blue_threshold]
# Do a bitwise or with the "|" character to identify
# pixels below the thresholds
thresholds = (image[:,:,0] < rgb_threshold[0]) \
| (image[:,:,1] < rgb_threshold[1]) \
| (image[:,:,2] < rgb_threshold[2])
color_select[thresholds] = [0,0,0]
plt.imshow(color_select)
# Display the image
plt.imshow(color_select)
# Uncomment the following code if you are running the code locally and wish to save the image
# mpimg.imsave("test-after.png", color_select)
@xanthas
Copy link

xanthas commented May 9, 2020

I want to understand what is happening between line 17 to 31. Explain me in depth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment