You can access the Image pixels in many ways:
1. One using the Inbuilt macro
2. One using the pointer to the image data
3. Getting the raw data from the image.

============================================================
Method 1: Using Inbuilt macro:
CV_IMAGE_ELEM( image_header, elemtype, y, x_Nc )

Suppose, we have 8-bit 1-channel image I (IplImage* img):

I(x,y) ~ CV_IMAGE_ELEM( img, uchar, y, x);

Suppose, we have 8-bit 3-channel image I (IplImage* img):

I(x,y)blue ~ CV_IMAGE_ELEM( img, uchar, y, x*3);
I(x,y)green ~ CV_IMAGE_ELEM( img, uchar, y, x*3+1);
I(x,y)red ~ CV_IMAGE_ELEM( img, uchar, y, x*3+2);

============================================================

Method 2: Using pointer to the image data:

Suppose, we have 8-bit 1-channel image I (IplImage* img):

I(x,y) ~ ((uchar*)(img->imageData + img->widthStep*y))[x]

Suppose, we have 8-bit 3-channel image I (IplImage* img):

I(x,y)blue ~ ((uchar*)(img->imageData + img->widthStep*y))[x*3]
I(x,y)green ~ ((uchar*)(img->imageData + img->widthStep*y))[x*3+1]
I(x,y)red ~ ((uchar*)(img->imageData + img->widthStep*y))[x*3+2]

============================================================

Method 3: Using the raw data:
uchar *data;
cvGetRawData(img, (uchar**)&data);

I(x, y) = data[x*img->Width + y]

对于rgb图像

读入像素RGB

int B = CV_IMAGE_ELEM(img, unsigned char, row, col*3+0);
int G = CV_IMAGE_ELEM(img, unsigned char, row, col*3+1);
int R = CV_IMAGE_ELEM(img, unsigned char, row, col*3+2);

写入像素RGB

CV_IMAGE_ELEM(img, unsigned char, row, col*3+0) = 90;

CV_IMAGE_ELEM(img, unsigned char, row, col*3+1) = 90;

CV_IMAGE_ELEM(img, unsigned char, row, col*3+2) = 90;

修改单个像素,再说得详细点,

1.3通道时:CV_IMAGE_ELEM(image, unsigned char, i, j*3+k) = gray_val;            //0<=k<3

2.单通道时:CV_IMAGE_ELEM(image, unsigned char, i, j) = gray_val;

3.通用方法:CvScalar s;
                 s=cvGet2D(img,i,j); // get the (i,j) pixel value

s.val[0]=111;         //单通道就只有这个有效
                 s.val[1]=111;
                 s.val[2]=111;
                 cvSet2D(img,i,j,s);//set the (i,j) pixel value

CV_IMAGE_ELEM的方法比cvGet2D,cvSet2D快了十倍左右(据说)

转自:http://blog.csdn.net/lkbwx/article/details/5548416

opencv 中对一个像素的rgb值或像素值进行操作的几个常用小办法【转】的更多相关文章

  1. 判断是否是同一人的方法——equals()?在Person类中提供一个比较的方法compare()返回boolean值?对象自己和自己比?

    判断是否是同一人的方法——equals() 不能直接用per1==per2,这不是对象内容的比较而是存放对象地址的值得比较 在Person类中提供一个比较的方法compare()返回boolean值 ...

  2. eclipse 中main()函数中的String[] args如何使用?通过String[] args验证账号密码的登录类?静态的主方法怎样才能调用非static的方法——通过生成对象?在类中制作一个方法——能够修改对象的属性值?

    eclipse 中main()函数中的String[] args如何使用? 右击你的项目,选择run as中选择 run configuration,选择arguments总的program argu ...

  3. opencv中CV_IMAGE_ELEM的用法读取每个像素

    可以使用OpenCV定义的宏来提取象素值假设灰度图像image,存取其i行j列的象素可以这样:CV_IMAGE_ELEM(image, uchar,y, x)如果是彩色图像就是CV_IMAGE_ELE ...

  4. 前端JQuery中获取一个div下的多个id值

    获取所有的Id值,方法是通过div.class获取全局的值,然后再提取具体的Id值 方法一:用for循环,因为$("div.class")获取的是一个数组,通过循环读取出数组中的每 ...

  5. spring boot-controller中的一个方法获取其他方法返回的值

    @RequestMapping("/test") public String getData() { return "redirect:/other";} re ...

  6. opencv中的.at方法

    opencv中的.at方法是用来获取图像像素值得函数: interpolation:差值 histogram:直方图

  7. 图像金字塔及其在 OpenCV 中的应用范例(下)

    前言 本文将主要讲解如何使用 OpenCV 实现图像分割,这也是图像金字塔在 OpenCV 中的一个重要应用. 关于图像分割 在计算机视觉领域,图像分割(Segmentation)指的是将数字图像细分 ...

  8. opencv中的更通用的形态学

    为了处理更为复杂的情况,opencv中还支持更多的形态学变换. 形态学名称 操作过程 操作名称 是否需要temp参数 开操作 open open(src)=先腐蚀,后膨胀  CV_MOP_OPEN 否 ...

  9. 从ACM中删除一个已经创建的Library

    从ACM中删除一个已经创建的Library,无法通过界面操作,须要手工从DB中删除.须要删除的表记录有: RECENTUPDATE 找到字段Name等于该libraryName的那条记录删除掉 del ...

随机推荐

  1. Sublime Text 收藏笔记

    Sublime Text:初学者不知道的那些事 转载自: http://blog.jobbole.com/23949/

  2. yii2 输出xml格式数据

    作者:白狼 出处:http://www.manks.top/yii2_xml_response.html.html本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文 ...

  3. Linux PHP5.3升级PHP5.5.33 (CentOS)

    由于要使用了laravel5.1,php要升级到5.5以上.具体环境是Aliyun Cent OS 7.0.由于阿里的yum源lastest只有5.4,laravel5.1必须php5.5,加了几个网 ...

  4. Linux多线程同步方式

    当多个线程共享相同的内存时,需要确保每个线程看到一致的数据视图,当多个线程同时去修改这片内存时,就可能出现偏差,得到与预期不符合的值.为啥需要同步,一件事情逻辑上一定是有序的,即使在并发环境下:而操作 ...

  5. ASP.NET Core 1.0 安装并发布到Centos 7.2 使用jexus 5.8.2

    安装运行环境 sudoyuminstall libunwind libicu 下载.net core https://www.microsoft.com/net/download 下载完后上传文件 安 ...

  6. ELF Format 笔记(一)—— 概述

    ilocker:关注 Android 安全(新手) QQ: 2597294287 ELF Object files 参与程序的链接和执行,从这两个角度分别有两种视图: ELF header 位于文件的 ...

  7. Android界面隐藏软键盘的探索(兼findViewById返回null解决办法)

    最近写的APP,老师说我的登陆界面虽然有ScrollView滑动,但用户体验不太好,因为软键盘会挡住输入框或登录button(小米Pad,横屏,当指定只能输入数字时没找到关闭系统自带键盘的下箭头). ...

  8. 自学一个月的java了

    不知道这篇博文有不有朋友看到.先自我介绍一下,硕士一年级下学期. 对编程感兴趣,硕士一年级下学期转学计算机专业,目前刚好一个月的时间.接触计算机也是刚好一个月的时间. 学习了java,javaweb. ...

  9. STL vector

    STL vector vector是线性容器,它的元素严格的按照线性序列排序,和动态数组很相似,和数组一样,它的元素存储在一块连续的存储空间中,这也意味着我们不仅可以使用迭代器(iterator)访问 ...

  10. 拓扑排序 POJ2367Genealogical tree[topo-sort]

    ---恢复内容开始--- Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4875   A ...