2013-07-18 140 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頻道圖像。所以它可能是尺寸。