OpenCV reshape函數(shù)實現(xiàn)矩陣元素序列化
在opencv中,reshape函數(shù)比較有意思,它既可以改變矩陣的通道數(shù),又可以對矩陣元素進行序列化,非常有用的一個函數(shù)。
函數(shù)原型:
C++: Mat Mat::reshape(int cn, int rows=0) const
參數(shù)比較少,但設置的時候卻要千萬小心。
cn: 表示通道數(shù)(channels), 如果設為0,則表示保持通道數(shù)不變,否則則變?yōu)樵O置的通道數(shù)。
rows: 表示矩陣行數(shù)。 如果設為0,則表示保持原有的行數(shù)不變,否則則變?yōu)樵O置的行數(shù)。
首先設置一個初始矩陣:一個20行30列1通道的一個矩陣
int main()
{
Mat data = Mat(20, 30, CV_32F); //設置一個20行30列1通道的一個矩陣
cout << "行數(shù): " << data.rows << endl;
cout << "列數(shù): " << data.cols << endl;
cout << "通道: " << data.channels() << endl;
system("pause");
return 1;
}
輸出:

第一次變化:通道數(shù)不變,將矩陣序列化1行N列的行向量。
int main()
{
Mat data = Mat(20, 30, CV_32F); //設置一個20行30列1通道的一個矩陣
cout << "行數(shù): " << data.rows << endl;
cout << "列數(shù): " << data.cols << endl;
cout << "通道: " << data.channels() << endl;
cout << endl;
Mat dst = data.reshape(0, 1);
cout << "行數(shù): " << dst.rows << endl;
cout << "列數(shù): " << dst.cols << endl;
cout << "通道: " << dst.channels() << endl;
system("pause");
return 1;
}

第二次變化:通道數(shù)不變,將矩陣序列化N行1列的列向量。
int main()
{
Mat data = Mat(20, 30, CV_32F); //設置一個20行30列1通道的一個矩陣
cout << "行數(shù): " << data.rows << endl;
cout << "列數(shù): " << data.cols << endl;
cout << "通道: " << data.channels() << endl;
cout << endl;
Mat dst = data.reshape(0, data.rows*data.cols);
cout << "行數(shù): " << dst.rows << endl;
cout << "列數(shù): " << dst.cols << endl;
cout << "通道: " << dst.channels() << endl;
system("pause");
return 1;
}

可見,序列成列向量比行向量要麻煩一些,還得去計算出需要多少行。但我們可以先序列成行向量,再轉置
Mat dst = data.reshape(0, 1); //序列成行向量 Mat dst = data.reshape(0, 1).t(); //序列成列向量
第三次變化:通道數(shù)由1變?yōu)?,行數(shù)不變。
int main()
{
Mat data = Mat(20, 30, CV_32F); //設置一個20行30列1通道的一個矩陣
cout << "行數(shù): " << data.rows << endl;
cout << "列數(shù): " << data.cols << endl;
cout << "通道: " << data.channels() << endl;
cout << endl;
Mat dst = data.reshape(2, 0);
cout << "行數(shù): " << dst.rows << endl;
cout << "列數(shù): " << dst.cols << endl;
cout << "通道: " << dst.channels() << endl;
system("pause");
return 1;
}

從結果可以看出,列數(shù)被分出一半,放在第二個通道里去了。
同理,如果通道數(shù)由1變?yōu)?,行數(shù)不變。則每通道的列數(shù)變?yōu)樵瓉淼娜种弧?/p>
需要注意的是,如果行保持不變,改變的通道數(shù)一定要能被列數(shù)整除,否則會出錯。
第四次變化:通道數(shù)由1變?yōu)?,行數(shù)變?yōu)樵瓉淼奈宸种弧?/span>
int main()
{
Mat data = Mat(20, 30, CV_32F); //設置一個20行30列1通道的一個矩陣
cout << "行數(shù): " << data.rows << endl;
cout << "列數(shù): " << data.cols << endl;
cout << "通道: " << data.channels() << endl;
cout << endl;
Mat dst = data.reshape(2, data.rows/5);
cout << "行數(shù): " << dst.rows << endl;
cout << "列數(shù): " << dst.cols << endl;
cout << "通道: " << dst.channels() << endl;
system("pause");
return 1;
}

可見,不管怎么變,都遵循這樣一個等式:
變化之前的? rows*cols*channels = 變化之后的 rows*cols*channels
我們只能改變通道數(shù)和行數(shù),列數(shù)不能改變,它是自動變化的。
但是要注意的是,在變化的時候,要考慮到是否整除的情況。如果改變的數(shù)值出現(xiàn)不能整除,就會報錯。
最后,我們再驗證一下:opencv在序列化的時候是行序列化還是列序列化呢?
我們知道,在matlab里面,是列序列化, 即取值為從上到下,從左到右,opencv又是怎么樣的呢
int main()
{
Mat data = (Mat_<int>(2, 3) << 1, 2, 3, 10, 20, 30); //2行3列的矩陣
cout << data << endl;
Mat dst1 = data.reshape(0, 6); //通道不變,序列成列向量
cout <<endl<< dst1 << endl;
Mat dst2 = data.reshape(0, 1); //通道不變,序列成行向量
cout << endl << dst2 << endl;
system("pause");
return 1;
}

從結果看出,不管是變化成行向量還是列向量,opencv都是行序列化,即從左到右,從上到下,與matlab是不一樣的。
簡單的一個函數(shù),功能卻很強大!你會用了嗎?
到此這篇關于OpenCV reshape函數(shù)實現(xiàn)矩陣元素序列化的文章就介紹到這了,更多相關OpenCV reshape的內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用c語言判斷100以內(nèi)素數(shù)的示例(c語言求素數(shù))
這篇文章主要介紹了使用c語言判斷100以內(nèi)素數(shù)的示例(c語言求素數(shù)),需要的朋友可以參考下2014-03-03

