Hint 1 - creating a square mask
Example: Creating a 100 x 100 grid containing a square where the centre of that square is at (80, 40) and the width is 30.
To check whether an (x, y) position is within the square we want to define, we want to evaluate if these two conditions are both satisfied: \[|x - x_0| = w/2\] and \[|y - y_0| = w/2\]
where - \(x\), \(y\) are the x, y positions within the grid - \(x_0\), \(y_0\) are the centre of the square - \(w\) - is both the full width and height of the square - \(| |\) means the magnitude of the difference
This code will show one way to create a mask which contains a square. This will take the most direct approach and evaluate every index in the 2D array one at a time.
Start by creating an array of zeros for our overall grid:
Next we can define the parameters we need for the square:
So, to check whether a given position is within our mask (the square we are defining), we need to find a way to use our equations above and write them in code.
We’ve created a grid of size of 100 x 100 so, as an example, let’s use the position (50, 50) for our grid:
Now we can build this up to look at every element in the 2D array we have created and check whether this is inside the square (we will leave the value as 0) or outside the square (we will set the value to 1). To do this we can loop through every column and every row and check the result of our conditions:
We don’t need an else block here because the array we have defined already contains zeros. This means if our conditin doesn’t match we don’t need to update the values within the array.
We could check where our mask has been set to 1 using the np.where function. For a two-dimensional array like square_mask, this will return two arrays containing the positions of the match - one for the first dimension and one for the second dimension.
Each pair of values (one in the first array and one the second) represents a 2D position in our array:
We can look at this mask using matplotlib and a plotting option called imshow (documentation; example from image gallery):
In this plot, the white represents the values in the mask (values of 1) and the black represents the values outside the mask (values of 0).
Extended: Leveraging numpy functionality
An alternative and more efficient way to do this would be to leverage the properties of numpy arrays and the available numpy functions and indexing.
We can start in the same way and create our array of zeros of the right shape
We can represent the pixel position for our x and y dimensions using numbers for 0 to 99 (for x) and 0 to 99 (for y) since our shape is 100 x 100.
We can then create a pair of 2D grids, X and Y, to match to the shape of our mask using a function called np.meshgrid(). This stretches our x and y coordinate values to create our 2D numpy arrays.
We can find the positions where our conditions are met within our grid using the np.where() function. From here we can filter our mask using integer array indexing and change these values from 0 to 1 within our mask.