MATLAB coder で C++の生成時に「こ​の変数では中かっこの​インデックス付けはサ​ポートされていません​」と出る

14 Ansichten (letzte 30 Tage)
啓司
啓司 am 24 Nov. 2023
Bearbeitet: 啓司 am 24 Nov. 2023
現在image prossingのfibermetricというフィルタを使用しています。
これをC++に変換したいと考えており、
関数としてfibermetricを作成し、生成できない関数はcoder.extrinsicを使用して関数を宣言しました。
コードは以下のようになります。
function B = fibermetric(V,varargin)
%FIBERMETRIC Enhance elongated or tubular structures in images or 3D grayscale volumes.
% fibermetricは画像内や3Dグレースケールの細長買ったりチューブ状の構造を強調する
%
% B = FIBERMETRIC(A) enhances tubular structures in intensity image A
% B = FIBERMETRIC(A) で画像Aのチューブ状構造を強調する
%
% using Hessian based multiscale filtering. B contains the maximum
% マルチスケールフィルタに基づくヘッシアンを使っている。 Bは最大値を含む
%
% response of the filter at a thickness that approximately matches the
% size of the tubular structure to detect.
% チューブ状構造におおよそ合う暑さにフィルタが反応し、検知する
%
% B = FIBERMETRIC(V, ___) enhances tubular structures in the volume V.
% B = FIBERMETRIC(V, ___) は量Vでのチューブ構造を強調する
%
% B = FIBERMETRIC(A, THICKNESS) enhances the tubular structures of
% thickness THICKNESS in A.
% B = FIBERMETRIC(A, THICKNESS)はAの暑さの構造チューブを強調する
%
% THICKNESS is a scalar or a vector in pixels
% which characterizes the thickness of tubular structures.
% 引数THICKNESSは、チューブ状構造の厚さを特徴づけるピクセルにおけるスカラーとベクトルである
%
% It should be
% of the order of the width of the tubular structures in the image
% domain.
% それは画像領域のチューブ状構造の幅の命令であるべきである
%
% When not provided, THICKNESS is [4, 6, 8, 10, 12, 14].
% 何も供給しないなら、THICKNESS は [4, 6, 8, 10, 12, 14]になる
%
%
% B = FIBERMETRIC(___, Name, Value) enhances the tubular structures in
% the image using name-value pairs to control different aspects of the
% filtering algorithm.
% B = FIBERMETRIC(___, Name, Value)で画像内の名前の値とフィルタリングアルゴリズムの違った表面の制御
% とペアにしてチューブ状構造を強調する
%
% Parameters include:
% パラーメータは含める:
%
% 'StructureSensitivity' - Specifies the sensitivity/threshold for
% differentiating the tubular structure from the
% background and is dependent on the gray scale
% range of the image. Default value is
% 0.01*diff(getrangefromclass(V)).
% '構造感度' - 画像のグレースケールの範囲に応じてチューブ状構造を区別するために
% 感度/閾値を指定する
% デフォルトの値は0.01*diff(getrangefromclass(V))
%
% 'ObjectPolarity' - Specifies the polarity of the tubular structures
% with respect to the background. Available options are:
%
% 'bright' : The structure is brighter than the background.(Default)
% 'dark' : The structure is darker than the background.
% 'オブジェクト極性'- 背景に関してチューブ状構造の極性を指定する
% 'bright' : 構造は背景よりも明るい(デフォルト設定)
% 'dark' : 構造は背景よりも暗い
%
% Class Support
% クラスサポート
% -------------
% Input image A must be a 2D grayscale image or a 3D volumetric image and
% can be of class uint8, int8, uint16, int16, uint32, int32, single, or double.
% 入力する画像Aは2Dのグレースケール画像か3D画像、uint8, int8, uint16, int16, uint32, int32, single, or double.
% のいづれかになる
%
% It must be real and nonsparse. The output variable B is of class single unless the
% input is of type double in which case the output is also double.
% それは本物であり非スペースであるべきだ。出力変数Bは入力タイプがdouble型で出力もまたdoubleで
% なければsingleのクラスである
%
% Remarks
% 備考
% -------
%
% The function FIBERMETRIC changed in version 9.4 (R2018b).
% FIBERMETRIC関数はversion 9.4 (R2018b)に変更した
%
% Previous versions
% of the Image Processing Toolbox used a different default for the
% parameter 'StructuralSensitivity' which used to be half the
% maximum of Hessian norm.
% 以前の画像処理ツールボックスのバージョンは構造感度においてヘッシアン規格の
% 最大値の半分のパラーメータという違ったデフォルトが使われていた
%
% If you need the same results produced
% by the previous implementation use the function 'maxhessiannorm' to
% to find the 'StructureSensitivity' value which is 0.5*(output of maxhessiannorm).
% This is only supported for 2D images.
% もし、あなたが以前に実装された同じような関数'maxhessiannorm'を使用したいなら、
% 0.5*(output of maxhessiannorm)となっている構造感度を探せ
% これは2D画像のみ対応している
%
% Reference
% 参照論文
% ---------
% Frangi, Alejandro F., et al. "Multiscale vessel enhancement filtering."
% Medical Image Computing and Computer-Assisted Intervention -- MICCAI 1998.
% Springer Berlin Heidelberg, 1998. 130-137
%
% Example
% 使い方の例
% -------
% % Find threads approximately 7 pixels thick
% A = imread('threads.png');
% B = fibermetric(A, 7, 'ObjectPolarity', 'dark', 'StructureSensitivity', 7);
% figure; imshow(B); title('Possible tubular structures 7 pixels thick')
% C = B > 0.15;
% figure; imshow(C); title('Thresholded result')
%
% See also edge, imgradient.
% エッジと、勾配のないところも見ている
% Copyright 2016-2020 The MathWorks, Inc.
% 著作権は2016-2020でThe MathWorks, Inc.
% matlabcoderに対応していないかった関数を呼び出す関数
coder.extrinsic("matlab.images.internal.errorIfgpuArray")
coder.extrinsic("matlab.images.internal.stringToChar")
coder.extrinsic("imgaussfilt3")
coder.extrinsic("images.internal.builtins.fibermetric")
matlab.images.internal.errorIfgpuArray(V,varargin{:});
args = matlab.images.internal.stringToChar(varargin);
% 入力引数の処理
[thickness, c, objPolarity] = parseInputs(V, args{:});
thickness = double(thickness);
% Passing object Polarity as a boolean flag to C++ code (instead of string)
% 目的極性をboolen flagとしてC++コードに通過する(文字列の変わりに)
if strcmp(objPolarity,'bright')
isBright=true;
elseif strcmp(objPolarity,'dark')
isBright=false;
end
% Casting input to single if non-floating datatype
% 非フローティングタイプであったらsingleとしてキャストする
classOriginalData = class(V);
% Default value for Structural Sensitivity := datatypeRange/100
% 構造感度=データタイプの範囲/100のためのデフォルト変数
if isempty(c)
c = cast(diff(getrangefromclass(V))/100,classOriginalData);
end
switch (classOriginalData)
case 'uint32'
V = double(V);
case 'double'
otherwise
V = single(V);
end
% Output can be double or single
% 出力はdoubleかsingleにすることができる
B = zeros(size(V),'like',V);
for id = 1:numel(thickness)
sigma = thickness(id)/6;
if (ismatrix(V))
Ig = imgaussfilt(V, sigma, 'FilterSize', 2*ceil(3*sigma)+1);
elseif (ndims(V)==3)
Ig = imgaussfilt3(V,sigma, 'FilterSize', 2*ceil(3*sigma)+1);
end
out = images.internal.builtins.fibermetric(Ig, c, isBright, sigma);
B = max(B,out);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% fibermetricにおける入力引数の処理関数 %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [thickness, c, objPolarity] = parseInputs(A, varargin)
% 関数の呼び出し
coder.extrinsic("inputParser")
% 引数1~6の確認
narginchk(1,6);
% 引数Aが条件を満たしているかの確認
validateInp(A);
% 引数のパースとバリデーションを行うためのオブジェクトを生成
parser = inputParser();
% 引数の部分一致の許可
parser.PartialMatching = true;
% 選ぶことのできる厚さの追加
parser.addOptional('Thickness', 4:2:14, @validateThickness);
% パラーメータ'StructureSensitivity'の追加
parser.addParameter('StructureSensitivity', [], @validateStructureSensitivity);
% パラーメタ'ObjectPolarity''bright'の追加
parser.addParameter('ObjectPolarity','bright', @validateObjectPolarity);
% 引数に沿ってパースする
parser.parse(varargin{:});
% 出力変数への代入
parsedInputs = parser.Results;
thickness = parsedInputs.Thickness;
c = parsedInputs.StructureSensitivity;
objPolarity = validatestring(parsedInputs.ObjectPolarity, {'bright','dark'});
end
function validateInp(A)
allowedImageTypes = {'uint8', 'uint16', 'uint32', 'double', 'single', 'int8', 'int16', 'int32'};
validateattributes(A, allowedImageTypes, {'nonempty',...
'nonsparse', 'real', 'finite', '3d'}, mfilename, 'A', 1);
anyDimensionOne = any(size(A) == 1);
if (isvector(A) || anyDimensionOne)
error(message('images:fibermetric:imageNot2or3D'));
end
end
function tf = validateThickness(thickness)
validateattributes(thickness, {'numeric'}, ...
{'integer', 'nonsparse', 'nonempty', 'positive', 'finite', 'vector'}, ...
mfilename, 'THICKNESS', 2);
tf = true;
end
function tf = validateStructureSensitivity(x)
validateattributes(x, {'numeric'}, ...
{'scalar', 'real', 'positive', 'finite', 'nonsparse', 'nonempty'}, ...
mfilename, 'StructureSensitivity');
tf = true;
end
function tf = validateObjectPolarity(x)
validateattributes(x, {'char'}, {}, mfilename, 'ObjectPolarity');
validatestring(x, {'bright','dark'}, mfilename, 'ObjectPolarity');
tf = true;
end
そして生成する際の引数をMATLAB上で実行できていた画像を参照し変数を自動定義しました。用いたコードは下のようになります。
I = imread('bright.jpg');
B = fibermetric(I,11,'ObjectPolarity','bright','StructureSensitivity', 7);
自動定義された変数は下のようになります。
しかし、この状態で生成ボタンを押すと
というようにエラーが出ます。(最初に挙げたプログラムはコメントを追加したため、エラーの示している行は最初に上げたプログラムの行を指してはいない。)
このエラーを解決する策をご教示いただきたいです。
個人的にはhttps://jp.mathworks.com/help/matlab/ref/varargin.htmlにあるvararginの
  • varargin に書き込むことはできません。入力引数に書き込む場合は、まず入力引数をローカル変数にコピーしてください。
という内容が怪しい気がしますが、何を書き込むのかこの文ではよくわからないです。
よろしくおねがいします。

Antworten (0)

Kategorien

Mehr zu Image Processing Toolbox finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!