1. 程式人生 > >Opencv中reshape函式要注意的細節

Opencv中reshape函式要注意的細節

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;

void PrintMat( Mat &_m)
{

    Mat m = Mat_<float>( _m );
    for ( int k1=0; k1<m.rows; k1++ )
    {
        for ( int k2=0; k2<m.cols; k2++ )
        {
            cout << m.at<float
>(k1,k2) << " "; } cout << endl; } cout << "m.rows: " << _m.rows << endl << "m.cols: " << _m.cols << endl; } int main() { Mat m = Mat::zeros(2,2,CV_32F); m.at<float>(0,0) = 1; m.at<float
>(0,1) = 2; m.at<float>(1,0) = 3; m.at<float>(1,1) = 4; m.reshape(0, 1); //沒有賦值給m,只是噹噹reshape的話,m的行列都不會顯示出來有改變 cout << "m:" << endl; PrintMat(m); cout << endl; cout << "m.reshape(0, 1):" << endl; m = m.reshape(0
, 1);//賦值給m的後,m的行列發生改變 cout << "m:" << endl; PrintMat(m) ; cout << endl; cout << "m.reshape(0, 4):" << endl; Mat m1 = m.reshape(0, 4); //也可以賦值給新的mat cout << "m1:" << endl; PrintMat(m1); cout << endl; return 0; }

執行結果:
這裡寫圖片描述