網(wǎng)站首頁 編程語言 正文
在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
相關(guān)推薦
- 2022-04-05 macOS下安裝JDK11和配置環(huán)境變量
- 2021-10-04 Flutter輸入框TextField屬性及監(jiān)聽事件介紹_Android
- 2022-08-07 使用pd.merge表連接出現(xiàn)多余行的問題解決_python
- 2022-09-22 提高接口并發(fā)量,防止崩潰
- 2022-03-17 C語言判斷數(shù)是否為素?cái)?shù)與素?cái)?shù)輸出_C 語言
- 2022-04-05 Python利用prettytable庫輸出好看的表格_python
- 2024-03-22 【IDEA】maven項(xiàng)目刷新依賴的兩種方式
- 2021-12-11 Redis之sql緩存的具體使用_Redis
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支