Digital Video Processing Asg 4
.pdf
keyboard_arrow_up
School
Arizona State University *
*We aren’t endorsed by this school
Course
509
Subject
Computer Science
Date
Dec 6, 2023
Type
Pages
10
Uploaded by SuperHumanOstrich3671
Digital Video Processing
Assignment 4, Due Sunday, October 22nd at 11:59pm
Name: Yash Ashok Dhamecha
ASU ID (emplid): 1229568290
ASURITE User ID: ydhamech
1.
Write Python code to demonstrate the use of the OpenCV function matchTemplate() to search
for matches between an image patch and an input image. Also, use the OpenCV function
minMaxLoc() to find the maximum and minimum values (as well as their positions) in the
output array of matchTemplate(). Use the images of Nadia provided on our website and
produce an output similar to that shown below. Note that when the template is found, a white
rectangle is drawn over the original image to show its location.
Answer:
Underlying code
import
cv2
import
numpy
as
np
import
matplotlib.pyplot
as
plt
# Load the input image and the template
input_image = cv2.imread(
'nadia1.jpg'
, cv2.IMREAD_GRAYSCALE)
template = cv2.imread(
'nadiatemplate.jpg'
, cv2.IMREAD_GRAYSCALE)
# Apply template matching
result = cv2.matchTemplate(input_image, template, cv2.TM_CCOEFF_NORMED)
# Get the position of the maximum value in the result array
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
# Define the width and height of the template for drawing the rectangle
w, h = template.shape[::-
1
]
# Draw a white rectangle around the matched area on a copy of the input image
matched_image = input_image.copy()
top_left = max_loc
bottom_right = (top_left[
0
] + w, top_left[
1
] + h)
cv2.rectangle(matched_image, top_left, bottom_right,
255
,
2
)
# Calculate the error map for the matched region
matched_region = input_image[top_left[
1
]:bottom_right[
1
], top_left[
0
]:bottom_right[
0
]]
error_map = cv2.absdiff(matched_region, template)
from
A4_Q2
import
call_function
# Display the images using Matplotlib
plt.figure(
figsize
=(
12
,
4
))
# Matching Result
plt.subplot(
131
)
plt.imshow(matched_image,
cmap
=
'gray'
)
plt.title(
'Matching Result'
)
plt.axis(
'off'
)
# Template
plt.subplot(
132
)
plt.imshow(template,
cmap
=
'gray'
)
plt.title(
'Template'
)
plt.axis(
'off'
)
# Darker Blurred Image (Error Map)
plt.subplot(
133
)
error_map = call_function(input_image)
plt.imshow(error_map,
cmap
=
'gray'
)
plt.title(
'Error Map'
)
plt.axis(
'off'
)
plt.tight_layout()
plt.show()
Output
Explanation
This code demonstrates template matching, to find a sub-region (template) in an image. Here's
an explanation of the code:
1.
Load Images
:
o
cv2.imread('nadia1.jpg', cv2.IMREAD_GRAYSCALE)
: Loads the input
image (
nadia1.jpg
) and converts it to grayscale. Grayscale images are easier
to process because they contain intensity information only (no color).
o
cv2.imread('nadiatemplate.jpg', cv2.IMREAD_GRAYSCALE)
: Loads the
template image (
nadiatemplate.jpg
) and converts it to grayscale.
2.
Template Matching
:
o
result
=
cv2.matchTemplate(input_image,
template,
cv2.TM_CCOEFF_NORMED)
: Applies template matching using the normalized
cross-correlation method (
cv2.TM_CCOEFF_NORMED
). This method compares the
template against the input image at various positions and calculates the
correlation coefficient. The result will be a 2D array (
result
) where high values
indicate good matches.
3.
Find Maximum Value and Location
:
o
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
: Finds
the minimum and maximum values and their respective locations in the result
array. In this case, we are interested in the maximum value and its location,
which indicates the best match.
4.
Draw Rectangle Around Matched Area
:
o
Using the maximum location obtained from
cv2.minMaxLoc
, a white rectangle
is drawn around the matched area in a copy of the input image.
5.
Calculate Error Map
:
o
matched_region
=
input_image[top_left[1]:bottom_right[1],
top_left[0]:bottom_right[0]]
: Extracts the region from the input image
that matches the template.
o
error_map = cv2.absdiff(matched_region, template)
: Calculates the
absolute difference between the matched region and the template. This is a
measure of dissimilarity between the template and the matched region.
6.
Display Images
:
o
The results are displayed using Matplotlib in three subplots:
§
The first subplot shows the input image with the white rectangle around
the matched area.
§
The second subplot displays the template used for matching.
§
The third subplot shows the "error map" which highlights the differences
between the matched region and the template.
•
Template Matching
: A technique to find a sub-region (template) in an image.
•
Normalized Cross-Correlation
: A similarity measure used in template matching.
•
Min-Max Location
: Finds the minimum and maximum values and their respective
positions in an array.
•
cv2.rectangle(...)
: Draws a rectangle on an image.
•
cv2.absdiff(...)
: Calculates the absolute difference between two images.
2
.
Use the code from question 1 to write an EBMA in Python. It should take two images and
calculate the motion vectors and then display the vectors using the OpenCV fuction, quiver().
Write your EBMA so that it has an adjustable block size and search region. Use the target and
anchor images provided on our website and produce an output similar to that shown below.
Answer:
Underlying code
import
cv2
import
numpy
as
np
import
matplotlib.pyplot
as
plt
# Define the function to perform EBMA motion estimation
def
EBMA_motion_estimation
(prev_frame, next_frame, block_size, search_region):
height, width = prev_frame.shape[:
2
]
motion_vectors = []
for
y
in
range
(
0
, height, block_size):
for
x
in
range
(
0
, width, block_size):
block_prev = prev_frame[y:y + block_size, x:x + block_size]
# Define search area for this block
search_y_start =
max
(
0
, y - search_region)
search_y_end =
min
(height, y + block_size + search_region)
search_x_start =
max
(
0
, x - search_region)
search_x_end =
min
(width, x + block_size + search_region)
search_area = next_frame[search_y_start:search_y_end, search_x_start:search_x_end]
min_mse =
float
(
'inf'
)
best_mv = (
0
,
0
)
for
dy
in
range
(-search_region, search_region +
1
):
for
dx
in
range
(-search_region, search_region +
1
):
shifted_block = search_area[dy:dy + block_size, dx:dx + block_size]
if
shifted_block.shape == block_prev.shape:
mse = np.mean((block_prev - shifted_block) **
2
)
if
mse < min_mse:
min_mse = mse
best_mv = (dx, dy)
motion_vectors.append(best_mv)
return
motion_vectors
def
call_function
(input_image):
error_map = cv2.GaussianBlur(input_image, (
21
,
21
),
20
)
return
error_map
if
__name__ ==
"__main__"
:
# Load the target and anchor images
target_image = cv2.imread(
'nadia2.jpg'
, cv2.IMREAD_GRAYSCALE)
anchor_image = cv2.imread(
'nadia3.jpg'
, cv2.IMREAD_GRAYSCALE)
# Define block size and search region
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
Related Questions
Motion History Image (MHI) technique uses a frame differencing inside to generate M(t). In its original form SFD is used. Instead of SFD, we can use
a) ABS,
b) TFD.
Write pseudo-code for each case separately.
arrow_forward
MATLAB plss help !!!
arrow_forward
Problem 3:
The histograms of two images are illustrated below. Sketch a transformation function for each
image that will make the image have a better contrast. Use the axis provided below to sketch your
transformation functions.
1/128
1209
64
192
255
100 155 255
arrow_forward
Define frame pointer.
arrow_forward
Computer Science
Let C be a black circular disk in front of a white background. The circular disk is parallel to the image plane. This disk is projected on the image plane through a pinhole. What is the shape of the disk’s projection on the image plane? [Hint: A circular disk parallel to the image plane is described algebraically as all 3D points [X,Y,Z]T with (X-X0) 2 +(Y-Y0) 2 =R2 , Z=Z0, where [X0,Y0,Z0] T is the center of the disk and R is its radius.]
arrow_forward
Given a 2D point (x, y) in screen space with an associated z-buffer value (zbuf), write out psuedocode for determining whether or not to draw that point.
arrow_forward
What role does the reflection vector play in computer graphics? The following should have at least two examples of each.
arrow_forward
In terms of computer graphics, what is the function of the reflection vector?
Minimum of two samples should be provided.
arrow_forward
Computer Science
Write a C++ program that uses OpenGL to render the content of an OBJ file. You mayignore the content of the file other than these entries: v..........Vertex specifications vt ........Texture coordinates vn........Normal vectors f ..........Faces
arrow_forward
Laplacian Image Pyramid
A Laplacian image pyramid is a multi-scale representation of a single image that highlights intensity discontinuities at multiple scales. It is
obtained by convolving an image with a Gaussian mask and subtracting the smoothed image from the original one. The next level of the pyramid
is obtain by repeating this process on the smoothed image after it is down-sampled.
Create a 3 level Laplacian image pyramid of the woods image (after conversion from uint8 to double format), using a Gaussian with a standard
deviation of 1.5, using "same" as the shape parameter for the conv2 function, and resizing by a scale factor of 0.5 using nearest-neighbour
interpolation. Display the images in the pyramid as subplots in the same window.
EXERCISE:
For the last level of the pyramid report the values at the following locations (correct to at least 2 decimal places):
row=3, column=5
row=1, column=2
arrow_forward
Laplacian Image Pyramid
A Laplacian image pyramid is a multi-scale representation of a single image that highlights intensity discontinuities at multiple scales. It is obtained by convolving an image with a Gaussian
mask and subtracting the smoothed image from the original one. The next level of the pyramid is obtain by repeating this process on the smoothed image after it is down-sampled.
Create a 3 level Laplacian image pyramid of the woods image (after conversion from uint8 to double format), using a Gaussian with a standard deviation of 1.5, using "same" as the shape
parameter for the conv2 function, and resizing by a scale factor of 0.5 using nearest-neighbour interpolation. Display the images in the pyramid as subplots in the same window.
arrow_forward
When it comes to computer graphics, what function does the reflection vector play? Using at least two examples, demonstrate that your assertion is correct.
arrow_forward
Wirte a matlab code to plot the 3D image of a helix: Use the following parameters and equations that define the axes in 3D: p=0.199; Pitch distance (distance between the turns)a=0.02999500; Radius of the helis wireb=0.191; Radius of the helixn = 5, is the number of helis turns. δ = atan(p/(2*pi*b)); The pitch anglex’ = b + a cos(Ө)y’ = -a sin(Ө) sin(δ)x = x’ sin(ф)+y’ cos(ф);y =-x’ cos(ф)+y’ sin(ф);z = p*ф/(2*pi)+a*sin(Ө)*cos(δ);
arrow_forward
Lighting Calculations
Let p be a point in the OCS, n be the point's surface normal in the OCS, lwcs be the light direction in the WCS, and ewes be the eye position in the WCS. All four vectors are 4D
homogeneous vectors with 0 or 1 in the last position, as appropriate. Let M be the object-to-world transform, V be the world-to-view transform, and P be the view-to-CCS transform.
Which sets of transformed point, normal, light direction, and eye position below can be used together to compute the Phong illumination at the point? Select all that apply.
• M
Pocs,
• M Pocs
VM Pocs
●
●
M nocs,
M nocs,
VM nocs,
PVM Pocs
lwcs, ewcs
Mlwes, Mewcs
Vlwes (ewes is not available)
PVlwes, PVewes (all followed by division by the fourth coordinate)
PVM nocs
arrow_forward
using matlab programming
arrow_forward
Please write python codes to smooth an image by using median filter and Bilateral filter separately.
You should give your codes, the input image and its outputs.
arrow_forward
How does vector imaging operate? What is it? Give an illustration to illustrate your argument. What file formats are associated with vector images?
arrow_forward
example python code for generating dot plots for two DNA sequence (in a text file, where the first line is the wild type DNA seq, and the second line is mutated).
arrow_forward
Molecular Dynamics
Write a computational code (Python 3) to calculate the minimum image of a system of N particles with periodic boundary conditions
arrow_forward
When it comes to computer graphics, what function does the reflection vector serve? Provide at least two different examples to demonstrate that your assertion is correct.
arrow_forward
Computer Science
Write a program to find the image gradients of any gray scale image of your choice in python only.
• Display the original, the gradient in x direction, the gradient in y direction, and the gradient magnitude images.
• Apply a simple threshold to the gradient magnitude image and display the gradient magnitude and the thresholding image. You may use 100 as your threshold value or any other appropriate value.
arrow_forward
ON MATLAB PLS
arrow_forward
Task 4: Write a Matlab code to do the following:
1. Read a "cameraman.tif image
2. Make the image smaller to [50, 50] and call it
small_img
3. Enlarge the output image from step 2 (small_img) into
[400, 400] using nearest neighbor interpolation.
4. Enlarge the output image from step 2 (small_img) into
[400, 400] using Bilinear interpolation.
arrow_forward
How does vector imaging operate, and what is it? Please illustrate your idea with an example. What do vector image filename extensions look like?
arrow_forward
Help with cg mcq
The digitization process i.e. the digital image has M rows and N columns, requires decisions about values for M, N, and for the number, L, of gray levels allowed for each pixel. The value M and N have to be:
a) M and N have to be positive integer
b) M and N have to be negative integer
c) M have to be negative and N have to be positive integer
d) M have to be positive and N have to be negative integer
arrow_forward
SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Related Questions
- Motion History Image (MHI) technique uses a frame differencing inside to generate M(t). In its original form SFD is used. Instead of SFD, we can use a) ABS, b) TFD. Write pseudo-code for each case separately.arrow_forwardMATLAB plss help !!!arrow_forwardProblem 3: The histograms of two images are illustrated below. Sketch a transformation function for each image that will make the image have a better contrast. Use the axis provided below to sketch your transformation functions. 1/128 1209 64 192 255 100 155 255arrow_forward
- Define frame pointer.arrow_forwardComputer Science Let C be a black circular disk in front of a white background. The circular disk is parallel to the image plane. This disk is projected on the image plane through a pinhole. What is the shape of the disk’s projection on the image plane? [Hint: A circular disk parallel to the image plane is described algebraically as all 3D points [X,Y,Z]T with (X-X0) 2 +(Y-Y0) 2 =R2 , Z=Z0, where [X0,Y0,Z0] T is the center of the disk and R is its radius.]arrow_forwardGiven a 2D point (x, y) in screen space with an associated z-buffer value (zbuf), write out psuedocode for determining whether or not to draw that point.arrow_forward
- What role does the reflection vector play in computer graphics? The following should have at least two examples of each.arrow_forwardIn terms of computer graphics, what is the function of the reflection vector? Minimum of two samples should be provided.arrow_forwardComputer Science Write a C++ program that uses OpenGL to render the content of an OBJ file. You mayignore the content of the file other than these entries: v..........Vertex specifications vt ........Texture coordinates vn........Normal vectors f ..........Facesarrow_forward
- Laplacian Image Pyramid A Laplacian image pyramid is a multi-scale representation of a single image that highlights intensity discontinuities at multiple scales. It is obtained by convolving an image with a Gaussian mask and subtracting the smoothed image from the original one. The next level of the pyramid is obtain by repeating this process on the smoothed image after it is down-sampled. Create a 3 level Laplacian image pyramid of the woods image (after conversion from uint8 to double format), using a Gaussian with a standard deviation of 1.5, using "same" as the shape parameter for the conv2 function, and resizing by a scale factor of 0.5 using nearest-neighbour interpolation. Display the images in the pyramid as subplots in the same window. EXERCISE: For the last level of the pyramid report the values at the following locations (correct to at least 2 decimal places): row=3, column=5 row=1, column=2arrow_forwardLaplacian Image Pyramid A Laplacian image pyramid is a multi-scale representation of a single image that highlights intensity discontinuities at multiple scales. It is obtained by convolving an image with a Gaussian mask and subtracting the smoothed image from the original one. The next level of the pyramid is obtain by repeating this process on the smoothed image after it is down-sampled. Create a 3 level Laplacian image pyramid of the woods image (after conversion from uint8 to double format), using a Gaussian with a standard deviation of 1.5, using "same" as the shape parameter for the conv2 function, and resizing by a scale factor of 0.5 using nearest-neighbour interpolation. Display the images in the pyramid as subplots in the same window.arrow_forwardWhen it comes to computer graphics, what function does the reflection vector play? Using at least two examples, demonstrate that your assertion is correct.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education