2013-07-18 143 views
1

我试图从文件加载图像作为CvMat(如mat1)并尝试创建另一个矩阵(mat2)与正在读取的文件大小相同并尝试减去原始矩阵(mat1)的平均值并尝试将该值插入到新矩阵(mat2)中,但不幸的是我得到一个错误。有人可以告诉我为什么吗?我在OpenCV中使用C API。以下是代码将图像矩阵大小复制到OpenCV中的另一个矩阵

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

是你的mat1.type CV_32FC1? – William

+0

@威廉我不知道。我只是试图从图像文件加载它。你知道我怎么检查它的类型? mat1的值是0和-2 – user227666

+3

你可以尝试'mat1-> type()'而不是'CV_32FC1'吗? – JonesV

回答

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. 
+0

您的第一个建议工程,但不是你的第二个,它显示错误 – user227666

2

cvCreateMat(行的cols)

这样,你会不会需要

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

交换的宽度和高度道具周围。

错误是因为您的目标(mat2)图像中的宽度高度尺寸错误或通道数量错误。我猜CvScalar返回1频道图像。所以它可能是尺寸。