vision.blobanalysis used in ROS code generation does not support dynamicMemoryAllocation for AllVariableSizeArrays, but needed for ROS code gen

1 view (last 30 days)
I want to use vision.blobanalysis in a matlab script to do some image processing on some images recived from ROS. I want use matlab coder to generate ROS code for deployment.
However when complieing it, it throws an error regarding the binary image input into the vision.blobanalysis object.:
??? This System object method does not support input of unbounded size.
Configure 'DynamicMemoryAllocation' to a mode other than 'AllVariableSizeArrays' to allow automatic size propagation.
The relevant part of the code is as follows:
BlobAnalysis = vision.BlobAnalysis('ExtentOutputPort',1,'OrientationOutputPort',1,'Connectivity',4,'MaximumCount', 3,'MaximumBlobArea', 12000000,'MinimumBlobArea', 10000);
[~,centroid,bbox,~,extent] = step(BlobAnalysis,BW);% error thrown for this line
I think the problem is that for ROS code gen, it requires dynamicMemoryAllocation enabled for AllVariableSizeArrays. So am unable to change this mode. However this seems to have caused the binary image input in to this function to be of an unbounded size, which seems to not be accepted by this function for code gen.
I know that the size of the binary image is fixed as a 360x640 logical, and so i have tried to fix the size using
Or using the other method using assert
assert(size(BW,1) <= 2000 & size(BW,2) <= 2000)
using both after BW's first delceration.
However neither work and still give me the same compile error.
So my question is does anyone know a way around this, or perhaps i am simply unable to use vision.BlobAnalysis, and might need to find an alternative.
Thanks so much for any help anyone can give me.

Answers (1)

Josh Chen
Josh Chen on 9 Feb 2022
Hello Matthew,
Since the "step" function does not want "unbounded" input, one idea that worth trying is to create an empty fixed size image, filling this image by the values in "BW", and then always pass the fixed size image into the "step" function. This way the size is always "bounded". The code would be something similar to:
% Create default empty image
defaultImage = zeros([360,640]);
% Fill the default image with values from BW
minRow = min(size(defaultImage,1), size(BW,1));
minCol = min(size(defaultImage,2), size(BW,2));
defaultImage(1:minRow,1:minCol) = BW(1:minRow, 1:minCol);
% Pass to "step" function
[~,centroid,bbox,~,extent] = step(BlobAnalysis,defaultImage);
Hope this helps,
Josh Chen
Josh Chen on 10 Feb 2022
Edited: Josh Chen on 10 Feb 2022
Hello Matthew,
I tried generating the code above, and it actually gave me another error:
??? This System object method does not support output of unbounded size.
Configure 'DynamicMemoryAllocation' to a mode other than 'AllVariableSizeArrays' to allow automatic size propagation.
This time it complains about the output as ubounded size... So I guess the "step" function relies on MATLAB Coder to propagation size for some of its outputs as well.
And you are right, ROS code-gen needs to use 'AllVariableSizeArrays' to generate coder::array based variable size array. Thus, it cannoot be switched to other mode.
The best I can think of is to return only those outputs that does not rely on MATLAB Coder to determine the size (Although this means you might not be able to get all you need..). For example, if you only return "centroid", this seems to work. I am able to generate code from the attached file with the following command:
>> cfg = coder.config('exe');
>> cfg.Hardware = coder.hardware('Robot Operating System (ROS)');
>> cfg.HardwareImplementation.ProdHWDeviceType = 'Intel->x86-64 (Windows64)';
>> cfg.HardwareImplementation.TargetHWDeviceType = 'Generic->MATLAB Host Computer';
>> cfg.HardwareImplementation.ProdLongLongMode = 1;
>> codegen cg -config cfg
Hope this helps,

Sign in to comment.


Find more on Network Connection and Exploration 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!