📄 lucaspersoblocs.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"><html xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <!--This HTML is auto-generated from an M-file.To make changes, update the M-file and republish this document. --> <title>LucasPersoBlocs</title> <meta name="generator" content="MATLAB 7.4"> <meta name="date" content="2008-05-30"> <meta name="m-file" content="LucasPersoBlocs"><style>body { background-color: white; margin:10px;}h1 { color: #990000; font-size: x-large;}h2 { color: #990000; font-size: medium;}/* Make the text shrink to fit narrow windows, but not stretch too far in wide windows. */ p,h1,h2,div.content div { max-width: 600px; /* Hack for IE6 */ width: auto !important; width: 600px;}pre.codeinput { background: #EEEEEE; padding: 10px;}@media print { pre.codeinput {word-wrap:break-word; width:100%;}} span.keyword {color: #0000FF}span.comment {color: #228B22}span.string {color: #A020F0}span.untermstring {color: #B20000}span.syscmd {color: #B28C00}pre.codeoutput { color: #666666; padding: 10px;}pre.error { color: red;}p.footer { text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;} </style></head> <body> <div class="content"> <h2>Contents</h2> <div> <ul> <li><a href="#2">Algorithme de Lucas & Kanade : découpage de l'image en blocs</a></li> <li><a href="#3">Chargement des images</a></li> <li><a href="#4">Filtrage</a></li> <li><a href="#5">Algorithme de Lucas & Kanade</a></li> <li><a href="#6">Résultat</a></li> </ul> </div><pre class="codeinput"><span class="comment">% Gaetan Boehringer, Bruno Jacquot & Frédéric Piegay</span></pre><h2>Algorithme de Lucas & Kanade : découpage de l'image en blocs<a name="2"></a></h2> <h2>Chargement des images<a name="3"></a></h2><pre class="codeinput">clear <span class="string">all</span>; close <span class="string">all</span>; clc;<span class="comment">% Lecture</span>im1o=double(imread(<span class="string">'LKtest1im1.bmp'</span>));im2o=double(imread(<span class="string">'LKtest1im2.bmp'</span>));[Lim,Cim]=size(im1o);<span class="comment">% Affichage</span>figure;subplot(1,2,1); imagesc(im1o);axis <span class="string">equal</span>; colormap(gray);title(<span class="string">'Image initiale'</span>);subplot(1,2,2); imagesc(im2o);axis <span class="string">equal</span>; colormap(gray); title (<span class="string">'Image décalée'</span>);</pre><img vspace="5" hspace="5" src="LucasPersoBlocs_01.png"> <h2>Filtrage<a name="4"></a></h2><pre class="codeinput">h = fspecial(<span class="string">'gaussian'</span>,10,3);im1 = imfilter(im1o,h);im2 = imfilter(im2o,h);<span class="comment">% Affichage des images filtrées</span>figure;subplot(1,2,1); imagesc(im1);axis <span class="string">equal</span>; colormap(gray);title(<span class="string">'Image initiale filtrée'</span>);subplot(1,2,2); imagesc(im2);axis <span class="string">equal</span>; colormap(gray);title(<span class="string">'Image décalée filtrée'</span>);</pre><img vspace="5" hspace="5" src="LucasPersoBlocs_02.png"> <h2>Algorithme de Lucas & Kanade<a name="5"></a></h2><pre class="codeinput"><span class="comment">% Gradients</span>f=[-1,8,0,-8,1]/12;im1fx=filter2(f,im1,<span class="string">'same'</span>); <span class="comment">% Gradient selon x</span>im1fy=filter2(f',im1,<span class="string">'same'</span>); <span class="comment">% Gradient selon y</span>imt=im2-im1; <span class="comment">% Gradient temporel</span><span class="comment">% Affichage des gradients</span>figure;subplot(1,3,1); imagesc(im1fx);axis <span class="string">equal</span>; colormap(gray);title (<span class="string">'Gradient selon x'</span>);subplot(1,3,2); imagesc(im1fy);axis <span class="string">equal</span>; colormap(gray);title (<span class="string">'Gradient selon y'</span>);subplot(1,3,3); imagesc(imt);axis <span class="string">equal</span>; colormap(gray);title (<span class="string">'Gradient temporel'</span>);<span class="comment">% Gestion des bords : suppression du bord (épaisseur 10 pixels)</span>bord=10;Ix1=im1fx(bord:Lim-bord,bord:Lim-bord);Iy1=im1fy(bord:Lim-bord,bord:Lim-bord);It=imt(bord:Lim-bord,bord:Lim-bord);<span class="comment">% Transfert vecteurs colonnes</span>Ix_y=[Ix1(:),Iy1(:)];It=It(:);<span class="comment">% Calcul de u et v</span>Ix_yINV=pinv(Ix_y); <span class="comment">% On inverse la matrice</span>fprintf(<span class="string">'Lucas Kanade : le déplacement moyen est de (pixels) :\n'</span>)-Ix_yINV*It <span class="comment">% Calcul des coordonnées du vecteur deplacement</span>moduloX=mod(Cim,5);<span class="comment">% Nombre de blocs de taille (5) selon x</span>moduloY=mod(Lim,5);<span class="comment">% Nombre de blocs de taille (5) selon y</span>k=1;<span class="comment">% variable d'itération</span><span class="keyword">for</span> i=1:5:Lim-moduloY-5 <span class="keyword">for</span> j=1:5:Cim-moduloX-5 GradBlocX=im1fx(i:i+4,j:j+4); <span class="comment">% Bloc de taille (5*5) du gradient selon x</span> GradBlocY=im1fy(i:i+4,j:j+4); <span class="comment">% Bloc de taille (5*5) du gradient selon y</span> GradBlocTemp=im2(i:i+4,j:j+4)-im1(i:i+4,j:j+4); <span class="comment">% Bloc de taille (5*5) du gradient temporel</span> GradBlocX_Y=[GradBlocX(:),GradBlocY(:)]; GradBlocTemp=GradBlocTemp(:);GradBlocX_YINV=pinv(GradBlocX_Y);res=-GradBlocX_YINV*GradBlocTemp; <span class="comment">% Vecteur déplacement de coordonnées u et v</span>RES(:,k)=res; <span class="comment">% Tableau où on regroupe les coordonnées des vecteurs déplacement de chaque bloc</span>k=k+1; <span class="keyword">end</span><span class="keyword">end</span>[X,Y]=meshgrid(1:5:Cim-moduloX-5,1:5:Lim-moduloY-5);[x1,y1]=size(X);[x2,y2]=size(Y);vectX=reshape(RES(1,:),x1,y1);vectY=reshape(RES(2,:),x2,y2);</pre><pre class="codeoutput">Lucas Kanade : le déplacement moyen est de (pixels) :ans = 0.0010 -0.8278</pre><img vspace="5" hspace="5" src="LucasPersoBlocs_03.png"> <h2>Résultat<a name="6"></a></h2><pre class="codeinput"><span class="comment">% Affichage des vecteurs déplacement</span>figurequiver(X,Y,vectX,vectY)colormap <span class="string">hsv</span><span class="comment">% On peut observer que l 'algorithme de Lucas & Kanade est particulièrement</span><span class="comment">% bien adapté à ce type d'application (faible déplacement).</span></pre><img vspace="5" hspace="5" src="LucasPersoBlocs_04.png"> <p class="footer"><br> Published with MATLAB® 7.4<br></p> </div> <!--##### SOURCE BEGIN #####% Gaetan Boehringer, Bruno Jacquot & Frédéric Piegay
%% Algorithme de Lucas & Kanade : découpage de l'image en blocs
%% Chargement des images
clear all; close all; clc;
% Lecture
im1o=double(imread('LKtest1im1.bmp'));
im2o=double(imread('LKtest1im2.bmp'));
[Lim,Cim]=size(im1o);
% Affichage
figure;
subplot(1,2,1); imagesc(im1o);axis equal; colormap(gray);title('Image initiale');
subplot(1,2,2); imagesc(im2o);axis equal; colormap(gray); title ('Image décalée');
%% Filtrage
h = fspecial('gaussian',10,3);
im1 = imfilter(im1o,h);
im2 = imfilter(im2o,h);
% Affichage des images filtrées
figure;
subplot(1,2,1); imagesc(im1);axis equal; colormap(gray);title('Image initiale filtrée');
subplot(1,2,2); imagesc(im2);axis equal; colormap(gray);title('Image décalée filtrée');
%% Algorithme de Lucas & Kanade
% Gradients
f=[-1,8,0,-8,1]/12;
im1fx=filter2(f,im1,'same'); % Gradient selon x
im1fy=filter2(f',im1,'same'); % Gradient selon y
imt=im2-im1; % Gradient temporel
% Affichage des gradients
figure;
subplot(1,3,1); imagesc(im1fx);axis equal; colormap(gray);title ('Gradient selon x');
subplot(1,3,2); imagesc(im1fy);axis equal; colormap(gray);title ('Gradient selon y');
subplot(1,3,3); imagesc(imt);axis equal; colormap(gray);title ('Gradient temporel');
% Gestion des bords : suppression du bord (épaisseur 10 pixels)
bord=10;
Ix1=im1fx(bord:Lim-bord,bord:Lim-bord);
Iy1=im1fy(bord:Lim-bord,bord:Lim-bord);
It=imt(bord:Lim-bord,bord:Lim-bord);
% Transfert vecteurs colonnes
Ix_y=[Ix1(:),Iy1(:)];
It=It(:);
% Calcul de u et v
Ix_yINV=pinv(Ix_y); % On inverse la matrice
fprintf('Lucas Kanade : le déplacement moyen est de (pixels) :\n')
-Ix_yINV*It % Calcul des coordonnées du vecteur deplacement
moduloX=mod(Cim,5);% Nombre de blocs de taille (5) selon x
moduloY=mod(Lim,5);% Nombre de blocs de taille (5) selon y
k=1;% variable d'itération
for i=1:5:Lim-moduloY-5
for j=1:5:Cim-moduloX-5
GradBlocX=im1fx(i:i+4,j:j+4); % Bloc de taille (5*5) du gradient selon x
GradBlocY=im1fy(i:i+4,j:j+4); % Bloc de taille (5*5) du gradient selon y
GradBlocTemp=im2(i:i+4,j:j+4)-im1(i:i+4,j:j+4); % Bloc de taille (5*5) du gradient temporel
GradBlocX_Y=[GradBlocX(:),GradBlocY(:)];
GradBlocTemp=GradBlocTemp(:);
GradBlocX_YINV=pinv(GradBlocX_Y);
res=-GradBlocX_YINV*GradBlocTemp; % Vecteur déplacement de coordonnées u et v
RES(:,k)=res; % Tableau où on regroupe les coordonnées des vecteurs déplacement de chaque bloc
k=k+1;
end
end
[X,Y]=meshgrid(1:5:Cim-moduloX-5,1:5:Lim-moduloY-5);
[x1,y1]=size(X);
[x2,y2]=size(Y);
vectX=reshape(RES(1,:),x1,y1);
vectY=reshape(RES(2,:),x2,y2);
%% Résultat
% Affichage des vecteurs déplacement
figure
quiver(X,Y,vectX,vectY)
colormap hsv
% On peut observer que l 'algorithme de Lucas & Kanade est particulièrement
% bien adapté à ce type d'application (faible déplacement).##### SOURCE END #####--> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -