Matrix Construction
Using only the NumPy primitive types and assembly methods (not numpy.array or explicit item assignment):
- numpy.ones
- numpy.zeros
- numpy.arange
- numpy.identity
- numpy.block
- numpy.diag
- NumPy array methods
- NumPy array arithmetic
Build the array named example_array of numpy.int values:
\[ \begin{bmatrix} 1 & 4 & 7 & 2 & 0 & 0 & 1 \\ 2 & 5 & 8 & 0 & 2 & 0 & 2 \\ 3 & 6 & 9 & 0 & 0 & 2 & 3 \\ 0 & 0 & 0 & 1 & 7 & 0 & 0 \\ 0 & 0 & 0 & 0 & 2 & 6 & 0 \\ 0 & 0 & 0 & 0 & 0 & 3 & 5 \\ 0 & 0 & 0 & 0 & 0 & 0 & 4 \end{bmatrix} \]
Edge Detection
In the provided file, you will find a collection of images. They have been thresholded - converted to black 0s and white 1s - and are ready to load in to NumPy (with some help from matplotlib for showing):
from matplotlib import pyplot, image
animal = image.imread('animals/turtle.png')
pyplot.imshow(animal,cmap="gray")
To demonstrate a common use of convolutions in image processing, we are going to do edge detection. This is often the first step in computer vision tasks - helping pick out objects in images.
In the case of a pre-thresholded file, what you need to find out is whether a pixel is different from any of its neighbors.
Write a convolution and clip - which takes the input array to an array animal_edges that is 1 if an adjacent - up, down, left, right, top left, top right, bottom left, or bottom right - value is different, and 0 if all adjacent values are the same.
You can view the result with:
pyplot.imshow(animal_edges,cmap="binary")
And save the result with:
pyplot.imsave("traced-turtles.png",animal_edges,cmap="binary")
Upload a single
.pyfile which creates 5 ndarrays:
example_arrayfrom part 1
turtle_edgesedge-detectedturtle.png
bird_edgesedge-detectedbird.png
cat_edgesof edge-detectedcat.png
dog_edgesof edge-detecteddog.pngThen upload a file that you edge detected that you found interesting - either one of the examples, or another that you found on the internet - or took yourself. (you can threshold greyscale images yourself in numpy with
numpy.where(a>threshold,1,0)- or for a graphical interface, in common photo editing software such as [GIMP].)
Convolutions are helpful for all manner of fast image and signal processing tasks, not just simple mathematics. You now have all the tools you need to use these convolutions on grayscale images of the world.