Passing individual varargin arguments from one function to another

52 Ansichten (letzte 30 Tage)
I'd like to make a wrapper around regionprops and am having trouble passing the arguments along.
This isn't a problem specific to regionprops but is more about trying to wrap a function that has varargin for input and retaining the flexibility.
regionprops has varargin for input: regionprops(varargin) One possible input is a labeled matrix.
I'd like to write a wrapper where I can modify that label matrix and then pass everything along to regionprops - essentially substituting my new matrix for varargin{1} but keeping everything else the same.
The rationale is that if the biggest label is really big but there are a lot of missing labels, regionprops is really slow.
If the labeled objects are not connected (mine aren't) then I can make it much faster by relabeling the objects with 1:#objects, calling regionprops on this labeled matrix, and then reconstructing the statistics for the original labels. I can do this by adding a few lines in a copy of the regionprops function itself, but I'd prefer to leave it alone and just wrap it.
But the problem is:
-----
rps=myWrapper(L,'Area','FilledImage','Centroid');
function modifiedStatsOut=myWrapper(varargin)
...
varargin{1}=modifiedLabelMatrix;
statsOut=regionprops(varargin);
-----
this fails because instead of varargin being a 1x4 cell as it is when passed into myWrapper, it is a 1x1 cell when it gets into regionprops
Thanks for any help, Scott

Akzeptierte Antwort

Image Analyst
Image Analyst am 1 Feb. 2015
You messed up the assignments. It should be like this:
rps=myWrapper(L,'Area','FilledImage','Centroid'); % Call from the main program.
function modifiedStatsOut = myWrapper(varargin)
% Extract the incoming labeled matrix.
originalLabeledImage = varargin{1};
% Now extract the measurements you want to make.
measurements = varargin(2:end);
% Now somehow you create modifiedLabelMatrix based on the incoming version.
% Combine blobs or whatever...
modifiedLabelMatrix = ...some code that uses originalLabeledImage....
% Now call regionprops() with new labeled matrix
statsOut = regionprops(modifiedLabelMatrix, measurements);
  8 Kommentare
David Young
David Young am 2 Feb. 2015
My answer was intended as a correction to the final line of code in the original question - hence I assumed that varargin{1} had been updated.
David Young
David Young am 2 Feb. 2015
It's really a rather trivial point I wanted to make, and maybe not worth labouring. It was this: you can make a wrapper that is robust, in that it accepts all the same input patterns as regionprops, as easily as making one that fails if the original image is the second argument.
To achieve generality, either use the original code from the question, with the final line modified as in my answer, or use Image Analyst's code, with the final line modified to
statsOut = regionprops(modifiedLabelMatrix, measurements{:});
It's then not necessary to do any additional checking or extraction of the second argument.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

David Young
David Young am 1 Feb. 2015
You need
statsOut=regionprops(varargin{:});
which passes the contents of varargin as a comma-separated list.

Community Treasure Hunt

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

Start Hunting!

Translated by