Passing individual varargin arguments from one function to another

조회 수: 51 (최근 30일)
Scott
Scott 2015년 2월 1일
댓글: David Young 2015년 2월 2일
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

채택된 답변

Image Analyst
Image Analyst 2015년 2월 1일
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
David Young
David Young 2015년 2월 2일
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 2015년 2월 2일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

David Young
David Young 2015년 2월 1일
You need
statsOut=regionprops(varargin{:});
which passes the contents of varargin as a comma-separated list.
  댓글 수: 1
Scott
Scott 2015년 2월 1일
Thanks - I had forgotten about this easy way to burst apart a cell array.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by