我正在尝试将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/