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

1 次查看(过去 30 天)
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

采纳的回答

Sebastian Castro
Sebastian Castro 2017-9-19
编辑:Sebastian Castro 2017-9-19
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 个评论
Sebastian Castro
Sebastian Castro 2017-9-22
Hmm... since bboxes is an output, and not just local data, you may need to do this instead of coder.varsize.
- Sebastian
Kerem Asaf Tecirlioglu
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
%

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Computer Vision with Simulink 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by