Learn how scale and rotation impact template matching in computer vision and discover techniques to handle these variations for robust object detection.
Template matching is a straightforward method for object detection in images, but its reliance on exact pixel matching makes it sensitive to changes in object orientation and size. This article presents a technique to enhance template matching in OpenCV, enabling it to handle object rotations and scale variations.
Template matching in OpenCV is a simple way to find an object in an image, but it doesn't handle rotation or scale changes. Here's how to improve it:
1. Generate Rotated and Scaled Templates:
import cv2
template = cv2.imread("template.png", 0)
for angle in range(0, 360, 10): # Rotate every 10 degrees
for scale in range(5, 15, 2): # Scale from 0.5x to 1.5x
# Rotate the template
rotation_matrix = cv2.getRotationMatrix2D((template.shape[1]/2, template.shape[0]/2), angle, scale/10)
rotated_template = cv2.warpAffine(template, rotation_matrix, (template.shape[1], template.shape[0]))
# Save or use the rotated and scaled template
# ...2. Perform Template Matching for Each Variation:
for rotated_template in rotated_templates:
result = cv2.matchTemplate(image, rotated_template, cv2.TM_CCOEFF_NORMED)
# ... (Find best match and store results)3. Filter Redundant Matches:
# Sort matches by confidence score
matches = sorted(matches, key=lambda x: x[1], reverse=True)
# Keep only the best match for each distinct location
filtered_matches = []
for match in matches:
# Check if match overlaps with existing matches
if not any(overlapping_rectangles(match[0], existing_match[0]) for existing_match in filtered_matches):
filtered_matches.append(match)Explanation:
matchTemplate function to find each variation within the target image.Note: This approach is computationally expensive. Consider using feature detection and matching techniques for more efficient and robust solutions.
This Python code uses OpenCV to find instances of a template image within a larger image, even if the template is rotated or scaled. It generates rotated and scaled versions of the template, then uses template matching to find the best matches within the target image. The code then filters out overlapping matches and draws rectangles around the remaining matches in the target image. Finally, it displays the image with the highlighted matches.
import cv2
# Load the template and target image
template = cv2.imread("template.png", 0)
image = cv2.imread("image.jpg", 0)
# Store rotated and scaled templates
rotated_templates = []
# Generate rotated and scaled templates
for angle in range(0, 360, 10):
for scale in range(5, 15, 2):
# Rotate the template
rotation_matrix = cv2.getRotationMatrix2D((template.shape[1]/2, template.shape[0]/2), angle, scale/10)
rotated_template = cv2.warpAffine(template, rotation_matrix, (template.shape[1], template.shape[0]))
rotated_templates.append(rotated_template)
# Store matches (x, y, confidence)
matches = []
# Perform template matching for each variation
for rotated_template in rotated_templates:
result = cv2.matchTemplate(image, rotated_template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
matches.append((max_loc, max_val))
# Function to check if rectangles overlap
def overlapping_rectangles(rect1, rect2):
x1, y1, w1, h1 = rect1
x2, y2, w2, h2 = rect2
return not (x1 + w1 < x2 or x1 > x2 + w2 or y1 + h1 < y2 or y1 > y2 + h2)
# Sort matches by confidence score
matches = sorted(matches, key=lambda x: x[1], reverse=True)
# Filter redundant matches
filtered_matches = []
for match in matches:
# Calculate bounding box for the match
top_left = match[0]
bottom_right = (top_left[0] + template.shape[1], top_left[1] + template.shape[0])
match_rect = (*top_left, *bottom_right)
# Check for overlap with existing matches
if not any(overlapping_rectangles(match_rect, existing_match[0]) for existing_match in filtered_matches):
filtered_matches.append((match_rect, match[1]))
# Draw rectangles around the best matches
for match in filtered_matches:
cv2.rectangle(image, match[0][:2], match[0][2:], (0, 0, 255), 2)
# Display the result
cv2.imshow("Matches", image)
cv2.waitKey(0)
cv2.destroyAllWindows()Explanation:
This code provides a more complete implementation, including drawing the bounding boxes of the detected matches. Remember that this approach can be computationally expensive, and more efficient methods might be necessary for real-time applications.
Strengths and Weaknesses:
Strengths:
Weaknesses:
Possible Optimizations:
Reduce Search Space:
Feature-Based Matching:
Alternative Approaches:
Applications:
Important Considerations:
This article presents a method for improving basic template matching in OpenCV to handle object rotation and scale changes.
Here's how it works:
matchTemplate function to find the best match for each rotated and scaled template within the target image.Pros:
Cons:
Recommendation:
While this method offers an improvement over basic template matching, consider using feature detection and matching techniques for more efficient and robust object detection in real-world applications.
This article explored techniques to enhance template matching in OpenCV, enabling it to handle object rotations and scale variations. By generating rotated and scaled versions of the template and performing template matching with each variation, the code can identify instances of the template even when it's not presented in its original orientation or size. The article provides a Python code example demonstrating this approach, including steps to filter out redundant matches and highlight the detected objects in the target image. While this method offers an improvement over basic template matching, it's important to note its computational cost. For more complex scenarios or real-time applications, exploring feature-based detection methods or machine learning-based object detection algorithms is recommended. These alternatives offer greater robustness, efficiency, and the ability to handle a wider range of transformations and variations in objects.
Scale-rotation-skew invariant template matching - OpenCV Q&A ... | Aug 2, 2016 ... A template matching algorithm that is rotation, scale and skew invariant already exists in OpenCV? In industrial robotics applications, those kinds ofĀ ...
Real-time Scale and Rotation Invariant Multiple Template Matching ... | Object detection is an important component in the field of computer vision. Real-time detection of objects of any scale or rotation is a major challenge facing the industry today. In this study, we proposed a real-time size and orientation invariant template matching algorithm and hardware structure. In addition, we proposed image pyramid generation, patch orientation detection, descriptor generation, and descriptor matching methods. First, Scale invariance is achieved by generating a pyramid of nine images from the input image to simultaneously detect objects of different scales. Then, construct a window equal to the size of the template image from the original image to obtain the center point and direction of the window. We achieve rotation invariance by creating and rotating a descriptor based on the orientation of the window. Finally, the object is detected by matching it with the descriptor of the template. The proposed algorithm was implemented in Xilinx Virtex-7 xc7v2000tflg1925-1 FPGA. Throughput was
Multi-Template Matching: matched ROI dimensions - Usage ... | Hi @LThomas, Iām using your Multi-Template-Matching plugin through Fiji (ImageJ 1.53k). I am using a square template (726 x 726 pix) to find a single match in an image (1024 x 1024 pix with a mm scale). The template is bmp format whilst the image is a DICOM image. The command I am using is: run("Template Matching Image", "template=[template.bmp] image=[image] flip_template_vertically flip_template_horizontally rotate=10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,...
Template Matching - Implementation - OpenMV Products - OpenMV ... | Is the below still the caseā¦Was wondering if the template matching can handle rotation or scale changes at all? (Was it improved to use a rolling integral image window?) Template matching using something called an āintegral imageā to work. Template matching works by having the camera look a like 32x32 image to find out of a larger image (e.g. 320x240). That said, template matching doesnāt handle rotation or scale changes at all. It just slides around the image you want to find on the large ...