0% found this document useful (0 votes)
11 views10 pages

Digital Image Processing

answers to some basic questions

Uploaded by

Deschanel Nfor
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)
11 views10 pages

Digital Image Processing

answers to some basic questions

Uploaded by

Deschanel Nfor
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

Digital Image Processing Notes for Beginners

University of Buea, Faculty of Engineering and Technology


Department of Computer Engineering, 2024-2025
Prepared based on notes by Dr. SOP DEFFO Lionel Landry

Preface
Welcome to Digital Image Processing! This document is designed to make the
subject easy to understand, even if you’re new to the topic. We’ll explore what
digital images are, how we can manipulate them, and how computers can ”see”
and understand images. Think of this as a guide to teaching a computer to look at
pictures the way we do—by recognizing objects, enhancing details, or removing
noise. We’ll use simple explanations, analogies (like comparing image process-
ing to cooking), and examples to break down complex ideas. Let’s dive in!

1 Chapter 1: Introduction to Digital Image Process-


ing
1.1 What is a Digital Image?
A digital image is like a grid of tiny colored squares called pixels (short for pic-
ture elements). Each pixel is a small dot with a value that represents its color or
brightness. Imagine a digital image as a mosaic made of thousands of tiny tiles,
where each tile has a specific shade or color.

• Grayscale Images: Each pixel has one value, representing a shade of gray
(like a black-and-white photo). For example, 0 might be black, 255 might
be white, and values in between are shades of gray.
• Color Images: Each pixel has three values (Red, Green, Blue, or RGB) to
create colors. Some images also include a fourth value called ”Alpha” for
transparency.
• Why Digital? A digital image is an approximation of the real world be-
cause it breaks a scene into a finite grid of pixels. This makes it easier for
computers to process.

Example: Think of a digital image like a puzzle. Each piece (pixel) has a color,
and when you put all the pieces together, you see the whole picture, like a sunset
or a cat.

1.2 What is Digital Image Processing?


Digital image processing is about using computers to change or analyze images
to make them more useful. It’s like editing a photo on your phone to make it look
better or teaching a computer to recognize what’s in the picture.

1
There are two main goals:

1. Improve Images for Humans: Enhance images to make them clearer, brighter,
or more appealing. For example, removing blur from a photo.

2. Understand Images for Machines: Help computers ”see” and interpret


images, like identifying a stop sign for a self-driving car.

Image processing can range from simple tasks (like brightening an image)
to complex ones (like detecting objects). It connects to fields like image analy-
sis (extracting information) and computer vision (teaching machines to under-
stand scenes).

1.3 A Brief History of Digital Image Processing


Digital image processing has a cool history, starting long before smartphones or
Instagram filters:

• 1920s: Early digital images were used in newspapers. The Bartlane system
sent coded images via submarine cables between London and New York,
reconstructing them on telegraph printers.

• Late 1920s: Improved systems used photographic techniques for better im-
age quality and more shades (tones).

• 1960s: The space race boosted digital image processing. In 1964, computers
enhanced moon images from the Ranger 7 probe, aiding NASA missions like
Apollo.

• 1970s: Medical imaging took off. In 1979, the Nobel Prize was awarded for
inventing tomography, which powers CAT scans.

• 1980s–Today: Digital image processing is everywhere—enhancing photos,


adding artistic effects, inspecting products in factories, detecting crimes,
and powering self-driving cars.

2 Chapter 2: Low-Level Transformations


2.1 Morphological Transformations
Morphological transformations are like sculpting an image to change its shape.
They work on binary images (where pixels are either 0 for black or 1 for white)
and use a small grid called a structuring element or kernel to decide how to
modify the image.

2
2.1.1 Erosion

Erosion is like sanding down the edges of a white object in a binary image. The
kernel slides over the image, and a pixel stays white (1) only if all pixels under
the kernel are white. If any pixel is black (0), the center pixel becomes black.
• Effect: Shrinks white objects and removes small white specks (noise).
• Use: Cleaning up noise or separating connected objects.
• Example Code:
import cv2
import numpy as np
img = cv2.imread(’j.png’, 0) % Grayscale image
kernel = np.ones((5,5), np.uint8) % 5x5 kernel of ones
erosion = cv2.erode(img, kernel, iterations=1)

2.1.2 Dilation

Dilation is the opposite—it makes white objects grow. A pixel becomes white (1)
if at least one pixel under the kernel is white.
• Effect: Expands white objects, filling small gaps.
• Use: Restoring objects shrunk by erosion or joining broken parts.
• Example Code:
dilation = cv2.dilate(img, kernel, iterations=1)

2.1.3 Other Morphological Operations

• Opening: Erosion followed by dilation. Great for removing noise without


shrinking objects too much.
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
• Closing: Dilation followed by erosion. Fills small holes in objects.
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
• Morphological Gradient: Difference between dilation and erosion, show-
ing object outlines.
gradient = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)
• Top Hat: Difference between the original image and its opening. Highlights
small bright details.
tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, np.ones((9,9), np.ui
• Black Hat: Difference between closing and the original image. Highlights
small dark details.
blackhat = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, np.ones((9,9), n

3
2.2 Image Denoising
Denoising is like cleaning up a blurry or grainy photo. Noise is random varia-
tions in pixel values, like static on a TV. The idea is to average out noise to reveal
the true image.
Non-Local Means Denoising: This method looks for similar patches in the
image and averages them to reduce noise. Imagine you have a noisy photo of a
sky. Instead of blurring the whole image, you find patches that look similar (e.g.,
blue sky patches) and average them to smooth out noise while keeping details.

• How It Works: For each pixel, take a small window (e.g., 5x5), find similar
windows elsewhere in the image, and average their values.

• OpenCV Functions:

– cv2.fastNlMeansDenoising(): For grayscale images.


– cv2.fastNlMeansDenoisingColored(): For color images.
– cv2.fastNlMeansDenoisingMulti(): For video frames (grayscale).
– cv2.fastNlMeansDenoisingColoredMulti(): For video frames (color).

• Parameters:

– h: Controls filter strength (e.g., 10). Higher values remove more noise
but may blur details.
– templateWindowSize: Size of the patch (e.g., 7, odd).
– searchWindowSize: Size of the search area for similar patches (e.g.,
21, odd).

• Example Code (Color Image):

import cv2
import numpy as np
img = cv2.imread(’bear.png’)
dst = cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 15)

• Example Code (Video):

cap = cv2.VideoCapture(’vtest.avi’)
img = [cap.read()[1] for i in range(5)] % Read 5 frames
gray = [cv2.cvtColor(i, cv2.COLOR_BGR2GRAY) for i in img]
gray = [np.float64(i) for i in gray]
noise = np.random.randn(*gray[1].shape) * 10
noisy = [np.uint8(np.clip(i + noise, 0, 255)) for i in gray]
dst = cv2.fastNlMeansDenoisingMulti(noisy, 2, 5, None, 4, 7, 35)

4
2.3 Arithmetic Operations
Arithmetic operations, like adding or subtracting, change an image’s brightness
or contrast. Think of it like adjusting the brightness slider on your phone.

• Addition: Increases brightness by adding a value to each pixel.

• Subtraction: Decreases brightness by subtracting a value.

• Example Code:

import cv2
import numpy as np
image = cv2.imread(’lena.png’)
matrix = np.ones(image.shape, dtype=”uint8”) * 75 % Matrix of 75s
added = cv2.add(image, matrix) % Brightens image
subtracted = cv2.subtract(image, matrix) % Darkens image

2.4 Filters: Blurring and Sharpening


Filters use convolution, which is like applying a recipe to each pixel by com-
bining it with its neighbors. The kernel is the recipe, telling us how much each
neighbor contributes.

• Blurring: Averages neighboring pixels to smooth the image. Example: A


3x3 kernel with all values 19 averages 9 pixels to reduce noise.

• Sharpening:
 Emphasizes
 differences to highlight edges. Example: A kernel
−1 −1 −1

like −1 9 −1 boosts the center pixel.
−1 −1 −1

• Example Code:

import cv2
import numpy as np
image = cv2.imread(’lena.png’)
kernel_blur = np.ones((3,3), np.float32) / 9 % Blurring kernel
kernel_sharpen = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]]) % Sha
blurred = cv2.filter2D(image, -1, kernel_blur)
sharpened = cv2.filter2D(image, -1, kernel_sharpen)

2.5 Bitwise Operations and Masking


Bitwise operations work on binary images to combine or mask shapes. Think of
them as using stencils to control which parts of an image show up.

• AND: Shows only where both images are white (1).

• OR: Shows where either image is white.

5
• XOR: Shows where one image is white, but not both.

• NOT: Inverts an image (white becomes black, black becomes white).

• Example Code:

import cv2
import numpy as np
square = np.zeros((300,300), np.uint8)
cv2.rectangle(square, (50,50), (250,250), 255, -1)
ellipse = np.zeros((300,300), np.uint8)
cv2.ellipse(ellipse, (150,150), (150,150), 30, 0, 180, 255, -1)
bitwise_and = cv2.bitwise_and(square, ellipse)
bitwise_or = cv2.bitwise_or(square, ellipse)
bitwise_xor = cv2.bitwise_xor(square, ellipse)
bitwise_not = cv2.bitwise_not(ellipse)

3 Chapter 3: Middle-Level Transformations: Image


Segmentation
3.1 What is Image Segmentation?
Image segmentation is like coloring a picture book, where you assign different
colors to different objects. It divides an image into regions (segments) based on
pixel similarities, like color or texture. Instead of processing the whole image,
we focus on important parts.

• Output: Segments can be contours (outlines) or masks (where each region


has a unique color or value).

• Why It Matters: In cancer detection, segmentation helps identify the shape


of cancerous cells, which bounding boxes (used in object detection) can’t
do.

3.2 Types of Image Segmentation


• Semantic Segmentation: Labels all pixels of the same object type with one
color. Example: All people in an image are pink, and the background is
black.

• Instance Segmentation: Labels each object individually, even if they’re


the same type. Example: Person 1 is red, Person 2 is green, and the back-
ground is black.

6
3.3 Region-Based Segmentation
This method groups pixels based on their values, like brightness. It’s like sorting
candies by color.

• Thresholding: Sets a cutoff value to separate objects from the background.


If a pixel’s value is above the threshold, it’s an object; below, it’s the back-
ground.

• Global Threshold: One threshold for the whole image.

• Local Threshold: Multiple thresholds for multiple objects.

• Example Code:

from skimage.color import rgb2gray


import numpy as np
import matplotlib.pyplot as plt
image = plt.imread(’1.jpeg’)
gray = rgb2gray(image)
gray_r = gray.reshape(gray.shape[0] * gray.shape[1])
for i in range(gray_r.shape[0]):
if gray_r[i] > gray_r.mean():
gray_r[i] = 1 % Object
else:
gray_r[i] = 0 % Background
gray = gray_r.reshape(gray.shape[0], gray.shape[1])
plt.imshow(gray, cmap=’gray’)

• Pros: Simple, fast, works well with high contrast.

• Cons: Fails if objects and background have similar values.

3.4 Edge Detection Segmentation


Edges are where pixel values change sharply, like the boundary between a coin
and a table. We use filters to find these edges.

• Sobel Operator: Detects horizontal and vertical edges using kernels like:
   
1 2 1 −1 0 1
Horizontal =  0 0 0  , Vertical = −2 0 2
−1 −2 −1 −1 0 1

• Laplacian Operator: Detects all edges with a kernel like:


 
1 1 1
1 −8 1
1 1 1

• Example Code:

7
from scipy import ndimage
image = plt.imread(’index.png’)
gray = rgb2gray(image)
sobel_horizontal = np.array([[1,2,1], [0,0,0], [-1,-2,-1]])
sobel_vertical = np.array([[-1,0,1], [-2,0,2], [-1,0,1]])
out_h = ndimage.convolve(gray, sobel_horizontal, mode=’reflect’)
out_v = ndimage.convolve(gray, sobel_vertical, mode=’reflect’)

• Pros: Good for high-contrast images.


• Cons: Struggles with noisy images or low contrast.

3.5 Clustering-Based Segmentation


Clustering groups similar pixels, like sorting marbles by color. The k-means al-
gorithm is popular:
1. Pick k random cluster centers.
2. Assign each pixel to the nearest center.
3. Update centers as the average of assigned pixels.
4. Repeat until centers don’t change.
• Example (Color Quantization): Reduce an image to k colors.

import cv2
import numpy as np
pic = plt.imread(’1.jpeg’)
pic_n = pic.reshape(pic.shape[0] * pic.shape[1], pic.shape[2])
pic_n = np.float32(pic_n)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1
K = 5
ret, label, center = cv2.kmeans(pic_n, K, None, criteria, 10, cv2.KME
center = np.uint8(center)
pic2 = center[label.flatten()].reshape(pic.shape)

• Pros: Works well for small datasets, no training needed.


• Cons: Slow for large images, needs predefined k, struggles with irregular
shapes.

3.6 Summary of Segmentation Techniques


Technique Advantages Limitations
Region-Based Simple, fast, great Fails with similar
for high contrast pixel values
Edge Detection Good for clear Sensitive to noise,
boundaries low contrast
Clustering Works on small Slow, needs prede-
datasets, flexible fined clusters

8
4 Chapter 4: Middle-Level Transformations: Object
Detection
4.1 What is Object Detection?
Object detection is like teaching a computer to point at objects in a picture and
say, ”That’s a car!” or ”That’s a person!” It identifies what objects are in an image
and where they are, usually by drawing bounding boxes around them.

• Example: In a self-driving car, object detection spots pedestrians so the car


can avoid them.

• Applications: Traffic control, quality inspection, surveillance, sports anal-


ysis.

4.2 Classification vs. Segmentation vs. Object Detection


• Classification: Labels the whole image. Example: ”This is a beach.” It an-
swers, ”What’s in the image?”

• Segmentation: Labels each pixel to create regions. Example: Coloring all


people pink in an image.

• Object Detection: Draws boxes around objects and labels them. Example:
Box around each car with the label ”Car.”

4.3 Approaches to Object Detection


1. Naive Approach: Divide the image into four parts (e.g., top-left, bottom-
right). Classify each part to check for objects. Problem: Too coarse, misses
parts of objects.

2. More Divisions: Split the image into many small patches. Classify each
patch. Problem: Creates too many overlapping boxes.

3. Structured Divisions: Use a grid (e.g., 10x10), define centroids, and try
patches of different sizes. Improvement: More precise but still has over-
laps.

4. Efficient Approach: Increase grid size (e.g., 20x20) and use more patch
shapes. Add an intermediate classifier to filter out background patches,
reducing the number of patches to classify.

5. Deep Learning Approach: Use neural networks to:

• Reduce image dimensions.


• Suggest patches to check.
• Predict tight bounding boxes.

Example: Models like YOLO or Faster R-CNN for accurate, fast detection.

9
4.4 Bounding Boxes
Bounding boxes are rectangles around objects, defined by coordinates (xmin , ymin , xmax , ymax ).
They come with a label (e.g., ”Pedestrian”) and a confidence score (e.g., 0.95).

• Use: Pinpoint object locations and types.

• Example: In an image, a car at (100, 150, 300, 250) labeled ”Car” (0.95).

10

You might also like