当我试图访问3D简历::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;编辑:
将代码更新为以下内容,使我可以为数组下标编译错误类型‘’,
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;
}编译错误位于我使用[]访问通道索引的位置。我想这不是接入频道的正确方式。
发布于 2017-10-16 05:17:10
使用多个通道访问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;
}多亏了这个answer。
https://stackoverflow.com/questions/46738031
复制相似问题