OpenCV笔记-读取和修改图像数据

图像结构IplImage:

typedef struct _IplImage

{

    int  nSize;             /* 结构体大小(单位字节) */

    int  ID;                /* 版本 (=0)*/

    int  nChannels;         /* 通道数,大多数OpenCV函数支持1,2,3,4通道 */

    int  alphaChannel;      /* OpenCV忽略 */

    int  depth;             /* 像素位深即每个像素所占的位数:支持IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16S,

                               IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F */

    char colorModel[4];     /* OpenCV忽略 */

    char channelSeq[4];     /* ditto */

    int  dataOrder;         /* 0 - interleaved color channels, 1 - separate color channels.

                               cvCreateImage can only create interleaved images */

    int  origin;            /* 0 - top-left origin,

                               1 - bottom-left origin (Windows bitmaps style).  */

    int  align;             /* Alignment of image rows (4 or 8).

                               OpenCV ignores it and uses widthStep instead.    */

    int  width;             /* 图像宽度(单位像素)                           */

    int  height;            /* 图像高度(单位像素)                          */

    struct _IplROI *roi;    /* Image ROI. If NULL, the whole image is selected. */

    struct _IplImage *maskROI;      /* Must be NULL. */

    void  *imageId;                 /* "           " */

    struct _IplTileInfo *tileInfo;  /* "           " */

    int  imageSize;         /* 图像数据字节数

                               (等于image->height*image->widthStep

                               注意字节对齐)*/

    char *imageData;        /* 对齐的图像数据指针         */

    int  widthStep;         /* 对齐的图像数据每行字节数    */

    int  BorderMode[4];     /* OpenCV忽略                    */

    int  BorderConst[4];    /* Ditto.                                 */

    char *imageDataOrigin;  /* Pointer to very origin of image data

                               (not necessarily aligned) -

                               needed for correct deallocation */

}

IplImage;

例子:

#include "highgui.h"

#include <cstdio>



int main()

{

    IplImage *image = cvLoadImage("EW.bmp");//加载图像

    if(image == NULL)

    {//加载图像失败

        printf("加载图像出错!\n");

        return 1;

    }

    cvNamedWindow("读取和修改图像数据");//创建一个命名窗口



    uchar *pchar;

    int width = image->width;//读取图像宽度

    int height = image->height;//读取图像高度

    int channel = image->nChannels;//读取图像通道数

    int widthStep = image->widthStep;//读取图像一行像素的字节数



    int i, j;

    for(i = 0; i < height; ++i)

    {

        pchar = (uchar*)image->imageData + i * widthStep;

        for(j = 0; j < width; ++j)

        {

            uchar *temp = pchar + j * channel;

            temp[0] += 50;//通道B

            temp[1] += 50;//通道G

            temp[2] += 50;//通道R

        }

    }



    cvShowImage("读取和修改图像数据", image);

    cvWaitKey(0);

    cvReleaseImage(&image);

    cvDestroyWindow("读取和修改图像数据");

    return 0;

}

 

你可能感兴趣的:(opencv)