我有一个640 x 480 CV_8UC3 Mat image并且想要执行 k 均值分割。所以我需要将它转换为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
Run Code Online (Sandbox Code Playgroud)
难道我做错了什么?
您需要将 的结果分配给reshape矩阵:
colorMat = colorMat.reshape (1, colorMat.rows * colorMat.cols);
Run Code Online (Sandbox Code Playgroud)
你可以在这里(第二个片段)看到一个完整的例子。