illustrate fspecial function for linear motion blur

1 view (last 30 days)
I am trying illustrate fspecial('motion',x,y) function In help of mathlab I see the guide for creating kernel 1. Construct an ideal line segment with the desired length and angle, centered at the center coefficient of h. 2. For each coefficient location (i,j), compute the nearest distance between that location and the ideal line segment. 3. h = max(1 - nearest_distance, 0); 4. Normalize h:h = h/(sum(h(:))); example: fspecial('motion',3,45) I describe like this 2. a = [sqrt(2) sqrt(2)/2 0; sqrt(2)/2 0 sqrt(2)/2; 0 sqrt(2)/2 sqrt(2);] 3. h = max(1-a,0); 4. h = h/(sum(h(:)))
and the result is: 0 0.0702 0.2397 0.0702 0.2397 0.0702 0.2397 0.0702 0 but using fspecial('motion',3,45) the result is 0 0.0754 0.1883 0.0754 0.3215 0.0754 0.1883 0.0754 0 Please tell me where am I wrong ? What is the kernel of linear motion blur in fspecial function? Thanks.

Answers (1)

Keqi Ma
Keqi Ma on 9 Feb 2021
Following is the code for 'motion' kernel in Matlab. There is no max opearation at all. You can follow this code to understand the 'motion' kernel.
len = max(1,p2);
half = (len-1)/2;% rotate half length around center
phi = mod(p3,180)/180*pi;
cosphi = cos(phi);
sinphi = sin(phi);
xsign = sign(cosphi);
linewdt = 1;
% define mesh for the half matrix, eps takes care of the right size
% for 0 & 90 rotation
sx = fix(half*cosphi + linewdt*xsign - len*eps);
sy = fix(half*sinphi + linewdt - len*eps);
[x y] = meshgrid(0:xsign:sx, 0:sy);
% define shortest distance from a pixel to the rotated line
dist2line = (y*cosphi-x*sinphi);% distance perpendicular to the line
rad = sqrt(x.^2 + y.^2);
% find points beyond the line's end-point but within the line width
lastpix = find((rad >= half)&(abs(dist2line)<=linewdt));
%distance to the line's end-point parallel to the line
x2lastpix = half - abs((x(lastpix) + dist2line(lastpix)*sinphi)/cosphi);
dist2line(lastpix) = sqrt(dist2line(lastpix).^2 + x2lastpix.^2);
dist2line = linewdt + eps - abs(dist2line);
dist2line(dist2line<0) = 0;% zero out anything beyond line width
% unfold half-matrix to the full size
h = rot90(dist2line,2);
h(end+(1:end)-1,end+(1:end)-1) = dist2line;
h = h./(sum(h(:)) + eps*len*len);
if cosphi>0,
h = flipud(h);
end

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!