opencv reshape函數說明


轉自http://blog.csdn.net/yang6464158/article/details/20129991

reshape有兩個參數:

其中,參數:cn為新的通道數,如果cn = 0,表示通道數不會改變。

參數rows為新的行數,如果rows = 0,表示行數不會改變。

注意:新的行*列必須與原來的行*列相等。就是說,如果原來是5行3列,新的行和列可以是1行15列,3行5列,5行3列,15行1列。僅此幾種,否則會報錯。

具體調用也很簡單,代碼如下所示:

 

[cpp]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. #include <iostream>  
  2. #include <opencv/cv.h>  
  3. #include <opencv/highgui.h>  
  4. int main()  
  5. {  
  6.     cv::Mat testMat = cv::Mat::zeros ( 5, 3, CV_8UC3 );  
  7.     std::cout << "size of testMat: " << testMat.rows << " x " << testMat.cols << std::endl;  
  8.     std::cout<<"testMat = "<<testMat<<std::endl;  
  9.     cv::Mat result = testMat.reshape ( 0, 3 );  
  10.     std::cout << " size of original testMat: " << testMat.rows << " x " << testMat.cols << std::endl;  
  11.     std::cout << " size of reshaped testMat: " << result.rows << " x " << result.cols << std::endl;  
  12.     std::cout << "result = " << result << std::endl;  
  13.     cv::waitKey(0);  
  14.     system("pause");  
  15.     return 0;  
  16. }  

結果如下:

 

 

比如說:下面的情況就會報錯:

 

[cpp]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. #include <iostream>  
  2. #include <opencv/cv.h>  
  3. #include <opencv/highgui.h>  
  4. int main()  
  5. {  
  6.     cv::Mat testMat = cv::Mat::ones ( 5, 3, CV_8UC3 );  
  7.     std::cout << "size of testMat: " << testMat.rows << " x " << testMat.cols << std::endl;  
  8.     std::cout<<"testMat = "<<testMat<<std::endl;  
  9.     cv::Mat result = testMat.reshape ( 0, 6 );  
  10.     std::cout << " size of original testMat: " << testMat.rows << " x " << testMat.cols << std::endl;  
  11.     std::cout << " size of reshaped testMat: " << result.rows << " x " << result.cols << std::endl;  
  12.     std::cout << "result = " << result << std::endl;  
  13.     cv::waitKey(0);  
  14.     system("pause");  
  15.     return 0;  
  16. }  

因為行和列的乘積不相等

 

結果如下:



我們在使用reshape的時候一定不能用定義的Mat類型賦給原來的類型,必須重新定義一個新類。

可以這樣:

 

[cpp]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. unsigned char v11[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };   
  2. cv::Mat A = cv::Mat(3, 4, CV_8U, v11);   
  3. cv::Mat B = A.reshape(1, 12);    

但不能這樣:

 

 

[cpp]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
    1. cv::Mat testMat = cv::Mat::zeros ( 5, 3, CV_8UC3 );  
    2. std::cout << "size of testMat: " << testMat.rows << " x " << testMat.cols << std::endl;  
    3.   
    4. testMat.reshape ( 0, 1 );  
    5. std::cout << " size of reshaped testMat: " << testMat.rows << " x " << testMat.cols << std::endl;  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM