zoukankan      html  css  js  c++  java
  • opencv 中对一个像素的rgb值进行操作的几个常用小办法

    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快了十倍左右(据说)

  • 相关阅读:
    WebService通过SOAP头进行身份验证
    【笔记】NIOS II spi详解
    【笔记】关于NIOS II PIO的几个问题
    【笔记】NIOS II Interval Timer Core详解
    【原创】基于NIOS II SPI的ads8321驱动
    【笔记】MATLAB一些常用函数
    【原创】解决on_chip_memory过小的问题,解决Unable to reach errno ...的问题
    【原创】基于FPGA的等精度测频方法(学习实验)
    【原创】等效采样状态机控制工程(测试通过,待完善说明书)
    【原创】基于NIOS II的ADS1256 SPI驱动
  • 原文地址:https://www.cnblogs.com/CVArt/p/2503478.html
Copyright © 2011-2022 走看看