0% found this document useful (0 votes)
3 views

TemplateMatching

The document provides a step-by-step guide on importing images and videos into Matlab/Python/C++ and performing template matching for image processing. It details the process of extracting a region from an image, using template matching to locate it, and stabilizing a shaky video using similar techniques. The instructions include code snippets for implementation in Matlab, highlighting the use of vision functions for template matching and video processing.

Uploaded by

22151145
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

TemplateMatching

The document provides a step-by-step guide on importing images and videos into Matlab/Python/C++ and performing template matching for image processing. It details the process of extracting a region from an image, using template matching to locate it, and stabilizing a shaky video using similar techniques. The instructions include code snippets for implementation in Matlab, highlighting the use of vision functions for template matching and video processing.

Uploaded by

22151145
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Họ tên:Trương Minh Thành

MSSV:22151145
1. Import an image to Matlab/python/C++
clear all;close all;clc
I = imread('a1.jpg');
figure;imshow(I);

1.1.Extract a region in the image as a template

I = rgb2gray(I);
T = I(310:365, 355:470);
figure; imshow(T); title('template');

1.2.Using template matching method based on the area correlation to find the location

of the template on image


h = vision.TemplateMatcher;
Loc = step(h, I, T);
figure; imshow(I);title('Matching location');
hold on
plot(Loc(1,1),Loc(1,2),'*r','linewidth',5);
Question 2:
2.1.Import an video to Matlab/python/C++
filename = "shaky_car.avi";
hVideoSource = VideoReader(filename);

2.2.Using template matching to stabilize that video


hTM = vision.TemplateMatcher("ROIInputPort", true, ...
"BestMatchNeighborhoodOutputPort", true);
%%
% Create a System object to display the original video and the stabilized
% video.
hVideoOut = vision.VideoPlayer("Name", "Video Stabilization");
hVideoOut.Position(1) = round(0.4*hVideoOut.Position(1));
hVideoOut.Position(2) = round(1.5*(hVideoOut.Position(2)));
hVideoOut.Position(3:4) = [650 350];
%%
% Here we initialize some variables used in the processing loop.
pos.template_orig = [109 100]; % [x y] upper left corner
pos.template_size = [22 18]; % [width height]
pos.search_border = [15 10]; % max horizontal and vertical displacement
pos.template_center = floor((pos.template_size-1)/2);
pos.template_center_pos = (pos.template_orig + pos.template_center - 1);
W = hVideoSource.Width; % Width in pixels
H = hVideoSource.Height; % Height in pixels
BorderCols = [1:pos.search_border(1)+4 W-pos.search_border(1)+4:W];
BorderRows = [1:pos.search_border(2)+4 H-pos.search_border(2)+4:H];
sz = [W, H];
TargetRowIndices = ...
pos.template_orig(2)-1:pos.template_orig(2)+pos.template_size(2)-2;
TargetColIndices = ...
pos.template_orig(1)-1:pos.template_orig(1)+pos.template_size(1)-2;
SearchRegion = pos.template_orig - pos.search_border - 1;
Offset = [0 0];
Target = zeros(18,22);
firstTime = true;
% above to stabilize the input video.
while hasFrame(hVideoSource)
input = im2gray(im2double(readFrame(hVideoSource)));
% Find location of Target in the input video frame
if firstTime
Idx = int32(pos.template_center_pos);
MotionVector = [0 0];
firstTime = false;
else
IdxPrev = Idx;
ROI = [SearchRegion, pos.template_size+2*pos.search_border];
Idx = hTM(input,Target,ROI);
MotionVector = double(Idx-IdxPrev);
end
[Offset, SearchRegion] = updatesearch(sz, MotionVector, ...
SearchRegion, Offset, pos);
% Translate video frame to offset the camera motion
Stabilized = imtranslate(input, Offset, "linear");
Target = Stabilized(TargetRowIndices, TargetColIndices);
% Add black border for display
Stabilized(:, BorderCols) = 0;
Stabilized(BorderRows, :) = 0;
TargetRect = [pos.template_orig-Offset, pos.template_size];
SearchRegionRect = [SearchRegion, pos.template_size + 2*pos.search_border];
% Draw rectangles on input to show target and search region
input = insertShape(input, "rectangle", [TargetRect; SearchRegionRect],...
"ShapeColor", "white");
% Display the offset (displacement) values on the input image
txt = sprintf("(%+05.1f,%+05.1f)", Offset);
input = insertText(input(:,:,1),[191 215],txt,"FontSize",16, ...
"FontColor", "white", "BoxOpacity", 0);
% Display video
hVideoOut([input(:,:,1) Stabilized]);
end

You might also like