Home Artificial Intelligence OpenCV Tutorial in Python – GreatLearning

OpenCV Tutorial in Python – GreatLearning

0
OpenCV Tutorial in Python – GreatLearning

[ad_1]

opencv tutorial
City Deep Learning
  1. What is Computer Vision?
  2. How does a computer read an image?
  3. What is OpenCV?
  4. OpenCV Installation
  5. Read & Save Images 
  6. Basic Operation On images
  7. OpenCV Resize Image
  8. OpenCV Image Rotation
  9. OpenCV Drawing Functions
  10. OpenCV Blob Detection
computer vision

What is Computer Vision?

The term Computer Vision (CV) is used and heard very often in artificial intelligence (AI) and deep learning (DL) applications. The term essentially means giving a computer the ability to see the world as we humans do.

Computer Vision is a field of study which enables computers to replicate the human visual system. As already mentioned above, It’s a subset of artificial intelligence which collects information from digital images or videos and processes them to define the attributes. The entire process involves image acquiring, screening, analysing, identifying and extracting information. This extensive processing helps computers to understand any visual content and act on it accordingly. 

Computer vision projects translate digital visual content into explicit descriptions to gather multi-dimensional data. This data is then turned into a computer-readable language to aid the decision-making process. The main objective of this branch of artificial intelligence is to teach machines to collect information from pixels. 

How does a computer read an image?

How does a human mind apprehend an image? When you see the image below,what do you actually see and how do you say what is in the Image?

You  most probably look for different shapes and colours in the Image and that might help you decide that this is an image of a dog. But does a computer also see it in the same way? The answer is no.

A digital image is an image composed of picture elements, also known as pixels, each with finite, discrete quantities of numeric representation for its intensity or grey level. So the computer sees an image as numerical values of these pixels and in order to recognise a certain image, it has to recognise the patterns and regularities in this numerical data.

Here is a hypothetical example of how pixels form an image. The darker pixels are represented by a number closer to the zero and lighter pixels are represented by numbers approaching one. All other colours are represented by the numbers between 0 and 1. 

But usually, you will find that for any colour image, there are 3 primary channels – Red, green and blue and the value of each channel varies from 0-255. In more simpler terms we can say that a digital image is actually formed by the combination of three basic colour channels  Red, green, and blue whereas for a grayscale image we have only one channel whose values also vary from 0-255.

What is OpenCV?

OpenCV ( Open Source Computer Vision Library) is an open source software library for computer vision and machine learning. OpenCV was created to provide a shared infrastructure for applications for computer vision and to speed up the use of machine perception in consumer products. OpenCV, as a BSD-licensed software, makes it simple for companies to use and change the code. There are some predefined packages and libraries that make our life simple and OpenCV is one of them

Gary Bradsky invented OpenCV in 1999 and soon the first release came in 2000. This library is based on optimised C / C++ and supports Java and Python along with C++ through an interface. The library has more than 2500 optimised algorithms, including an extensive collection of computer vision and machine learning algorithms, both classic and state-of-the-art.Using OpenCV it becomes easy to do complex tasks such as identify and recognise faces, identify objects, classify human actions in videos, track camera movements, track moving objects, extract 3D object models, generate 3D point clouds from stereo cameras, stitch images together to generate an entire scene with a high resolution image and many more.

Python is a user friendly language and easy to work with but this advantage comes with a cost of speed, as Python is slower to languages such as C or C++.So we extend Python with C/C++, which allows us to write computationally intensive code in C/C++ and create Python wrappers that can be used as Python modules. Doing this, the code is fast, as it is written in original C/C++ code (since it is the actual C++ code working in the background) and also, it is easier to code in Python than C/C++. OpenCV-Python is a Python wrapper for the original OpenCV C++ implementation.

OpenCV installation

There are many ways in which you can install OpenCV on your computer. Here are some:

Install using Anaconda

Anaconda is a conditional free and open-source distribution of the Python and R programming languages for scientific computing, that aims to simplify package management and deployment. You can download it from here and install it.

After successfully installing anaconda, just go to the anaconda prompt and use this command to install OpenCV:

conda install -c conda-forge opencv  

After this command is successfully executed, OpenCV will be available on your computer.Now let us see some other ways to install OpenCV

For Windows

You can use pip to install OpenCV on windows. Pip is a de facto standard package-management system used to install and manage software packages written in Python and it usually comes in installed when you install Python. If you do not have Python installed, I would suggest download it from here. Use this command in the command prompt to install OpenCV:

pip install opencv-python  

After installing it,do check if it is installed successfully.For that just go to the command prompt and type ‘python’ and hit enter.You should see some message like this:

If this is not the message you see, I suggest reinstalling python into your system. Next type import cv2 and if there is no error then it is installed successfully.

For Mac

You can use homebrew to install OpenCV as it makes it really easy and you just have to use this command for installing:

brew install opencv

Now that you have installed the OpenCV onto your system, let’s see how it works.

Read & Save Images

Now for OpenCV to work on any image, it must be able to read it. Here we will see how to read a file and save it after we are done with it. Let’s see how to do it:

Imread function in OpenCV

We use the imread function to read images,here is the syntax of this function

cv2.imread(path, flag)

The path parameter takes a string representing the path of the image to be read.The file should be in the working directory or we must give the full path to the image.The other parameter is the flag which is used to specify how our image should be read. Here are possible values that it takes and their working:

cv2.IMREAD_COLOR: It specifies to convert the image to the 3 channel BGR 
colour image. Any transparency of image will be neglected. It is the default
flag. Alternatively, we can passinteger value 1 for this flag.
cv2.IMREAD_GRAYSCALE: It specifies to convert an image to thesingle channel 
grayscale image. Alternatively, we can pass integer value 0 for this flag.
cv2.IMREAD_UNCHANGED: It specifies to load an image as such including alpha 
channel.Alternatively, we can pass integer value -1 for this flag.

Usually the method imread() returns an image that is loaded from the specified file but in case the image cannot be read because of unsupported file format, missing file, unsupported or invalid format, it just returns a matrix. Here is a example in which we read an image from my storage.

#importing the opencv module  
import cv2  
# using imread('path') and 1 denotes read as  color image  
img = cv2.imread('dog.jpg',1)  
#This is using for display the image  
cv2.imshow('image',img)  
cv2.waitKey() # This is necessary to be required so that the image doesn't close immediately.  
#It will run continuously until the key press.  
cv2.destroyAllWindows() 

Imwrite function in OpenCV

We can use OpenCV’s imwrite() function to save an image in a storage device and the file extension defines the image format as shown in the example below. The syntax is the following:

cv2.imwrite(filename, image)  
Parameters:
filename: A string representing the file name. The filename must include image format.
image: It is the image that is to be saved.

 

Here is an example in which we use this function:

import cv2  
# read image  
img = cv2.imread(r'C:UsersMirzadog.jpeg', 1)  
# save image  
status = cv2.imwrite(r'C:UsersMirzadog.jpeg',img)  
print("Image written sucess? : ", status)  

If the file is successfully written then this function returns True and thus it is important to store the outcome of this function.In the example above,we have done the same and used the ‘status’ variable to know if the file is written successfully.

Basic Operation On images

In this section,we are going to discuss some of the basic operations that we can do on the images once we have successfully read them.The operations we are going to do here ae:

  • Access pixel values and modify them
  • Access image properties
  • Set a Region of Interest (ROI)
  • Split and merge image channels

Access pixel values and modify them

So there are basically two ways to access a pixel value in an Image and modify them.First let us see how we can access a particular pixel value of an image.

import numpy as np
import cv2 as cv
img = cv.imread(r'C:UsersMirzadog.jpeg')
px = img[100,100]
print( px )
Output:
 [157 166 200] 

Now as you can see we got a list containing 3 values.As we know OpenCV stores the color image as BGR color image,so the first value in the list is the value of the blue channel of this particular pixel, and the rest are values for green and red channels.

We can also access only one of the channels as shown below:

# accessing only blue pixel
blue = img[100,100,0]
print( blue )
Output
 157 

To modify the values, we just need to access the pixel and then overwrite it with a value as shown below:

img[100,100] = [255,255,255]
print( img[100,100] )
Output:
 [255 255 255] 

This method to access and modify the pixel values is slow so you should make use of NumPy library as it is  optimized for fast array calculations. For accessing individual pixel values, the Numpy array methods, array.item() and array.itemset() are considered better as  they always return a scalar. However, if you want to access all the B,G,R values, you will need to call array.item() separately for each value as shown below:

# accessing RED value
img.item(10,10,2)
>>59
# modifying RED value
img.itemset((10,10,2),100)
img.item(10,10,2)
>>100

Access Image properties

What do we mean by image properties here? Often it is important to know the size(total number of pixels in the image), number of rows, columns, and channels.We can access the later three by using the shape() method as shown below:

print( img.shape )
>>(342, 548, 3)
print( img.size )
>>562248

So here we have three numbers in the returned tuple,these are number of rows, number of columns and number of channels respectively.Incase an image is grayscale, the tuple returned contains only the number of rows and columns.

Often a large number of errors in OpenCV-Python code are caused by invalid datatype so img.dtype which returns the image datatype is very important while debugging.Here is an example:

print( img.dtype )
>>uint8

Image ROI(Region of interest)

Often you may come across some images where you are only interested in a specific region.Say you want to detect eyes in an image,will you search the entire image,possibly not as that may not fetch accurate results.But we know that eyes are a part of face,so it is better to detect a face first ,thus here the face is our ROI.You may want to have a look at the article Face detection using Viola-Jones algorithm  where we detect the faces and then find eyes in the area we found faces.

Splitting and Merging Image Channels

We can also split the channels from an image and then work on each channel separately.Or sometimes you may need to merge them back together,here is how we do it:

But this method is painfully slow,so we can also use the Numpy to do the same,here is how:

b,g,r = cv.split(img)
img = cv.merge((b,g,r))
b = img[:,:,0]
g = img[:,:,1]
r = img[:,:,2]

Now suppose you want to just set all the values in the red channel to zero, here is how to do that:

#sets all values in red channel as zero
img[:,:,2] = 0

OpenCV Resize Image

Usually when working on images,we often need to resize the images according to certain requirements.Mostly you will do such operation in Machine learning and deep learning as it reduces the time of training of a neural network. As the number of pixels in an image increases, the more is the number of input nodes that in turn increases the complexity of the model. We use an inbuilt resize() method to resize an image.

Syntax: cv2.resize(s, size,fx,fy,interpolation)  
Parameters:
s - input image (required).
size - desired size for the output image after resizing (required)
fx - Scale factor along the horizontal axis.(optional)
fy - Scale factor along the vertical axis.
Interpolation(optional) - This flag uses following methods:
 INTER_NEAREST – a nearest-neighbor interpolation
 INTER_LINEAR – a bilinear interpolation (used by default) 
 INTER_AREA – resampling using pixel area relation. It may be a preferred method for    image decimation, as it gives moire’-free results. But when the image is zoomed, it is similar to the INTER_NEAREST method.
 INTER_CUBIC – a bicubic interpolation over 4×4 pixel neighborhood 
 INTER_LANCZOS4 – a Lanczos interpolation over 8×8 pixel neighborhood

Here is an example of how we can use this method:

import cv2
import numpy as np

#importing the opencv module  
import cv2  
# using imread('path') and 1 denotes read as  color image  
img = cv2.imread('dog.jpg',1)  
print(img.shape)
img_resized=cv2.resize(img, (780, 540),  
               interpolation = cv2.INTER_NEAREST) 
cv2.imshow("Resized",img_resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

[ad_2]

Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here