[英]How to access indexes of a 3D CV::Mat
尝试访问3D CV :: Mat的索引时遇到分段错误。 代码如下,
int channel = 3;
int sizes[] = { imageheight, imageWidth};
CV::Mat test(2, sizes, CV_8UC3)
for(int i=0;i<image2D->size();i++)
{
Point2D &_point = image2D->at(i);
test.at<unsigned char>(_point.y,_point.x,0) = _point.rgb.r;
test.at<unsigned char>(_point.y,_point.x,1) = _point.rgb.g;
test.at<unsigned char>(_point.y,_point.x,2) = _point.rgb.b; // Segmentation fault in this line
}
以下方法不会崩溃,但会输出黑色图像。 我不确定我是否做得正确,
unsigned char *ptest = test.ptr<unsigned char>(_point.y);
ptest[channel*_point.x+ 0] = _point.rgb.r;
ptest[channel*_point.x+ 1] = _point.rgb.g;
ptest[channel*_point.x+ 2] = _point.rgb.b;
编辑:
将代码更新为以下代码,使我可以为数组下标编译错误无效类型'unsigned char [int]' ,
Matrix test(imageheight, imageWidth, CV_8UC3);
for(int i=0;i<image2D->size();i++)
{
Point2D &_point = image2D->at(i);
// Compile error on the below 3 lines.
test.at<unsigned char>(_point.y, _point.x)[0] = _point.rgb.b;
test.at<unsigned char>(_point.y, _point.x)[1] = _point.rgb.g;
test.at<unsigned char>(_point.y, _point.x)[2] = _point.rgb.r;
}
编译错误在我使用[]访问通道索引的位置。 我想这不是访问频道的正确方法。
通过多个渠道访问cv :: Mat的最简单方法,
cv::Mat3b test(imageheight, imageWidth, CV_8UC3);
for(int i=0;i<image2D->size();i++)
{
Point2D &_point = image2D->at(i);
test.at<cv::Vec3b>(_point.y, _point.x)[0] = _point.rgb.b;
test.at<cv::Vec3b>(_point.y, _point.x)[1] = _point.rgb.g;
test.at<cv::Vec3b>(_point.y, _point.x)[2] = _point.rgb.r;
}
对于单通道cv :: Mat
cv::Mat test(imageheight, imageWidth, CV_32FC1);
for(int i=0;i<image2D->size();i++)
{
Point2D &_point = image2D->at(i);
test.at<float>(_point.y, _point.x) = _point.r;
}
感谢这个答案 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.