Search code examples
matlabcomputer-visionsimulinkmatlab-cvst

Geometric transformation using MATLAB Function block


I tried to apply a geometric transformation to a video using MATLAB Function block in Simulink. I connected a 'From Multimedia File' block to MATLAB Function block ant its output to a 'To Video Display' block. Inside MATLAB Function Block this code:

function outputImage = fcn(inputImage,theta)


coder.extrinsic('affine2d')
coder.extrinsic('imwarp')
outputImage = coder.nullcopy(inputImage);


tform = affine2d([cosd(theta) -sind(theta) 0; sind(theta) cosd(theta) 0; 0 0 1]);

outputImage = imwarp(inputImage,tform);

where angle theta is a constant block of 10. The code above is an example from 'affine2d' function examples. It returns me following error:

Size mismatch for MATLAB expression 'imwarp'. Expected = 120x160x3 Actual = 146x179x3 Block MATLAB Function (#108) While executing: State During Action 

but I don't understand where is the error (why Actual=146x179x3). The input video is a RGB video file ('vipmen.avi') from CV toolbox demos.

EDIT: I solved it: I used Apply Geometric Transformation block for translation and a Rotate block for rotation with:

H = [1 0; 0 1; Ox-X  Oy-Y] %where Ox,Oy are image framework origin coordinates and X,Y are offset coordinates. 

Indeed after translation, the "new" image framework origin is the (X,Y) point and the Rotate block rotates it about (X,Y).


Solution

  • The Computer Vision System Toolbox includes a block called Apply Geometric Transformation, which will do what you need and save you the trouble of using MATLAB function block.

    Edit: The Apply Geometric Transformation block has been deprecated as of R2015b release. Please use the Warp block instead.