Using vision.Cas​​cadeObjec​t​Detector on Simulink

Hello, I read the previous answers about the same problem but I am still not able to solve my problem. What i want is to detect water bottles and get their centroids. Here is my code,
function centroids = fcn(img)
coder.extrinsic('vision.CascadeObjectDetector');
detector = vision.CascadeObjectDetector('...\Bottle.xml');
bboxes = step(detector,img);
centroids = [bboxes(:,1)+(bboxes(:,3)/2),bboxes(:,2)+(bboxes(:,4)/2)];
And the errors are,
Unexpected MATLAB operator.
Function 'MATLAB Function' (#88.264.265), line 5, column 21:
":"
Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Errors occurred during parsing of MATLAB function 'MATLAB Function'
Component:MATLAB Function | Category:Coder error
Simulink cannot determine sizes and/or types of the outputs for block 'MATLAB Function' due to errors in the block body, or limitations of the underlying analysis. The errors might be inaccurate. Fix the indicated errors, or explicitly specify sizes and/or types for all block outputs.
Component:MATLAB Function | Category:Coder error
Simulink cannot determine sizes and/or types of the outputs for block 'MATLAB Function' due to errors in the block body, or limitations of the underlying analysis. The errors might be inaccurate. Fix the indicated errors, or explicitly specify sizes and/or types for all block outputs.
Component:Simulink | Category:Model error
Error occurred in 'Swarm_Control/MATLAB Function'.
Component:Simulink | Category:Model error

 Respuesta aceptada

Sebastian Castro
Sebastian Castro el 19 de Sept. de 2017
Editada: Sebastian Castro el 19 de Sept. de 2017

0 votos

The issue here is that the step method of vision.CascadeObjectDetector can return a variable number of bounding boxes, from zero to (technically) infinite. See below for more info on variable-sized signals:
You can deal with this variable-size signal by either bounding the max size of the output or enabling dynamic memory allocation in your model.
A further option is to create a "wrapper" MATLAB function that uses vision.CascadeObjectDetector and always fills, for example, the N largest bounding boxes into a fixed-size output. Then, you can call that from Simulink without worrying about variable-sized signals. Instead, you have to pre-allocate that N-by-4 array of bounding boxes to, e.g., zeros and make sure you handle all the edge cases for filling that array with the output of the detector.
With this last approach you will still need to handle the variable-sized signals as above, but you will make sure that the output of your MATLAB Function block to the rest of the model is of a fixed size. It certainly helps with memory usage.
- Sebastian

7 comentarios

Thanks for the quick answer. But it seems I still can't solve the problem. I changed the dynamic memory allocation threshold to 0 I but get the same errors as before. I also tried to set the size as 'Variable size' but now I have to set an upper bound. I got really stuck on this. I'd appreciate any further help.
Best regards.
I do not think you want to set a threshold for dynamic memory allocation. You want it to be high because you might need a lot of memory if you have several bounding boxes being output.
- Sebastian
Ok, I set threshold to 65536 and i get these
After that, I tried to set my output variable's size as 'Variable Size' and I get these with the code of,
function bboxes = fcn(img)
coder.extrinsic('vision.CascadeObjectDetector');
coder.extrinsic('step(detector,img)');
detector =vision.CascadeObjectDetector('Bottle.xml');
bboxes = zeros(921600,4);
bboxes = step(detector,img);
After I did some research and ran onto,
What to do ?
You can either use an assert statement or coder.varsize to place an upper bound on the size.
assert(size(bboxes,1)<1000)
or
coder.varsize('bboxes',[1000 4]);
- Sebastian
I am still getting the same error
Output 'bboxes' has variable size but the upper bound is not specified; explicit upper bound must be provided.
While having
coder.varsize('bboxes',[1000 4]);
inserted in the code. I'd be really pleased if I could make this work. The code works on the Matlab environment perfectly but with the Simulink, it continuously gives me the same errors overall. Can Simulink handle variable sized output signals? Because that's what I am trying to do.If yes, what am I doing wrong?
Hmm... since bboxes is an output, and not just local data, you may need to do this instead of coder.varsize.
- Sebastian
Thanks a lot , got it working with the code
function bboxes = fcn(img)
coder.extrinsic('vision.CascadeObjectDetector');
coder.extrinsic('step(detector,img)');
detector =vision.CascadeObjectDetector('...Bottle.xml');
bboxes = zeros(1,4);
bboxes=step(detector,img);
And setting
%

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Community Treasure Hunt

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

Start Hunting!

Translated by