【问题标题】:OpenCV: Mat::reshape() does nothing [duplicate]OpenCV:Mat::reshape() 什么都不做[重复]
【发布时间】:2017-06-30 09:30:33
【问题描述】:

我有一个 640 x 480 CV_8UC3 Mat image 并且想要执行 k-means 分割。所以我需要把它转换成CV_32F(这里没问题),重塑它,然后运行k-means。

问题是reshape() 什么都不做:

cv::Mat colorMat;
image.convertTo (colorMat, CV_32FC3);
std::cout << colorMat.size () << "; ch: " << colorMat.channels () << std::endl; // [640 x 480]; ch: 3
colorMat.reshape (1, colorMat.rows * colorMat.cols); // Here I expect it to become [307200 x 3], 1 channel - each column representing a color component
std::cout << colorMat.size () << "; ch: " << colorMat.channels () << std::endl; // [640 x 480]; ch: 3

我做错了吗?

【问题讨论】:

    标签: c++ opencv


    【解决方案1】:

    你需要将reshape的结果赋值给一个矩阵:

    colorMat = colorMat.reshape (1, colorMat.rows * colorMat.cols);
    

    你可以看here(第二个sn-p)一个完整的例子。

    【讨论】:

    • 是的,我只是在阅读this question 后写了一个类似的答案。他们显然缺乏文档中的返回值描述......
    • 其实doc表示返回值为Mat,但可能不够清楚。
    猜你喜欢
    • 2016-05-07
    • 2019-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 2016-03-31
    相关资源
    最近更新 更多