我正在尝试将文件中的图像作为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
答案 0 :(得分: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.
答案 1 :(得分:2)
cvCreateMat(rows,cols)
因此你不需要
CvMat* mat2 = cvCreateMat(mat1->height, mat1->width, CV_32FC1);
交换宽度和高度道具。
错误是因为您在目标(mat2)图像中获得了错误的宽度高度尺寸或错误的通道数。我猜CvScalar会返回一个1通道图像。所以它可能是尺寸。