C++版OpenCV_01_图像数字化

1 篇文章 0 订阅

图像数字化,持续更新中

OpenCV中Mat类中的常用函数

  1. 读入图像
#include<opencv2/core.hpp>
#include<opencv2/highgui.hpp>
using namespace std;

int main(int argc,char*argv[])
{
    // 读入原始图像
    Mat img = imread(argv[1], IMREAD_ANYCOLOR);
    if (!img.dat)                   // 判断是否读入图像
        return -1;
    imshow("origin img", img);      // 显示图像
    waitKey(0);
    return 0;
}
  1. 创建Mat并查看Mat类中的成员变量和成员函数
// 创建Mat的语法函数
// Mat a = Mat(int rows,int cols,int type)
// Mat b = Mat(Size(int rows,int cols),int type)

#inlcude<open2/core/core.hpp>
using namespace cv;

int main()
{
    // 通过Mat生成矩阵
    Mat m = Mat(2,3,cv32FC(1));
    Mat m0 = Mat(Size(2,3),cv32FC(1));
    Mat m1,m2;
    // 通过creat()生成矩阵
    m1.creat(2,3,cv32FC1);
    m2.creat(Size(2,3),cv32FC1);
    Mat m3 = Mat::ones(2,3,cv32FC1);
    Mat m4 = Mat::zeros(2,3,cv32FC1);
    // 初始化小型矩阵
    Mat m5 = (Mat_<int>(2,3)<<1,2,3,4,5,6); // 初始化小型矩阵
    // 获取矩阵的行列
    cout<<"rows:"<<m5.rows<<endl;
    cout<<"cols:"<<m5.cols<<endl;
    Size size = m5.size();
    cout<<"cols:"<<m5.channels()<<endl;
    // total()获取行列的乘积
    cout<<"cols:"<<m5.total()<<endl;
    // 获取坐标轴个数
    cout<<"cols:"<<m5.dims()<<endl;
    // m.at<float>(r,c) 获取mat中的元素
    for (int i=0;i<m5.rows;++i)
    {
        for (int j=0;j<m5.cols;++j)
        {
            cout<<m.at<int>(i,j)<<",";
        }
        cout<<endl;
    }
    // m.at<type>(r,c) == m.at<type>(point(c,r))
    for (int i=0;i<m5.rows;++i)
    {
        for (int j=0;j<m5.cols;++j)
        {
            cout<<m.at<int>(point(i,j))<<",";
        }
        cout<<endl;
    }
    // ptr() 获取每一行的首地址
       for (int i=0;i<m5.rows;++i)
    {
        const int*p = m.ptr<int>(i);
        for (int j=0;j<m5.cols;++j)
        {
            cout<<p[j]
        }
        cout<<endl;
    }
    
    // 判断mat存储的内存空间是否连续
    if(m5.isContinuous())
    {
        int *ptr = m5.ptr<int>(0);
        for (int i=0;i<m5.rows*m5.cols;++n)
        {
            cout<<ptr[i]<<",";
        }
    }
    
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值