【opencv】图像数字化——认识OpenCV中的Mat类( 7 访问多通道Mat对象中的值)

本文介绍了OpenCV中多通道Mat对象的几种访问方式,包括使用at()成员函数、ptr()成员函数、isContinuous()和ptr()结合、利用step和data成员变量,以及通道的分离和合并操作split()和merge()。这些方法对于处理和操作多通道图像数据至关重要。
摘要由CSDN通过智能技术生成

7 访问多通道Mat对象中的值

7.1使用成员函数at()

#include <opencv2/core/core.hpp>  
#include<iostream>  
using namespace std;
using namespace cv;
int main()
{
    Mat mm = (Mat_<Vec3f>(2, 2) << Vec3f(1, 11, 21), Vec3f(2, 12, 32), Vec3f(3, 13, 23), Vec3f(4, 24, 34));
    cout << mm << endl;
    for (int r = 0; r < mm.rows; r++) {
        for (int c = 0; c < mm.cols; c++) {
            cout << mm.at<Vec3f>(r, c) << ",";
        }
        cout << endl;
    }
    return 0;
}

在这里插入图片描述

7.2使用成员函数ptr()

#include <opencv2/core/core.hpp>  
#include<iostream>  
using namespace std;
using namespace cv;
int main()
{
    Mat mm = (Mat_<Vec3f>(2, 2) << Vec3f(1, 11, 21), Vec3f(2, 12, 32), Vec3f(3, 13, 23), Vec3f(4, 24, 34));
    cout << mm << endl;
    for (int r = 0; r < mm.rows; r++) {
        //每行首元素的地址
        Vec3f* ptr = mm.ptr<Vec3f>(r);
        for (int c = 0; c < mm.cols; c++) {
            cout << ptr[c] << ",";
        }
        cout << endl;
    }
    return 0;
}

在这里插入图片描述

7.3使用成员函数isContinuous()和ptr()

#include <opencv2/core/core.hpp>  
#include<iostream>  
using namespace std;
using namespace cv;
int main()
{
    Mat mm = (Mat_<Vec3f>(2, 2) << Vec3f(1, 11, 21), Vec3f(2, 12, 32), Vec3f(3, 13, 23), Vec3f(4, 24, 34));
    cout << mm << endl;
   if(mm.isContinuous()){
       //指向多通道矩阵的第一个元素的指针
        Vec3f* ptr = mm.ptr<Vec3f>(0);
        for (int c = 0; c < mm.cols*mm.rows; c++) {
            cout << ptr[c] << endl;
        }
    }
    return 0;
}

在这里插入图片描述

7.4使用成员变量step和data

#include <opencv2/core/core.hpp>  
#include<iostream>  
using namespace std;
using namespace cv;
int main(){
    Mat mm = (Mat_<Vec3f>(2, 2) << Vec3f(1, 11, 21), Vec3f(2, 12, 32), Vec3f(3, 13, 23), Vec3f(4, 24, 34));
    cout << mm << endl;
  for(int r=0;r<mm.rows;r++){
      for(int c=0;c<mm.cols;c++){
            Vec3f* ptr = (Vec3f*)(mm.data + mm.step[0] * r + c * mm.step[1]);
            cout << *ptr << ",";
        }
        cout << endl;
    }
    return 0;
}

在这里插入图片描述

7.5分离通道split(mm,vec)

  • 将所有向量第一个值组成的单通道矩阵作为第一通道,以此类推
#include <opencv2/core/core.hpp>  
#include<iostream>  
using namespace std;
using namespace cv;
int main(){
    Mat mm = (Mat_<Vec3f>(2, 2) << Vec3f(1, 11, 21), Vec3f(2, 12, 32), Vec3f(3, 13, 23), Vec3f(4, 24, 34));
    cout << mm << endl;
    vector<Mat> planes;
    split(mm, planes);
    cout << planes[0] << endl;//第一个通道
    cout << planes[1] << endl;//第二个通道
    cout << planes[2] << endl;//第三个通道
    
    return 0;
}

在这里插入图片描述

7.6合并通道merge( , , )

  • 将单通道矩阵存储在Mat中:
#include <opencv2/core/core.hpp>  
#include<iostream>  
using namespace std;
using namespace cv;
int main(){
    Mat planes0 = (Mat_<int>(2, 2) << 1,2,3,4);
    Mat planes1 = (Mat_<int>(2, 2) << 5, 6, 7, 8);
    Mat planes2 = (Mat_<int>(2, 2) << 9, 10, 11, 12);
    Mat planes[] = { planes0 ,planes1 ,planes2 };
    Mat mat;
    merge(planes,3,mat);
    cout << mat << endl;
    return 0;
}

在这里插入图片描述

  • 存储在vector容器中,使用merge重载函数:
#include <opencv2/core/core.hpp>  
#include<iostream>  
using namespace std;
using namespace cv;
int main(){
    Mat planes0 = (Mat_<int>(2, 2) << 1,2,3,4);
    Mat planes1 = (Mat_<int>(2, 2) << 5, 6, 7, 8);
    Mat planes2 = (Mat_<int>(2, 2) << 9, 10, 11, 12);
    vector<Mat> plane;
    plane.push_back(planes0);
    plane.push_back(planes1);
    plane.push_back(planes2);
    Mat mat;
    merge(plane, mat);
    cout << mat << endl;
    return 0;
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值