gpt4 book ai didi

将图像矩阵大小复制到 OpenCV 中的另一个矩阵

转载 作者:太空宇宙 更新时间:2023-11-03 21:58:35 24 4
gpt4 key购买 nike

我正在尝试从文件中加载图像作为 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

最佳答案

如果不知道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.

关于将图像矩阵大小复制到 OpenCV 中的另一个矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17723422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com