Thursday, December 26, 2024

Ever Explored an Image? Dive In Now!


If you have not, it is time you do it. Python lends itself to be the way to unveil the magic behind images. While python is the main tool you need libraries to help along.

There is a famous adage, if you want to control something you need to measure it first. In order to change or use image in a yet to unknown ways, you need to unravel an image. Image processing is all about how in myriad ways you can use images for aesthetic, scientific and medical fields. Python with a plethora of image and other supportive libraries such as PIL, OpenCV, NumPy, etc. is great for this purpose. First of all we need to understand Pixel, it is like understanding a point in geometry, the fundamental building block.

What is a pixel?

The unveiling of an image starts with the building block of images, the pixels - geometrical structures with very minute dimensions that have some color in them.

Pixels are actually square. Each pixel is a tiny square of color that, when combined with other pixels, forms the images you see on screens. This grid of square pixels is what allows digital displays to render images with precision and clarity. The concept of pixels being square is fundamental to how digital images are created and displayed. In a sense, pixels are abstract that depend on the device on which we see the image. The physical size of a pixel is device dependent. The physical size of a pixel has to be derived from a knowledge of the resolving power of the device, the dots per inch seen on the device. While in Micro LEDs they are about 50 microns they can be fraction of a millimeter in large LEDs.  

Images are a grid of pixels, each with a specific color. When viewed together, these pixels form the images we see.

What is NumPy and what is its role in image processing?

In the process of understanding images, NumPy plays a crucial role. This library helps to separate an image into three distinct parts: height, width, and color. Furthermore, it isolates the colors red, green, and blue, placing them into their respective channels. By dismantling the image with NumPy, we can access the five important components of an image. These components can be manipulated using various image libraries in Python.

Also, while NumPy can disassemble an image, it can also reconstruct an image and it is extremely versatile. NumPy depends for its speed on the core C programming constructs but implemented for a high level language like Python.

Finding the height and width of an image using NumPy in Python:

This program takes in an image and find its shape with three arguments, height, width and number of channels. You can also see the result of running this code in PyCharm.

This image Kiss.jpg has height and width of 7401 and 7376 with three colors.

Finding the height, width and colors:

The following code unravels the image (the python program is using PyCharm interface):



1. When this code is run the image dimensions (height and width) are returned.

2. It separates the 3 color channels into its constituents.

3. It gives three images and saves them to a files in the same project

In arriving at these results it has,

1. Reduced the size of image by resizing so that final image is not distorted, visible and without any display related artifacts. (When this code was run without resizing, it returned 3 very large images all in grey).

2. After resizing, I have extracted the three channel information. Remember, it is zero-based.

3. Each channel (red, green, and blue) is populated by values from the image in a two-step process. In the first step, an array with the same dimensions as the image is created, but all elements are initialized to zero for the chosen channel. In the second step, this array is filled with the values of the pertinent color from the image.

4. It creates separate images of the same image in separate colors, one each for red, green and blue and shows them.

5. It also writes to file, the images for each of these channels.

As you can see, we have the height, width and three separate images of the image looking at ony red, green and blue values in its pixels.






Here is the code, should you try to run for your image:

========================================

import cv2
import numpy as np

# Load the image
image = cv2.imread(r'C:\Users\hoden\PycharmProjects\exploreImage\Images\TheKiss.jpg')
# Get the dimensions of the image
height, width, channels = image.shape

# Print the dimensions
print(f'Width: {width} pixels, Height: {height} pixels')

# resizing the image to display smaller sizes on my display device
resized_image = cv2.resize(image, (200, 200))

# Extract individual color channels
blue_channel = resized_image[:, :, 0]
green_channel = resized_image[:, :, 1]
red_channel = resized_image[:, :, 2]

# Create separate images for each channel
blue_image = np.zeros_like(resized_image)
blue_image[:, :, 0] = blue_channel

green_image = np.zeros_like(resized_image)
green_image[:, :, 1] = green_channel # Assign green channel to green image

red_image = np.zeros_like(resized_image)
red_image[:, :, 2] = red_channel

# Display the extracted channels as images
cv2.imshow('Blue Channel', blue_image)
cv2.imshow('Green Channel', green_image)
cv2.imshow('Red Channel', red_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

# Save the extracted channels as images (optional)
cv2.imwrite('blue_channel.jpg', blue_image)
cv2.imwrite('green_channel.jpg', green_image)
cv2.imwrite('red_channel.jpg', red_image)



 

 





No comments: