P3-Arithmetic and Logic Operations
P3-Arithmetic and Logic Operations
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.
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?
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.
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