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 回答