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. WPF学习之路(三) 属性与依赖

    类型是DependencyProperty的属性是依赖属性 依赖属性不同于普通的.Net属性,类似于一个计算过程,根据依赖的值得到最终值. 为什么引入依赖属性: MSDN原文 One of the p ...

  2. 使用iText库创建PDF文件

    前言 译文连接:http://howtodoinjava.com/apache-commons/create-pdf-files-in-java-itext-tutorial/ 对于excel文件的读 ...

  3. Oracle 外连接和 (+)号的用法

    对于外连接,Oracle中可以使用“(+)”来表示,9i可以使用LEFT/RIGHT/FULL OUTER JOIN,下面将配合实例一一介绍.1. LEFT OUTER JOIN:左外关联 SELEC ...

  4. JS代码判断字符串中有多少汉字

    $("form").submit(function () { var content = editor.getContentTxt(); var sum = 0; re = /[\ ...

  5. Linux 性能优化工具 perf top

    1. perf perf 是一个调查 Linux 中各种性能问题的有力工具. NAME perf - Performance analysis tools for Linux SYNOPSIS per ...

  6. vs2013外接程序”VMDebugger”加载异常处理

    cmd → regedit HKEY_LOCAL_MACHINE → OFTWARE → Wow6432Node → Microsoft  → VisualStudio → 12.0 → AddIns ...

  7. 【mysql】高可用集群之MMM

    一.复制的常用拓扑结构 复制的体系结构有以下一些基本原则: (1)    每个slave只能有一个master: (2)    每个slave只能有一个唯一的服务器ID: (3)    每个maste ...

  8. android 滑动滚动条调节音量

    利用滚动条滑动控制音量: 定义: private SeekBar mseekBarvolume: 以下实现代码: //调节音量--begin------------------------- //音量 ...

  9. cookie和session详解

    cookie和session的区别 二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择,都纪录下来.当下次你再光临同 ...

  10. @RestController注解下返回到jsp视图页面

    spring4.1中添加了@RestController注解很方便,集成了@ResponseBody注解,无需再在每个方法前添加了..但是却发现个问题..之前用@Controller注解的时候经常会如 ...