Binarization of Image using NumPy (2024)

In this article, we will learn how to binarize an image using NumPy, and of course, we will use OpenCV for reading the image both in grayscale and RGB.

To understand what binary is — binary is something that is made of two things. In computer terminology, binary is just 0 and 1. If we were to relate the same in the images, it is said as black and white images where —

  • 0 signifies Black
  • 1 signifies White.

At the initial stages of learning image processing, we often think of a grayscale image as a binary image. Although it is not. But slowly when we pick up the subject, we realize how wrong we were. So, moving ahead, we will learn how to binarize the image with both using the library and without using the library (NumPy is used for matrix operations just to avoid the slowness of the program when used the regular for loops). Besides this, we will also use Matplotlib to plot the results.

The binary operation works really well for the grayscale images. The problem with the color (RGB) images is that each pixel is a vector representing 3 unique values one for Red, one for Green, and one for Blue.

A typical grayscale image’s matrix would look like —

array([[162, 162, 162, ..., 170, 155, 128],
[162, 162, 162, ..., 170, 155, 128],
[162, 162, 162, ..., 170, 155, 128],
...,
[ 43, 43, 50, ..., 104, 100, 98],
[ 44, 44, 55, ..., 104, 105, 108],
[ 44, 44, 55, ..., 104, 105, 108]], dtype=uint8)

A typical RGB image’s matrix would seem like —

array([[[226, 137, 125], ..., [200, 99, 90]],
[[226, 137, 125], ..., [200, 99, 90]],
[[226, 137, 125], ..., [200, 99, 90]],
...,
[[ 84, 18, 60], ..., [177, 62, 79]],
[[ 82, 22, 57], ..., [185, 74, 81]],
[[ 82, 22, 57], ..., [185, 74, 81]]], dtype=uint8)

If we were to separate R, G, and B pixels from the above matrix. We get.

R matrix

array([[226, 226, 223, ..., 230, 221, 200],
[226, 226, 223, ..., 230, 221, 200],
[226, 226, 223, ..., 230, 221, 200],
...,
[ 84, 84, 92, ..., 173, 172, 177],
[ 82, 82, 96, ..., 179, 181, 185],
[ 82, 82, 96, ..., 179, 181, 185]], dtype=uint8)

G matrix

array([[137, 137, 137, ..., 148, 130, 99],
[137, 137, 137, ..., 148, 130, 99],
[137, 137, 137, ..., 148, 130, 99],
...,
[ 18, 18, 27, ..., 73, 68, 62],
[ 22, 22, 32, ..., 70, 71, 74],
[ 22, 22, 32, ..., 70, 71, 74]], dtype=uint8)

B matrix

array([[125, 125, 133, ..., 122, 110, 90],
[125, 125, 133, ..., 122, 110, 90],
[125, 125, 133, ..., 122, 110, 90],
...,
[ 60, 60, 58, ..., 84, 76, 79],
[ 57, 57, 62, ..., 79, 81, 81],
[ 57, 57, 62, ..., 79, 81, 81]], dtype=uint8)

Whatever operation we compute on the grayscale image, we will need to compute the same on the RGB image but for 3 times separating R, G, and B pixels and finally merging them as a proper RGB image.

Binarization of Image using NumPy (2024)
Top Articles
How to Minimize the Risk of Lost Packages
What Is the SHA-256 Algorithm & How It Works - SSL Dragon
Ffxiv Act Plugin
Windcrest Little League Baseball
Wannaseemypixels
Mama's Kitchen Waynesboro Tennessee
Seething Storm 5E
Ecers-3 Cheat Sheet Free
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
South Ms Farm Trader
David Turner Evangelist Net Worth
No Hard Feelings Showtimes Near Cinemark At Harlingen
[Birthday Column] Celebrating Sarada's Birthday on 3/31! Looking Back on the Successor to the Uchiha Legacy Who Dreams of Becoming Hokage! | NARUTO OFFICIAL SITE (NARUTO & BORUTO)
Bj Alex Mangabuddy
List of all the Castle's Secret Stars - Super Mario 64 Guide - IGN
Vanessawest.tripod.com Bundy
TBM 910 | Turboprop Aircraft - DAHER TBM 960, TBM 910
Libinick
Persona 5 Royal Fusion Calculator (Fusion list with guide)
Amazing deals for Abercrombie & Fitch Co. on Goodshop!
Doublelist Paducah Ky
Ceramic tiles vs vitrified tiles: Which one should you choose? - Building And Interiors
Reviews over Supersaver - Opiness - Spreekt uit ervaring
2487872771
Margaret Shelton Jeopardy Age
Doctors of Optometry - Westchester Mall | Trusted Eye Doctors in White Plains, NY
Usa Massage Reviews
Gunsmoke Tv Series Wiki
Busch Gardens Wait Times
Missing 2023 Showtimes Near Grand Theatres - Bismarck
Pch Sunken Treasures
Nicole Wallace Mother Of Pearl Necklace
Ni Hao Kai Lan Rule 34
Unity Webgl Player Drift Hunters
Louisville Volleyball Team Leaks
Temu Y2K
Qlima© Petroleumofen Elektronischer Laserofen SRE 9046 TC mit 4,7 KW CO2 Wächter • EUR 425,95
Metro Pcs Forest City Iowa
All Obituaries | Sneath Strilchuk Funeral Services | Funeral Home Roblin Dauphin Ste Rose McCreary MB
Emily Tosta Butt
Nina Flowers
Bustednewspaper.com Rockbridge County Va
Squalicum Family Medicine
Gas Buddy Il
Lorton Transfer Station
Conan Exiles Colored Crystal
303-615-0055
9294027542
4Chan Zelda Totk
Sleep Outfitters Springhurst
Pilot Travel Center Portersville Photos
Latest Posts
Article information

Author: Clemencia Bogisich Ret

Last Updated:

Views: 5694

Rating: 5 / 5 (80 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Clemencia Bogisich Ret

Birthday: 2001-07-17

Address: Suite 794 53887 Geri Spring, West Cristentown, KY 54855

Phone: +5934435460663

Job: Central Hospitality Director

Hobby: Yoga, Electronics, Rafting, Lockpicking, Inline skating, Puzzles, scrapbook

Introduction: My name is Clemencia Bogisich Ret, I am a super, outstanding, graceful, friendly, vast, comfortable, agreeable person who loves writing and wants to share my knowledge and understanding with you.