matlab - Matlab到openCV代码转换

标签 matlab opencv

我正在尝试将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] );
    }
}

我不知道是否可以吗?

最佳答案

我认为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键-对于Matlab))

关于matlab - Matlab到openCV代码转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22093413/

相关文章:

python - 如何在应用轮廓后从图像中提取文本

c++ - Boost/OpenCV 错误:不匹配调用 '(boost::_mfi::dm<void(cv::Mat*, cv::VideoCapture*), Recorder>)

matlab - 如何更改箱线图中轴的字体大小和线宽?

c++ - MATLAB中如何调用命令程序

python - 如何区分Python或Matlab是否错误/故障?

matlab - 查找一对值出现的次数

plot - 使用副标题覆盖 ('visible' , 'off' ) matlab 中的图形属性

python - 为什么我的 3D numpy 数组中的值在我将其写入文件时会发生变化?

python - Cap.read()是否从文件流中跳过相机流中的帧,但不跳过?

c++ - 使用 GPU (C++/OpenCV) 显示文件中的图像