Q:

How does numPy's advanced indexing work for boolean masks?

I am very new to Python, and I am struggling with understanding how the following code works. 
I have an image read into a numpy array. Similar to this:
# Read the image
image = mpimg.imread('images/rgb-road.png')
After this I set a 3-value boundary list rgb_boundary[123,231,122]. The values are irrelevant. 
Then comes the confusing part.
boundary = (image[:,:,0] < rgb_boundary[0]) \
            | (image[:,:,1] < rgb_threshold[1]) \
            | (image[:,:,2] < rgb_threshold[2])
image[boundary] = [0,0,0]

It is the combination of my poor knowledge of Python syntax and working with images that is causing the issue. 
I would be extremely happy if somebody could explain what is happening in the above piece of code. Especially in the line where we have the image[boundary] assignment. My image values are changed, but I don't understand how this is working in Python.
In addition, if there is a resource where I can read about how/why this is working, please feel free to refer me to it.
Thanks! 

A:

Let's start with the first piece that executes:
image[:,:,0] < rgb_boundary[0]

Now, rgb_boundary[0] is simply 123, so what this does is find the locations where the first color (red) in the image is less than 123 intensity (so about half-bright, since 255 is max-bright).
The result of the above expression will be a 2D boolean array, which is True wherever the red color byte is less than 123, and False elsewhere.
We can then understand this code:
boundary = (image[:,:,0] < rgb_boundary[0]) \
         | (image[:,:,1] < rgb_boundary[1]) \
         | (image[:,:,2] < rgb_boundary[2])

It is creating a 2D mask called boundary which will be True for any pixel where red is less than 123, or green is less than 231, or blue is less than 122.  It will be False wherever none of those conditions are met.
Finally:
image[boundary] = [0,0,0]

This sets the selected pixels to black.  In NumPy, a boolean mask like boundary can be used with "fancy indexing" to create a "view" of an array.  In this case, the "view" is used to assign [0,0,0] (black) to image wherever the boundary condition is True.

