Propose Fixed-Point Data Types Using an Instrumented Mex Function

2 views (last 30 days)
This is my function:
function [mask] = computeMask (I, threshold, win_var, borderx, bordery)
coder.extrinsic('imfilter');
%-- compute Variances of the RGB and IR patches
mean_I = imfilter(I,ones(win_var)/win_var/win_var,'same','symmetric','conv');
mean2_I = imfilter(I.^2,ones(win_var)/win_var/win_var,'same','symmetric','conv');
std_I= real(sqrt(mean2_I-mean_I.^2)+1e-5);
mask= std_I>threshold;
% add the border of the image to the mask
% (filters are not complete at the border)
mask(1:((bordery-1)/2),:)=0;
mask((end-(bordery-1)/2):end,:)=0;
mask(:,1:((borderx-1)/2))=0;
mask(:,(end-(borderx-1)/2):end)=0;
% add the area where the vignetting is too strong
Cy=floor(size(mask,1)/2);
Cx=floor(size(mask,2)/2);
[x,y]=meshgrid(1:size(mask,2),1:size(mask,1));
mask(((x-Cx).^2+(y-Cy).^2)>8e4)=0;
%mask(((x-Cx).^2+(y-Cy).^2)>1.3e4)=0;
return
When trying to build Build "Instrumented MEX Function" I get these errors: ??* ? Expected either a logical, char, int, fi, single, or double. Found an mxArray. MxArrays are returned from calls to the MATLAB interpreter and are not supported inside expressions. They may only be used on the right-hand side of assignments and as arguments to extrinsic functions.*
Error in ==> computeMask Line: 21 Column: 18: std_I= real(sqrt(mean2_I-mean_I.^2)+1e-5);
How to fix these problems? please help me !!! Thank you very much

Answers (1)

Ryan Livingston
Ryan Livingston on 2 Apr 2014
My first suggestion is if you are using R2014a, then imfilter is supported for code generation so you may be able to avoid declaring it as an extrinsic function:
If you need to use it as an extrinsic function for other reasons please read on.
When using the output of an extrinsic function, as you are doing with imfilter, it may often be necessary to preinitialize the variable to which it is assigned. Doing so is what tells the code generation software about the type, size and complexity of the value returned. If foo is extrinsic, then you could use:
x = zeros(3,4);
x = foo(y);
to call foo extrinsically and assign the output as a 3-by-4 array. See:
for more details.
So you would just need to assign the values of the calls to imfilter to be the correct size, type etc. From the documentation for imfilter, the output is the same size and type as the input. So you should be able to try something like:
mean_I = zeros(size(I),'like',I);
mean_I = imfilter(I,ones(win_var)/win_var/win_var,'same','symmetric','conv');
mean2_I = zeros(size(I),'like',I);
mean2_I = imfilter(I.^2,ones(win_var)/win_var/win_var,'same','symmetric','conv')

Community Treasure Hunt

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

Start Hunting!