运动估计算法实现.docx
- 文档编号:4882385
- 上传时间:2022-12-11
- 格式:DOCX
- 页数:14
- 大小:222KB
运动估计算法实现.docx
《运动估计算法实现.docx》由会员分享,可在线阅读,更多相关《运动估计算法实现.docx(14页珍藏版)》请在冰豆网上搜索。
运动估计算法实现
运动估计算法实现
一、实验目的:
1、掌握运动估计算法的实现原理。
在视频编码和处理系统中,运动估计和运动补偿技术对降低视频序列时间冗余度、提高编码效率起着非常关键的作用。
运动估计的准确程度将直接决定视频编码器的编码效率。
它极大地消除了视频序列的帧间相关性。
运动估计算法的复杂性将直接决定视频压缩编码系统的复杂性,如何提高运动估计的效率,使运动估计算法的搜索过程更快速、更高效一直是人们研究的热点。
掌握运动估计的块匹配算法,以及快速运动估计算法。
2、掌握运动估计算法的研究现状及多种计算方法。
3、学习基于块的全搜索运动估计算法,研究分析其Matlab实现程序过程,并补充完成程序,对实验结果进行分析比较。
二、实验内容
1、分析基于块匹配的全搜索运动估计算法程序,画出motionEstAnalysis.m文件流程图
2、编程补充完成costFuncMAD.m文件中最小绝对误差计算函数costFuncMAD()和imgPSNR.m文件中峰值信噪比PSNR计算函数imgPSNR()的程序,最终输出运动矢量场;
3、掌握运动补偿算法,编程补充minCost()函数;
4、了解多种快速运动估计算法,例如三步法搜索法、菱形搜索法等。
总结实验结果。
三、实验原理
在帧间预测编码中,由于活动图像邻近帧中的景物存在着一定的相关性。
因此,可将活动图像分成若干块或宏块,并设法搜索出每个块或宏块在邻近帧图像中的位置,并得出两者之间的空间位置的相对偏移量,得到的相对偏移量就是通常所指的运动矢量,得到运动矢量的过程被称为运动估计。
运动矢量和经过运动匹配后得到的预测误差共同发送到解码端,在解码端按照运动矢量指明的位置,从已经解码的邻近参考帧图像中找到相应的块或宏块,和预测误差相加后就得到了块或宏块在当前帧中的位置。
运动估计的准确程度往往用补偿图像与原图像比较的PSNR来衡量表示。
四、实验要求
1、对实验程序motionEstAnalysis.m进行分析,完成主程序流程图。
函数流程图:
2、编写补充完成部分不全程序代码,调试程序使其能正确运行
(1)motionEstAnalysis.m
%ThisscriptusesalltheMotionEstimationalgorithmswrittenforthe
%finalprojectandsavetheirresults.
closeall
clearall
%imageName='caltrain.avi';
mbSize=4;
p=7;
imgP1=double(imread('imagP.bmp'));
imgI1=double(imread('imagI.bmp'));
imgP=imgP1(:
:
1);
imgI=imgI1(:
:
1);
[rowcol]=size(imgI);
%ExhaustiveSearch
%»ùÓÚ¿éµÄÈ«ËÑË÷Ëã·¨
[BlockCenter,motionVect,computations]=motionEstES(imgP,imgI,mbSize,p);
%PÖ¡µ±Ç°Öع¹Í¼Ïñ
imgPComp=motionComp(imgI,motionVect,mbSize);
%PÖ¡µ±Ç°Í¼ÏñºÍPÖ¡µ±Ç°Öع¹Í¼ÏñµÄPSNRÖµ
%ESpsnr(i+1)=imgPSNR(imgP,imgPComp,255);
n=255;
PSNR=imgPSNR(imgP,imgPComp,255)
%MSE=(1/(n*n))*sum(sum((imgP-imgPComp).^2));
%PSNR=10*log(max(max(imgPComp)).^2/MSE);
%psnr=PSNR;
%EScomputations(i+1)=computations;
%PÖ¡µ±Ç°Öع¹Îó²îͼÏñ
imagePDiff=imgP-imgPComp;
figure;
imageI=uint8(imgI);
imageP=uint8(imgP);
imagePComp=uint8(imgPComp);
imagePDiff=uint8(imagePDiff);
subplot(221);imshow(imageI);
title('IÖ¡²Î¿¼Í¼Ïñ');
subplot(222);imshow(imageP);
title('PÖ¡µ±Ç°Í¼Ïñ');
subplot(223);imshow(imagePComp);
title('PÖ¡µ±Ç°Öع¹Í¼Ïñ');
subplot(224);imshow(imagePDiff);
title('PÖ¡µ±Ç°Öع¹Îó²îͼÏñ');
%»Ô˶¯Ê¸Á¿Í¼
figure;
quiver(BlockCenter(2,:
),BlockCenter(1,:
),motionVect(2,:
),motionVect(1,:
),.2,'r');
axis([03200240]);
fori=mbSize:
mbSize:
col-mbSize
x=[i,i];
y=[0,row];
line(x,y,'LineStyle','-','Marker','none');
end
forj=mbSize:
mbSize:
row-mbSize
x=[0,col];
y=[j,j];
line(x,y,'LineStyle','-','Marker','none');
end
xlabel('X');
ylabel('Y');
(2)motionEstES()
%Computesmotionvectorsusingexhaustivesearchmethod(全搜索法计算运动矢量)
%
%Input
%imgP:
Theimageforwhichwewanttofindmotionvectors(当前图像)
%imgI:
Thereferenceimage(参考图像)
%mbSize:
Sizeofthemacroblock(宏块尺寸)
%p:
Searchparameter(readliteraturetofindwhatthismeans)(搜索参数)
%
%Ouput
%motionVect:
themotionvectorsforeachintegralmacroblockinimgP(当前图像中每一个积分宏块的运动矢量)
%EScomputations:
Theaveragenumberofpointssearchedforamacroblock(每个宏块搜索的平均点数)
%
%WrittenbyArohBarjatya
function[BlockCenter,motionVect,EScomputations]=motionEstES(imgP,imgI,mbSize,p)%定义函数文件motionEstES.m,imgP、imgI、mbSize、p为传入参数,BlockCenter、motionVect、EScomputations为返回参数
[rowcol]=size(imgI);%将参考图像的行数赋值给row,列数赋值给col
blockcenter=zeros(2,row*col/mbSize^2);
vectors=zeros(2,row*col/mbSize^2);%定义全0的矢量矩阵的大小
costs=ones(2*p+1,2*p+1)*65537;%定义最小绝对差矩阵的大小
computations=0;%搜索点数赋初值为0
%westartofffromthetopleftoftheimage(从图像左上角开始)
%wewillwalkinstepsofmbSize(以宏块尺寸为步长)
%foreverymarcoblockthatwelookatwewilllookfor
%aclosematchppixelsontheleft,right,topandbottomofit(对于每一个宏块,在它的上下左右找到与搜索参数p最匹配的像素)
mbCount=1;%搜索的宏块数赋初值为1
%1为循环起始值,mbSize为步长值,row-mbSize+1为循环终止值
fori=1:
mbSize:
row-mbSize+1
forj=1:
mbSize:
col-mbSize+1
%theexhaustivesearchstartshere(全搜索开始)
%wewillevaluatecostfor(2p+1)blocksvertically
%and(2p+1)blockshorizontaly(我们将计算水平方向上(2p+1)个块的最小绝对差和垂直方向上(2p+1)个块的最小绝对差)
%misrow(vertical)index(m为行指数)
%niscol(horizontal)index(n为列指数)
%thismeanswearescanninginrasterorder
form=-p:
p%水平方向上位移矢量范围
forn=-p:
p%垂直方向上位移矢量范围
%
%row/Vertco-ordinateforrefblock(参考块的行(垂直方向)的范围)
refBlkVer=i+m;
%col/Horizontalco-ordinate(参考块的列(水平方向)的范围)
refBlkHor=j+n;
%如果参考块的行列范围的任意一个在已经搜索过的宏块之外,则继续下一步的搜索
if(refBlkVer<1||refBlkVer+mbSize-1>row...
||refBlkHor<1||refBlkHor+mbSize-1>col)
continue;
end
costs(m+p+1,n+p+1)=costFuncMAD(imgP(i:
i+mbSize-1,j:
j+mbSize-1),...
imgI(refBlkVer:
refBlkVer+mbSize-1,refBlkHor:
refBlkHor+mbSize-1),mbSize);
%搜索下一个点
computations=computations+1;
end
end
%Nowwefindthevectorwherethecostisminimum
%andstoreit...thisiswhatwillbepassedback.(现在找到有最小绝对差的矢量并存储它,这就是将被返回的东西)
%
blockcenter(1,mbCount)=i+mbSize/2-1;
blockcenter(2,mbCount)=j+mbSize/2-1;
%findswhichmacroblockinimgIgaveusminCost(找到参考图像中最小绝对差的宏块)
[dx,dy,min]=minCost(costs);
%rowco-ordinateforthevector(矢量的行集合)
vectors(1,mbCount)=dy-p-1;
%colco-ordinateforthevector(矢量的列集合)
vectors(2,mbCount)=dx-p-1;
%搜索下一个宏块
mbCount=mbCount+1;
costs=ones(2*p+1,2*p+1)*65537;
end
end
BlockCenter=blockcenter;
motionVect=vectors;%返回当前图像中每一个积分宏块的运动矢量
EScomputations=computations/(mbCount-1);%返回每个宏块搜索的平均点数
(3)costFuncMAD()
%ComputestheMeanAbsoluteDifference(MAD)forthegiventwoblocks(对给定的两个块计算最小绝对差)
%Input
%currentBlk:
TheblockforwhichwearefindingtheMAD(当前块)
%refBlk:
theblockw.r.t.whichtheMADisbeingcomputed(参考块)
%n:
thesideofthetwosquareblocks
%
%Output
%cost:
TheMADforthetwoblocks(两个块的最小绝对差)
%
%WrittenbyArohBarjatya
%定义函数文件costFuncMAD.m,currentBlk、refBlk、n为传入参数,cost为返回参数
functioncost=costFuncMAD(currentBlk,refBlk,n)
%补充下面程序
cost=sum(sum(abs(currentBlk-refBlk)))/(n*n);
(4)minCost()
%Findstheindicesofthecellthatholdstheminimumcost(找到拥有最小绝对差的点的指数)
%
%Input
%costs:
Thematrixthatcontainstheestimationcostsforamacroblock(包含宏块的估计代价的矩阵)
%
%Output
%dx:
themotionvectorcomponentincolumns(列方向上运动矢量组成)
%dy:
themotionvectorcomponentinrows(行方向上运动矢量组成)
%
%WrittenbyArohBarjatya
function[dx,dy,min]=minCost(costs)
[row,col]=size(costs);
%wecheckwhetherthecurrentvalueofcostsislessthenthealreadypresentvalueinmin.
%Ifitsindedsmallerthenweswaptheminvaluewiththecurrentoneandnotetheindices.
%(检测costs的当前值是否比已经出现的最小值小。
如果小的话,我们将当前值与最小值对调,并注明指数)
%补充下面程序
minnum=65536;
x=8;
y=8;
fori=1:
row
forj=1:
col
if(costs(i,j) minnum=costs(i,j); x=i; y=j; end end end dx=x; dy=y; min=minnum; (5)motionComp.m %Computesmotioncompensatedimageusingthegivenmotionvectors(用给定的运动矢量计算运动补偿图像) %Input %imgI: Thereferenceimage(参考图像) %motionVect: Themotionvectors(运动矢量) %mbSize: Sizeofthemacroblock(宏块大小) % %Ouput %imgComp: Themotioncompensatedimage(运动补偿图像) %WrittenbyArohBarjatya functionimgComp=motionComp(imgI,motionVect,mbSize) [rowcol]=size(imgI); %westartofffromthetopleftoftheimage(从图像左上角开始) %wewillwalkinstepsofmbSize(以宏块大小为步长) %foreverymarcoblockthatwelookatwewillreadthemotionvector(对于看到的每一个宏块,读出它的运动矢量) %andputthatmacroblockfromrefernceimageinthecompensatedimage(并将参考图像中的该宏块放到补偿图像中) mbCount=1; fori=1: mbSize: row-mbSize+1 forj=1: mbSize: col-mbSize+1 %dyisrow(vertical)index(dy为垂直方向上的指数) %dxiscol(horizontal)index(dx为水平方向上的指数) %thismeanswearescanninginorder dy=motionVect(1,mbCount); dx=motionVect(2,mbCount); refBlkVer=i+dy; refBlkHor=j+dx; if(refBlkVer<1||refBlkVer+mbSize-1>row... ||refBlkHor<1||refBlkHor+mbSize-1>col) imageComp(i: i+mbSize-1,j: j+mbSize-1)=imgI(i: i+mbSize-1,j: j+mbSize-1); continue; end imageComp(i: i+mbSize-1,j: j+mbSize-1)=imgI(refBlkVer: refBlkVer+mbSize-1,refBlkHor: refBlkHor+mbSize-1);mbCount=mbCount+1; end end imgComp=imageComp; (6)imgPSNR() %Computesmotioncompensatedimage'sPSNR(计算运动补偿图像的峰值信噪比) % %Input %imgP: Theoriginalimage(原始图像) %imgComp: Thecompensatedimage(补偿图像) %n: thepeakvaluepossibleofanypixelintheimages(图像中任何一个像素的可能的峰值) % %Ouput %psnr: Themotioncompensatedimage'sPSNR(运动补偿图像的峰值信噪比) % %WrittenbyArohBarjatya functionpsnr=imgPSNR(imgP,imgComp,n) %补充下面程序 MSE=(1/(n*n))*sum(sum((imgP-imgComp).^2)); PSNR=10*log10(255^2/MSE); psnr=PSNR; 五、实验结果与分析
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 运动 估计 算法 实现