我試圖從文件加載圖像作爲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
是你的mat1.type CV_32FC1? – William
@威廉我不知道。我只是試圖從圖像文件加載它。你知道我怎麼檢查它的類型? mat1的值是0和-2 – user227666
你可以嘗試'mat1-> type()'而不是'CV_32FC1'嗎? – JonesV