Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is OpenCV?
3.
Installation of OpenCV
4.
Implementation
4.1.
Reading an image 
4.2.
Writing an Image
4.3.
Display an Image
5.
Frequently Asked Questions 
5.1.
What is Computer Vision?
5.2.
What are the functions used to read, write and display the image?
5.3.
Where are OpenCV libraries installed?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Image Read-Write-Display in OpenCV

Author Anju Jaiswal
2 upvotes

Introduction

In simple words, the visual representation of something is an image, and it could be dealt with easily using computer vision (from a machine learning perspective).

Reading, displaying, and writing images are essential to image processing and computer vision. You'll need to read the pictures even when cropping, resizing, rotating, or to apply different filters to process images. So it would help if you mastered some basic operations.

This blog explains how we can read, write and display an image with the help of OpenCV.

Also See, Image Sampling 

What is OpenCV?

As we have already discussed, OpenCV is in detail. OpenCV is a python library. Its complete form is Open Source Computer Vision Library.

OpenCV is a widely used framework for computer vision model implementations and applications.

There are various applications such as face detection, tracking moving objects, and object disclosure such as face mask detection, social distancing, etc. If you are interested in more depth about OpenCV, check these Coding Ninjas.

Installation of OpenCV

Hoping you are already familiar with the anaconda prompt, run the following command to install  OpenCV.

Implementation

The image used in this blog is Aster. Download it in your local system, and start implementing the function step by step.

Reading an image 

Syntax: cv2.imread(para1,para2)

Para1: Path of the image

Para2: The way the image should be read (default is cv2.IMREAD_COLOR)

Return: This method returns an image loaded from the specified file.

The waitKey() function is a keyboard-binding function.

  • It takes a single argument: the time (in milliseconds) during which a window will be displayed.
  • If the user presses any key during this time, the process continues.
  • Once 0 is passed, the system permanently waits for a critical click.
  • You can also set a function for specific keystrokes, such as a Q key or ESC key on the keyboard.

 

The destroyAllWindowsA() function eliminates all the windows we have created. If a particular window needs to be demolished, assign that specific window name as an argument. Using destroyAllWindows () also deletes a window or image in the system's main memory.

#Importing OpenCV

import cv2

#Loading the image
img = cv2.imread("Aster.jpg",cv2.IMREAD_COLOR)

cv2.imshow("FLower",img)

# waitKey() waits for a key press to close the window and 0 specifies indefinite loop
cv2.waitkey(0)

#cv2.destroyAllWindows() simply destroys all the windows we created.

cv2.destroyAllWindows()

 

Output:

 

cv2.IMREAD_GRAYSCALE: It specifies loading an image in grayscale mode. Alternatively, we can pass the integer value 0 for this flag.

#Load image in grey
gimg = cv2.imread("Aster.jpg",cv2.IMREAD_GRAYSCALE)
cv2.imshow("gray image",gimg)
cv2.waitkey(0)
cv2.destroyAllWindows() 

 

Writing an Image

Syntax: cv2.imwrite(Para_1,Para_2)

Para_1: The first parameter is the filename, including file name extension (for example, .png, .jpg etc.). OpenCV uses this filename extension to specify a file format.

Para_2:The second argument is the image you want to save.

Return value: It returns the true if the image is saved successfully.

#Importing opencv
import cv2
#Loading the image
img = cv2.imread("Aster.jpg",cv2.IMREAD_COLOR)
filename = "saveimg.jpg"
cv2.imwrite(filename,img)

 

As we can see, the 'Aster.jpg' image file got saved as a 'saveimg.jpg' file in the folder.

Display an Image

Syntax: cv2.imshow(Para_1,Para_2)

Para_1: A string representing the window name in which the image is displayed.

Para_2: It is the image that we want to display

Return value: It doesn't return anything.

#Importing opencv
import cv2
#Loading the image
img = cv2.imread("Aster.jpg",cv2.IMREAD_COLOR)
cv2.imshow("Flower",img)
cv2.waitkey(0)
cv2.destroyAllWindows

 

Output:

Also read, Sampling and Quantization

Frequently Asked Questions 

What is Computer Vision?

A field of Artificial intelligence that help computers to understand the content of the digital image and gain meaningful insights.

What are the functions used to read, write and display the image?

For reading the image cv2.imread() , for writing cv2.imwrite() and for diplaying cv2.imshow() functions are used in OpenCV.

Where are OpenCV libraries installed?

OpenCV libraries are directly installed under the lib folder in your OpenCV directory on Windows and, in the case of ubuntu, present in the folder /usr/lib.

Conclusion

Here, you learned to use the, imread (), imshow (), and imwrite () work for reading and displaying images, waitKey () and destroyAllWindows () functions, and display functionality, closing an image window by pressing a key and clearing any opened image window from memory. Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses, refer to the mock test and problems; look at the interview experiences and interview bundle for placement preparations.

Keep Learning!

Live masterclass