Created
August 26, 2017 20:03
-
-
Save anubhavsinha/5b63d0470508235687d287253e0e1f1c to your computer and use it in GitHub Desktop.
region_masking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Region masking is a technique used to run a image processing or computer vision task within a specific region of interest | |
# Step 1. Build a selector for the region | |
# Step 2. Build a selector for the pixels of interest | |
# Step 3. AND the two selectors. It's that simple. | |
from matplotlib import image | |
from matplotlib import pyplot | |
import numpy | |
view_from_windshield = image.imread('test.jpg') | |
# Building the region selector | |
canvas = view_from_windshield.shape | |
#pyplot.imshow(view_from_windshield) | |
#pyplot.show() | |
# Set a triangle region | |
# We need left, right and apex co-ordinates (x,y) (0,0) is top left | |
print(canvas) | |
# x,y | |
left = [0,540] | |
right = [960,540] | |
apex = [480,300] | |
# y = ax+b | |
# solve for a and b | |
# polyfit((x1,x2,x3..),(y1,y2,y3...), degree_of_ploynomial_to_fit) | |
# for straight line polyfit((x1,x2),(y1,y2),1) output a tuple of two parameters | |
left_line = numpy.polyfit((left[0], apex[0]),(left[1], apex[1]),1) | |
right_line = numpy.polyfit((right[0], apex[0]),(right[1], apex[1]),1) | |
base_line = numpy.polyfit((left[0], right[0]),(left[1], right[1]),1) | |
# create co-ordinates using meshgrid | |
# x,y = numpy.meshgrid([0,1,...539],[0,1,...959]) for image 960 x 540 | |
# x,y is a matrix of co-ordinate values (x,y) denotes a point. | |
# arange(n,m) is n included to m not included so it takes care of our range 0, 539 instead of 0, 540. | |
x,y = numpy.meshgrid(numpy.arange(0,canvas[1]), numpy.arange(0,canvas[0])) | |
# The region of interest is (x>0&y>left_line[0].x+left_line[1]) but x>0 is always true | |
# similarily (y>right_line[0].x+right_line[1]) and y<base_line[0].x+base_line[1] | |
region_selector = (y>(left_line[0]*x+left_line[1]))&(y>(right_line[0]*x+right_line[1]))&(y<(base_line[0]*x+base_line[1])) | |
# Building the color selector | |
threshold = [200,200,200] | |
working_copy = numpy.copy(view_from_windshield) | |
working_copy2 = numpy.copy(view_from_windshield) | |
pixel_selector = (working_copy[:,:,0] < threshold[0])|(working_copy[:,:,1] < threshold[1])|(working_copy[:,:,2] < threshold[2]) | |
working_copy2[~pixel_selector & region_selector] = [255,0,0] | |
pyplot.imshow(working_copy2) | |
pyplot.show() |
Author
anubhavsinha
commented
Aug 26, 2017
But, as it happens, lane lines are not always the same color, and even lines of the same color under different lighting conditions (day, night, etc) may fail to be detected by our simple color selection.
What we need is to take our algorithm to the next level to detect lines of any color using sophisticated computer vision methods.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment