Power Law Transformation

Power Law Transformation

For instance, when you scan a document, the output image might have a lower quality than the original input image. We thus need a way to improve the quality of output images so they can be visually more expressive for the viewer, and this is where image enhancement comes into play.

When we enhance an image, what we are doing is sharpening the image's features, such as its contrast and edges.

It is important to note that image enhancement does not increase the information content of the image, but rather increases the dynamic range of the chosen features, eventually increasing the image's quality.

So here we actually don't know what the output image would look like, but we should be able to tell (subjectively) whether there were any improvements or not, like observing more details in the output image, for instance.

Image enhancement is usually used as a preprocessing step in the fundamental steps involved in digital image processing (i.e. segmentation, representation).

There are many techniques for image enhancement, but I will be covering two techniques in this tutorial: image inverse and power law transformation. We'll have a look at how we can implement them in Python. Let's get started!

Image Inverse

As you might have guessed from the title of this section (which can also be referred to as image negation), image inverse aims to transform the dark intensities in the input image to bright intensities in the output image, and bright intensities in the input image to dark intensities in the output image.

In other words, the dark areas become lighter, and the light areas become darker.

Let's start this tutorial by simplifying some theoretical concepts. When we talk about image enhancement, we essentially mean improving the quality and visual expressiveness of an image compared to its original version.

For example, when you scan a document, the resulting image may have lower quality than the original. In such cases, we need methods to enhance the output image, making it visually more appealing and informative for viewers.

Image enhancement involves techniques to sharpen features like contrast and edges.

It's important to note that image enhancement doesn't increase the information content of the image itself. Instead, it focuses on expanding the dynamic range of selected features, ultimately enhancing the image's overall quality.

Consequently, the specific appearance of the output image may not be known in advance, but improvements should be noticeable subjectively, such as observing more details.

Image enhancement is often used as a preprocessing step in digital image processing workflows, alongside other fundamental steps like segmentation and representation.

While various techniques exist for image enhancement, this tutorial will cover two specific techniques: image inverse and power law transformation. We'll explore how to implement these techniques using Python. Let's dive in!

Image Inverse:

As the name suggests, image inverse (also known as image negation) aims to transform the dark intensities in the input image into bright intensities in the output image, and vice versa.

In simpler terms, areas that are originally dark become lighter, while initially bright areas become darker.

To illustrate this concept, let's consider a code example in Python


import cv2 import numpy as np # Load the input image image = cv2.imread('input_image.jpg', 0) # Read the image in grayscale # Perform image inverse inverse_image = cv2.bitwise_not(image) # Display the original and inverse images cv2.imshow('Original Image', image) cv2.imshow('Inverse Image', inverse_image) cv2.waitKey(0) cv2.destroyAllWindows()         

In this example, we use the OpenCV library to read an input image in grayscale. Then, the cv2.bitwise_not() function is applied to obtain the inverse of the image. Finally, we display both the original and inverse images using cv2.imshow().

By running this code, you'll observe the transformation of dark areas to lighter ones and vice versa in the displayed images.

This concludes the explanation and code example for the image inverse technique. In the next section, we will explore the power law transformation technique.

Let's start by clarifying the notation. In this context, I(i,jrepresentts the intensity value of the pixel located at position (i,j). For grayscale images, the intensity values range from 0 to 255, where (i,j) corresponds to the row and column values, respectively. When applying the image inverse operator to a grayscale image, the output pixel value O(i,j) is computed using the following formula:

O(i,j) = 255 - I(i,j)

Nowadays, most images are in color format, consisting of three channels: red, green, and blue, commonly referred to as RGB images. In this case, the inverse operation is applied independently to each channel. The output image will have the following values at pixel (i,j):

O_R(i,j) = 255 - R(i,j)

O_G(i,j) = 255 - G(i,j)

O_B(i,j) = 255 - B(i,j)

Now, let's explore how we can implement the image inverse operator in Python. For simplicity, we'll demonstrate it on a grayscale image. However, I'll provide some insights on applying the operator to a color image, leaving the complete program as an exercise.

To work with color images, you can utilize the Python Imaging Library (PIL). Start by downloading a sample image, such as "baboon.png," with dimensions of 500x500 pixels. Suppose you want to extract the intensity values of the red, green, and blue channels at pixel location (325, 432). You can achieve this as follows:

No alt text provided for this image
from PIL import Image im = Image.open('baboon.png') print(im.getpixel((325, 432)))         

According to the documentation, the getpixel() method retrieves the pixel value at a given position. When you run the script, you might notice that you only obtain a single value, such as 138. Where are the intensity values for the RGB channels? The problem lies in the image's mode. Check the mode using the following statement:

print(im.mode)         

If the output is "P," it means the image was read in palette mode. To retrieve the intensity values for the different channels, you need to convert the image to RGB mode using the convert () method:

rgb_im = im.convert('RGB')         

Now, if you run rgb_im.getpixel((325, 432)), you should receive a tuple of values, such as (180, 168, 178). This represents the intensity values for the red, green, and blue channels, respectively.

To summarize the steps we've covered so far, here's a Python script that returns the RGB values of an image:

from PIL import Image im = Image.open('baboon.png') rgb_im = im.convert('RGB') print(rgb_im.getpixel((325, 432)))         

Before moving on to the image inverse operator, there's one more point to address. The example above retrieves the RGB value for a single pixel, but when applying the inverse operator, you need to perform it on all the pixels.

To print out the intensity values for each channel of every pixel, you can use the following code snippet:

from PIL import Image im = Image.open('baboon.png') rgb_im = im.convert('RGB') width, height = im.size for w in range(width): for h in range(height): print(rgb_im.getpixel((w, h)))         

At this stage, I'll leave it as an exercise for you to figure out how to apply the image inverse operator to all the color image channels (i.e., RGB) for each pixel.

Let's take a look at a practical example that demonstrates the application of the image inverse operator on a grayscale image.

No alt text provided for this image

For this illustration, please download the image "boat.png" which will serve as our test image. Here's a preview of the image:

No alt text provided for this image

Image Preview: Sample image for the tutorial

To accomplish this task, we'll utilize the powerful numpy library. The Python script for applying the image inverse operator on the provided image is as follows:

No alt text provided for this image
import cv2 import numpy as np from PIL import Image img = Image.open('boat.png') array_img = np.array(img) image_invert = np.invert(array_img) cv2.imwrite('new_boat.jpg', image_invert)         

Numpy is a versatile Python package extensively used in scientific computing. We also employ OpenCV-Python, a library specifically designed for solving computer vision problems.

OpenCV-Python comes bundled with numpy, so there's no need to install numpy separately if you install OpenCV-Python first. In the code snippet, we first open the image using Pillow (PIL) and then convert it into a numpy array.

Next, we utilize the invert () function from numpy to perform the image inversion and save the newly inverted image using OpenCV. The invert () function swaps the white and black values in the image.

After executing the script, you'll find the original image on the left and the inverted image on the right:

No alt text provided for this image

Image Comparison: Original vs. Inverted

Observe how some image features become clearer after applying the image inverse operator. Take note of the clouds and the lighthouse in the inverted image on the right.

Moving on, let's explore another operator called the Power Law Transformation, which is also known as gamma correction. At pixel (i,j), the operator is defined by the equation:

p(i,j) = k * I(i,j)^gamma

In this equation, I(i,j) represent the intensity value at location (i,j), while k and gamma are positive constants. For simplicity, we'll assume k=1, meaning we mainly adjust the value of gamma. Thus, the equation can be simplified to:

p(i,j) = I(i,j)^gamma

To implement the Power Law Transformation operator, we'll utilize OpenCV and NumPy libraries. If you're new to NumPy, you can refer to my tutorial on "Introducing NumPy" to familiarize yourself with the library. For this example, we'll use the image "boat.tiff" as our test image. Please download it before proceeding.

No alt text provided for this image

Here's the Python script for performing the Power Law Transformation operator:

import cv2 import numpy as np im = cv2.imread('boat.tiff') im = im / 255.0 im_power_law_transformation = cv2.pow(im, 0.6) cv2.imshow('Original Image', im) cv2.imshow('Power Law Transformation', im_power_law_transformation) cv2.waitKey(0)         

In this script, we choose a gamma value of 0.6. The resulting figure will display the original image on the left and the image after applying the Power Law Transformation operator on the right:

No alt text provided for this image

Image Comparison: Original vs. Power Law Transformed

You can observe the effects of different gamma values on the transformed image. Increasing gamma darkens the image, while decreasing gamma brightens it.

You might be wondering about the practical application of the Power Law Transformation.

No alt text provided for this image

In reality, various devices used for image acquisition, printing, and display respond according to the power law transformation operator. This is because the human brain processes images using gamma correction.

For instance, gamma correction is essential when we want to display images correctly, ensuring optimal image contrast on computer monitors or television screens.

To conclude, in this tutorial, you have learned how to enhance images using Python. We explored the image inverse operator to highlight features and the significance of the Power Law Transformation operator for displaying images accurately on computer monitors and television screens.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了