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

时间:2015-06-13 07:03:09

标签: c# matlab matlab-deployment

我试图从我的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; 

        }
    }

Matlab代码

    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

1 个答案:

答案 0 :(得分:0)

这是因为Runtracker期望根据错误的类型为int的参数,并且您传递的是int[]类型的参数,即整数数组。

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

希望这有帮助。