0% found this document useful (0 votes)
45 views57 pages

Document From Sindhu Reddy... ??

This document provides an overview of advanced image processing techniques using OpenCV, covering topics such as blending images, adjusting contrast and brightness, adding text, and smoothing techniques. It includes step-by-step instructions for using Adobe Photoshop and mentions alternative software and online tools for similar tasks. Additionally, it discusses specific filters like the median and Gaussian filters, their applications, and guidelines for effective use.

Uploaded by

konerusindhu83
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views57 pages

Document From Sindhu Reddy... ??

This document provides an overview of advanced image processing techniques using OpenCV, covering topics such as blending images, adjusting contrast and brightness, adding text, and smoothing techniques. It includes step-by-step instructions for using Adobe Photoshop and mentions alternative software and online tools for similar tasks. Additionally, it discusses specific filters like the median and Gaussian filters, their applications, and guidelines for effective use.

Uploaded by

konerusindhu83
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

UNIT IV

Advanced Image Processing Using Open CV


Introduction Advanced Image Processing Using Open CV
Image processing plays a crucial role in various fields such as computer vision, medical imaging,
robotics, and more. Open CV (Open Source Computer Vision Library) is a powerful open-source
library designed to aid in computer vision tasks, including image processing. In this introduction,
we'll explore the basics of advanced image processing using Open CV.

4.1 Blending Two Image:


Blending two images involves combining them in a way that elements from both images contribute
to the final result. There are various techniques and tools you can use for image blending, and the
choice depends on your specific needs and the software you have available. I'll provide a general
overview using a popular image editing software like Adobe Photoshop, but similar concepts apply
to other tools.
Using Photoshop:

1. Open Both Images:


 Open Adobe Photoshop and load the two images you want to blend.
2. Layer Setup:
 Each image will be on a separate layer. You can copy and paste one image onto the
other or use the "Place" command to import one image into the other.
3. Adjust Opacity:
 Select the top layer and adjust its opacity. This allows you to see both images
simultaneously, making it easier to align and blend them.
4. Layer Mask:
 Add a layer mask to the top layer. This allows you to selectively reveal or hide parts
of the top image.
5. Gradient Mask:
 Use a gradient on the layer mask to create a smooth transition between the two
images. Select the gradient tool, choose a black-to-white gradient, and apply it to
the layer mask.
6. Brush Tool:
 Use the Brush tool on the layer mask to manually refine the blending. Painting with
black hides parts of the top layer, and painting with white reveals them.
7. Adjustment Layers:
 Use adjustment layers (e.g., Hue/Saturation, Curves) to match the colour and tone
of the blended images.
8. Merge or Save:
 Once you're satisfied with the blending, you can either merge the layers or save the
file in a format that supports layers (e.g., PSD).
Using Other Software:
If you're using a different software, the general steps might be similar but the specific tools and
procedures could vary. Most image editing tools have layer functionalities and masks, allowing you
to blend images in a similar way.
Remember to explore the documentation or online tutorials for your specific software for more
detailed instructions.
Online Tools:
There are also online tools and apps that provide simpler interfaces for image blending, requiring
little to no technical skills. These tools often have presents or easy-to-use sliders for blending.
Keep in mind that the key to successful blending is practice and experimentation. Feel free to adjust
settings, try different techniques, and refine until you achieve the desired result.
4.2 Changing Contrast and Brightness:
Adjusting contrast and brightness in an image can significantly impact its overall appearance. Most
image editing software, including Adobe Photoshop and many free alternatives, offer tools or
adjustments to modify contrast and brightness. Here's a general guide using Adobe Photoshop:
Using Photoshop:
1. Open Image:
 Open the image you want to adjust in Adobe Photoshop.
2. Adjustment Layers:
 Go to the "Layer" menu and choose "New Adjustment Layer." Select either
"Brightness/Contrast" or "Levels."
 Brightness/Contrast:
 In the "Properties" panel, adjust the "Brightness" and "Contrast" sliders to
achieve the desired effect.
 Levels:
 In the "Properties" panel, you'll see a histogram with three sliders: black,
grey, and white.
 Adjusting the black and white sliders can alter the contrast, and moving the
grey slider can affect the overall brightness.
3. Direct Adjustments:
 Alternatively, you can directly adjust brightness and contrast without using
adjustment layers. Go to the "Image" menu, select "Adjustments," and choose
"Brightness/Contrast" or "Levels."
4. Curves Adjustment:
 For more control, you can use the "Curves" adjustment. This allows you to adjust
brightness and contrast by manipulating a curve in a graphical interface.
 Go to "Layer" > "New Adjustment Layer" > "Curves."
 Experiment by dragging points on the curve to see how it affects the image.
5. Preview Changes:
 Regularly toggle the visibility of the adjustment layer or undo changes (Ctrl+Z or
Command) to compare the modified image with the original.
6. Save or Export:
 Once you're satisfied, save your image or export it in the desired format.
Using Other Software:
If you're using a different image editing tool, the basic concepts of adjusting brightness and contrast
will be similar, but the specific steps and interfaces may vary. Look for terms like "Brightness,"
"Contrast," "Levels," or "Curves" in the menu options.
Online Tools:
Some online image editors also provide sliders or simple controls for adjusting brightness and
contrast. Websites like Pixel, Canvas, or Photopia offer easy-to-use interfaces for basic adjustments.
Remember, when adjusting these parameters, it's often a good idea to make subtle changes and
evaluate the impact on the overall image. Extreme adjustments can lead to loss of detail or
introduce artefacts.
4.3 Adding Text to Images:
Adding text to images is a common task in graphic design, and it can be done using various image
editing tools. I'll provide a general guide using Adobe Photoshop, but similar functionalities are
available in other software as well.
Using Photoshop:
1. Open Image:
 Open Adobe Photoshop and load the image to which you want to add text.
2. Select the Text Tool:
 Choose the "T" or Text Tool from the toolbar.
3. Create a Text Layer:
 Click on the image where you want to start your text. This creates a new text layer.
4. Type Your Text:
 A text box will appear. Type the desired text.
5. Adjust Font and Size:
 Use the options in the toolbar or the Character panel to set the font, size, colour,
and other text properties.
6. Move and Transform Text:
 You can move the text by selecting the Move Tool (V) and dragging the text layer.
Use the Transform controls (Ctrl+T or Command+T) to resize or rotate the text.
7. Text Effects:
 Explore layer styles for additional effects like drop shadows, outlines, or gradients.
Right-click on the text layer in the Layers panel and choose "Blending Options."
8. Save or Export:
 Once you're satisfied, save your image or export it in the desired format.
Using Other Software:
If you're using a different image editing tool, the process will be quite similar. Look for a Text or Type
tool in the toolbar and follow the steps to create and customize your text.
Online Tools:
Several online tools also allow you to add text to images. Websites like canvas, Pixel, or Photopia
offer easy-to-use interfaces for basic text addition.
Tips:
 Readability: Ensure that the text is easily readable against the background. Adjust the text
colour and background as needed.
 Contrast: Create enough contrast between the text and the background for better visibility.
 Alignment: Pay attention to the alignment of your text. Centred, left-aligned, or right-
aligned text can convey different visual styles.
 Layer Organization: Keep your text on a separate layer to easily make changes or
adjustments later.
Adding text to images is a creative process, so feel free to experiment with different fonts, sizes, and
styles to achieve the look you desire.
4.4 Smoothing:
Smoothing" in the context of image editing generally refers to reducing noise or creating a smoother
appearance in an image. This process can help make the image look cleaner and more polished. Here
are some common techniques for smoothing images:
1. Gaussian Blur:
 Using Photoshop:
1. Duplicate the layer you want to smooth.
2. Go to "Filter" > "Blur" > "Gaussian Blur."
3. Adjust the radius to control the level of smoothing.
4. Mask or erase parts of the duplicated layer to reveal details where needed.
2. Surface Blur:
 This is a variation of Gaussian Blur that preserves edges while smoothing textures.
1. Duplicate the layer.
2. Go to "Filter" > "Blur" > "Surface Blur."
3. Adjust the radius and threshold to control the smoothing effect.
3. Noise Reduction:
 Most image editing software has noise reduction tools to smooth out grainy or noisy areas.
 Using Photoshop:
1. Go to "Filter" > "Noise" > "Reduce Noise."
2. Adjust strength, preserve details, and reduce colour noise as needed.
4. Smudge Tool:
 In some cases, using the smudge tool can help smooth out specific areas.
 Select the Smudge tool, adjust its strength, and brush over areas that need
smoothing.
5. Brush Tool with Soft Edges:
 Manually brushing over areas with a soft-edged brush can provide a subtle smoothing effect.
 Lower the brush opacity for more control over the smoothing intensity.
6. Frequency Separation:
 This advanced technique separates high-frequency details from low-frequency tones,
allowing for more targeted smoothing.
 It involves creating two layers—one for details and one for colour/tones.
7. Third-Party Plugins:
 Some third-party plugins or filters specialize in image smoothing and noise reduction.
Examples include Topaz Denoise or Nik Collection.
Tips:
 Apply Smoothing Locally: Avoid applying smoothing uniformly across the entire image.
Apply it selectively to areas that need improvement.
 Maintain Details: Always balance smoothing with the preservation of important details. Too
much smoothing can lead to loss of image clarity.
 Use Masks: When applying smoothing effects, consider using layer masks. This allows you to
control where the smoothing is applied and where the original details remain.
Remember to always work on a duplicate layer or keep a copy of the original image to avoid
irreversible changes. Experiment with different techniques and settings to achieve the desired level
of smoothness while maintaining the overall quality of the image.
4.5 Images:
In image processing, filters are commonly used to enhance or modify images. Here are examples of
using three different filters—Median Filter, Gaussian Filter, and Bilateral Filter—using OpenCV in
Python.
4.5.1. Median Filter: The median filter is normally used to reduce noise in an image, somewhat
like the mean filter. However, it often does a better job than the mean filter of preserving useful
detail in the image.
How It Works

Like the mean filter, the median filter considers each pixel in the image in turn and looks at its
nearby neighbors to decide whether or not it is representative of its surroundings. Instead of simply
replacing the pixel value with the mean of neighboring pixel values, it replaces it with the median of
those values. The median is calculated by first sorting all the pixel values from the surrounding
neighborhood into numerical order and then replacing the pixel being considered with the middle
pixel value. (If the neighborhood under consideration contains an even number of pixels, the
average of the two middle pixel values is used.) Figure 1 illustrates an example calculation.

Figure 1 Calculating the median value of a pixel neighborhood. As can be seen, the central pixel
value of 150 is rather unrepresentative of the surrounding pixels and is replaced with the median
value: 124. A 3×3 square neighborhood is used here --- larger neighborhoods will produce more
severe smoothing.

Guidelines for Use

By calculating the median value of a neighborhood rather than the mean filter, the median filter has
two main advantages over the mean filter:

 The median is a more robust average than the mean and so a single very unrepresentative
pixel in a neighborhood will not affect the median value significantly.
 Since the median value must actually be the value of one of the pixels in the neighborhood,
the median filter does not create new unrealistic pixel values when the filter straddles an
edge. For this reason the median filter is much better at preserving sharp edges than the
mean filter.

The image
shows an image that has been corrupted by Gaussian noise with mean 0 and standard deviation ( )
8. The original image is

for comparison. Applying a 3×3 median filter produces

Note how the noise has been reduced at the expense of a slight degradation in image quality. The
image

has been corrupted by even more noise (Gaussian noise with mean 0 and 13), and

is the result of 3×3 median filtering. The median filter is sometimes not as subjectively good at
dealing with large amounts of Gaussian noise as the mean filter.

Where median filtering really comes into its own is when the noise produces extreme `outlier' pixel
values, as for instance in
which has been corrupted with `salt and pepper' noise, i.e. bits have been flipped with probability
1%. Median filtering this with a 3×3 neighborhood produces

in which the noise has been entirely eliminated with almost no degradation to the underlying image.
Compare this with the similar test on the mean filter.

Consider another example wherein the original image

has been corrupted with higher levels (i.e. p=5% that a bit is flipped) of salt and pepper noise

After smoothing with a 3×3 filter, most of the noise has been eliminated

If we smooth the noisy image with a larger median filter, e.g. 7×7, all the noisy pixels disappear, as
shown in
Note that the image is beginning to look a bit `blotchy', as graylevel regions are mapped together.
Alternatively, we can pass a 3×3 median filter over the image three times in order to remove all the
noise with less loss of detail

In general, the median filter allows a great deal of high spatial frequency detail to pass while
remaining very effective at removing noise on images where less than half of the pixels in a
smoothing neighborhood have been effected. (As a consequence of this, median filtering can be less
effective at removing noise from images corrupted with Gaussian noise.)

One of the major problems with the median filter is that it is relatively expensive and complex to
compute. To find the median it is necessary to sort all the values in the neighborhood into numerical
order and this is relatively slow, even with fast sorting algorithms such as quicksort. The basic
algorithm can, however,be enhanced somewhat for speed. A common technique is to notice that
when the neighborhood window is slid across the image, many of the pixels in the window are the
same from one step to the next, and the relative ordering of these with each other will obviously not
have changed. Clever algorithms make use of this to improve performance.

Interactive Experimentation

You can interactively experiment with this operator by clicking here.

Exercises

1. Using the image


explore the effect of median filtering with different neighborhood sizes.

2. Compare the relative speed of mean and median filters using the same sized neighborhood
and image. How does the performance of each scale with size of image and size of
neighborhood?
3. Unlike the mean filter, the median filter is non-linear. This means that for two
images A(x) and B(x):

Illustrate this to yourself by performing smoothing and pixel addition (in the order indicated
on each side of the above equation!) to a set of test images. Carry out this experiment on
some simple images, e.g.

and

Python Copy code:


import cv2
import numpy as np

# Read the image


image = cv2.imread('input_image.jpg')

# Apply Median Filter


median_filtered = cv2.medianBlur(image, ksize=5) # ksize is the kernel size, should be an odd
number

# Display the original and filtered images


cv2.imshow('Original Image', image)
cv2.imshow('Median Filtered Image', median_filtered)
cv2.waitKey(0)
cv2.destroyAllWindows()

4.5.2. Gaussian Filter:


The Gaussian smoothing operator is a 2-D convolution operator that is used to `blur' images and
remove detail and noise. In this sense it is similar to the mean filter, but it uses a
different kernel that represents the shape of a Gaussian (`bell-shaped') hump. This kernel has some
special properties which are detailed below.
How It Works

The Gaussian distribution in 1-D has the form:

where is the standard deviation of the distribution. We have also assumed that the distribution
has a mean of zero (i.e. it is centered on the line x=0). The distribution is illustrated in Figure 1.

Figure 1 1-D Gaussian distribution with mean 0 and =1

In 2-D, an isotropic (i.e. circularly symmetric) Gaussian has the form:

This distribution is shown in Figure 2.


Figure 2 2-D Gaussian distribution with mean (0,0) and =1

The idea of Gaussian smoothing is to use this 2-D distribution as a `point-spread' function, and this is
achieved by convolution. Since the image is stored as a collection of discrete pixels we need to
produce a discrete approximation to the Gaussian function before we can perform the convolution.
In theory, the Gaussian distribution is non-zero everywhere, which would require an infinitely large
convolution kernel, but in practice it is effectively zero more than about three standard deviations
from the mean, and so we can truncate the kernel at this point. Figure 3 shows a suitable integer
valued convolution kernel that approximates a Gaussian with a of 1.0. It is not obvious how to pick
the values of the mask to approximate a Gaussian. One could use the value of the Gaussian at the
centre of a pixel in the mask, but this is not accurate because the value of the Gaussian varies non-
linearly across the pixel. We integrated the value of the Gaussian over the whole pixel (by summing
the Gaussian at 0.001 increments). The integrals are not integers: we rescaled the array so that the
corners had the value 1. Finally, the 273 is the sum of all the values in the mask.

Figure 3 Discrete approximation to Gaussian function with =1.0


Once a suitable kernel has been calculated, then the Gaussian smoothing can be performed using
standard convolution methods. The convolution can in fact be performed fairly quickly since the
equation for the 2-D isotropic Gaussian shown above is separable into x and y components. Thus the
2-D convolution can be performed by first convolving with a 1-D Gaussian in the x direction, and
then convolving with another 1-D Gaussian in the y direction. (The Gaussian is in fact
the only completely circularly symmetric operator which can be decomposed in such a way.) Figure 4
shows the 1-D x component kernel that would be used to produce the full kernel shown in Figure 3
(after scaling by 273, rounding and truncating one row of pixels around the boundary because they
mostly have the value 0. This reduces the 7x7 matrix to the 5x5 shown above.). The y component is
exactly the same but is oriented vertically.

Figure 4 One of the pair of 1-D convolution kernels used to calculate the full kernel shown in Figure 3
more quickly.

A further way to compute a Gaussian smoothing with a large standard deviation is to convolve an
image several times with a smaller Gaussian. While this is computationally complex, it can have
applicability if the processing is carried out using a hardware pipeline.

The Gaussian filter not only has utility in engineering applications. It is also attracting attention from
computational biologists because it has been attributed with some amount of biological
plausibility, e.g. some cells in the visual pathways of the brain often have an approximately Gaussian
response.

Guidelines for Use

The effect of Gaussian smoothing is to blur an image, in a similar fashion to the mean filter. The
degree of smoothing is determined by the standard deviation of the Gaussian. (Larger standard
deviation Gaussians, of course, require larger convolution kernels in order to be accurately
represented.)

The Gaussian outputs a `weighted average' of each pixel's neighborhood, with the average weighted
more towards the value of the central pixels. This is in contrast to the mean filter's uniformly
weighted average. Because of this, a Gaussian provides gentler smoothing and preserves edges
better than a similarly sized mean filter.

One of the principle justifications for using the Gaussian as a smoothing filter is due to its frequency
response. Most convolution-based smoothing filters act as lowpass frequency filters. This means
that their effect is to remove high spatial frequency components from an image. The frequency
response of a convolution filter, i.e. its effect on different spatial frequencies, can be seen by taking
the Fourier transform of the filter. Figure 5 shows the frequency responses of a 1-D mean filter with
width 5 and also of a Gaussian filter with = 3.

Figure 5 Frequency responses of Box (i.e. mean) filter (width 5 pixels) and Gaussian filter ( = 3
pixels). The spatial frequency axis is marked in cycles per pixel, and hence no value above 0.5 has a
real meaning.

Both filters attenuate high frequencies more than low frequencies, but the mean filter exhibits
oscillations in its frequency response. The Gaussian on the other hand shows no oscillations. In fact,
the shape of the frequency response curve is itself (half a) Gaussian. So by choosing an appropriately
sized Gaussian filter we can be fairly confident about what range of spatial frequencies are still
present in the image after filtering, which is not the case of the mean filter. This has consequences
for some edge detection techniques, as mentioned in the section on zero crossings. (The Gaussian
filter also turns out to be very similar to the optimal smoothing filter for edge detection under the
criteria used to derive the Canny edge detector.)

Python Copy code


import cv2
import numpy as np

# Read the image


image = cv2.imread('input_image.jpg')

# Apply Gaussian Filter


gaussian_filtered = cv2.GaussianBlur(image, (5, 5), 0) # (5, 5) is the kernel size, 0 is the standard
deviation

# Display the original and filtered images


cv2.imshow('Original Image', image)
cv2.imshow('Gaussian Filtered Image', gaussian_filtered)
cv2.waitKey(0)
cv2.destroyAllWindows()

4.5.3. Bilateral Filter:


The basic idea underlying bilateral filtering is to do in the range of an image what traditional filters
do in its domain. Two pixels can be close to one another, that is, occupy nearby spatial location, or
they can be similar to one another, that is, have nearby values, possibly in a perceptually meaningful
fashion.
Consider a shift-invariant low-pass domain filter applied to an image:

The bold font for f and h emphasizes the fact that both input and output images may be multi-band.
In order to preserve the DC component, it must be

Range filtering is similarly defined:

In this case, the kernel measures the photometric similarity between pixels. The normalization
constant in this case is

The spatial distribution of image intensities plays no role in range filtering taken by itself. Combining
intensities from the entire image, however, makes little sense, since the distribution of image values
far away from x ought not to affect the final value at x. In addition, one can show that range filtering
without domain filtering merely changes the color map of an image, and is therefore of little use.
The appropriate solution is to combine domain and range filtering, thereby enforcing both geometric
and photometric locality. Combined filtering can be described as follows:

with the normalization

Combined domain and range filtering will be denoted as bilateral filtering. It replaces the pixel value
at x with an average of similar and nearby pixel values. In smooth regions, pixel values in a small
neighborhood are similar to each other, and the bilateral filter acts essentially as a standard domain
filter, averaging away the small, weakly correlated differences between pixel values caused by noise.
Consider now a sharp boundary between a dark and a bright region, as in figure 1(a).

(a) (b) (c)


Figure 1

When the bilateral filter is centered, say, on a pixel on the bright side of the boundary, the similarity
function s assumes values close to one for pixels on the same side, and values close to zero for pixels
on the dark side. The similarity function is shown in figure 1(b) for a 23x23 filter support centered
two pixels to the right of the step in figure 1(a). The normalization term k(x) ensures that the weights
for all the pixels add up to one. As a result, the filter replaces the bright pixel at the center by an
average of the bright pixels in its vicinity, and essentially ignores the dark pixels. Conversely, when
the filter is centered on a dark pixel, the bright pixels are ignored instead. Thus, as shown in figure
1(c), good filtering behavior is achieved at the boundaries, thanks to the domain component of the
filter, and crisp edges are preserved at the same time, thanks to the range component.

The Gaussian Case

A simple and important case of bilateral filtering is shift-invariant Gaussian filtering, in which both
the closeness function c and the similarity function s are Gaussian functions of the Euclidean
distance between their arguments. More specifically, c is radially symmetric:

where

is the Euclidean distance. The similarity function s is perfectly analogous to c :

where
is a suitable measure of distance in intensity space. In the scalar case, this may be simply the
absolute difference of the pixel difference or, since noise increases with image intensity, an
intensity-dependent version of it. Just as this form of domain filtering is shift-invariant, the Gaussian
range filter introduced above is insensitive to overall additive changes of image intensity. Of course,
the range filter is shift-invariant as well.

Experiments with Black-and-White Images

Figure 2 (a) and (b) show the potential of bilateral filtering for the removal of texture. The picture
"simplification" illustrated by figure 2 (b) can be useful for data reduction without loss of overall
shape features in applications such as image transmission, picture editing and manipulation, image
description for retrieval.

(a) (b)
Figure
2

Bilateral filtering with parameters sd =3 pixels and sr =50 intensity values is applied to the image in
figure 3 (a) to yield the image in figure 3 (b). Notice that most of the fine texture has been filtered
away, and yet all contours are as crisp as in the original image. Figure 3 (c) shows a detail of figure 3
(a), and figure 3 (d) shows the corresponding filtered version. The two onions have assumed a
graphics-like appearance, and the fine texture has gone. However, the overall shading is preserved,
because it is well within the band of the domain filter and is almost unaffected by the range filter.
Also, the boundaries of the onions are preserved.
(a) (b)

(c) (d)
Figur
e3

Experiments with Color Images

For black-and-white images, intensities between any two gray levels are still gray levels. As a
consequence, when smoothing black-and-white images with a standard low-pass filter, intermediate
levels of gray are produced across edges, thereby producing blurred images. With color images, an
additional complication arises from the fact that between any two colors there are other, often
rather different colors. For instance, between blue and red there are various shades of pink and
purple. Thus, disturbing color bands may be produced when smoothing across color edges. The
smoothed image does not just look blurred, it also exhibits odd-looking, colored auras around
objects.
(a) (b)

(c) (d)
Figure 4

Figure 4 (a) shows a detail from a picture with a red jacket against a blue sky. Even in this unblurred
picture, a thin pink-purple line is visible, and is caused by a combination of lens blurring and pixel
averaging. In fact, pixels along the boundary, when projected back into the scene, intersect both red
jacket and blue sky, and the resulting color is the pink average of red and blue. When smoothing,
this effect is emphasized, as the broad, blurred pink-purple area in figure 4 (b) shows.
To address this difficulty, edge-preserving smoothing could be applied to the red, green, and blue
components of the image separately. However, the intensity profiles across the edge in the three
color bands are in general different. Smoothing the three color bands separately results in an even
more pronounced pink and purple band than in the original, as shown in figure 4 (c). The pink-purple
band, however, is not widened as in the standard-blurred version of figure 4 (b).
A much better result can be obtained with bilateral filtering. In fact, a bilateral filter allows
combining the three color bands appropriately, and measuring photometric distances between
pixels in the combined space. Moreover, this combined distance can be made to correspond closely
to perceived dissimilarity by using Euclidean distance in the CIE-Lab color space. This color space is
based on a large body of psychophysical data concerning color-matching experiments performed by
human observers. In this space, small Euclidean distances are designed to correlate strongly with the
perception of color discrepancy as experienced by an "average" color-normal human observer. Thus,
in a sense, bilateral filtering performed in the CIE-Lab color space is the most natural type of filtering
for color images: only perceptually similar colors are averaged together, and only perceptually
important edges are preserved. Figure 4 (d) shows the image resulting from bilateral smoothing of
the image in figure 4 (a). The pink band has shrunk considerably, and no extraneous colors appear.
(a) (b) (c)
Figure 5

Figure 5 (c) shows the result of five iterations of bilateral filtering of the image in figure 5 (a). While a
single iteration produces a much cleaner image (figure 5 (b)) than the original, and is probably
sufficient for most image processing needs, multiple iterations have the effect of flattening the
colors in an image considerably, but without blurring edges. The resulting image has a much smaller
color map, and the effects of bilateral filtering are easier to see when displayed on a printed page.
Notice the cartoon-like appearance of figure 5 (c). All shadows and edges are preserved, but most of
the shading is gone, and no "new" colors are introduced by filtering.

Python Copy code


import cv2
import numpy as np

# Read the image


image = cv2.imread('input_image.jpg')

# Apply Bilateral Filter


bilateral_filtered = cv2.bilateralFilter(image, d=9, sigmaColor=75, sigmaSpace=75)

# Display the original and filtered images


cv2.imshow('Original Image', image)
cv2.imshow('Bilateral Filtered Image', bilateral_filtered)
cv2.waitKey(0)
cv2.destroyAllWindows()

Remember to replace 'input_image.jpg' with the actual filename or path of your input image.
Feel free to adjust the parameters (kernel size, standard deviation, etc.) according to your specific
requirements and the characteristics of your images. Experimenting with different parameters will
help you achieve the desired results.
4.6 Changing the Shape of Images:
Changing the shape of an image typically involves resizing, cropping, or transforming it in some way.
Here are some common operations you might perform using OpenCV in Python:

Image Transformation involves the transformation of image data in order to retrieve information
from the image or preprocess the image for further usage. In this tutorial we are going to
implement the following image transformation:
 Image Translation
 Reflection
 Rotation
 Scaling
 Cropping
 Shearing in x-axis
 Shearing in y-axis
What is OpenCV?
OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine
learning software library. OpenCV was built to provide a common infrastructure for computer
vision applications and to accelerate the use of machine perception in commercial products. By
using it, one can process images and videos to identify objects, faces, or even the handwriting of a
human. When it is integrated with various libraries, such as NumPy, Python is capable of
processing the OpenCV array structure for analysis.
Image Translation
In computer vision or image processing, image translation is the rectilinear shift of an image from
one location to another, so the shifting of an object is called translation. In other
words, translation is the shifting of an object’s location.

 Python3

import numpy as np

import cv2 as cv

img = cv.imread('girlImage.jpg', 0)

rows, cols = img.shape

M = np.float32([[1, 0, 100], [0, 1, 50]])

dst = cv.warpAffine(img, M, (cols, rows))

cv.imshow('img', dst)

cv.waitKey(0)
cv.destroyAllWindows()

In the above code, we have imported NumPy and OpenCV module then read the image by
using imread() function, and then translation takes place with the warpAffine() method which is
defined as follows:
In the first argument, we passed the image, in the second argument it takes a matrix as a
parameter in the matrix we give x = 100, which means we are telling the function to shift the
image 70 units on the right side and y= 50, which means we are telling the function to shift the
image 50 units downwards. In the third argument, where we mentioned the cols and rows, we
told the function to do not to crop the image from both the x and y sides.
dst = cv.warpAffine(img,M,(cols,rows))
Output:

Image Reflection
Image reflection is used to flip the image vertically or horizontally. For reflection along the x-axis,
we set the value of Sy to -1, Sx to 1, and vice-versa for the y-axis reflection.

 Python3

import numpy as np

import cv2 as cv

img = cv.imread('girlImage.jpg', 0)

rows, cols = img.shape

M = np.float32([[1, 0, 0],
[0, -1, rows],

[0, 0, 1]])

reflected_img = cv.warpPerspective(img, M,

(int(cols),

int(rows)))

cv.imshow('img', reflected_img)

cv.imwrite('reflection_out.jpg', reflected_img)

cv.waitKey(0)

cv.destroyAllWindows()

To flip the image horizontally:


M = np.float32([[1, 0, 0], [0, -1, rows],[0, 0, 1]])
To flip the image vertically:
M = np.float32([[-1, 0, cols], [0, 1, 0], [0, 0, 1]])
Output:

Image Rotation
Image rotation is a common image processing routine with applications in matching, alignment,
and other image-based algorithms, in image rotation the image is rotated by a definite angle. It is
used extensively in data augmentation, especially when it comes to image classification.
 Python3

import numpy as np

import cv2 as cv

img = cv.imread('girlImage.jpg', 0)

rows, cols = img.shape

M = np.float32([[1, 0, 0], [0, -1, rows], [0, 0, 1]])

img_rotation = cv.warpAffine(img,

cv.getRotationMatrix2D((cols/2, rows/2),

30, 0.6),

(cols, rows))

cv.imshow('img', img_rotation)

cv.imwrite('rotation_out.jpg', img_rotation)

cv.waitKey(0)

cv.destroyAllWindows()

We have used the get rotation matrix function to define the parameter required in the warpAffine
function to tell the function to make a matrix that can give a required rotation angle( here it is 30
degrees) with shrinkage of the image by 40%.
img_rotation = cv.warpAffine(img,
cv.getRotationMatrix2D((cols/2, rows/2), 30, 0.6),
(cols, rows))
Output:

Image Scaling
Image scaling is a process used to resize a digital image. We perform two things in the image
scaling either we enlarge the image or we shrink the image, OpenCV has a built-in
function cv2.resize() for image scaling.
Shrinking an image:
img_shrinked = cv2.resize(image, (350, 300),
interpolation = cv2.INTER_AREA)
Note: Here 350 and 300 are the height and width of the shrunk image respectively
Enlarging Image:
img_enlarged = cv2.resize(img_shrinked, None,
fx=1.5, fy=1.5,
interpolation=cv2.INTER_CUBIC)

 Python3

import numpy as np

import cv2 as cv

img = cv.imread('girlImage.jpg', 0)

rows, cols = img.shape

img_shrinked = cv.resize(img, (250, 200),

interpolation=cv.INTER_AREA)
cv.imshow('img', img_shrinked)

img_enlarged = cv.resize(img_shrinked, None,

fx=1.5, fy=1.5,

interpolation=cv.INTER_CUBIC)

cv.imshow('img', img_enlarged)

cv.waitKey(0)

cv.destroyAllWindows()

Output:

Image Cropping
Cropping is the removal of unwanted outer areas from an image.
cropped_img = img[100:300, 100:300]
OpenCV loads the image as a NumPy array, we can crop the image simply by indexing the array, in
our case, we choose to get 200 pixels from 100 to 300 on both axes.

 Python3
import numpy as np

import cv2 as cv

img = cv.imread('girlImage.jpg', 0)

cropped_img = img[100:300, 100:300]

cv.imwrite('cropped_out.jpg', cropped_img)

cv.waitKey(0)

cv.destroyAllWindows()

Output:

Image Shearing in X-Axis


While the shearing image is on the x-axis, the boundaries of the image that are parallel to the x-
axis keep their location, and the edges parallel to the y-axis change their place depending on the
shearing factor.
M = np.float32([[1, 0.5, 0], [0, 1, 0], [0, 0, 1]])
sheared_img = cv.warpPerspective(img, M,
(int(cols*1.5),
int(rows*1.5)))

 Python3
import numpy as np

import cv2 as cv

img = cv.imread('girlImage.jpg', 0)

rows, cols = img.shape

M = np.float32([[1, 0.5, 0], [0, 1, 0], [0, 0, 1]])

sheared_img = cv.warpPerspective(img, M, (int(cols*1.5), int(rows*1.5)))

cv.imshow('img', sheared_img)

cv.waitKey(0)

cv.destroyAllWindows()

Output:

Image Shearing in Y-Axis


When shearing is done in the y-axis direction, the boundaries of the image that are parallel to the
y-axis keep their location, and the edges parallel to the x-axis change their place depending on the
shearing factor.
M = np.float32([[1, 0, 0], [0.5, 1, 0], [0, 0, 1]])
sheared_img = cv.warpPerspective(img, M,
(int(cols*1.5),
int(rows*1.5)))

 Python3

import numpy as np

import cv2 as cv

img = cv.imread('girlImage.jpg', 0)

rows, cols = img.shape

M = np.float32([[1, 0, 0], [0.5, 1, 0], [0, 0, 1]])

sheared_img = cv.warpPerspective(img, M, (int(cols*1.5), int(rows*1.5)))

cv.imshow('sheared_y-axis_out.jpg', sheared_img)

cv.waitKey(0)

cv.destroyAllWindows()

Output:
4.6.1. Resizing Images:
Resizing is a straightforward way to change the shape of an image. PIL is the Python Imaging Library
which provides the python interpreter with image editing capabilities. The Image module provides
a class with the same name which is used to represent a PIL image. The module also provides a
number of factory functions, including functions to load images from files, and to create new
images.
Image.resize() Returns a resized copy of this image.

Syntax: Image.resize(size, resample=0)


Parameters:
size – The requested size in pixels, as a 2-tuple: (width, height).
resample – An optional resampling filter. This can be one of PIL.Image.NEAREST (use nearest
neighbour), PIL.Image.BILINEAR (linear interpolation), PIL.Image.BICUBIC (cubic spline
interpolation), or PIL.Image.LANCZOS (a high-quality downsampling filter). If omitted, or if the
image has mode “1” or “P”, it is set PIL.Image.NEAREST. Otherwise, the default filter is
Resampling.BICUBIC.
Returns type: An Image object.
Source

Image Used:

 Python3

# Importing Image class from PIL module

from PIL import Image

# Opens a image in RGB mode


im = Image.open(r"C:\Users\System-Pc\Desktop\ybear.jpg")

# Size of the image in pixels (size of original image)

# (This is not mandatory)

width, height = im.size

# Setting the points for cropped image

left = 4

top = height / 5

right = 154

bottom = 3 * height / 5

# Cropped image of above dimension

# (It will not change original image)

im1 = im.crop((left, top, right, bottom))

newsize = (300, 300)

im1 = im1.resize(newsize)

# Shows the image in image viewer

im1.show()

Output:
Another example:Here we use the different newsize value.

 Python3

# Importing Image class from PIL module

from PIL import Image

# Opens a image in RGB mode

im = Image.open(r"C:\Users\System-Pc\Desktop\ybear.jpg")

# Size of the image in pixels (size of original image)

# (This is not mandatory)

width, height = im.size

# Setting the points for cropped image

left = 6

top = height / 4
right = 174

bottom = 3 * height / 4

# Cropped image of above dimension

# (It will not change original image)

im1 = im.crop((left, top, right, bottom))

newsize = (200, 200)

im1 = im1.resize(newsize)

# Shows the image in image viewer

im1.show()

Output:

Python Copy code


import cv2

# Read the image


image = cv2.imread('input_image.jpg')

# Specify the new dimensions


new_width = 400
new_height = 300

# Resize the image


resized_image = cv2.resize(image, (new_width, new_height))

# Display the original and resized images


cv2.imshow('Original Image', image)
cv2.imshow('Resized Image', resized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

4.6.2. Cropping Images:


Cropping allows you to extract a region of interest from an image. Cropping an Image is one of the
most basic image operations that we perform in our projects. In this article, we will discuss how to
crop images using OpenCV in Python.

Stepwise Implementation
For this, we will take the image shown below.

Step 1: Read the image


cv2.imread() method loads an image from the specified file. If the image cannot be read (because
of the missing file, improper permissions, unsupported or invalid format) then this method returns
an empty matrix.
Note: When we load an image in OpenCV using cv2.imread(), we store it as a Numpy n-
dimensional array.
Example: Python program to read the image

 Python3
import cv2

# Read Input Image

img = cv2.imread("test.jpeg")

# Check the type of read image

print(type(img))

# Display the image

cv2.imshow('image', img)

cv2.waitKey(0)

cv2.destroyAllWindows()

Output:

Step 2: Get the image Dimensions


We can see the type of ‘img‘ as ‘numpy.ndarray‘. Now, we simply apply array slicing to our NumPy
array and produce our cropped image, So we have to find the dimensions of the image. For this we
will use the image.shape attribute.
Syntax:
image.shape
where image is the input image
Example: Python code to find the dimensions of the image,

 Python3

import cv2

# read the image

img = cv2.imread("test.jpeg")

print(type(img))

# Check the shape of the input image

print("Shape of the image", img.shape)

Output:

image shape

Step 3: Slice the image

Now we can apply array slicing to produce our final result.


Syntax :
image[rows,columns]
where
1. rows are the row slice
2. columns is the column slice
Example:

 Python3
import cv2

img = cv2.imread("test.jpeg")

print(type(img))

# Shape of the image

print("Shape of the image", img.shape)

# [rows, columns]

crop = img[50:180, 100:300]

cv2.imshow('original', img)

cv2.imshow('cropped', crop)

cv2.waitKey(0)

cv2.destroyAllWindows()

Output:
Python Copy code
import cv2

# Read the image


image = cv2.imread('input_image.jpg')

# Define the region to crop (x, y, width, height)


x, y, width, height = 100, 50, 300, 200

# Crop the image


cropped_image = image[y:y+height, x:x+width]

# Display the original and cropped images


cv2.imshow('Original Image', image)
cv2.imshow('Cropped Image', cropped_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
4.6.3. Rotating Images:
Rotating an image can change its shape and orientation. In this article, let’s see how to rotate an
Image using Python. By Image Rotation, the image is rotated about its center by a specified
number of degrees. The rotation of an image is a geometric transformation. It can be done either
by Forward Transformation (or) Inverse Transformation.

Here Image Processing Library with Pillow uses Inverse Transformation. If the Number Of Degrees
Specified for Image Rotation is Not an Integer Multiple of 90 Degrees, then some Pixel Values
Beyond Image Boundaries i.e Pixel values lying outside the Dimension of the image. Such Values
will not be displayed in the output image.
Method:1 Using Image Processing Library Pillow

 Python3

# import the Python Image

# processing Library

from PIL import Image

# Giving The Original image Directory

# Specified
Original_Image = Image.open("./gfgrotate.jpg")

# Rotate Image By 180 Degree

rotated_image1 = Original_Image.rotate(180)

# This is Alternative Syntax To Rotate

# The Image

rotated_image2 = Original_Image.transpose(Image.ROTATE_90)

# This Will Rotate Image By 60 Degree

rotated_image3 = Original_Image.rotate(60)

rotated_image1.show()

rotated_image2.show()

rotated_image3.show()

Output:
This is Image is Rotated By 180 Degree

This Image Is Rotated By 60 Degree


This Image Is Rotated By 90 Degree

The rotate() method of Python Image Processing Library Pillow Takes the number of degrees as a
parameter and rotates the image in Counter Clockwise Direction to the number of degrees
specified.
Method 2: Using Open-CV to rotate an image by an angle in Python
This is common that everyone knows that Python Open-CV is a module that will handle real-time
applications related to computer vision. Open-CV works with image processing
library imutils which deals with images. The imutils.rotate() function is used to rotate an image by
an angle in Python.

 Python3

import cv2 # importing cv

import imutils

# read an image as input using OpenCV

image = cv2.imread(r".\gfgrotate.jpg")

Rotated_image = imutils.rotate(image, angle=45)


Rotated1_image = imutils.rotate(image, angle=90)

# display the image using OpenCV of

# angle 45

cv2.imshow("Rotated", Rotated_image)

# display the image using OpenCV of

# angle 90

cv2.imshow("Rotated", Rotated1_image)

# This is used for To Keep On Displaying

# The Image Until Any Key is Pressed

cv2.waitKey(0)

Output:

Image Rotated Using Open-CV in 45 Degree


Image Rotated Using Open-CV in 90 Degree

Python Copy code


import cv2

# Read the image


image = cv2.imread('input_image.jpg')

# Specify the rotation angle


angle = 45

# Get the image center for rotation


center = (image.shape[1] // 2, image.shape[0] // 2)

# Perform the rotation


rotation_matrix = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated_image = cv2.warpAffine(image, rotation_matrix, (image.shape[1], image.shape[0]))

# Display the original and rotated images


cv2.imshow('Original Image', image)
cv2.imshow('Rotated Image', rotated_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
4.6.4. Flipping Images:
Flipping an image can also change its shape. Given an image the task here is to generate a Python
script to flip an image horizontally and vertically. Here the module used for the task is PIL and
transpose() function of this module.

Syntax:
transpose(degree)
Keywords FLIP_TOP_BOTTOM and FLIP_LEFT_RIGHT will be passed to transpose method to flip it.
 FLIP_TOP_BOTTOM – returns an original image flipped Vertically
 FLIP_LEFT_RIGHT- returns an original image flipped Horizontally

Approach

 Import module
 Open original image
 Transform the image as required
 Save the new transformed image.
Image in use:

Example: Flipping image vertically

 Python3

# importing PIL Module

from PIL import Image

# open the original image

original_img = Image.open("original.png")

# Flip the original image vertically

vertical_img = original_img.transpose(method=Image.FLIP_TOP_BOTTOM)

vertical_img.save("vertical.png")

# close all our files object

original_img.close()
vertical_img.close()

Output:

Example : Flip image horizontally

 Python3

# importing PIL Module

from PIL import Image

# open the original image

original_img = Image.open("original.png")

# Flip the original image horizontally

horz_img = original_img.transpose(method=Image.FLIP_LEFT_RIGHT)

horz_img.save("horizontal.png")

# close all our files object

original_img.close()

horz_img.close()

Output:
Python Copy code
import cv2

# Read the image


image = cv2.imread('input_image.jpg')

# Flip the image horizontally


flipped_image = cv2.flip(image, 1) # 0 for vertical flip, 1 for horizontal flip, -1 for both flips

# Display the original and flipped images


cv2.imshow('Original Image', image)
cv2.imshow('Flipped Image', flipped_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
These are basic examples, and you can combine these operations or apply more advanced
transformations based on your specific needs. Additionally, you can use OpenCV functions like
cv2.warpAffine for more complex geometrical transformations.

4.7 Effecting Image Thresholding:


Image thresholding is a technique used to segment an image into regions based on pixel intensity. It
involves setting a threshold value, and pixels with intensity values above or below this threshold are
assigned to different segments. Here's an example using OpenCV in Python:

python
Copy code Image Thresholding is an intensity transformation function in which the values of pixels
below a particular threshold are reduced, and the values above that threshold are boosted. This
generally results in a bilevel image at the end, where the image is composed of black and white
pixels. Thresholding belongs to the family of point-processing techniques. In this article, you will
learn how to perform Image Thresholding in OpenCV.
There are various ways of performing thresholding (Adaptive, Inverse, etc.), but the primary focus
of this article will be on binary thresholding and would touch upon other thresholding methods in
the end. For binary thresholding, we would be using the cv2.THRESH_BINARY flag
in cv2.threshold function.
For demonstration purposes, we would be using an image named test.jpg. Let’s see the code to
show the image.

 Python3

import cv2
# Loading the image named test.jpg

img = cv2.imread(r"ex2.jpg")

# Converting color mode to Grayscale

# as thresholding requires a single channeled image

img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cv2.imshow('Image', img)

Output:

Binary Thresholding
The function takes in argument a source image, threshold at which the cutoff has to take place,
maximum intensity value represented by the color space, the mode of thresholding and returns an
integer value (denoting result of the operation) and an image object containing the resultant
image after the processing.

 Python3

# Thresholding the image placing 127 intensity level as threshold

# Pixel values below 127 would be changed to Black

# Pixel values above 127 would be changed to White (255)

ret, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)

# Displaying the output image

cv2.imshow('Binary Threshold', thresh)

Output:
Binary – Inverse Thresholding

In this, the output will be the inverse of above output i.e. white pixel will be black and vice-versa.

 Python3

ret, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)

# Displaying the output image

cv2.imshow('Binary Inverse Threshold', thresh)

Output:

Truncate Thresholding

Similarly, the code for Truncate will be


 Python3

ret, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC)

# Displaying the output image

cv2.imshow('Truncate Threshold', thresh)

Output:

Zero Thresholding

Similarly, the code for Zero Thresholding will be :

 Python3
ret, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO)

# Displaying the output image

cv2.imshow('Truncate Threshold', thresh)

Output:

Inverse Thresholding to Zero

In this , we will do for Inverse Thresholding to zero .

 Python3

ret, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO_INV)


# Displaying the output image

cv2.imshow('Truncate Threshold', thresh)

Output:

import cv2
import numpy as np

# Read the image in grayscale


image = cv2.imread('input_image.jpg', cv2.IMREAD_GRAYSCALE)

# Apply a simple thresholding technique (binary thresholding)


threshold_value = 127
_, thresholded_image = cv2.threshold(image, threshold_value, 255, cv2.THRESH_BINARY)

# Display the original and thresholded images


cv2.imshow('Original Image', image)
cv2.imshow('Thresholded Image', thresholded_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
In this example: cv2.imread('input_image.jpg', cv2.IMREAD_GRAYSCALE): Reads the image in
grayscale. Thresholding is often applied to grayscale images.
cv2.threshold(image, threshold_value, 255, cv2.THRESH_BINARY): Applies binary thresholding. Pixels
with intensity values greater than threshold_value become white (255), and those below become
black (0).
You can adjust the threshold_value based on the characteristics of your image. If you want to apply
adaptive thresholding that adjusts the threshold locally in different regions of the image, you can use
cv2.adaptiveThreshold.

Adaptive Thresholding Example:


Python Copy code
import cv2
import numpy as np

# Read the image in grayscale


image = cv2.imread('input_image.jpg', cv2.IMREAD_GRAYSCALE)

# Apply adaptive thresholding


adaptive_thresholded_image = cv2.adaptiveThreshold(
image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2
)

# Display the original and adaptive thresholded images


cv2.imshow('Original Image', image)
cv2.imshow('Adaptive Thresholded Image', adaptive_thresholded_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
In this example, cv2.adaptiveThreshold calculates the threshold for small regions of the image and
thus handles variations in lighting conditions better.
Experiment with different thresholding techniques and parameters to achieve the desired
segmentation for your specific image. Thresholding is a versatile technique and can be used for
various image processing tasks such as object detection, image segmentation, and more.
4.8 Calculating Gradients:
Calculating gradients in an image is a fundamental operation in image processing and computer
vision. Gradients represent the rate of change of pixel intensity and are often used for tasks like
edge detection. Here's an example of how to calculate gradients using OpenCV in Python:

The gradient of a function simply means the rate of change of a function. We will
use numdifftools to find Gradient of a function.
Examples:
Input : x^4+x+1
Output :Gradient of x^4+x+1 at x=1 is 4.99

Input :(1-x)^2+(y-x^2)^2
Output :Gradient of (1-x^2)+(y-x^2)^2 at (1, 2) is [-4. 2.]
Approach:
 For Single variable function: For single variable function we can define directly using “lambda”
as stated below:-
g=lambda x:(x**4)+x+1
 For Multi-Variable Function: We will define a function using “def” and pass an array “x” and it
will return multivariate function as described below:-
 def rosen(x):
return (1-x[0])**2 +(x[1]-x[0]**2)**2
where ‘rosen’ is name of function and ‘x’ is passed as array. x[0] and x[1] are array elements in
the same order as defined in array.i.e Function defined above is (1-x^2)+(y-x^2)^2.
Similarly, We can define function of more than 2-variables also in same manner as stated above.
Method used: Gradient()
Syntax:
nd.Gradient(func_name)
Example:

import numdifftools as nd

g = lambda x:(x**4)+x + 1

grad1 = nd.Gradient(g)([1])

print("Gradient of x ^ 4 + x+1 at x = 1 is ", grad1)

def rosen(x):

return (1-x[0])**2 +(x[1]-x[0]**2)**2

grad2 = nd.Gradient(rosen)([1, 2])

print("Gradient of (1-x ^ 2)+(y-x ^ 2)^2 at (1, 2) is ", grad2)

Output:
Gradient of x^4+x+1 at x=1 is 4.999999999999998
Gradient of (1-x^2)+(y-x^2)^2 at (1, 2) is [-4. 2.]
Python Copy code
import cv2
import numpy as np

# Read the image in grayscale


image = cv2.imread('input_image.jpg', cv2.IMREAD_GRAYSCALE)

# Calculate the gradient using the Sobel operator


gradient_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3) # Sobel operator for x-direction
gradient_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3) # Sobel operator for y-direction

# Calculate the magnitude and angle of the gradient


magnitude = np.sqrt(gradient_x**2 + gradient_y**2)
angle = np.arctan2(gradient_y, gradient_x) * (180 / np.pi)

# Display the original and gradient images


cv2.imshow('Original Image', image)
cv2.imshow('Gradient X', cv2.convertScaleAbs(gradient_x)) # Convert to unsigned 8-bit for
visualization
cv2.imshow('Gradient Y', cv2.convertScaleAbs(gradient_y))
cv2.imshow('Gradient Magnitude', cv2.convertScaleAbs(magnitude))
cv2.imshow('Gradient Angle', angle.astype(np.uint8)) # Convert angle to unsigned 8-bit for
visualization
cv2.waitKey(0)
cv2.destroyAllWindows()
In this example:

cv2.Sobel: Applies the Sobel operator to calculate the gradient in the x and y directions. The ksize
parameter specifies the size of the Sobel kernel.
np.sqrt(gradient_x**2 + gradient_y**2): Calculates the magnitude of the gradient using the
Euclidean norm.
np.arctan2(gradient_y, gradient_x) * (180 / np.pi): Calculates the angle of the gradient in degrees.
It's important to note that the gradient images (gradient_x and gradient_y) can have positive and
negative values. The magnitude image represents the overall strength of the gradient, and the angle
image represents the direction.
Experiment with different gradient operators (Sobel, Scharr, Prewitt) and parameters to achieve the
desired results. Gradients are often used in various applications, such as edge detection, feature
extraction, and image segmentation.
4.9 Performing Histogram Equalization:
Histogram equalization is a technique used in image processing to enhance the contrast of an image
by adjusting the intensity values. It redistributes the intensity values to cover the entire range,
making the histogram more uniform. Here's an example of how to perform histogram equalization
using OpenCV in Python:

Histogram equalization is a method in image processing of contrast adjustment using the image’s
histogram.
This method usually increases the global contrast of many images, especially when the usable data
of the image is represented by close contrast values. Through this adjustment, the intensities can
be better distributed on the histogram. This allows for areas of lower local contrast to gain a
higher contrast. Histogram equalization accomplishes this by effectively spreading out the most
frequent intensity values. The method is useful in images with backgrounds and foregrounds that
are both bright or both dark.
OpenCV has a function to do this, cv2.equalizeHist(). Its input is just grayscale image and output is
our histogram equalized image.
Input Image :

Below is Python3 code implementing Histogram Equalization :

# import Opencv

import cv2

# import Numpy

import numpy as np

# read a image using imread

img = cv2.imread(\'F:\\do_nawab.png\', 0)

# creating a Histograms Equalization

# of a image using cv2.equalizeHist()


equ = cv2.equalizeHist(img)

# stacking images side-by-side

res = np.hstack((img, equ))

# show image input vs output

cv2.imshow(\'image\', res)

cv2.waitKey(0)

cv2.destroyAllWindows()

Output :
Python Copy code
import cv2
import numpy as np
from matplotlib import pyplot as plt

# Read the image in grayscale


image = cv2.imread('input_image.jpg', cv2.IMREAD_GRAYSCALE)

# Apply histogram equalization


equalized_image = cv2.equalizeHist(image)

# Display the original and equalized images


plt.figure(figsize=(10, 5))

plt.subplot(1, 2, 1)
plt.title('Original Image')
plt.imshow(image, cmap='gray')

plt.subplot(1, 2, 2)
plt.title('Equalized Image')
plt.imshow(equalized_image, cmap='gray')

plt.show()
In this example:

cv2.equalizeHist: Applies histogram equalization to the input image.

plt.imshow: Displays the original and equalized images side by side using Matplotlib.

Note: Histogram equalization is often more effective when applied to grayscale images. If you're
working with color images, you may want to convert them to grayscale before equalization.

Histogram equalization is particularly useful for enhancing the visibility of details in images with poor
contrast. Experiment with this technique to see how it affects the appearance of different images.

You might also like