Skip to main content

OpenCV part-1 Blog

The beauty of OpenCV:

In this article, i have written about the basic but useful operations we can do with OpenCV. OpenCV is a major tool which we can use for major deep learning algorithms. This is part one of the whole topic, for the next one you can see the link at the end. In the part-1, i have discussed some operations on images. In the next part, i have discussed video source operation like canny edge detection, smoothing and blurring, morphological transformation and many more. So without wasting any time let's get started

***important thing: I have used the jupyter notebook for deployment, if you wish to use your IDE then go on otherwise i will also write one more article on how to install Jupyter notebook using anaconda

Introduction:

OpenCV (Open source Computer Vision) is a Python programming function mainly aimed at real-time computer vision. This library majorly works on enabling computers to see, visualize and gives the output as the same human eye does. OpenCV is basically used to accelerate the use of Machine Learning and deep learning algorithms. These algorithms can be used to detect  & recognize faces, objects, Persons, license plate reading, optical character recognition etc. In this article, i am teaching you some basic operations of OpenCV which can be performed for deep learning and machine learning algorithms.


Installation for Linux users:

In order to use OpenCV,  you will need two main libraries with an optional third ->OpenCV, Numpy, Matplotlib.
First, you may need to install python3-pip
pip3 install numpy
pip3 install matplotlib
sudo apt-get install python3-OpenCV

matplotlib is a choice because it is an optional choice for displaying videos or frames other than OpenCV. Numpy is used for all the things "Number and Python". 
If you want to check whether the installation is successful or not then you can check it by using two methods, one is by checking all the library which has been installed in your python environment by using the command pip3 freeze
After running the following command, you will see the output like this:




Another way of doing this is to type python3 on your terminal and type:
import cv2
import numpy
import matplotlib
If you are getting no such error, then you are ready to go.

Some Basic understanding:

First of all, we all need to understand some basic paradigms when it comes to image and video analysis. As every video camera recording today, are nothing but just frames, displayed one after i,e 30-60+ times a second.
Next, a lot of image and video analysis tends down to simple source as much as possible. So we will try to convert each image into grayscale to make it simpler.  But on the other side, it might be a color filter or gradient or may be a combination of these two. Once the transformation of images is done, then analysis, then it will jump to its original form eventually. So without wasting any time we now further proceed. And one more thing, it all can be done with your basic webcam.

Loading images in OpenCV:

here's the code loading images using OpenCV python
import numpy as np
import cv2
from matplotlib import pyplot as plt
img=cv2.imread('#YOUR IMAGE FILE',cv2.IMREAD_GRAYSCALE)
cv2.waitkey(0)
cv2.destroyAllwindows()

first, we have imported a few things then we have used cv2.imread in order to read the image. Above we have used cv2.IMREAD_GRAYSCALE which will convert out input image into grayscale. 
The default mode is IMREAD_COLOR which is the image without any alpha channel(degree of opaqueness). Instead, we can also use number i.e 0 for grayscale, 1 for color, -1 for unchanged.
once we loaded the image, cv2.waitKey(0) is used to wait until any key is pressed, and then cv2.destroyAllwindows() will destroy all the windows

In order to save the image, we will use cv2.imwrite('#image new path name',#image)

Loading video Source:

 Now we will cover some basic operation for loading video source in OpenCV python

import numpy as np
import cv2
cap=cv2.VideoCapture(0 # for camera)
while(True):
             ret,frame=cap.read()
             gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
             cv2.imshow('frame',gray)
             if waitKey(1) & 0xFF==ord('q'):
                       break
cv2.destroyAllwindows()
cap.release()

The code above written is the code for loading video source using OpenCV python, first of all, we have imported some important libraries, then cv2.Videocapture(0) will return video from your webcam, now we have defined an infinite loop (which can be terminated by using the break statement which has written further). Then we have ret and frame which is equal to cap.read(). Basically, ret is boolean regarding whether or not there is return or not, all the frame in each frame is returned or not. Now first we should know that OpenCV reads colors as BGR(Blue, Green, Red), while most of the computers read an image as RGB(Red, Green, Blue). gray is a variable defined to convert the image into grayscale, then using cv2.imshow we have shown the video frames in BGR form.  if waitKey(1) & 0xFF==ord('q'):break, this statement runs once each frame and if we get press keyword  'q' then it will break. After that, we just called the functions to release videos.


***Important thing->My webcam is not working currently so, i have tested everything using a video file which is either .avi or .mp4. If you wish to do the same then instead of using 0 in function use path to your video file. For instance.. cv2.Videocapture('/home/music/openCVtutorial/video.mp4').


Drawing and Writing on Image:

By using OpenCV we can draw various shapes on images and even can write text on it, and as we humans can easily check out if our program is working easily as we hope.
So as usual our code look someting like this:
import numpy as np
import cv2
img=cv2.imread('#your image path',cv2.IMREAD_COLOR)#we are getting image in original format
cv2.line(img,(0,0),(150,150),(255,255,255),15)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllwindows()

Now, cv2.line() takes arguments:image name, start coordinates, end coordinates, color(BGR), line thickness 
here's the result:

Same for the rectangle:
cv2.rectangle(img,(0,0),(150,150),(255,255,255),20)
here's the result:

what if we want to write some text over there likewise if we want to show that its a rectangle. NO worries, its a bit easyyyy.....

font = cv2.FONT_HERSHEY_SIMPLEX

cv2.putText(img,'OpenCV Tuts!',(0,130), font, 1, (200,255,155), 2, cv2.LINE_AA)
here's the result:
We can also draw a circle on our image. To do that we just have to modify our draw function likewise:
cv2.circle(img,(100,69),55,(255,255,255),-1)
here, the parameters are an image, center of the circle, radius, color, thickness(here -1 is for thickness.)
here is the result:

Some arithmetics operations using OpenCV:

Now, we will go through some basic operations which we can do by using OpenCV, such as adding two images, inserting template on an image etc.
Every video is break into frames, and each frame break into pixels which is stored in rows and columns within frame. We will start that by reading an image:
import numpy as np
import cv2
img=cv2.imread('#your image path',cv2.IMREAD_COLOR)
px=img[55,55]
img[55,55]=[255,255,255]
px=img[55,55]
px=img[100:150,100:150]#Region of interference
img[100:150,100:150]=[255,255,255]
new_face=img[37:111,107:194]
img[0:74,0:87]=new_face\
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllwindows()

First of all, we imported important libraries, as usual, then we have read the image in color format(not in grayscale). Then after that, we reference some pixels and change it to the color format which doesn't seem a big deal for you i guess. Once we change pixels into a color format we took a small part of an image and then plot it to the original image.
here's the result:
well, you can see that a small part of an image which i have defined plotted to the corner of the original image.

Now, we will see some more operations on the image. Likewise, adding two images.
here's the code:
import cv2
import numpy as np
img1=cv2.imread('#your image path')
img2=cv2.imread('#your image path')
add=img1+img2
cv2.imshow('add',add)
cv2.waitKey(0)
cv2.destroyAllwindows()

***important thing->In order to add two images, there is one constraint that the size of both images must be the same because the color of the image ranges from 0-255.  I have added two same kinds of images so mine will visualize differently.
here's the result:
instead we also can write
cv2.add(img1,img2)

Thresholding in OpenCV:

In this section, we will cover thresholding for image and video analysis. The idea of thresholding is to simplify visual data for analysis. First, we will convert an image into grayscale which has a maximum value up to 255, the role of thresholding is to convert into white or black, based on its value. For instance, if we set a threshold of 130, then everything under 130 would convert into black and above 130 would convert into white.
Now we will do it practically.
import numpy as np
import cv2
img=cv2.imread('#your image path')
retval, threshold=cv2.threshold(img,12,255,cv2.THRESH_BINARY)
cv2.imshow('original',img)
cv2.imshow('threshold',threshold)
cv2.waitKey(0)
cv2.destroyAllwindows()
here for a cv2.threshold function, we took parameters as the image, threshold value, maximum threshold, type of threshold. Normally we will take threshold value between 125-255, But my image is a low light picture so i have a minimum to be 10.
here's the result:



but this image is visually better but hard to read. So instead we will first convert our image into grayscale.
so we will add a new line before the cv2.threshold function
grayscaled=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
retval, threshold=cv2.threshold(grayscaled,12,255,cv2.THRESH_BINARY)
here's the new result:
This is readable but we still can do it better by using adaptive threshold.
threshold=cv.adaptiveThreshold(grayscaled,12,255,cv2.THRESH_BINARY)

here's the new result:

Now you can see that it is quite visible than before.
That's all for the OpenCV part-1, for next session Here's the link:

https://tdbtech.blogspot.com/2019/02/opencv-part-2-blog.html
For the last session:
https://tdbtech.blogspot.com/2019/02/opencv-part-3-blog.html

Comments

Post a Comment

Popular posts from this blog

OpenCV part-2 Blog

The beauty of OpenCV Vol-2: In this article, i would like to talk about some more operations which we can perform using OpenCV. In my last article or the part-1 of this article, I've talked about the various operations which we can perform on images using OpenCV. Now in the second part of the article, i would like to share some more on OpenCV like Thresholding, Canny edge detection, Morphological transformations, Blurring and Smoothing and many more. So without wasting any time, let's get started. ***important thing->  Here's the link of the first part of this article,  https://tdbtech.blogspot.com/2019/02/opencv-part-1-blog.html Color Filtering in OpenCV: Here, we are going to cover some color filtering operations using OpenCV, revisiting bitwise operation, and we will also specify a certain color in order to just show it. Have you guys wondered how the area of green screen removes in the VFX or Superhero based movies?. Just like that, we will filter

OpenCV part-3 Blog

The beauty of OpenCV Vol-3. In this final article, i would like to discuss some advance operation which we can perform using OpenCV. Here in this article, we are going to discuss Grabcut foreground extraction, Feature matching, Haar cascade object, MOG background. In my last two articles, i have discussed some basic operations which can be done by using OpenCV like reading an image, loading video source, Drawing and writing on images, thresholding, Canny Edge Detection etc, these were just basic operations which are basically in order to understand how things would look like when you are doing computer vision projects likewise, how does your own model would print the type of object at on a single frame, how do the bounding boxes print on a frame, how do we show the frames while running our model, how do we test our results on a video frame and many more. If you don't get this, i will write a blog in the future on how the computer vision projects work and what are the backend