Learn OPENCV Chapter III Iplimage types

Source: Internet
Author: User
Tags image line scalar

Iplimage type definition:

typedef struct _IPLIMAGE {int nSize;                /* sizeof (iplimage) */int ID;         /* Version (=0) */int nchannels;      /* Most of the OpenCV functions support for 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 is also a structure. There are several members:

Nchannels: Refers to the number of channels 1,2,3,4. such as grayscale can be expressed in 1 channels, RGB with 3 channels, RGBA with 44 channels, HSI with three channels

Depth refers to type ipl_depth_8u (8-bit unsigned type, range 0~255), ipl_depth_8s (8-bit signed type -128~ 127), ipl_depth_16s (16-bit signed), Ipl_depth_32s, Ipl_    depth_32f and ipl_depth_64f. The usual RGB type is represented by ipl_depth_8u

PS: Here for the type dedicated to the image Iplimage is to separate the number of channels and data types. For matrix type Cvmat It is represented together, such as CV_8UC1 (single-channel 8-bit unsigned) CV_32SC3 represents 3-channel 32-bit signed, and so on.


Widthstep: Refers to the number of bytes of the image line, such as the previous line of the pointer is Uchar *P1, then the corresponding next line pointer is P1+withstep

Note: Widthstep does not necessarily equal nchannels * width. For 3-channel ipl_depth_8u (that is, one pixel is represented by 3 bytes), Widthstep may not be equal to 3*cols.


ROI: Refers to regin of interest, which is the area of interest.  The energy of the area of interest is represented as a rectangle. In the iplimage where the area of interest is set, the ROI area is processed only when the image processing function in the OPENCV is used.

Set the method:

Cvapi (void) Cvsetimageroi (iplimage* image, Cvrect rect); This sets up an ROI region

Cvapi (void) Cvresetimageroi (iplimage* image); Reset ROI Area

such as: Iplimage *pimg = Cvloadimage ("1.jpg"); Cvsetimageroi (Pimg, Cvrect (100,200,400,400)); Cvnamedwindow ("Out");

Re-use Cvshowimage ("Out", PIMG); Only this portion of the image in the Cvrect (100,200,400,400) area of the image is displayed in the window out.

ImageData: The data part of the image, ImageData is a pointer to the memory address where the image data is stored

Iplimage related functions: Cvapi (iplimage*) cvloadimage (const char* filename, int iscolor cv_default (cv_load_image_color));

Cvapi (iplimage*) cvcreateimage (cvsize size, int depth, int channels)//dynamically allocates iplimage structure, and allocates data memory. ImageData Point

Cvapi (void) cvcreateimagedata (cvarr* arr)//Allocate only image data memory, directed by ImageData

Cvapi (iplimage*) cvcreateimageheader (cvsize size, int depth, int channels); Assign only iplimage structures

Cvapi (void) cvreleaseimage (iplimage** image)

Cvapi (void) cvreleasedata (cvarr* arr)

Cvapi (void) cvreleasedata (cvarr* arr)


Conversion of Cvmat and Iplimage Cvapi (iplimage*) cvgetimage (const cvarr* arr, iplimage* image_header);

Cvapi (cvmat*) cvgetmat (const cvarr* arr, cvmat* header, int* COI cv_default (NULL), int allownd cv_default (0));

Examples are as follows:

Iplimage *pimg = cvloadimage ("1.jpg");
Cvmat *pmat = Cvcreatematheader (Pimg->rows, Pimg->cols, cv_8uc3);  Here only the Cvmat structural body
cvgetmat (PIMG, PMat) is assigned;   Note at this time Cvmat and Iplimage share the data section ... If the release is one, the other will not have the data part
//... process 
cvreleaseimageheader (&pimg)  ;//release only iplimage structure
Cvreleasemat (&pmat);  Releasing Cvmat structure and data memory

The opposite conversion is almost.


Finally, note the storage structure of the image: in the memory that ImageData points to

For general RGB images, such as general color images read with Cvloadimage

The time of storage is staggered, the order of attention

BGRBGRBGR, (instead of RGB order)


Image + Operation function:

/* DST (mask) = SRC1 (mask) + SRC2 (mask), this is the addition of two images of the same size, data type, number of channels */
Cvapi (void)  Cvadd (const cvarr* SRC1, const CV arr* Src2, cvarr* DST,
                    const cvarr* Mask cv_default (NULL));

/* DST (mask) = src (mask) + value, this is a fixed value for the number of channels of the image Plus */
cvapi (void)  cvadds (const cvarr* SRC, cvscalar value, CvA rr* DST,
                     const cvarr* Mask cv_default (NULL));

For example: read a pair of RGB color images from 1.jpg, then the middle part of the image, r+20, G + +, B + 50

Example with a mask

#include <opencv.hpp>//If you use a lower version, such as opencv2.0 and below, the header file is replaced with Cv.h, cxcore.h, highgui.h #include <memory> using 

namespace Std;
	void Fun (Iplimage *pimg) {memset (pimg->imagedata,0,pimg->height * pimg->widthstep);
		for (int i= pimg->height*3/8;i<pimg->height*5/8;++i) {char *p = pimg->imagedata + i * pimg->widthstep;
		for (int j = 0;j<pimg->width;++j) {P[j] = 255;
	}}} int main () {Iplimage *pimg = cvloadimage ("F:\\images\\11.bmp");
	Cvshowimage ("in", PIMG); Cvscalar scalar = cvscalar (50,30,20);
	The storage order is BGR cvrect rect = Cvrect (PIMG-&GT;WIDTH/4,PIMG-&GT;HEIGHT/4,PIMG-&GT;WIDTH/2,PIMG-&GT;HEIGHT/2);
	Cvsetimageroi (Pimg,rect);
	Iplimage *pmask = Cvcreateimage (Cvsize (rect.width,rect.height), ipl_depth_8u,1);
	Fun (Pmask);
	Cvshowimage ("Mask", pmask);
	
	Cvwaitkey (0);
	Cvadds (Pimg,scalar,pimg,pmask);
	Output Cvresetimageroi (PIMG);
	Cvshowimage ("Out", PIMG);
	Cvwaitkey (0);
	Cvdestroyallwindows ();
	Cvreleaseimage (&AMP;PIMG);
return 0;
 }





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.