Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 重塑OpenCV Mat失败_C++_Opencv_Matrix - Fatal编程技术网

C++ 重塑OpenCV Mat失败

C++ 重塑OpenCV Mat失败,c++,opencv,matrix,C++,Opencv,Matrix,我无法重塑我的cv::Mat 这是我的密码 #include "opencv2/core/core.hpp" #include "opencv2/contrib/contrib.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/objdetect/objdetect.hpp" #include <boost/algorithm/string

我无法重塑我的
cv::Mat

这是我的密码

#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include <boost/algorithm/string.hpp>
#include <stdlib.h>
#include <vector>
#include <string>
#include <assert.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <iterator>
#include <sstream>



int main(int argc, const char* argv[])
{
    std::cout << "Start\n";
    cv::Mat eigenvectors = (cv::Mat_<float>(2,2) << 4, 3, 2, 1);
    std::cout << "eigenvectors shape " << eigenvectors.size() << std::endl;
    cv::Mat reshaped = eigenvectors.reshape(4, 1);
    std::cout << reshaped.size() << std::endl;
    std::cout << reshaped << std::endl;
}
为什么我的程序声称拥有1x1矩阵,但却持有4x1矩阵的值?它只是在这个维度上这样做

当我扩展代码以包含这些测试时

reshaped = eigenvectors.reshape(1, 4);
std::cout << reshaped.size() << std::endl;
std::cout << reshaped << std::endl;
reshaped = eigenvectors.reshape(2, 2);
std::cout << reshaped.size() << std::
这是一个错误还是我做错了什么

编辑:


为了提高这个结果的谷歌相关性,我的另一个症状是,由于我的“重塑”,我也丢失了我的
Mat

请确保您完全理解
restrape()
的效果以及传递给该方法的参数的含义。以下是有关它的OpenCV文档:

Mat Mat::重塑(整数cn,整数行=0)常数

因此,在您的第一个代码片段中,您通过四个元素初始化了特征向量:4、3、2、1<代码>重塑()
为这些元素创建另一个具有给定大小参数的矩阵

cv::Mat reformated=特征向量。reformate(4,1)-这里是
[1x1]
4通道矩阵。所有元素都存储在
重塑矩阵的单个4通道元素中,正如输出所示。要创建具有所需行数和列数的矩阵,请相应地设置通道数和列数。例如,如果有一个大小为
[4x4]
的单通道矩阵,并且希望它有2行8列,则只需调用
重塑(1,2)


希望有帮助。

大多数人的困惑是,opencv reformate()函数不同于Matlab的reformate,在Matlab中,您可以手动提供新的numorrows和numocol。使用openCV,您只需提供新的图像维度(通道数)作为第一个参数,然后提供行数作为第二个参数


Open CV会自动为您计算出新列的数量。

这很奇怪:您的第一个代码片段对我来说很好。您确定您正在将(1,4)传递到
重塑
,而不仅仅是(4)?谢谢您的帮助。我显然是从错误的角度来看待这个问题的。当我重新塑造(4,1)
时,我想要一个有四列的矩阵,而不是四个“通道”(我对通道的理解是,它们就像是结合在一起形成颜色的东西,比如红色、蓝色、绿色,所以“大小”返回是有意义的)。是否有某种方法可以通过OpenCV来指定这一点,或者这是实现它的唯一方法?以防解决方案中不清楚这一点(我在查看时肯定错过了它)。我的错误是把“频道”和栏目搞混了。我通过传入
cn=1
并传入所需的行号解决了问题。
reshaped = eigenvectors.reshape(1, 4);
std::cout << reshaped.size() << std::endl;
std::cout << reshaped << std::endl;
reshaped = eigenvectors.reshape(2, 2);
std::cout << reshaped.size() << std::
[1 x 4]
[4; 3; 2; 1]
[1 x 2]
[4, 3; 2, 1]
Parameters:   

    cn – New number of channels. If the parameter is 0, the number of channels remains the same.
    rows – New number of rows. If the parameter is 0, the number of rows remains the same.