1

ファイルから画像を CvMat として (mat1 として) 読み込もうとしており、読み取られているファイルと同じサイズの別の行列 (mat2) を作成しようとしており、元の行列 (mat1) の平均値を減算しようとしています。新しいマトリックス(mat2)に値を挿入しますが、残念ながらエラーが発生しています。理由を教えてもらえますか?OpenCVでC APIを使用しています。以下はコードです

   CvMat* mat1 = cvLoadImageM(argv[2], CV_LOAD_IMAGE_UNCHANGED); // load an image from a file as a CvMat 

   CvMat*  mat2 = cvCreateMat(mat1->width, mat1->height, CV_32FC1); // trying to create a matrix as same width and height as the image file being loaded.

   CvScalar avg = cvAvg(mat1); // calculating the avg of all elements of matrix

   cvSubS(mat1, avg, mat2); // subtracting the average value from the original matrix and inserting the new values to matrix mat2

エラーは

   OpenCV Error: Assertion failed (src1.size == dst.size && src1.channels() == dst.channels()) in cvAddS, file /home/Documents/opencv-2.4.5/modules/core/src/arithm.cpp, line 2783 terminate called after throwing an instance of 'cv::Exception' what():  /homeDocuments/opencv-2.4.5/modules/core/src/arithm.cpp:2783: error: (-215) src1.size == dst.size && src1.channels() == dst.channels() in function cvAddS
4

2 に答える 2

4

の型がわからない場合はmat1、 を使用mat1->typeして取得できます。

以下を使用してみてください。

CvMat*  mat2 = cvCreateMat(mat1->width, mat1->height, mat1->type); // trying to create a matrix as same width and height as the image file being loaded.
于 2013-07-18T13:21:11.147 に答える
2

cvCreateMat(行、列)

そのため、必要ありません

CvMat*  mat2 = cvCreateMat(mat1->height, mat1->width, CV_32FC1); 

幅と高さの小道具を交換します。

エラーは、ターゲット (mat2) 画像の幅と高さの寸法が間違っているか、チャンネル数が間違っているためです。CvScalar は 1 チャンネルの画像を返すと思います。そのため、おそらく寸法です。

于 2013-07-18T13:21:31.683 に答える