So you wanna learn to code for computer vision. Maybe you got a project or product to build Or just out of interest. (Im pretty sure its the first reason).
WHY GO THROUGH THE TROUBLE OF LEARNING IT??
Lemme give a lil more motivation to learn. What the hell is cv and why do we need to know it?
Some Uses of Computer Vision
Facial Recognition: We can detect faces which helps in phone authentication or Security check. Google photos uses this to categorize the photos.
Object Tracking: To track an object about its movements,like in google maps.
Object Detection: Used in Autonomous cars to detect obstacles and avoid collision.
Basics of Operations on Arrays using Numpy
Before jumping into the coding part using opencv we must have a basic knowledge on operations on arrays like indexing, slicing etc..
Well how can we feed an image for processing? An image is made up of an array of pixels (kind of like small building bricks in legos). Each pixel has a numerical value assigned. So we need to have a basic knowledge on operations on arrays such indexing,slicing etc.. Numpy library supports these arrays and operations on them. We use import command is used to implement numpy library.
Lets goof around with some Numpy library functions commands.
np.array() ->List is converted to array
np.zeros(shape=) ->Gives an array with all values assigned with zeroes.
shape ->Gives number of elements in the array
reshape(rows,col) ->Converts the array into matrix of given shape.
np.arange(start,end) -> Results in array with values between start and end values
array[row:col] ->Slices the array to given row and column length.
Example Code Practice
> import numpy as np > list1=[14,56,5,6] > arr1=np.array(list1) > type(arr1) numpy.ndarray
> np.zeros(shape=(2,2)) array([[0., 0. ], [0., 0.]])
> arr1.shape (4,)
> arr1.reshape((2,2)) array([[14, 56], [ 5, 6]])
> bigarr=np.arange(0, 10).reshape((2,5)) > bigarr array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]])
> bigarr[:, 2] array([2, 7])
For more practice on Numpy visit -https://numpy.org/devdocs/user/quickstart.html
Show an Image
We use a library called PIL(Python Imaging Library) and Matplotlib( For visualization) and import Image from PIL.
Image.open(imageFilePath) -> Opens and identifies the given image file
Each pixel has a shape. For example, (1080,1080,3) which is (pixel height,pixel width,color channels). The 3 color channels are red, green and blue(RGB).
plt.imshow(PIL_image) -> Displays the image
Display selective channels
plt.imshow(array[:,:,0]) -> Displays image with only Red channel.
plt.imshow(array[:,:,1]) -> Displays image with only Green channel.
plt.imshow(array[:,:,2]) -> Displays image with only Blue channel.
Display Gray image
What if we want a gray image like in old movies?
The parameter cmap=’gray’ is used to convert color images into a gray image. plt.imshow(array[:,:,0],cmap=’gray’) -> Displays image with grayscale values.