迭代垫
Iterating through Mat
我在遍历 OpenCV 的坐标时遇到问题 Mat
:
cv::Mat picture = cv::Mat(depth.rows, depth.cols, CV_32F);
for (int y = 0; y < depth.rows; ++y)
{
for (int x = 0; x < depth.cols; ++x)
{
float depthValue = (float) depth.at<float>(y,x);
picture.at<float>(y, x) = depthValue;
}
}
cv::namedWindow("picture", cv::WINDOW_AUTOSIZE);
cv::imshow("picture", picture);
cv::waitKey(0);
结果图片:
之前(深度)
之后(图片)
看起来是
1.缩放和
2.停在宽度的三分之一左右。有什么想法吗?
看起来你的深度图像有 3 个通道。
BW 图像 (B=G=R)
的所有通道值都相同,因此您有 BGRBGRBGR
而不是 GrayGrayGray
,并且您试图访问它,因为它是 1 个通道,即为什么图像被水平拉伸 3 倍。
在 运行 循环之前尝试 cv::cvtColor(depth,depth,COLOR_BGR2GRAY)
。
你的迭代代码是正确的。
相反,错误的是 cv::Mat depth
类型假设。
正如建议的那样,根据失真,这可能是 CV_U8C3
。
要获取此类 CV_8UC3
矩阵的像素值,您可以使用:
cv::Vec3i depthValue = depth.at<cv::Vec3i>(y,x);
然后用这个标量做任何你想做的事。
例如,如果您的 depth
是 CV_8UC3
类型,并且距离编码在前两个字节中(MSB 在前),您可以通过以下方式获得距离:
float distance = depthValue[0] * 255 + depthValue[1];
我在遍历 OpenCV 的坐标时遇到问题 Mat
:
cv::Mat picture = cv::Mat(depth.rows, depth.cols, CV_32F);
for (int y = 0; y < depth.rows; ++y)
{
for (int x = 0; x < depth.cols; ++x)
{
float depthValue = (float) depth.at<float>(y,x);
picture.at<float>(y, x) = depthValue;
}
}
cv::namedWindow("picture", cv::WINDOW_AUTOSIZE);
cv::imshow("picture", picture);
cv::waitKey(0);
结果图片:
之前(深度)
之后(图片)
看起来是 1.缩放和 2.停在宽度的三分之一左右。有什么想法吗?
看起来你的深度图像有 3 个通道。
BW 图像 (B=G=R)
的所有通道值都相同,因此您有 BGRBGRBGR
而不是 GrayGrayGray
,并且您试图访问它,因为它是 1 个通道,即为什么图像被水平拉伸 3 倍。
在 运行 循环之前尝试 cv::cvtColor(depth,depth,COLOR_BGR2GRAY)
。
你的迭代代码是正确的。
相反,错误的是 cv::Mat depth
类型假设。
正如建议的那样,根据失真,这可能是 CV_U8C3
。
要获取此类 CV_8UC3
矩阵的像素值,您可以使用:
cv::Vec3i depthValue = depth.at<cv::Vec3i>(y,x);
然后用这个标量做任何你想做的事。
例如,如果您的 depth
是 CV_8UC3
类型,并且距离编码在前两个字节中(MSB 在前),您可以通过以下方式获得距离:
float distance = depthValue[0] * 255 + depthValue[1];