MATLAB Answers

Matlab coder with bwboundries

2 views (last 30 days)
Tej Patel
Tej Patel on 18 Apr 2017
Commented: Timo on 4 Dec 2018
I am getting an issue trying to compile a simulink block. This block contains a matlab function that uses this function call inside:
B = bwboundaries(BW, 8, 'noholes');
Note that BW is simply a binary image of known size.
When trying to compile, I get an error for the function bwboundries returning an unbounded value:
Computed maximum size of the output of function 'bwboundaries' is not bounded.
Static memory allocation requires all sizes to be bounded. The computed size is [:? x 1].
Function 'bla' (#40.123.153), line 7, column 5:
"bwboundaries(BW, 8, 'noholes')"
This simulink model is intended to run on a Beaglebone Black if that helps.
I've tried to declare the variable size of BW and of B with coder.varsize but it did not help.
  1 Comment
Nikhil Sreekumar
Nikhil Sreekumar on 28 Apr 2017
Can you mention how you used the coder.varsize? Was it something like this?
coder.varsize('B', [100 1])
Assuming the number of objects and holes won't go above 100.

Sign in to comment.

Answers (1)

Ankit Bhardwaj
Ankit Bhardwaj on 24 Apr 2017
According to the following documentation, code generation for 'bwboundaries' function requires 'conn' and 'options' (i.e. 2nd and 3rd parameter) to be a compile-time constant.
If this does not help, please provide a reproduction model and information about the MATLAB version you are using.
Timo on 4 Dec 2018
Hi Nikhil and others,
I am facing the same issue. Since B is a cell array, the coder.varsize('B', [100 1]) command needs to come after the first call of bwboundaries right? To be sure, as Ankit suggested, I also declared the conn variable with coder.varsize, and all variables that are derived from B afterwards in my code.
To give some context to the code below: I am looking for the pixel coordinates (X,Y) of the edge of the largest Blob detected with bwboundaries. Since I do analysis on a region-of-interest, I have another separate function to return the actual pixel coordinates for given ROI-coordinates. If no feature is detected, I simply return NaNs.
In the model explorer, the variables Xe, Ye are set to be variable size with bounds. I turned dynamic memory allocation off since this code needs to be RT at some point.
Many thanks for your help,
function [Xe,Ye,qual] = get_boundary_perim(afterthreshmedian,ROI_pars) %#codegen
coder.varsize('afterthreshmedian',size(ROI_pars.ROI),[0 0]);
conn = 8;
B = bwboundaries(afterthreshmedian,conn);
coder.varsize('B', [100 1],[1 0]);
n = length(B);
if ~isempty(B)
Xetemp = cell(1,n);
Yetemp = cell(1,n);
lengths = zeros(1,n);
for ii = 1:n
xy = B{ii};
lengths(ii) = length(xy);
[Xetemp{ii},Yetemp{ii}] = ROIind2imag(ROI_pars,xy(:,2),xy(:,1));
qual = true;
[~,ind] = max(lengths);
Xe = Xetemp{ind};
Ye = Yetemp{ind};
Xe = NaN;
Ye = NaN;
qual = false;

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by