【发布时间】:2015-03-20 19:37:27
【问题描述】:
我正在尝试在 c++ 中创建此代码/我正在使用 openCV,但不必这样做。
wi = size(Gr, 2);
he = size(Gr, 1);
cropFactor = 0.10;
[x, y] = meshgrid( round(cropFactor * wi):round( (1-cropFactor)*wi ), round(cropFactor * he):round((1-cropFactor)*he) );
xy = sub2ind(size(Gr), y(:), x(:));
这是我目前所拥有的
int width = dst.cols;
int height = dst.rows;
double cropFactor = 0.10;
cv::Mat1i X,Y;
Utilities::Meshgrid(Utilities::MatlabRound(cropFactor * width), Utilities::MatlabRound((1 - cropFactor) * width), Utilities::MatlabRound(cropFactor * height), Utilities::MatlabRound((1-cropFactor) * height),X, Y);
Utilities::Sub2Ind(width, height, X, Y);
round() 函数
int Utilities::MatlabRound(double numberToRound)
{
return floor( numberToRound + 0.5);
}
这是我的 meshgrid() 函数,它按预期工作
void Utilities::Meshgrid(int startX, int endX, int startY, int endY, cv::Mat1i &X, cv::Mat1i & Y)
{
std::vector<int> vec_x, vec_y;
for (int i = startX; i <= endX; i++)
{
vec_x.push_back(i);
}
for (int i = startY; i <= endY; i++)
{
vec_y.push_back(i);
}
cv::Mat x = cv::Mat(vec_x);
cv::Mat y = cv::Mat(vec_y);
cv::repeat(x.reshape(1,1), y.total(), 1, X);
cv::repeat(y.reshape(1,1).t(), 1, x.total(), Y);
}
但是我无法理解什么是下标以及如何实现 Sub2Ind 函数
你能解释一下吗?
更新我已经实现了 sub2ind 请看我的回答
【问题讨论】:
-
您应该小心这一点,因为 Matlab 逐列存储矩阵,而 C++ 逐行存储它们。所以
sub2ind必须考虑到这种差异... -
@Shai 谢谢我知道,你能解释一下 Sub2Ind 是如何工作的吗?
-
我创建了 sub2Ind 函数,该函数适用于我的 openCV 案例,希望对其他人有所帮助
-
请发布您的工作解决方案作为答案。
-
@Shai 好的我已经发布了我的实现作为答案