Skip to content

Instantly share code, notes, and snippets.

@anubhavsinha
Created August 23, 2017 20:49
Show Gist options
  • Save anubhavsinha/36e6b53a2ba073c26aa7ec59a6df1389 to your computer and use it in GitHub Desktop.
Save anubhavsinha/36e6b53a2ba073c26aa7ec59a6df1389 to your computer and use it in GitHub Desktop.
color selection using numpy
# Color selection
# Step1. Read the image
# Step2. Set the threshold (In our case it is to filter out everything except white)
# Step3. Build a selector and
# Steo4. Apply the selector
# Picking the right color model is very important for a successful computer vision task.
# LUV, HSV, RGB, L*a*b etc.
# To be able to read the image file we will use the image package from matplotlib
from matplotlib import image
# To be able to render or plot the image we will use the pyplot package from matplotlib
from matplotlib import pyplot
# Read the file from disk into a data structure
view_from_windshield = image.imread('test.jpg')
# Render the image
pyplot.imshow(view_from_windshield)
pyplot.show()
# Now we want to understand the image analytically before we can run some processing over it
# First make a copy of exisint information so that we don't overwrite it. This is just workflow hygine.
# Also, we want to have the information in a handy data structure. numpy is good.
import numpy
view_from_windshield_copy = numpy.copy(view_from_windshield)
# We can see that it is exactly the same image. So, numpy copy is a real deal. not a fraud.
pyplot.imshow(view_from_windshield_copy)
pyplot.show()
# Now, let's see what all information we have inside the view_from_windshield_copy data structure
# It's a numpy array. Multi-dimensional
# shape gives us a good high level understanding of the size of different dimensions
print(view_from_windshield_copy.shape)
# So, it is 960 width, 540 height and 3 channels (We know that the image it a rectangle. Just look at it!)
# So, the output format is rows, columns for a matrix, and channels.
# rows corresponds to height as it runs from top to bottom vertically
# columns run left to rights and denotes the height
# so far so good.
# Now we want to set every pixel with certain value for the 3 channels into black.
# Do we know the three channels having values corresponding to the RGB color model?
# I guess that is what matplotlib's image package gives us by deafult.
# Let's read one of the pixels. I will pick a pixel at position (10,10) - (0,0) is top most left most.
# print(view_from_windshield_copy[10,10])
# What we want is if this value is not (r,g,b) we are going to turn it into (0,0,0).
# This will give us an image showing pixels only matching (r,g,b) and all others black.
# One way is we could run a loop over all pixels.
# But, whole point of using numpy is to be able to do effcient matrix transformations.
# So, here we go.
threshold = [190,190,190]
# we will construct a selection filter for this threshold. Anything below this value doesn't get passed.
pixel_selector = (view_from_windshield[:,:,0] < threshold[0])|(view_from_windshield[:,:,1] < threshold[1])|(view_from_windshield[:,:,2] < threshold[2])
# print(view_from_windshield_copy[:,:,2])
# Now we set the pixels which were selected to black
view_from_windshield_copy[pixel_selector] = [0,0,0]
# Let's see what we got
pyplot.imshow(view_from_windshield_copy)
pyplot.show()
@anubhavsinha
Copy link
Author

test

@anubhavsinha
Copy link
Author

output

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