2017-02-12 148 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