Hint 2 - applying a rectangular mask
Example: Applying a mask containing a rectangle to an image
I have downloaded an online image of a house for this example (from Vector Stock). This image contains four different rooms and I want to use a rectangular mask so that only the office room is displayed. As described previously, we can open this image using a the Image sub-module of a module called pillow (imported as PIL) which understands how to read image files. We can then convert this input into a numpy array object.
im is a three dimensional numpy array object - includes height, width and colour channel (R, G, B).
The imshow function knows how to interpret this 3D shape to show a colour image.
Create a rectanglar mask using a function
I can define an function called inside_rectangle() to tell me whether a given (x, y) position is within a rectangle for a set of parameters. This will return True if the position is inside the rectangle and False otherwise.
The rectangle can be evaluated using the conditions:
\[|x - x_0| = w/2\] and \[|y - y_0| = h/2\]
where - \(x\), \(y\) are the x, y positions within the grid - \(x_0\), \(y_0\) are the centre of the rectangle - \(w\), \(h\) - are the full width and height of the rectangle - \(| |\) means the magnitude of the difference
For this mask, I want to define the parameters as follows:
The inside_rectangle function allows you to pass the parameters for your position and your rectangle and can tell you whether the position is within your rectangle (returns a True value) or not (returns a False value). For example if we had a position of (50, 50) we could use the function as follows:
Here we used the output of inside_rectangle as our condition directly (Python sees this if True or if False and follows the logic accordingly).
We can use the inside_rectangle() function to define our mask - looping over every element and checking the x, y position with the inside_rectangle() function with the appropriate parameters:
As before, we don’t need an else block here because the array we have defined already contains zeros.
Extended: See alternative numpy method mask in Hint 1 notebook for a more efficient way to create a square mask. Could you extend this method to reproduce a rectangular mask (without the need for the inside_rectangle function)?
Apply mask to the image
Now we have created rect_mask, we need to apply this to my image. We can do this by multiplying im by rect_mask and this will combine element-wise. This means that each pixel in the image will be multiplied by the corresponding value within the mask. Multipying by 0 will result in the output pixel also being 0 but multiplying by 1 will retain the original value in that pixel.
For creating the im_masked output there a few ways you could do this - the upshot is that you want to create an array of the right shape: - Create an empty array of the right shape - Create a new variable called im_masked which is a copy of the im using the copy() function (method)
Because im is a three dimensional array (height x weight x colour channel), one way to apply the mask is to do this for each of the three colours (R, G, B channels) separately.