日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

OpenCV?reshape函數(shù)實(shí)現(xiàn)矩陣元素序列化_C 語言

更新時間: 2021-12-24 編程語言

在opencv中,reshape函數(shù)比較有意思,它既可以改變矩陣的通道數(shù),又可以對矩陣元素進(jìn)行序列化,非常有用的一個函數(shù)。

函數(shù)原型:

C++: Mat Mat::reshape(int cn, int rows=0) const

參數(shù)比較少,但設(shè)置的時候卻要千萬小心。

cn: 表示通道數(shù)(channels), 如果設(shè)為0,則表示保持通道數(shù)不變,否則則變?yōu)樵O(shè)置的通道數(shù)。

rows: 表示矩陣行數(shù)。 如果設(shè)為0,則表示保持原有的行數(shù)不變,否則則變?yōu)樵O(shè)置的行數(shù)。

首先設(shè)置一個初始矩陣:一個20行30列1通道的一個矩陣

int main()
{
    Mat data = Mat(20, 30, CV_32F);  //設(shè)置一個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);  //設(shè)置一個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);  //設(shè)置一個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;
}

可見,序列成列向量比行向量要麻煩一些,還得去計(jì)算出需要多少行。但我們可以先序列成行向量,再轉(zhuǎn)置

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);  //設(shè)置一個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;
}

從結(jié)果可以看出,列數(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);  //設(shè)置一個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)不能整除,就會報(bào)錯。

最后,我們再驗(yà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;
}

從結(jié)果看出,不管是變化成行向量還是列向量,opencv都是行序列化,即從左到右,從上到下,與matlab是不一樣的。

簡單的一個函數(shù),功能卻很強(qiáng)大!你會用了嗎?

原文鏈接:https://www.cnblogs.com/denny402/p/5035535.html

欄目分類
最近更新