#include #include //opencv 常用矩阵操作 int main(int argc, char* argv[]) { //----------------------------------------- //0. MAT 定义 //----------------------------------------- //初始化时指定尺寸和数据类型 //尺寸:CvSize (int w=0, int h=0) //数据类型命名方式:CV_位宽|类型|通道数 //位宽:8、16、32、64 //类型:U S、 整数有无符号 F 浮点数 //通道数:Cxx 表示通道数目,如果没有,则是默认的单通道 cv::Mat a(cv::Size(6, 5), CV_8U); std::cout << "a = " << std::endl << a << std::endl; //初始化时有时也会跟上Scalar参数,表示给不同通道的赋值,取值依赖于之前定义的通道数 cv::Mat b(cv::Size(2, 2), CV_8UC2, cv::Scalar(100,200)); std::cout << "b = " << std::endl << b << std::endl; cv::Mat c(cv::Size(3,3), CV_8UC3, cv::Scalar(10,20,30)); std::cout << "c = " << std::endl << c << std::endl; //----------------------------------------- //1. MAT API 初始化 //----------------------------------------- cv::Mat mz = cv::Mat::zeros(cv::Size(5, 5), CV_8U); //全零 std::cout << "mz = " << std::endl << mz << std::endl; cv::Mat mo = cv::Mat::ones(cv::Size(5, 5), CV_8U); //全1矩阵 std::cout << "mo = " << std::endl << mo << std::endl; cv::Mat me = cv::Mat::eye(cv::Size(5, 5), CV_8U); //对角线为1的矩阵 std::cout << "me = " << std::endl << me << std::endl; //----------------------------------------- //3. MAT 矩阵加减运算 //----------------------------------------- cv::Mat msum = mo + me; std::cout << "msum =" << std::endl << msum << std::endl; cv::Mat msub = mo - me; std::cout << "msub = " << std::endl << msub << std::endl; //----------------------------------------- //4. MAT 矩阵乘法运算 //----------------------------------------- //----------------------------------------- //5. MAT 矩阵转置\求逆 //----------------------------------------- cv::Mat mt(cv::Size(4, 5), CV_8U, cv::Scalar(20)); cv::Mat mtt = mt.t(); std::cout << " mtt = " << std::endl << mtt << std::endl; cv::Mat me2 = cv::Mat::eye(cv::Size(5, 5), CV_32F); cv::Mat minv = me2.inv(); std::cout << "minv = " << std::endl << minv << std::endl; //----------------------------------------- //6. MAT 矩阵 均值 和 方差 //----------------------------------------- cv::Mat mean; cv::Mat stddev; meanStdDev(a, mean, stddev); std::cout << "mean : " << mean << std::endl; std::cout << "stddev : " << stddev << std::endl; //需要说明的是,如果src是多通道图像或多维矩阵, //则函数分别计算不同通道的均值与标准差, //因此返回值mean和stddev为对应维度的向量 meanStdDev(b, mean, stddev); std::cout << "mean : " << mean << std::endl; std::cout << "stddev : " << stddev << std::endl; //----------------------------------------- //7. MAT 设置单元矩阵 //----------------------------------------- cv::Mat mi(cv::Size(4,4), CV_8U); cv::setIdentity(mi, cv::Scalar::all(20)); std::cout << "mi = " << std::endl << mi << std::endl; getchar(); } |