Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
86 views

P3-Arithmetic and Logic Operations

The document discusses various arithmetic operations that can be performed on digital images including addition, subtraction, multiplication, and division at the pixel level and describes how to use functions like imadd, imsubtract, immultiply, and imdivide in MATLAB to perform these operations while handling issues like overflow, underflow, and truncation. It also provides examples of how arithmetic operations can be used to blend images, calculate differences between images, adjust image brightness, and compute the average of multiple images.

Uploaded by

yakaellah
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views

P3-Arithmetic and Logic Operations

The document discusses various arithmetic operations that can be performed on digital images including addition, subtraction, multiplication, and division at the pixel level and describes how to use functions like imadd, imsubtract, immultiply, and imdivide in MATLAB to perform these operations while handling issues like overflow, underflow, and truncation. It also provides examples of how arithmetic operations can be used to blend images, calculate differences between images, adjust image brightness, and compute the average of multiple images.

Uploaded by

yakaellah
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Arithmetic and Logic

Operations
Digital Image Processing
Arithmetic Operations
• Arithmetic operations involving images are typically
performed on a pixel-by-pixel basis; that is, the operation
is independently applied to each pixel in the image.
• Given a 2D array (X) and another 2D array of the same
size or a scalar (Y), the resulting array, Z, is obtained by
calculating
X opn Y = Z
where opn is a binary arithmetic (+, −, ×, / ) operator
Aritmetic Operation: Addition
• Addition is used to blend the pixel contents
from two images or add a constant value to
pixel values of an image.
• Adding the contents of two monochrome
images causes their contents to blend
• Adding a constant value (scalar) to an image
causes an increase (or decrease if the value is
less than zero) in its overall brightness.
• A process sometimes referred to as additive
image offset.
• Adding random amounts to each pixel value is
a common way to simulate additive noise.
• The resulting (noisy) image is typically used as
a test image for restoration algorithms.
Overflow Issues
• When adding two images, you must • Normalization consists in storing the
be careful with values that exceed the intermediate result in a temporary
maximum pixel value for the data variable (W) and calculating each resulting
type being used. pixel value in Z using equation.

• There are two ways of dealing with


this overflow issue: normalization and
truncation. • where f is the current pixel in W, Lmax is
• Truncation consists in simply limiting the maximum possible intensity value
(e.g., 255 for uint8 or 1.0 for double), g is
the results to the maximum positive
the corresponding pixel in Z, fmax is the
number maximum pixel value in W, and fmin is
• that can be represented with the the minimum pixel value in W.
adopted data type
Overflow Issues (example)
• For the two 3 × 3 monochrome
images below (X and Y), each of
which represented as an array of
unsigned integers, 8-bit (uint8),
calculate Z = X + Y, using
• Normalization, and
• Truncation.
Aritmetic Operation: Subtraction
• Subtraction is often used to detect differences between two
images.
• Such differences may be due to several factors, such as artificial
addition to or removal of relevant contents from the image (e.g.,
using an image manipulation program), relative object motion
between two frames of a video sequence, and many others.
• Subtracting a constant value (scalar) from an image causes a
decrease in its overall brightness, a process sometimes referred to
as subtractive image offset.
• When subtracting one image from another or a constant (scalar)
from an image, you must be careful with the possibility of
obtaining negative pixel values as a result.
• There are two ways of dealing with this underflow issue:
• treating subtraction as absolute difference (which will always
result in positive values proportional to the difference between the
two original images without indicating, however, which pixel was
brighter or darker), and
• truncating the result, so that negative intermediate values
become zero.
Underflow Issues (example)
• For the two 3 × 3 monochrome images
below (X and Y), each of which
represented as an array of unsigned
integers, 8-bit (uint8), calculate
(a) Z = X − Y,
(b) Z = Y − X, and
(c) Z = |Y − X|.
• For parts (a) and (b), use truncation to
deal with possible negative values.
Negative Image
• Image subtraction can also be used to
obtain the negative of an image

• where Lmax is the maximum possible


intensity value (e.g., 255 for uint8 or
1.0 for double), f is the pixel value in
X, g is the correspond1ing pixel in Z
Aritmetic Operation:
Multiplication and Division
• Multiplication and division by a scalar are often used to perform brightness adjustments
on an image.
• This process sometimes referred to as multiplicative image scaling—makes each pixel
value brighter (or darker) by multiplying its original value by a scalar factor:
• if the value of the scalar multiplication factor is greater than one, the result is a brighter
image;
• if it is greater than zero and less than one, it results in a darker image.
• Multiplicative image scaling usually produces better subjective results than the additive
image offset process described previously.

Multiplication and division by a constant:


(a) original image (X);
(b) (b) multiplication result (X × 0.7);
(c) (c) division result (X/0.7).
Aritmetic Operation: Combining
• It is sometimes necessary to combine several arithmetic
operations applied to one or more images, which may
compound the problems of overflow and underflow
discussed previously.
• To achieve more accurate results without having to
explicitly handle truncations and round-offs, the IPT offers
a built-in function to perform a linear combination of two
or more images: imlincomb.
• This function computes each element of the output
individually, in double-precision floating point.
• If the output is an integer array, imlincomb truncates
elements that exceed the range of the integer type and
rounds off fractional values.
Linear Combination (example)
Calculate the average of the three 3 × 3
monochrome images below (X, Y, and Z),
each of which represented as an array of
unsigned integers, 8-bit (uint8), using
a. imadd and imdivide without explicitly
handling truncation and round-offs;
b. imadd and imdivide, but this time
handling truncation and round-offs; and The result in (a) is incorrect due to
truncation of intermediate results.
c. imlincomb.

Both (b) and (c) produce correct results, but the solution
using imlincomb is much more elegant and concise.
Arithmetic Operations
Goal
• The goal of this tutorial is to learn how to perform arithmetic operations on
images.

Objectives
• Learn how to perform image addition using the imadd function.
• Explore image subtraction using the imsubtract function.
• Explore image multiplication using the immultiply function.
• Learn how to use the imdivide function for image division.
Procedure
• The IPT offers four functions to aid in image arithmetic:
imadd, imsubtract, immultiply, and imdivide
• Image addition can be used to brighten (or darken) an image by adding
(subtracting) a constant value to (from) each pixel value. It can also be used to
blend two images into one.
Arithmetic Operations: Brighten Image
Question 1
What are the maximum and minimum
values of the original and the adjusted
image? Explain your results.
Hint: max and min MatLab function

Question 2
How many pixels had a value of 255 in the
original image and how many have a value
of 255 in the resulting image?
Hint:
Use imadd documentation to find more! - Logic Index Filter:
A(A logic_operator constant)
- number of element MatLab Function
Arithmetic Operations: Blend Image
• Have you found your image in
blend way?
• Try to enlarge the data type
and adjusted the display!
Arithmetic Operations: The Different
• Image subtraction is useful when • By subtracting one image from another, we can
determining whether two images are the highlight the differences between the two.
same.

It might not be so clear to see the different! • Use the zoom tool to zoom into the right area of
the difference image about halfway down the
image.
• You will notice that a small region of pixels is
faintly white.
Arithmetic Operations: The Different
(cont’d)
• The difference image above does not quite
seem to display all the details of the
missing building.
• This is because when we performed image
subtraction, some of the pixels resulted
in negative values, but were then set to 0
by
• the imsubtract function (the function does
this on purpose to keep the data within
grayscale range).
• What we really want to do is calculate the
absolute value of the difference between
two images.

the new image may look the same as the previous one!
But, it represents both positive and negative differences between the two images
Arithmetic Operations: The Different
(cont’d)
• To see this difference better, we will
scale both difference images for
display purposes, so their values
occupy the full range of the gray
scale.

Question 3
How did we scale the image output?
Question 4
What happened when we scaled the difference images?
Question 5
Why does the last image show more detail than the
others?
Arithmetic Operations: Dynamic Scaling
Question 6
When dynamically scaling the moon image,
why did the dark regions around the moon
not become brighter as in the normally
adjusted image?

“Multiplication is the process of multiplying the values of each pixel


of same coordinates in two images. This can be used for a
brightening process known as dynamic scaling, which results in a
more naturally brighter image compared to directly adding a constant
to each pixel.”
Arithmetic Operations: Special Effect
• Image multiplication can also be used for special effects such as an artificial 3D look.
• By multiplying a flat image with a gradient, we create the illusion of a 3D textured
surface.
Arithmetic Operations: Inverse Dynamic
Scaling
• Image division can be used as the
inverse operation to dynamic scaling.
• Image division is accomplished with the
imdivide function.
• When using image division for this
purpose, we can achieve the same effect
using the immultiply function.
(Try it in multiply way)

Question 7
Why did the multiplication procedure produce the same result as division?

Question 8
Write a small script that will verify that the images produced from division and multiplication are equivalent!
Arithmetic Operations: Extract
Background
• Another use of the image division process is to extract the background from an image.
• This is usually done during a preprocessing stage of a larger, more complex operation.

This image could represent a document that was


scanned under inconsistent lighting conditions!
Arithmetic Operations:
Extract Background (cont’d)
• Because of the background, the text in this image cannot be
processed directly we must preprocess the image before we
can do anything with the text.
• If the background were homogeneous, we could use image
thresholding to extract the text pixels from the background.
• Thresholding is a simple process of converting an image to its
binary equivalent by defining a threshold to be used as a cutoff
value:
anything below the threshold will be discarded (set to 0) and
anything above it will be kept (set to 1 or 255, depending on
the data class we choose).
Arithmetic Operations:
Extract Background (cont’d)
• we can see that even though we attempted to segregate the
image into dark and light pixels, it produced only part of the
text we need (on the upper right portion of the image).
• If an image of the background with no text on it is available,
we can use the imdivide function to extract the letters.
• To obtain such background image in a real scenario, such as
scanning documents, a blank page that would show only the
inconsistently lit background could be scanned.
Arithmetic Operations:
Extract Background (cont’d)
Question 9
Would this technique still work if we were unable to obtain the
background image?
LOGIC OPERATIONS
• Logic operations are performed in a
bit-wise fashion on the binary
contents of each pixel value.
• The AND, XOR, and OR operators
require two or more arguments,
whereas the NOT operator requires
only one argument.
• The most common logic operations
applied to binary images, using the
following convention:
• 1 (true) for white pixels and 0 (false)
for black pixels.
LOGIC OPERATIONS
AND, OR, XOR, and NOT operations can implement on
monochrome images.
• The AND and OR operations can be used to combine
images for special effects purposes.
• They are also used in masking operations, whose goal The AND operation applied to monochrome images:
is to extract a region of interest (ROI) from an image. (a) X; (b) Y; (c) X AND Y.
• The XOR operation is often used to highlight
differences between two monochrome images.
• It is, therefore, equivalent to calculating the absolute
difference between two images.
• The NOT operation extracts the binary complement of
each pixel value, which is equivalent to applying the
The OR operation applied to monochrome images:
“negative” effect on an image.
(a) X; (b) Y; (c) X OR Y
LOGIC OPERATIONS AND
REGION OF INTEREST PROCESSING
Goal
The goal of this tutorial is to learn how to perform logic operations on images.

Objectives
• Explore the roipoly function to generate image masks.
• Learn how to logically AND two images using the bitand function.
• Learn how to logically OR two images using the bitor function.
• Learn how to obtain the negative of an image using the bitcmp function.
• Learn how to logically XOR two images using the bitxor function.
REGION OF INTEREST (ROI)
• Logic operators are often used for image
masking.
• We will use the roipoly function to create
Question 1 the image mask.
How do we add points to the polygon? • Once we have a mask, we will use it to
perform logic operations on the selected
Question 2 image.
How do we delete points from the polygon?

Question 3
How do we end the process of creating a polygon?

Question 4
What class is the variable bw?

Question 5
What does the variable bw represent?
LOGIC OPERATIONS
• Logic functions operate at the bit level; • In order for us to use the bw image in any
that is, the bits of each image pixel are logical calculation, we must ensure that it
compared individually, and the new bit is consists of the same number of bits as the
calculated based on the operator we are original image.
using (AND, OR, or XOR).
• Because the bw image already has the
• This means that we can compare only two correct number of rows and columns, we
images that have the same number of bits need to convert only the image to uint8,
per pixel as well as equivalent dimensions. so that each pixel is represented by 8 bits.

Question 7
What happens when we logically AND the two images?
LOGIC OPERATIONS
• To see how to OR two images, we must
first visit the bitcmp function, which is
used for complementing image bits (NOT).

Question 8
What happened when we complemented the bw2 image?

Question 9
Why did we need to complement the mask? What would have happened if we used the original mask
to perform the OR operation?
LOGIC OPERATIONS
• The IPT also includes function
imcomplement, which performs the same
operation as the bitcmp function,
complementing the image.
• The function imcomplement allows input
images to be binary, grayscale, or RGB,
whereas bitcmp requires that the image
be an array of unsigned integers.

Question 10
How can we check to see that the bw_cmp2 image is the same as the bw_cmp image?
LOGIC OPERATIONS
• The XOR operation is
commonly used for finding
differences between two
images.
• Logic operators are often
combined to achieve a
particular task, for example
darken an image only within
a region of interest.
LOGIC OPERATIONS

Question 11
How could we modify the above code to display the original image within the region of interest
and the darker image elsewhere?
Thank You

You might also like