【问题标题】:Matlab to openCV code conversionMatlab 到 openCV 代码转换
【发布时间】:2014-03-01 15:20:57
【问题描述】:

我正在尝试将 MatLab 中的代码转换为 OpenCV,但由于我不太了解编程,所以我对以下几行感到困惑

MatLab 代码:

[indx_row, indx_col] = find(mask ==1);
Indx_Row = indx_row;
Indx_Col = indx_col;

for ib = 1:nB;
    istart = (ib-1)*n + 1;
    iend   = min(ib*n, N);
    indx_row = Indx_Row(istart:iend);
    indx_col = Indx_Col(istart:iend); 

openCV 代码:

vector <Point> index_rowCol;
for(int i=0; i<mask.rows; i++)
{
    for(int j=0; j<mask.cols; j++)
    {
        if( mask.at<float>(i,j) == 1 )
        {
            Point pixel;
            pixel.x = j;
            pixel.y = i;

            index_rowCol.push_back(pixel);
        }
    }
}


//Code about the "for loop" in MatLab code
for(int ib=0 ; ib<nB; ib++)
{
    int istart = (ib-1)*n; 
    int iend = std::min( ib*n, N );

    index_rowCol.clear();// Clearing the "index_rowCol" so that we can fill it again from "istart" to "iend"4
    for(int j = istart; j<iend; j++)
    {
        index_rowCol.push_back( Index_RowCol[j] );
    }
}

我无法理解是否可以?

【问题讨论】:

  • 你能解释一下你想用这个 Matlab 代码做什么吗?使用 OpenCV 可能有更简单的方法来实现这一点。

标签: matlab opencv


【解决方案1】:

我认为min函数的使用有误。 这里

for ib = 1:nB;
    istart = (ib-1)*n + 1;
    iend   = min(ib*n, N);

ib - 是数组 [1,2,3..nB],你将每个值与 N 进行比较。结果你也得到了数组。

因此结果: ib - 是数组,istart - 是数组,iend 也是数组。

在 C++ 实现中

for(int ib=0 ; ib<nB; ib++)
{
    int istart = (ib-1)*n; 
    int iend = std::min( ib*n, N );

您使用标量(这里 ib、istars 和 iend 是标量)。

为了更好地理解上述代码的工作原理,请使用逐步调试。 (设置断点并运行代码然后按(F10 key-for matlab))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    • 2012-07-25
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    相关资源
    最近更新 更多