Analytics Vidhya
Published in

Analytics Vidhya

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

Photo by Hari Nandakumar on Unsplash

In our previous post A Tip A Day — Python Tip #7: OpenCV — CV2: imread() and resize(), we have explored a simple image and its pixel values.
The pixel values can range from 0 to 256. Each number represents a color code.
When using the image as it is and passing through a Deep Neural Network, the computation of high numeric values may become more complex.
To reduce this we can normalize the values to range from 0 to 1.

In this way, the numbers will be small and the computation becomes easier and faster.
As the pixel values range from 0 to 256, apart from 0 the range is 255. So dividing all the values by 255 will convert it to range from 0 to 1.

import cv2 
import matplotlib.pyplot as plt
##Read image
image_path="4.png"
img=cv2.imread(image_path,0)
plt.imshow(img, cmap="gray")

Output:

lets take a look at one of the white pizel’s value.

img[17,14] Output: 
253

I gave it a try to visualize the array values using seaborn heatmap and below is the result.

import seaborn as sns plt.figure(figsize=(30,30)) 
sns.heatmap(img, annot= True, cmap="binary")

Output:

In the above output the values range from 0 to 256.

Normalization:

import seaborn as sns img = img/255 plt.figure(figsize=(30,30)) 
sns.heatmap(img, annot= True, cmap="binary")

Output:

In this result, you can see that pixels that make the strokes in “4” image are 0.93 or 0.99 or near by values.

Hope you can understand why and how we do normalize simple images.

Lets explore more about images in upcoming posts.

More interesting Python tips:

A Tip A Day — Python Tip #6 — Pandas Merge

A Tip A Day — Python Tip #5 — Pandas Concat & Append

Best way to Impute NAN within Groups — Mean & Mode

https://devskrol.com/index.php/category/python/

Originally published at https://devskrol.com on February 19, 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

Python:Mutable, Immutable… everything is object!

EP. 1 — Installation of Godot Engine and configuration of a development environment for C#

Wix and Wordpress are Expensive So I Decided to Build and Host my Website Myself

Debugging and Testing in Golang

Reducing Enumerable — Part Two: Chartreuse, Master of Map

Custom CSS for heart rate overlays by Stromno

Algorithmic Thinking for Data Structure and Algorithms

How to Read Instagram Messages Without Seen in Hindi | Instagram Hacks 2021

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

The filter() Method in Simple English (JS)

Difference between Var, let and const in Javascript.

Python — Everything is object

The Best Way to Know What Is React