OpenCV old version of Iplimage use

Source: Internet
Author: User
Tags image processing library

Original source: http://blog.csdn.net/ihadl/article/details/7403486

Iplimage structure

Since OPENCV is mainly aimed at the processing of computer vision, the most important structure in the library is the iplimage structure. The iplimage structure is sourced from Intel's other library of Intel Image Processing Library (IPL), which is primarily for image processing. The iplimage structure is specifically defined as follows:

typedef struct _IPLIMAGE

{

int nSize; /* Iplimage Size */

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

int nchannels; /* Most OPENCV functions support either three-way or 4 channels */

int Alphachannel; /* Ignored by OPENCV */

int depth; /* Bit depth of pixels, mainly with the following supported formats: IPL_DEPTH_8U, ipl_depth_8s, ipl_depth_16u,ipl_depth_16s, Ipl_depth_32s,

ipl_depth_32f and ipl_depth_64f * *

Char colormodel[4]; /* Ignored by OPENCV */

Char channelseq[4]; /* Ditto */

int dataorder;  /* 0-Cross Access color channel, 1-Separate color channel. Only Cvcreateimage can create a cross-access image */

int origin; /* Image Origin location: 0 for top-left structure, 1 for bottom-left structure */

int align; /* Image row arrangement (4 or 8), OpenCV ignored, use widthstep instead of */

int width; /* Image wide pixels */

int height; /* Image High pixel number */

struct _iplroi *roi; /* Image area of interest, when the value is not empty, only the area is processed */

struct _iplimage *maskroi; /* must be NULL in OPENCV */

void *imageid; /* Ditto */

struct _ipltileinfo *tileinfo; /* Ditto */

int imageSize; /* Image data Size (Imagesize=image->height*image->widthstep in cross-access format), per byte */

Char *imagedata; /* point to arranged image data */

int widthstep; /* Image row Size arranged in bytes */

int bordermode[4]; /* Marginal end mode, ignored in OpenCV */

int borderconst[4]; /* Ditto */

Char *imagedataorigin; /* Pointer pointing to a different image data structure (not required) is intended to correct the image memory allocation */

} iplimage;

The iplimage structure is the basis of the entire OPENCV function library, and the function cvcreatimage is used when defining the structure variable, and the variable definition method is as follows:

iplimage* src= "/cvcreateimage" (Cvsize (400,300), ipl_depth_8u,3);

The above sentence defines a iplimage pointer variable src, the image size is 400x300, Image color depth 8-bit, 3-channel image.

Description of important structural elements:

Depth and Nchannels

Depth represents the color depth, using the macro defined below, Nchannels is the number of channels, or 4.
Macro definitions for Depth:
ipl_depth_8u, unsigned 8bit integer (8u)
ipl_depth_8s, signed 8bit Integer (8s)
Ipl_depth_16s, signed 16bit Integer (16s)
Ipl_depth_32s, signed 32bit Integer (32s)
Ipl_depth_32f,32bit floating point, single precision (32f)

Ipl_depth_64f,64bit floating point, double (64f) (note: The color depth here refers to the type of variable that the data of a single channel is saved to, such as data in RGB24 format, channel number 3, color depth is ipl_depth_8u)

Origin and Dataorder


The origin variable can have two values: Ipl_origin_tl or IPL_ORIGIN_BL, which represent the image coordinate system origin in the upper-left or lower-left corner, respectively. Correspondingly, in the field of computer vision, an important source of error is that the definition of Origin location is not uniform. For example, the source of the image, different operating systems, video decoding codec, different storage methods, and so on, can cause the origin of the change in location. For example, you might think you're sampling from the face above the image, but you're actually sampling near the skirt below the image. Initially, you should check the origin of the image in your system, which can be achieved by drawing a shape on top of the image.
The value of the Dataorder can be either Ipl_data_order_pixel or Ipl_data_order_plane, which defines how the color data is arranged when the multi-channel image data is stored, if ipl_data_order_pixel , the channel color data arrangement will be BGRBGR ... Staggered arrangement, if it is ipl_data_order_plane, the color values of each channel together, there are several channels, there are several "color plane." In most cases, the arrangement of channel color data is interlaced.
Widthstep is similar to step in Cvmat, which is the width of the image computed as a number of bytes. The member variable ImageData holds a pointer to the first address of the image data area.
Finally there is an important parameter ROI (region of interest Area of interest), which is a variable of the Iplroi struct type. The IPLROI structure contains XOFFSET,YOFFSET,HEIGHT,WIDTH,COI member variables, where Xoffset,yoffset is the x, y coordinates, and COI represents channel Ofinterest (the channels of interest). Sometimes, the OPENCV image function does not work on the entire image, but on one part of the image. This is where we can use the ROI member variable. If the ROI is set in the Iplimage variable, the OPENCV function uses that ROI variable. If the COI is set to a value other than 0, the operation on the image will only be used on the channel specified by the COI. Unfortunately, many OPENCV functions ignore the value of COI.

Accessing data in an image

Just like the elements in the matrix, we want to access the data in the image in the most straightforward way, for example, if we have a three-channel HSV image (the HSV color attribute pattern is a way to determine the color based on the three basic attributes of the color: Hue h, saturation s, and lightness V), To set the saturation and lightness of each point to 255, we can use pointers to traverse the image, and compare it with the traversal of the matrix:
void Sat_sv (iplimage* img) {

for (int y=0; yuchar* ptr = (uchar*) (
Img->imagedata + y * img->widthstep
);
for (int x=0; x<width; x + +) {
PTR[3*X+1] = 255;
PTR[3*X+2] = 255;
}
}
}
Note that the 3*x+1,3*x+2 method, because each point has three channels, so set. Another type of ImageData member is uchar*, which is the byte pointer type, so unlike the Cvmat data pointer Type (union), it does not need to be as troublesome as Cvmat (remember the STEP/4,STEP/8 situation).


ROI and Widthstep


Roi and Widthstep are important in real-world work, and in many cases, using them can improve the speed of computer visual code execution. This is because they allow manipulation of a small portion of an image, rather than an entire image. In OpenCV, all functions for image manipulation support ROI, and if you want to open ROI, you can use the function Cvsetimageroi () and pass a rectangular subwindow to the function. and Cvresetimageroi () is used to turn off ROI.
void Cvsetimageroi (iplimage* image,cvrect rect);
void Cvresetimageroi (iplimage* image);
Note that in a program, once you have used the ROI to do the appropriate operation, you must use Cvresetimageroi () to turn off ROI, otherwise the definition of ROI is used when other operations are executed.

After many experiments, it is found that there are 3 ways to choose Iplimage. (1) iplimage = Cvcreateimageheader (Cvsize (width,heigth), ipl_depth_8u,1); Cvsetdata (iplimage,pgraybuffer,width*1); /The 3rd parameter is the number of row bytes cvreleaseimageheader (&iplimage); free (pgraybuffer);//pgraybuffer is the space in the program for grayscale data. Assigned by Calloc (2) iplimage = Cvcreateimage (Cvsize (width,heigth), ipl_depth_8u,1);iplimage->imagedata=pgraybuffer; Iplimage->imagedataorigin=pgraybuffer;cvreleaseimageheader (&iplimage); free (Pgraybuffer);(3) IplImage = Cvcreateimage (Cvsize (width,heigth), ipl_depth_8u,1); memcpy (iplimage->imagedata,pgraybuffer,iplimage-> imageSize); Cvreleaseimage (&iplimage); free (pgraybuffer); conclusion: All of the 3 methods are correct, but it is recommended to use paragraph (1). In short, remember that if the space imagedata points to is initialized by Cvcreateimage, then it is freed with Cvreleaseimage, and if imagedata points to a space that is later specified by the user, Then use Vreleaseimageheader (&iplimage), release, and release the allocated space by the user itself. In fact (1) The Things Cvsetdata do and (2) the two pointers are assigned similar. "OpenCV Error:unknown error code-49 (deallocation error)" will appear if allocation and release are not well matched, such as self-allocating space paired with Cvreleaseimage (&iplimage); This type of error.   Also, add, "Create a new iplimage with Cvcreateimage, and then assign the members of the struct."is very superfluous
, Cvcreateimage has initialized the individual members of the struct based on the width, height, number of channels, color depth, etc. of the parameters, and does not need to be manually assigned. Here to mention the Char*imagedataorigin; the description of this variable is that the pointer points to a different image data structure (not the one that must be arranged) and is intended to correct the memory allocation of the image. In fact, the initialized Imagedataorigin and ImageData point to the same address. Since it was initialized like this, I didn't change it, and it didn't have any effect. OpenCV Forum on the Sunlighta Hero's explanation is: "Reserved data interface it!" There is a sentence above: The iplimage structure comes from the Intel Image processing Library (which is itself). OpenCV only supports one subset of these. ” Note: Memory alignment under Linux is 4 bytes by default and can be modified by the following statement: #progma Pack (n)


Five commonly used functions (I/O)
1. Image loading function

The function Cvloadimage loads the specified image file and returns a iplimage pointer to the file. The function supports images in formats such as BMP, JPG, PNG, TIFF, and so on. Its function prototype is as follows:

iplimage* cvloadimage (const char* filename, int iscolor);

where filename is the name of the image to be loaded, including the extension of the image, IsColor is an auxiliary parameter item, an optional positive number, 0 and negative three values, a positive value is loaded as a three-channel image, and 0 indicates that the image is a single-channel image, and negative numbers indicate that the image-loaded channel is determined by itself.

2. Window definition functions

The function Cvnamedwindow defines a window for displaying an image. Its function prototype is as follows:

int Cvnamedwindow (const char* name, unsigned long flags);

Where name is the window name and flags is the window property indicator value, you can choose Cv_window_autosize and 2 values. Cv_window_autosize indicates that the window size is the same as the original image size, and 0 indicates that the image is displayed in a fixed window size.

3. Image Display function

The function cvshowimage is to display the image in the specified window, and its function is prototyped as follows:

void Cvshowimage (const char* name, const cvarr* image);

Where name is the window name, and image is a pointer to the type of the Iplimage, which is generally the pointer.

4. Image Preservation function

The function Cvsaveimage saves a pointer variable of type iplimage with the specified file name, and its function prototype is as follows:

int cvsaveimage (const char* filename, const cvarr* image);

where filename is the image save path and name, image is the iplimage pointer variable.

5. Image Destruction function

The function Cvreleaseimage destroys the defined iplimage pointer variable, freeing up memory space. Its function prototype is as follows:

void Cvreleaseimage (iplimage** image);

Where image is a defined iplimage pointer.

OpenCV old version of Iplimage use

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.