www.pudn.com > imageregistration.rar > grad.m, change:2015-04-23,size:1393b


% 计算梯度  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%      
function [Ix, Iy, It] = grad(im1, im2);      
%%%% 定义梯度大小      
sizey = size(im1,1);      
sizex = size(im1,2);      
     
%%%% 求相邻点 %%%%      
im1_right = im1(1:sizey-1, 2:sizex);      
im1_down = im1(2:sizey,1:sizex-1);      
im1_down_right = im1(2:sizey,2:sizex);      
im2_right = im2(1:sizey-1, 2:sizex);      
im2_down = im2(2:sizey,1:sizex-1);      
im2_down_right = im2(2:sizey,2:sizex);      
     
%%%% 计算 Ix %%%%      
Ix = zeros(sizey,sizex);      
Ix(1:sizey-1,1:1:sizex-1) = 0.25.*(im1_right + im2_right + im1_down_right + im2_down_right - ...      
   (im1(1:sizey-1, 1:sizex-1) + im2(1:sizey-1,1:sizex-1) + im1_down + im2_down));      
Ix(sizey,:) = 0;      
Ix(:,sizex) = 0;      
     
%%%% 计算 Iy %%%%      
Iy = zeros(sizey,sizex);      
Iy(1:sizey-1,1:1:sizex-1) = 0.25.*(im1_down + im2_down + im1_down_right + im2_down_right - ...      
    (im1(1:sizey-1, 1:sizex-1) + im2(1:sizey-1, 1:sizex-1) + im1_right + im2_right));      
Iy(sizey,:) = 0;      
Iy(:,sizex) = 0;      
     
%%%% 计算 It %%%%      
It = zeros(sizey,sizex);      
It(1:sizey-1,1:1:sizex-1) = 0.25.*(im2(1:sizey-1,1:sizex-1) + im2_right + im2_down + im2_down_right - ...      
   (im1(1:sizey-1,1:sizex-1) + im1_right + im1_down + im1_down_right));      
It(sizey,:) = 0;      
It(:,sizex) = 0;