2017-02-12 154 views
0

我有一个640 x 480 CV_8UC3Mat image并且想要执行k均值分割。所以我需要将它转换为CV_32F(这里没有问题),重塑它,并运行k-means。OpenCV:Mat :: reshape()什么都不做

问题是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 

我做错什么了吗?

回答

1

您需要的reshape结果分配给一个矩阵:

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

你可以看到here(第二片段)一个完整的例子。

+0

是的,我只是写了一个类似的答案阅读[这个问题](http://stackoverflow.com/questions/13400239/reshaping-a-matrix-failed-in-opencv-2-4-3)。他们在文档中显然缺少返回值描述... – Alex

+0

其实[doc](http://docs.opencv.org/master/d3/d63/classcv_1_1Mat.html#a4eb96e3251417fa88b78e2abd6cfd7d8)显示返回值是一个' Mat',但可能还不够清楚。 – Miki