Free Online Tutorials ==>> COVID-19 Projects (Using AI-ML) || Machine Learning || Python Programming || DBMS || OOPs using C++ || DSA || Java Programming || Linux/Unix || C Programming
Go to your University || Python Lab || DSA Lab || AI & Machine Learning Lab || Linux Lab || OOPs Lab || DBMS Lab || JAVA Lab ||| Free Online Tutorials ||| 
companies for Campus Placement || Logical Reasoning || Quantitative Aptitude || General English || Technical-MCQ and Interview Questions || HR Interview Questions
0 like 0 dislike
299 views
in Examples, Exercises and Projects by Goeduhub's Expert (7.8k points)

Opencv is used to extract information from images. Several operations on images like resizing, coloring, reading, writing and many more can be implemented using OpenCV. In this tutorial we have covered some of the commonly used OpenCV functions and operations.

1 Answer

0 like 0 dislike
by (695 points)
selected by
 
Best answer

OpenCV Operations

Introduction

OpenCV is an open-source library used for computer vision. The main purpose of OpenCV is to extract content of images. For more information on OpenCV visit Working of OpenCV.

 It allows us to do several operations on images like resizing, coloring, reading, writing and many more. In this tutorial we have covered some of the commonly used OpenCV functions and operations.

Operations of OpenCV


1. Importing Library

cv2 module in the root of Python's site-packages. In this, everything is returned as ndarray, lists,tuples,dictionary, etc. We first import python cv2 module to access opencv functions.

import cv2


2. Reading Images using OpenCV

Using imread function, we can read any image from our work spaces. 

Inside imread function, we pass the path of the image where it is present. 

image = cv2.imread('goedu.png')


3. Displaying Images using OpenCV

Using imshow function, we can show any image from our work spaces. 

Inside imshow function, we pass two parameters. 

Inside quotations, the window name is given with which we want to open our image.

And the second parameter is the image name that we want to display. 

cv2.imshow('Goeduhub',image)


4. WaitKey in OpenCV 

waitKey() is a keyboard binding function. 

It waits for specified milliseconds for any keyboard event. 

waitKey(0) will show the image infinitely until any key is pressed.

cv2.waitKey(0)


5. Destroying Windows in OpenCV

This destroys all the windows created.

cv2.destroyAllWindows()


6. imshow, waitKey and destroyAllWindows together

For smooth execution of the program run the three commands together so that after image is displayed it is waits for particular time period and then automatically destroys all windows. Otherwise without waitkey the kernel might hang and you will have to restart the kernel. 

This output displays a hanged kernel.

cv2.imshow('Goeduhub',image)

cv2.waitKey(0)  

cv2.destroyAllWindows()

 


7. Saving Images using OpenCV

Using imwrite we save the image in our local disk.

We are passing two parameters. 

In first parameter we are passing the name with which we want to save the image along with path

The second parameter is the name of the original image that it is to be saved.

cv2.imwrite('original.png',image)


8. Resizing Images using OpenCV

We can resize the images using resize function.

The first parameter is the image name

The second parameter is the width and height of the resized image respectively.

r = cv2.resize(image,(500,500))


9. Accessing Image Properties in OpenCV

  • Shape - We can access the shape of the image using shape function. It gives out three features. Height, width and no. of channels.

  • Height - The first or 0th element of shape is the height.

  • Width - The first or 1st element of shape is the weight.

  • Size - Using size function we can get the image size. 

  • Channels - The first or 2nd element of shape is the number of channels. Here 3 refers to blue, green and red channel.

# shape

shape = image.shape

# height

height = image.shape[0]  

# width

width = image.shape[1]  

# channels

channels = image.shape[2]  

# size

size1 = image.size 

# Printing all the properties

print("height : ",height)

print("width : ",width)

print("channels : ",channels)

print("size : ",size1)

print("Image Shape : ",shape)

height :  213
width :  237
channels :  3
size :  151443
Image Shape :  (213, 237, 3)

10. Region of Interest

Region of interest or popularly known as ROI is used for extracting only selected regions from the image.

Here we have passed the pixel values of row and column we want to select from the original image.

100:200 means from 100th row/column to 200th row/column.

roi = image[100:200,100:200]


11. Changing Color using OpenCV

We can change the color of the image by cvtColor function of cv2 module.

We are passing two parameters here. 

The first parameter is the image name of the original image that you want to change color of.

The second parameter is the name of color format in which we want to change.

In this example we are changing it to gray image. 

gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)


12. Splitting and Merging

An image's BGR channels can be split into their planes when needed. Then, the individual channels can be merged back together from the BGR image again.

We can split the channels using split function.

Then merge the three channels using merge function.

b,g,r = cv2.split(image)  

image = cv2.merge((b,g,r))  

  Realize your learning potential with courses starting at ₹ 420
 Placements:   List of companies | Logical Reasoning Questions | Quantitative Aptitude Questions | General English Questions | Technical-MCQ and Interview Questions
 Online Free Training:  MACHINE LEARNINGPython Programming | Database Management System(DBMS) | Object Oriented Programming(OOPs) using C++ | Data Structures and Algorithms(DSA) | Java Programming | Linux/Unix | C Programming
Exams: List of Exams After Graduation | List of Engineering Entrance Examinations (UG/PG) | JEE Main | JEE Advanced | GATE | IES | ISROList of PSUs
 Download Previous Year Papers For:  GATE | IES | RAJASTHAN TECHNICAL UNIVERSITY (RTU-Kota)RPSC Technical Exams | ISRO
 Goeduhub
About Us | Contact Us   Social::   |  | 
...