I'm getting segmentation fault while trying to access indexes of a 3D CV::Mat. The code is following,
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
}
The following way doesn't crash but it outputs a black image. I'm not sure if I'm doing it correctly,
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;
EDIT:
Updated the code to the following gives me compile error invalid types ‘unsigned char[int]’ for array subscript,
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;
}
Compile error is in the position where I access the channel index with []. I guess this is not the right way to access channels.