Chapter 3
Chapter 3
Advantages of negative :
Produces an equivalent of a photographic negative.
Enhances white or gray detail embedded in dark regions
Cont’d..
Example 1: the following matrix represents the pixels values of an 8-bit image
(r) , apply negative transform and find the resulting image pixel values.
The image is loaded using We use Matplotlib to plot the The original image is displayed using
cv2.imread(). The image histograms. For the grayscale image, plt.imshow(). The cv2.cvtColor(image,
color image.
variable contains the original we plot the histogram in black. For
the color image, we plot the
cv2.COLOR_BGR2RGB) function converts the
color image from OpenCV's default BGR
The image is then converted to histograms for each color channel in format to RGB format for correct display in
grayscale using cv2.cvtColor() blue, green, and red. Matplotlib.
for grayscale histogram The plt.xlim([0, 256]) ensures that the
calculation. x-axis of each histogram spans the
entire range of intensity values (from 5. Tight Layout:
2. Calculating Histograms: 0 to 255).
plt.tight_layout() ensures that the subplots do
Grayscale Histogram: not overlap.
cv2.calcHist([gray_image], [0],
None, [256], [0, 256]) computes
the histogram for the grayscale
image. The histogram will have
256 bins (one for each intensity
value from 0 to 255).
Color Histograms: For the
color image, histograms for the
Blue, Green, and Red channels
are computed separately using
cv2.calcHist(). The channel
indices are 0 for Blue, 1 for
Green, and 2 for Red.
Histogram processing
1. Image Loading: The image 3. Histogram Sliding (Stretching): Using 5. Displaying Images: Both the original and
is loaded in grayscale using
cv2.imread(). Make sure to
the formula provided, we calculate the
new pixel values and stretch the image
the stretched images are displayed side by
side using Matplotlib for easy comparison.
replace 'image.jpg' with the intensity values. 6. Saving the Image: Optionally, the
correct path to your image. 4. Plotting Histograms: We calculate stretched image is saved to the disk using
2. Identify Min and Max and plot the histograms of the original cv2.imwrite().
Values: We use np.min(image) and stretched images using
and np.max(image) to find the cv2.calcHist(). The histograms are
minimum and maximum pixel plotted using Matplotlib.
values in the image. These are
used to stretch the pixel
intensities to the full range of
[0, 255].
Cont’d..
Histogram Stretching
In histogram stretching, contrast of an image is increased.
The contrast of an image is defined between the maximum and minimum value
of pixel intensity.
Histogram Equalization
Histogram equalization is used for equalizing all the pixel values of an image.
Transformation is done in such a way that uniform flattened histogram is
produced.
Histogram Equalization is a technique in
image processing that enhances the contrast
of an image by adjusting the intensity values.
The goal is to distribute the pixel intensities
evenly across the available range (e.g., from
0 to 255 in 8-bit grayscale images).
This method improves the visibility of details
in an image, especially when the image has
poor contrast or is too dark/bright.
How Histogram Equalization Works:
1. Compute the histogram of the image, which
represents the frequency of each intensity
value.
2. Compute the cumulative distribution
function (CDF) from the histogram. This
function helps us map the original intensities
to new values.
3. Normalize the CDF so that it spans the full
range of pixel values (from 0 to 255).
4. Map the original intensities to the new
Python Code for Histogram Equalization:
import cv2 # Step 2: Calculate histograms of the # Step 4: Display the original and equalized
import numpy as np
import matplotlib.pyplot as plt
original and equalized images
original_hist = cv2.calcHist([image], [0],
images
plt.figure(figsize=(12, 6))
None, [256], [0, 256])
# Load the image (grayscale) equalized_hist = # Original Image
image = cv2.calcHist([equalized_image], [0], plt.subplot(1, 2, 1)
cv2.imread('image.jpg', None, [256], [0, 256]) plt.imshow(image, cmap='gray')
cv2.IMREAD_GRAYSCALE) plt.title('Original Grayscale Image')
# Replace 'image.jpg' with # Step 3: Plot the original and equalized plt.axis('off')
your image path histograms
plt.figure(figsize=(12, 6)) # Equalized Image
# Check if image is loaded plt.subplot(1, 2, 2)
correctly # Original Image Histogram plt.imshow(equalized_image, cmap='gray')
if image is None: plt.subplot(1, 2, 1) plt.title('Equalized Grayscale Image')
raise ValueError("Image plt.plot(original_hist, color='black') plt.axis('off')
not found. Please check the file plt.title('Original Histogram')
path.") plt.xlim([0, 256]) plt.tight_layout()
plt.show()
# Step 1: Apply Histogram # Equalized Image Histogram
Equalization using OpenCV plt.subplot(1, 2, 2) # Optionally, save the equalized image
equalized_image = plt.plot(equalized_hist, color='black') cv2.imwrite('equalized_image.jpg',
cv2.equalizeHist(image) plt.title('Equalized Histogram') equalized_image)
plt.xlim([0, 256])
plt.tight_layout()
plt.show()
Code Explanation:
1. Image Loading: 3. Histogram Calculation: 5. Displaying Images:
The image is loaded in cv2.calcHist() calculates the histogram Both the original and equalized images are
grayscale using cv2.imread(). of the original and equalized images. displayed using plt.imshow() for visual
Make sure the file path The histograms represent the comparison. This shows how histogram
('image.jpg') is correct for distribution of pixel intensities in the equalization improves image contrast.
your specific image. images. 6. Saving the Equalized Image:
2. Histogram Equalization: 4. Plotting Histograms:
cv2.equalizeHist(image) The equalized image can be saved using
applies the histogram Using Matplotlib, we plot the cv2.imwrite().
equalization algorithm histograms of both the original and the
provided by OpenCV. It equalized images side by side. This
enhances the contrast by shows how the equalized image has a
redistributing the pixel values more uniform intensity distribution
to cover the entire intensity compared to the original.
range [0, 255].1
Spatial filtering
Fundamental of Spatial filtering
Filter term in “Digital image processing” is referred to the sub image
Spatial filtering is a technique used to enhance the image based on the spatial
characteristics of the image.
Filtering is a fundamental operation in image processing. It can be used for:
1- Image enhancement
2- Noise reduction
3- Edge detection,
4- Sharpening.
Cont’d..
The concept of filtering has been applied in the:
Frequency domain, where it rejects some frequency components while
accepting others.
The term frequency in an image tells about the rate of change of pixel values.
Spatial domain, Spatial filtering modifies an image by replacing the value of
each pixel with a function of the values of the pixel and
its neighbors (filtering is pixel neighborhood operation).
Cont’d..
Smoothing filter is used for blurring and noise reduction in the image.
Blurring is pre-processing steps for removal of small details and
Noise Reduction is accomplished by blurring.
The commonly used smoothing filters are Averaging and Gaussian
filters.
It can be performed using the convolution operation
Combining Spatial Enhancement Methods
In real applications, it is hard to know what kind of noise has been added
to an image.
Therefore, it is difficult to find a unique filter that can appropriately
enhance this noisy image.
However, it is possible if several de-blurring methods can be combined in
a framework in order to pursue a maximum demising outcome.
We explain one of the combinatorial techniques using an X-ray example.
Cont’d..
Illustrates a male chest’s X-ray image.
The purpose of the process is to highlight the middle cross section of the
image using the combination of sharpening and edge detection
Reading Assignment