OpenCV Learning: 图像像素Mat操作方法2

原文写于2013年7月31日

1. at方法

使用at模板函数能访问某行某列的像素

例如:

img1.at<cv::Vec3b>(j,i)

访问其各个通道:

img1.at<cv::Vec3b>(j,i)[0]   b通道

img1.at<cv::Vec3b>(j,i)[1]   g通道

img1.at<cv::Vec3b>(j,i)[2]    r通道

2.ptr方法

使用ptr模板可以得到行数据的头指针

uchar * data=img1.ptr<uchar>(j);//得到j行指针(储存行数据的地址)

访问第i个像素各个通道:

本质是根据指针移步实现,根据图像结构来移步

data[i*img1.nchanles+0]

data[i*img1.nchanles+1]

data[i*img1.nchanles+2]

3.iterator方法(迭代器)

迭代器方法,类似一维数组的模式,利用迭代器操作,方便操作像素

cv::Mat_<cv::Vec3b>::iterator it=img1.begin<cv::Vec3b>();cv::Mat_<cv::Vec3b>::iterator it_end=img1.end<cv::Vec3b>();

访问像素:

(*it)[0] b通道

(*it)[1] g通道

(*it)[2] r通道

it++

// test12.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include <iostream>
using namespace std;
using namespace cv;
 
 
int _tmain(int argc, _TCHAR* argv[])
{
    Mat img1=imread("d:\\lena.jpg");//512*512的有点大了,不利于数据观察
    namedWindow("demo");
    imshow("demo",img1);
    int i,j;
    //方法1:at方法
    cout<<"方法1:at方法"<<endl;
    cout<<"-----------------------------------------------"<<endl;
    for(j=0;j<img1.rows;j++)//按照图像布局打印数值
    {
        for(i=0;i<img1.cols;i++) //注意J和I的参数顺序,表示第I行J列
        {
            cout<<(int)img1.at<cv::Vec3b>(j,i)[0];//b通道
            //cout<<" ";
            cout<<(int)img1.at<cv::Vec3b>(j,i)[1];//g通道
            //cout<<" ";
            cout<<(int)img1.at<cv::Vec3b>(j,i)[2];//r通道
            //cout<<" ";
            cout<<"\t";
 
        }
        cout<<endl;
    }
    
    cout<<"-----------------------------------------------"<<endl;
    //方法2:ptr方法
    cout<<"方法2:ptr方法"<<endl;
    cout<<"-----------------------------------------------"<<endl;
    for(j=0;j<img1.rows;j++)
    {
        uchar * data=img1.ptr<uchar>(j);//得到行指针(储存行数据的地址)
        for (i=0;i<img1.cols*img1.channels();i++)//行数据的长度为 列数*通道数
        {
            //仿照方式格式打印出来
            cout<<(int)data[i];b通道
            i++;
            cout<<(int)data[i];//g通道
            i++;
            cout<<(int)data[i];//r通道
            cout<<"\t";
 
 
        }
        cout<<endl;
    }
    cout<<"-----------------------------------------------"<<endl;
    cout<<"方法3:iterator方法"<<endl;//
    cout<<"-----------------------------------------------"<<endl;
    cv::Mat_<cv::Vec3b>::iterator it=img1.begin<cv::Vec3b>();
    cv::Mat_<cv::Vec3b>::iterator it_end=img1.end<cv::Vec3b>();
    for (i=0;it!=it_end;it++)//类似一维数组了
    {
        cout<<(int)(*it)[0];//b
        cout<<(int)(*it)[1];//g
        cout<<(int)(*it)[2];//r
        cout<<"\t";
        i++;
        if (i>0&&i%img1.cols==0)
        {
            cout<<endl;//
        }
 
    }
 
    cout<<"-----------------------------------------------"<<endl;
    waitKey(0);
    return 0;
 
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值