OpenCVで疎な行列を作りたいと思います。置くか、行列要素のデータにアクセスOpenCv SparseMatrixを使うにはどうすればいいですか?
:
にはどうすればいいように、この行列のための基本的な操作を行うことができます。
乾杯
OpenCVで疎な行列を作りたいと思います。置くか、行列要素のデータにアクセスOpenCv SparseMatrixを使うにはどうすればいいですか?
:
にはどうすればいいように、この行列のための基本的な操作を行うことができます。
乾杯
Sets and Sparse Matrices、ページ23:OpenCVのに
スパース行列は、要素を格納するためのCvSetを使用します。 C++インタフェースを使用して
CvSparseMat* get_color_map(const IplImage* img)
{
int dims[] = { 256, 256, 256 };
CvSparseMat* cmap = cvCreateSparseMat(3, dims, CV_32SC1);
for(int i = 0; i < img->height; i++) for(int j = 0; j < img->width; j++)
{
uchar* ptr=&CV_IMAGE_ELEM(img,uchar,i,j*3);
int idx[] = {ptr[0],ptr[1],ptr[2]};
((int*)cvPtrND(cmap,idx))[0]++;
}
// print the map
CvSparseMatIterator it;
for(CvSparseNode *node = cvInitSparseMatIterator(mat, &iterator);
node != 0; node = cvGetNextSparseNode(&iterator))
{
int* idx = CV_NODE_IDX(cmap,node);
int count=*(int*)CV_NODE_VAL(cmap,idx);
printf(“(b=%d,g=%d,r=%d): %d\n”, idx[0], idx[1], idx[2], count);
}
return cmap;
}
より適切な場合があります。 documentation [1]のサンプルコードではモジュロ演算が不足しているため、失敗することに注意してください。
const int dims = 2;
int size[] = {3, 20}; // rows and columns if in two dimensions
SparseMat sparse_mat(dims, size, CV_32F);
for(int i = 0; i < 1000; i++) {
// create a random index in dims dimensions
int idx[dims];
for(int k = 0; k < dims; k++)
idx[k] = rand() % size[k];
sparse_mat.ref<float>(idx) += 1.f;
}
cout << "bottom right element @ (2,19) = " << sparse_mat.ref<float>(2,19) << "\n";
Mat dense;
sparse_mat.convertTo(dense, CV_32F);
cout << dense;
は、以下の出力を
bottom right element @ (2,19) = 19
[9, 23, 13, 26, 18, 13, 18, 15, 13, 17, 13, 18, 19, 6, 20, 20, 12, 15, 15, 15;
17, 17, 14, 16, 12, 14, 17, 15, 15, 18, 24, 18, 13, 22, 18, 11, 18, 22, 17, 15;
19, 16, 14, 10, 18, 19, 10, 17, 18, 15, 24, 22, 18, 18, 18, 23, 21, 16, 14, 19]
[1] OpenCVのリファレンスマニュアルを与えます。バージョン2.4.3。 2012、p。 46.