opencv学习笔记[10]:IplImage数据结构

1.IplImage数据结构

typedef struct _IplImage

{

    int  nSize;             /* sizeof(IplImage) */

    int  ID;                /* version (=0)*/

    int  nChannels;         /* Most of OpenCV functions support 1,2,3 or 4 channels */

    int  alphaChannel;      /* Ignored by OpenCV */

    int  depth;             /* Pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16S,

                               IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F are supported.  */

    char colorModel[4];     /* Ignored by 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;             /* Image width in pixels.                           */

    int  height;            /* Image height in pixels.                          */

    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 data size in bytes

                               (==image->height*image->widthStep

                               in case of interleaved data)*/

    char *imageData;        /* Pointer to aligned image data.         */

    int  widthStep;         /* Size of aligned image row in bytes.    */

    int  BorderMode[4];     /* Ignored by OpenCV.                     */

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

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

                               (not necessarily aligned) -

                               needed for correct deallocation */

}

IplImage;

掌握IplImage的几个重要的数据结构:

  • Width
  • Height
  • Depth: usually use IPL_DEPTH_*U/S/F
  • nChannals :can be valued 1,2,3,4
  • origin: only can be valued IPL_ORIGIN_TL or IPL_ORIGIN_BL
  • dataOrder: can be valued by IPL_DATA_ORDER_PIXEL or IPL_DATA_ORDER_PLANE
  • widthStep :临行同列之间的字节数

2.访问图像数据

利用指针遍历(矩阵遍历)的方法,如下程序是最高效的方法:

#include <cv.h>

#include <highgui.h>

 

//仅最大化hsv图像的 s 和 v 部分

//因为这是一个三通道图像 所以c通道在x行的位置是3*x+c

//与CvMat不同的是:IplImage的imageData结构

int saturate_sv(IplImage * img)

{

         if(img->nChannels!=3)

                   return (0);

         for(int y=0;y<img->height;y++)

         {

                   uchar * ptr = (uchar *)img->imageData + img->widthStep * y;

                   for(int x=0;x<img->width;x++)

                   {

                            ptr[3*x+1]=255;

                            ptr[3*x+2]=255;

                   }

         }

         return 1;

}

int main()

{

         cvNamedWindow("test-1");

         cvNamedWindow("test-2");

         IplImage * image=cvLoadImage("I:\\资料\\opencv相关资料\\OpenCV教程\\图像与视频\\airplane.png");

         cvShowImage("test-1",image);

 

         int result = saturate_sv(image);     

         cvShowImage("test-2",image);

 

         cvWaitKey();

         cvReleaseImage(&image);

         cvDestroyWindow("test-1"); 

         cvDestroyWindow("test-2");

         printf("%d",result);

}

 

3.ROI

下面的例子说明用imageROI来增加某范围的像素

int main()

{

       IplImage * src;

       if((src=cvLoadImage("I:\\资料\\opencv相关资料\\OpenCV教程\\图像与视频\\airplane.png",1)) != 0)

       {

              int x = 200;

              int y = 200;

              int width = 200;;

              int height = 200;

              int add = 150;

              cvSetImageROI(src,cvRect(x,y,width,height));

              cvAddS(src,cvScalar(add),src);

              cvResetImageROI(src);

              cvNamedWindow("roi-add");

              cvShowImage("roi-add",src);

              cvWaitKey();

       }

       return 0;

}

 

其他方法:

#include <cv.h>

#include <highgui.h>

 

int main()

{

       IplImage * interest_img = cvLoadImage("I:\\资料\\opencv相关资料\\OpenCV教程\\图像与视频\\airplane.png");

       CvRect  interest_rect = cvRect(100,100,100,100);

             

       IplImage * sub_img = cvCreateImageHeader(cvSize(interest_rect.width,interest_rect.height),interest_img->depth,interest_img->nChannels);

 

       sub_img->origin = interest_img->origin;

       sub_img->widthStep = interest_img->widthStep;

       sub_img->imageData = interest_img->imageData + interest_rect.x * interest_img->nChannels + interest_rect.y* interest_img->widthStep;

      

       cvAddS(sub_img,cvScalar(1),sub_img);

 

       cvNamedWindow("roi-add");

       cvShowImage("roi-add",sub_img);

       cvWaitKey();

       cvReleaseImageHeader(&sub_img);

}

posted on 2013-03-05 15:28  0fengfan0  阅读(2224)  评论(0编辑  收藏  举报

导航