0
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;
EDIT:
以下にコードは私が配列添字のエラー無効なタイプの「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;
}
コンパイルエラーは[]でチャンネルインデックスにアクセスする位置にあります。私はこれがチャンネルにアクセスする正しい方法ではないと思う。
行列のコンストラクタの最初の引数はチャネルの数でなければならず、3ではなく3でなければなりません。MatrixではなくMatクラスを使用しているはずです。 –
質問を編集しました。行列はcv :: Matのtypedefです。最初の引数は 'sizes'配列のサイズでしょうか? – Tahlil
[docs](https://docs.opencv.org/3.1.0/d3/d63/classcv_1_1Mat.html)を確認してください。最初の引数は次元の数でなければなりません。編集:ちょうど私が最初のコメントのチャネルの数は、次元を意味したことを実現。あなたのsizes配列には、3番目のdimのチャンネル数も含まれているはずです。 –