Analytics Vidhya
Published in

Analytics Vidhya

Python Tip #7: OpenCV — CV2: imread() and resize() | Dev Skrol

Photo by Hari Nandakumar on Unsplash

In this article we are going to learn some of the major use of OpenCV or CV2.

OpenCV is a library mainly used to read/write and manipulate images.

You can find the official page here https://pypi.org/project/opencv-python/.

Installation: pip install opencv-python

Importing Library: import cv2

Supported Versions: 3.6, 3.7, 3.8, 3.9

Basic understanding of image:

We all know that an image is nothing but an array of pixels.
Each pixel is a number.

Based on the number the color of the pixel will be.

Example: 0 — Black and 1 — White.

Read Image: cv2.imread()

Lets consider we have a small image of 28 X 28 pixels.

import cv2 
##Read image
image_path="4.png"
img=cv2.imread(image_path,0)
#Now we have the image in img variable.
#To visualize the image we can use matplotlib.pyplot
import matplotlib.pyplot as plt
plt.imshow(img, cmap="gray")

From the above program, you can see that we have a image “4.png”.
cv2.imread(filename, 0) — Read and returns the image. 0 mentions the color of the image. 0 as second parameter will read the image in gray-scale mode. i.e. the image will only be a 2D array and the 3rd dimension for color will not be added.

This image can be visualized using matplotlib.pyplot imshow(img). If we do not mention the cmap value, matplotlib will automatically assign a colormap to it.

The shape of this image can be found using the shape attribute as it is an array of pixels(again numbers).

Lets explore the values of the array.

The array shape is 2D 28 X 28. To have a good view of array in notebook result, I have resized the image to 18 X 18.

import numpy as np 
number_arr = np.asarray(img)
img_resized = cv2.resize(img, (18,18))
print(np.asarray(img_resized))

You can see that there is a 4 shape in the array where numbers are > 0.

Interesting! isn’t it? Enjoy!

We will meet with a new tip in Python. Thank you! 👍

Like to support? Just click the heart icon ❤️.

Happy Programming!🎈.

You may like:

Digital Image Processing — Morphological Operations (Erosion & Dilation) explained in detail with OpenCV

A Tip A Day — Python Tip #8: Why should we Normalize image pixel values or divide by 255?

Originally published at https://devskrol.com on February 11, 2021.

--

--

--

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Recommended from Medium

Employee rostering using Neo4j

The benefits of Service Level Agreements in custom Enterprise software consulting

From Uruguay to Spain in a blink of an eye

Level 0 — Coding Standard with EditorConfig

EditorConfig | A coding standard system

What the Heck Is an Open Source Program Office Anyway?

Opportunities in your pocket

Externalise application configs for your Spring Boot apps using AWS Parameter Store

How to curl single file using access token in gitlab

Alt Text

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Asha Ponraj

Asha Ponraj

Data Science & Machine Learning Enthusiast | Software Developer | Blogger | https://devskrol.com/ | www.linkedin.com/in/asha-ponraj-a5a76b50

More from Medium

How to tap into the asymmetric power of small acts

The Inexorable Relationship Between Autonomy and a Lithium-Dependent Green Future

Dog Breed Classifier

How to Add an Interactive Map to Your React.js Project— Mapbox “react-map-gl”(Hooks)