将 List 作为参数从 C# 项目传递给 Matlab 函数

本文关键字:Matlab 函数 项目 List 参数 | 更新日期: 2023-09-27 17:55:46

我正在尝试从我的 C# 项目运行快速压缩跟踪算法。我想要的是,(在 C# 中)将图像转换为灰度图像后将图像传递给 Matlab 函数

  Runtracker 

而不是将图像放在 Matlab 代码中,因为我将应用一些操作来指定包含要开始跟踪的图像的数组的索引。
但是当我传递灰度图像列表时,我收到一个错误说

"'trackerNative.FCT.Runtracker(int)'的最佳重载方法匹配有一些无效的参数。

你能帮我解决这个问题吗? 将 C# 中的图像列表传递给 matlab 函数。

C# 代码

  //to convert to graScale
    public double[,] to_gray(Bitmap img)
    {
        int w = img.Width;
        int h = img.Height;
        double[,] grayImage = new double[w, h];
        for (int i = 0; i < w; i++)
        {
            for (int x = 0; x < h; x++)
            {
                Color oc = img.GetPixel(i, x);
                grayImage[i, x] = (double)(oc.R + oc.G + oc.B) / 3;
            }
        }
        return grayImage;
    }

    //to get the files from specified folder
    public static String[] GetFilesFrom(String searchFolder, String[] filters, bool isRecursive)
    {
        List<String> filesFound = new List<String>();
        var searchOption = isRecursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
        foreach (var filter in filters)
        {
            filesFound.AddRange(Directory.GetFiles(searchFolder, String.Format("*.{0}", filter), searchOption));
        }
        return filesFound.ToArray();
    }

    //Button to run the matlab function 'Runtracker'
    private void button1_Click(object sender, EventArgs e)
    {
        FCT obj = new FCT();  //FCT is a matlab class
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            string file_path = openFileDialog1.FileName;
            var filters = new String[] { "png" };
            var files = GetFilesFrom(file_path, filters, false);
            List< double[,] > imgArr = new List< double[,] >();
            for (int i = 0; i < files.Length; i++)
            {
                Bitmap image = new Bitmap(files[i]);
                double[,] grayScale_image = to_gray(image);
                imgArr[i] = grayScale_image;                     
            }

            object m = obj.Runtracker(imgArr);  //the error occured 
            //Bitmap output = m as Bitmap; 
        }
    }

矩阵代码

    function Runtracker(input_imgArr)
    clc;clear all;close all;
    rand('state',0); 
    %%  
    [initstate] = initCt(1);% position of the detected face in the 1st frame
    num = length(input_imgArr);% number of frames
    %%
    x = initstate(1);% x axis at the Top left corner
    y = initstate(2);
    w = initstate(3);% width of the rectangle
    h = initstate(4);% height of the rectangle
    %---------------------------
    img = imread(input_imgArr(1));
    img = double(img);
    %% 
    trparams.init_negnumtrain = 50;%number of trained negative samples
    trparams.init_postrainrad = 4;%radical scope of positive samples
    trparams.initstate = initstate;% object position [x y width height]
    trparams.srchwinsz = 25;% size of search window
    %-------------------------
    %% Classifier parameters
    clfparams.width = trparams.initstate(3);
    clfparams.height= trparams.initstate(4);
    % feature parameters
    % number of rectangle from 2 to 4.
    ftrparams.minNumRect =2;
    ftrparams.maxNumRect =4;
    M = 100;% number of all weaker classifiers, i.e,feature pool
    %-------------------------
    posx.mu = zeros(M,1);% mean of positive features
    negx.mu = zeros(M,1);
    posx.sig= ones(M,1);% variance of positive features
    negx.sig= ones(M,1);
    lRate = 0.85;% Learning rate parameter
    %% Compute feature template
    [ftr.px,ftr.py,ftr.pw,ftr.ph,ftr.pwt] = HaarFtr(clfparams,ftrparams,M);
    %% Compute sample templates
    posx.sampleImage = sampleImgDet(img,initstate,trparams.init_postrainrad,1);
    negx.sampleImage =     sampleImg(img,initstate,1.5*trparams.srchwinsz,4+trparams.init_postrainrad,trpar ams.init_negnumtrain);
    %% Feature extraction
    iH = integral(img);%Compute integral image
    posx.feature = getFtrVal(iH,posx.sampleImage,ftr);
    negx.feature = getFtrVal(iH,negx.sampleImage,ftr);
    [posx.mu,posx.sig,negx.mu,negx.sig] =  
    classiferUpdate(posx,negx,posx.mu,posx.sig,negx.mu,negx.sig,lRate);%     
    update distribution parameters
    %% Begin tracking
    for i = 2:num
    img = imread(input_imgArr(i));
    imgSr = img;% imgSr is used for showing tracking results.
    img = double(img); 
    iH = integral(img);%Compute integral image
    %% Coarse detection
    step = 4; % coarse search step
    detectx.sampleImage = 
    sampleImgDet(img,initstate,trparams.srchwinsz,step);    
    detectx.feature = getFtrVal(iH,detectx.sampleImage,ftr);
    r = ratioClassifier(posx,negx,detectx.feature);% compute the classifier for all samples
    clf = sum(r);% linearly combine the ratio classifiers in r to the final classifier
    [c,index] = max(clf);
    x = detectx.sampleImage.sx(index);
    y = detectx.sampleImage.sy(index);
    w = detectx.sampleImage.sw(index);
    h = detectx.sampleImage.sh(index);
    initstate = [x y w h];
    %% Fine detection
    step = 1;
    detectx.sampleImage = sampleImgDet(img,initstate,10,step);    
    detectx.feature = getFtrVal(iH,detectx.sampleImage,ftr);
    r = ratioClassifier(posx,negx,detectx.feature);% compute the classifier for all samples
    clf = sum(r);% linearly combine the ratio classifiers in r to the final classifier
    [c,index] = max(clf);
    x = detectx.sampleImage.sx(index);
    y = detectx.sampleImage.sy(index);
    w = detectx.sampleImage.sw(index);
    h = detectx.sampleImage.sh(index);
    initstate = [x y w h];
    %% Show the tracking results
    imshow(uint8(imgSr));
    rectangle('Position',initstate,'LineWidth',4,'EdgeColor','r');
    hold on;
    text(5, 18, strcat('#',num2str(i)), 'Color','y', 'FontWeight','bold', 'FontSize',20);
    set(gca,'position',[0 0 1 1]); 
    pause(0.00001); 
    hold off;
    %% Extract samples 
    posx.sampleImage = sampleImgDet(img,initstate,trparams.init_postrainrad,1);
    negx.sampleImage =   
  sampleImg(img,initstate,1.5*trparams.srchwinsz,4+trparams.init_postrainrad,trparams.init_negnumtrain);
    %% Update all the features
    posx.feature = getFtrVal(iH,posx.sampleImage,ftr);
    negx.feature = getFtrVal(iH,negx.sampleImage,ftr); 
    [posx.mu,posx.sig,negx.mu,negx.sig] =     classiferUpdate(posx,negx,posx.mu,posx.sig,negx.mu,negx.sig,lRate);% update   distribution parameters  
    end
    end

将 List 作为参数从 C# 项目传递给 Matlab 函数

这是因为Runtracker根据错误需要 int 类型的参数,而您正在传递 int[] 类型的参数,即整数数组。

传递的参数和预期的参数应匹配,以便成功执行该方法。

希望这有帮助。