Opencv Mat矩陣的一些基本操作和遇過的問題

1、Mat矩陣值傳遞的時候,如果在函數內部類型發生改變時候,傳出來的數據會變化,所以需要在函數內部重新複製一塊區域給他,才能傳出想要的Mat

void getDes(Mat& des)
{
	temp = Mat(10,10,CV_32FC1);
	temp.copyTo(des);
}
void main()
{
	Mat descriptor;
	getDes(descriptor);
}

2、使用colRange()和rowRange()對矩陣進行擴充(https://blog.csdn.net/mikedadong/article/details/51305640

    Mat comMatR(Mat Matrix1,Mat Matrix2,Mat &);//函數聲明
    Mat comMatC(Mat Matrix1,Mat Matrix2,Mat &);//函數聲明

    //comMatR(conbine matrix as row):combine  Matrix1 and Matrix2 to MatrixCom as row ,just as the matlab expression :MatrixCom=[Matrix1 Matrix1]
    Mat comMatR(Mat Matrix1,Mat Matrix2,Mat &MatrixCom)
    {

        CV_Assert(Matrix1.rows==Matrix2.rows);//行數不相等,出現錯誤中斷    
        MatrixCom.create(Matrix1.rows,Matrix1.cols+Matrix2.cols,Matrix1.type());
        Mat temp=MatrixCom.colRange(0,Matrix1.cols);
        Matrix1.copyTo(temp);
        Mat temp1=MatrixCom.colRange(Matrix1.cols,Matrix1.cols+Matrix2.cols);
        Matrix2.copyTo(temp1);  
        return MatrixCom;
    }

    //comMatR(conbine matrix as col):combine  Matrix1 and Matrix2 to MatrixCom as rows ,just as the matlab expression :MatrixCom=[Matrix1;Matrix1]
    Mat comMatC(Mat Matrix1,Mat Matrix2,Mat &MatrixCom)
    {   
        CV_Assert(Matrix1.cols==Matrix2.cols);//列數不相等,出現錯誤中斷    
        MatrixCom.create(Matrix1.rows+Matrix2.rows,Matrix1.cols,Matrix1.type());
        Mat temp=MatrixCom.rowRange(0,Matrix1.rows);
        Matrix1.copyTo(temp);
        Mat temp1=MatrixCom.rowRange(Matrix1.rows,Matrix1.rows+Matrix2.rows);
        Matrix2.copyTo(temp1);  
        return MatrixCom;
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章