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

Difference between value type and a reference type in iOS swift?

BigQuery Cursor equivalent and Dynamic SQL.

What is the difference between Quality Control and Quality Assurance?

Introducing my little app to track any product’s price on Amazon.

Inner API Gateway for Cloud With Kong

How to download audio from tumbler?

My Preparation Process for the LS 129 Written Assessment

4 outlines of mountains representing lessons 1–4, a plateau at lesson 5, and real mountains representing the assessments.

How Garbage First Garbage Collector (G1GC) affected the performance of our back-end

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

BIRCH- An Efficient Hierarchical Algorithm for Clustering Very Large Databases

Your Guide to Adventuring Aruba

My Graduation Speech to Journalists

Why Event Cameras Are The Next Step in Computer Vision