Computer vision is one of the most important branch of a data science. Computer vision is a field of study that seek to replicate human visionary system in a computer. This enable the computer to acquire, screen, analyze, identify and extracting information from image, video or other visual outputs.
Today I’m going to bring you to one of the most popular tools work with computer vision which is Open CV
OpenCV is an open source library that support multiple programing language like Python, Java and C++. It was first released in 2000 and now it has grown to have more than 25000 algorithm including computer vision and machine learning. With such a comprehensive algorithm you can do complex task of computer vision like identifying object, identifying faces and emotion, and even identifying human action by using OpenCV. In this article I’m going to share to you about the basic of OpenCV using python language.
1. Install OpenCV
Before we get started, first we need to install OpenCV into our computer. You just need to type the command
pip install opencv-python
or if you’re using Anaconda, you can type this command
conda install -c conda-forge opencv
2. Read, Show and Save Images
One of the most basic use of OpenCV is to read image
We can see here the picture that is read by cv2 have weird coloring. That’s because by default cv2.imread function read the image in BGR format. To load the image in a RGB format we need to type an extra function.
You can type the cv2.cvtColor() function to convert the color and then use cv2.COLOR_BGR2RGB function to convert the BGR color format to RGB color format.
Beside using matplotlib to show the image OpenCV already read. you can also use cv2.imshow() or if you are opening your notebook in Google Colab you can use cv2_imshow() instead.
Now if you’re done with your picture processing and want to save the picture you can type cv2.imwrite(‘filename.jpg’, image) with ‘filename.jpg’ is the name of the file you want to save as
We can see now in the example i type the command cv2.imwrite(‘trooper_small.jpg’, img) and the storm trooper image now is saved with the trooper_small.jpg filename
3. Resize Image
Sometimes when doing a Computer Vision for instance when you need to use your image data as a model deep learning input, you need to specify your image size to match the model input. In OpenCV there is a function that can help you with that which is cv2.resize()
I’m going to give you an example on ow to use the resize function
Here I’m upscaling the size of the image to 500 height and 500 width. The interpolation parameter is used to set the interpolation that is used in resizing the image, in this example I’m using nearest interpolation, if you want to know more about what other interpolation available visit here
Here’s another example of me using resize function. This time I’m downscaling the image to 100 height and 100 width.
4. Crop Image
Another great image processing technique you can incorporate by using OpenCV is image cropping. Let’s say you only want to use specific part of the image as an input for to train your model or maybe to do a data inference. Then you can solve that problem by cropping your image for the specific part
If you want to crop your image using OpenCV, you can do it by using slicing method on the image array. I will give you an example where I’m gonna crop the storm trooper image from the upscaled image.
Here you can see me using slicing method to set the cropping coordinate for the storm trooper image. If you want to crop another part of the image you can change the slicing value to your desired outcome.
5. Image Rotation
In OpenCV there is a simple function to rotate your image using cv2.flip(). I will give you an example on how to use this flip() function. First I’m going to load a new image data.
There is 2 input for cv2.flip() function, the first one is the image that you wanna flip and the second one is the flip code, 1 will flip the image along x-axis or a horizontal flip and 0 will flip the image vertically or along the y-axis.
Using cv2.flip() are a good alternative for data augmentation in case you need your picture from a different angle. But there’s a limitation to it which is when you use cv2.flip() you cannot set how much degree the image rotation is done. The flip will always be 180 degree. So you can’t always use it for data augmentation.
There’s another method by using cv2.getRotationMatrix2D() to define the rotation matrix and combine it with cv2.warpAffine() function to transform the image array with rotation matrix.
To create the rotation matrix first you need to find the image center. You can do that by dividing the height and width of image by 2. After that you can set how much the rotation is done by imputing a value in the angle input. The angle input are going to decide how much degree of rotation that is done by the images. After you have the rotation matrix you can use the cv2.warpAffine() function to transform the images according to the rotation matrix.
6. Image Filtering with Convolution
Another awesome feature of OpenCV is the ability to filter the image using convolution kernel. Image filtering are used to reduce noise in the image and create a blurring and smoothing effect. I’m going to give you an example of its usage.
In this example I’m using cv2.filter2D() function with identity matrix as the convolution kernel to filter the images. The result shows a blurred images after filtering was done. You can adjust the kind of filtering you want to use by defining different kernel matrix.
There is another common filtering method that OpenCV can do and have its own dedicated function which is gaussian blur. I’m about to show you an example of it’s usage.
In these example you can see that the function i use is cv2.GaussianBlur() and there’s three argument I imputing , the first one is the image that i want to apply gaussian blur. The second arguments is the kernel size for the gaussian blur. The last argument is the kernel standard deviation.
7. Edge detection
The last basic function of OpenCV I’m about to explain is edge detection. which is to identify the boundaries or edges of object in image. The primary use of edge detection is to extract features of images to be used for object identification and classification.
Here is an example of edge detection. I’m using the cv2.sobel() function with input argument dx=1 and dy=0 to see the edge in the x direction.
In this example i reverse the argument from the last one with input dx=0 and dy=1, to see the edge of image from y direction.
In this last example I input both dx=1 and dy=1 to detect image edges from both the x axis and y axis.
As you can see from the three example you can detect image edges from multiple point of view and there’s a big difference in each result when you see it from multiple angle.
From the explanation above we can conclude that OpenCV is such a versatile library to use to tackle your everyday computer vision problems. I hope that this article spark an interest for you to use OpenCV.
With such a comprehensive library, there are still more of OpenCV function that haven’t been covered by this article that’s yet for you to discover.