SoFunction
Updated on 2024-11-10

Detailed explanation of Python + OpenCV for basic image manipulation

present (sb for a job etc)

As you know, OpenCV is a free open source library for computer vision and image manipulation.

OpenCV is written in C++ and has thousands of optimized algorithms and functions for all kinds of image manipulation. Many real-life operations can be solved using OpenCV. Examples include video and image analysis, real-time computer vision, object detection, lens analysis, and more.

Many companies, researchers and developers have contributed to the creation of OpenCV. Using OpenCV is easy and OpenCV comes with many tools and features. Let's use OpenCV to perform interesting image manipulations and see the results.

a morphological transformation

Morphological transformation is an image processing method that transforms an image based on shape. This process helps in the representation and portrayal of the shape of the region. These transformations use structural elements applied to the input image and generate the output image.

Morphological operations are used for a variety of purposes, including removing noise from an image, locating intensity bumps and holes in an image, and connecting different elements in an image.

There are two main types of morphological transformations: corrosion and expansion.

corrode (degrade chemically)

Erosion is a morphological operation performed to reduce the size of a foreground object. The boundaries of the foreign object are slowly eroded. Eroding has many applications in image editing and conversion; erosion reduces the image pixels. Pixels on the object boundaries are also removed.

Erosion is simple to implement in Python with the help of the kernel.

Let's start using code in Python to implement corrosion.

First, we import Open CV and Numpy.

import cv2
import numpy as np

Now we read the image.

image = ("")

Picture:

We created a kernel needed to perform the erosion operation and implemented it using the built-in OpenCV functions.

# Creating kernel
kernel = ((5, 5), np.uint8)
# Using () method 
image_erode = (image, kernel)

Now, let's save the file and view it.

filename = 'image_erode1.jpg'
# Using () method
# Saving the image
(filename, image_erode)

Picture:

As we can see, the image is now eroded, the sharpness and edges are reduced and the image is blurred. Eroding can be used to hide or remove parts of an image or to hide information in an image.

Let's try different types of corrosion.

kernel2 = ((3, 3), np.uint8)
image_erode2 = (image, kernel2, cv2.BORDER_REFLECT)

Now we save the image file.

filename = 'image_erode2.jpg'
# Using () method
# Saving the image
(filename, image_erode2)

Picture:

Now, let's see what's swell.

dilatation

The expansion process is the opposite of erosion. When an image expands, instead of shrinking, the foreground object expands. Something in the image expands near the boundary and forms an inflated object.

Bright areas of an image tend to "glow" after expansion, which usually results in image enhancement. Expansion is therefore used for image correction and enhancement.

Let's implement Dilation using Python code.

kernel3 = ((5,5), np.uint8)
image_dilation = (image, kernel, iterations=1)

Now we save the image.

filename = 'image_dilation.jpg'
# Using () method
# Saving the image
(filename, image_dilation)

Picture:

As we can see, the image is now brighter and more intense.

Creating Borders

Adding borders to images is very simple and can be done very quickly by our mobile gallery apps or editing apps. But now let's use Python to create borders for images.

## Using () method
image_border1 = (image, 25, 25, 10, 10, cv2.BORDER_CONSTANT, None, value = 0)

Now, let's save the image.

filename = 'image_border1.jpg'
# Using () method
# Saving the image
(filename, image_border1)

Picture:

Here we have added a simple black border to the image. Now, let's try some mirrored borders.

#making a mirrored border
image_border2 = (image, 250, 250, 250, 250, cv2.BORDER_REFLECT)

Now we save the image.

filename = 'image_border2.jpg'
# Using () method
# Saving the image
(filename, image_border2)

Picture:

It's funny, it looks like something out of Dr. Strange's Mirror Dimension.

Let's try something else.

#making a mirrored border
image_border3 = (image, 300, 250, 100, 50, cv2.BORDER_REFLECT)

Now we save the image.

filename = 'image_border3.jpg'
# Using () method
# Saving the image
(filename, image_border3)

Picture:

intensity transformation

Typically, images undergo intensity transformations for various reasons. These are done directly on the image pixels in the spatial domain. Operations such as image thresholding and contrast processing are done using intensity transformations.

logarithmic transformation

Logarithmic transformation is an intensity transformation operation in which pixel values in an image are replaced with their logarithmic values.

The logarithmic transformation is used to brighten or enhance an image as it expands the darker pixels in the image to a higher pixel value.

Let's implement the logarithmic transformation.

# Apply log transform.
c = 255/((1 + (image)))
log_transformed = c * (1 + image)
# Specify the data type.
log_transformed = (log_transformed, dtype = np.uint8)

Now we save the image.

('log_transformed.jpg', log_transformed)

Picture:

The image becomes very bright.

linear transformation

We will apply a segmented linear transformation to the image. This transformation is also done on the spatial domain. This method is used to modify the image for a specific purpose. It is called segmented linear transformation because only one part of it is linear. The most commonly used segmented linear transformation is contrast stretching.

Typically, if an image is clicked in low-light conditions and the surrounding illumination is poor, the resulting image has low contrast. Contrast stretching increases the range of intensity levels in the image and the contrast stretching function increases monotonically, thus maintaining the order of pixel intensities.

Now, let's implement contrast stretching.

def pixelVal(pix, r1, s1, r2, s2):
    if (0 <= pix and pix <= r1):
        return (s1 / r1)*pix
    elif (r1 < pix and pix <= r2):
        return ((s2 - s1)/(r2 - r1)) * (pix - r1) + s1
    else:
        return ((255 - s2)/(255 - r2)) * (pix - r2) + s2
# Define parameters.
r1 = 70
s1 = 0
r2 = 140
s2 = 255
# Vectorize the function to apply it to each value in the Numpy array.
pixelVal_vec = (pixelVal)
# Apply contrast stretching.
contrast_stretch = pixelVal_vec(image, r1, s1, r2, s2)
# Save edited image.
('contrast_stretch.jpg', contrast_stretch)

Picture:

Here, the image is improved and a higher contrast can be observed.

Denoising color images

Denoising a signal or image means removing unwanted signals and information to get useful ones. Denoising to remove unwanted noise and to better analyze and process the image.

Let's denoise a color image in Python.

denoised_image = (image, None, 15, 8, 8, 15)

Now we save the image.

# Save edited image.
('denoised_image.jpg', denoised_image)

Picture:

We can see a lot of things we want, like the background and sky have been removed.

Analyzing images using histograms

Histograms are essential visuals in any form of analysis. A histogram of an image is an exciting way to understand the global description, and histograms can be used to quantitatively analyze an image. The histogram of an image represents the occurrence of gray levels in an image.

We can use histograms to understand the distribution of pixel intensities in a digital image, and we can also use histograms to understand the primary colors.

Let's plot a histogram.

from matplotlib import pyplot as plt
histr = ([image],[0],None,[256],[0,256])
(histr)

Output:

# alternative way to find histogram of an image
((),256,[0,256])
()

Output:

The graph shows the number of pixels in the color range 0 to 255 on the image. We can see that there is a good distribution of all types of colors.

Now, let's convert the image to black and white and generate a histogram.

grey_image = (image, cv2.COLOR_BGR2GRAY)
histogram = ([grey_image], [0], None, [256], [0, 256])
(histogram, color='k')

Output:

This distribution is quite different from the previous one. This is mainly due to the fact that the images are converted to grayscale and then analyzed.

Now, we perform a color histogram.

for i, col in enumerate(['b', 'g', 'r']):
    hist = ([image], [i], None, [256], [0, 256])
    (hist, color = col)
    ([0, 256])
()

Output:

We can see that the number of pixels in blue and green is much higher than in red. This is obvious because there are a lot of blue and green areas in the image.

So we can see that plotting image histograms is a good way to understand the intensity distribution of an image.

To this article on the detailed Python OpenCV for basic image manipulation of the article is introduced to this, more related Python OpenCV image manipulation content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!