Passing individual varargin arguments from one function to another

67 ビュー (過去 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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImage Segmentation and Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by