我有一个 Mat2d 矩阵,其中每个元素都是一个 2D vector 。例如:
[[x0, y0], [x1, y1]
[x2, y2], [x3, y3]]
我想将这些 vector 中的每一个乘以 Mat1d 相机矩阵:
[fx, 0, cx,
0, fy, cy,
0, 0, 1]
(每个 vector 代表网格中顶点的位置,我想将其从相机空间转换为像素空间。)
对于本例,所得矩阵为:
[[x0 * fx + cx, y0 * fy + cy], [x1 * fx + cx, y1 * fy + cy]
[x2 * fx + cx, y2 * fy + cy], [x3 * fx + cx, y3 * fy + cy]]
实现这一目标最直接、最有效的方法是什么?
这是我当前的方法:
Mat2d points = getMesh();
Mat1d cameraMtrx = getCameraMtrx();
for(int col = 0; col < points.cols; col++){
for(int row = 0; row < points.rows; row++){
points.at<Vec2d>(row, col).val[0] = points.at<Vec2d>(row, col)[0] * cameraMtrx.at<double>(0, 0) + cameraMtrx.at<double>(0, 2);
points.at<Vec2d>(row, col).val[1] = points.at<Vec2d>(row, col)[1] * cameraMtrx.at<double>(1, 1) + cameraMtrx.at<double>(1, 2);
}
}
最佳答案
OpenCV Documentation已经详细介绍了有效迭代 cv::Mat
的各种方法,在所提供的方法中,最有效的方法是使用 cv::LUT()
,但是从在这个问题的上下文中,我猜输入矩阵值的范围不固定,因此无法创建查找表,这对于 RGB 图像非常有帮助,因为我们知道事先最小值为0
,最大值为255
,因此我们可以轻松创建一个查找表,但在这个问题中,我们需要将两个矩阵相乘假设不是图像,所以我们会选择 The efficient way .
int cameraMatrix[] = {2, 0, 10, 0, 4, 20, 0, 0, 1};
cv::Mat mat(2, 2, CV_32FC2, cv::Scalar(100, 20));
cv::Size contSize = mat.size();
// Calculate the length of array if the input matrix was flatten, in case of continuous matrix only.
if (mat.isContinuous()) {
contSize.width *= contSize.height;
contSize.height = 1;
}
cv::Vec2f* ptr;
for (int i = 0; i < contSize.height; ++i)
{
ptr = mat.ptr<cv::Vec2f>(i);
for (int j = 0; j < contSize.width; ++j)
{
ptr[j] = cv::Vec2f(ptr[j].val[0]*cameraMatrix[0] + cameraMatrix[2], ptr[j].val[1] * cameraMatrix[4] + cameraMatrix[5]);
}
}
关于c++ - OpenCV:如何有效地将 Mat2d 矩阵的每个元素乘以 Mat1d 矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43239129/