我正在尝试从文件中将图像加载为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

最佳答案

如果您不知道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.

10-06 08:50