Digital Video Processing Asg 4

.pdf

School

Arizona State University *

*We aren’t endorsed by this school

Course

509

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

10

Uploaded by SuperHumanOstrich3671

Report
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