How Image Processing Revolutionizes Data Science (Day -1)

How Image Processing Revolutionizes Data Science (Day -1)

ยท

4 min read

#Day 1: Unleash Your Inner Demon Slayer: Read and Manipulate Images with Tanjiro!

Intro:

Welcome, aspiring image slayers! Join Tanjiro Kamado from Demon Slayer as we embark on a 9-day journey to master image manipulation. Today, we learn the basics, just like Tanjiro trains relentlessly to hone his skills. Get ready to load, display, and explore images like never before!

I'd be glad to provide a comprehensive response that combines the strengths of previous responses, addresses feedback, and offers an exceptional step-by-step guide for Day 1 of your image processing series:

Prerequisites:

  • Libraries: Use pip (the Python package installer) to install the following libraries:
pip install pillow matplotlib imageio opencv-python

These libraries provide powerful tools for various image processing tasks:

  • Pillow (PIL Fork): For basic image loading, manipulating pixels, and saving results.

  • matplotlib: For visualizing images using plots and figures.

  • imageio: For handling diverse image formats and advanced loading/saving options.

  • OpenCV (Open Source Computer Vision Library): A comprehensive library for real-time computer vision, offering extensive image processing and manipulation capabilities.

Work Environment is Kaggle you can visit and explore it open source and free!!!!!!

Key Concepts:

1. Loading Images (Drawing Your Sword):

  • We'll use Python libraries like PIL, matplotlib, imageio, and OpenCV to access images, just like Tanjiro draws his Nichirin blade.

  • Each library has its own style, but they all allow us to "summon" images into our environment.

  • Choose an image you want to work with (e.g., tanjiro.png). Replace the path with your actual image location:

#import necessary libraries
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import imageio
import cv2

#you can use and of the following to open an image
# Using Pillow (PIL)
img_pillow = Image.open("path/to/tanjiro.png")

# Using matplotlib
img_matplotlib = plt.imread("path/to/tanjiro.png")

# Using imageio
img_imageio = imageio.imread("path/to/tanjiro.png")

# Using OpenCV (BGR by default)
img_opencv = cv2.imread("path/to/tanjiro.png", 1)  # 1 for color, 0 for grayscale

Note: "You can use any to open/Read the image but for now I am using PIL to import !!!!!!"

#import necessary libraries
from PIL import Image

tanjiro_image = Image.open("path/to/tanjiro.png")

2. Displaying Images (Visualizing the Enemy):

  • Similar to how Tanjiro analyzes his opponents, we'll use libraries like matplotlib to "see" the images we loaded as on subplot graph.

  • This helps us understand their content, colors, and structure.

Example (matplotlib):

import matplotlib.pyplot as plt

plt.imshow(tanjiro_image)
plt.show()

With OpenCV

  • By default, the image is saved in the BGR color space. As a result, the blue, green, and red color channels, respectively

  • OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.imread() method loads an image from the specified file. If the image cannot be read (because of the missing file, improper permissions, or unsupported or invalid format) then this method returns an empty matrix.

import cv2 as cv
import matplotlib.pyplot as pl
img = cv.imread('../input/digital-image/tanjiro.png',1)
pl.imshow(img)

3. Color Spaces (Adapting Your Form):

  • Images use different color spaces like RGB (red, green, blue) and BGR (blue, green, red).

  • Think of it like Tanjiro adapting his fighting style to different demons. We'll learn how to convert between these spaces.

Example (OpenCV):

import cv2

tanjiro_bgr = cv2.imread("path/to/tanjiro.png")
tanjiro_rgb = cv2.cvtColor(tanjiro_bgr, cv2.COLOR_BGR2RGB) #bgr to rgb

4. Image Dimensions (Assessing the Target):

  • Just like Tanjiro measures the size and shape of demons, we'll explore an image's width, height, and channels (color components),

  • If image is grayscale channe is =0: (width , height, channel)

  • This information helps us understand the image's structure and perform further analysis.

Example (OpenCV):

Python

print("Image dimensions:", tanjiro_bgr.shape)
Image Dimension: (662, 892)

Challenge:

  1. Experiment with different libraries and load multiple images. Display them side-by-side.

  2. Try converting the image to grayscale (removing color information).

  3. Extract specific information like pixel values or a particular color channel.

Remember: This is just the beginning! Like Tanjiro's training, mastering image processing takes practice. Keep exploring, experiment, and ask questions. Together, we'll become image manipulation masters!

Did you find this article valuable?

Support TechLearn India by becoming a sponsor. Any amount is appreciated!

ย