Parsing icvloadimage Functions

Source: Internet
Author: User

Let's take a look at the icvloadimage function.

View plaincopy to clipboardprint?
/*-----------------------------------------------------------------
* Although there are different data return methods based on parameters, one is iplimage * and the other is matrix.
* Format, but in fact, internal data is stored through matrices, so it is not needed here
* There are two more functions: one returns iplimage * and the other returns cmat *. Directly return void *
*---------------------------------------------------------------*/
Static void *
Icvloadimage (const char * filename, int flags, bool load_as_matrix)
{
Grfmtreader * reader = 0; // Reader Pointer.
Iplimage * image = 0; // returns the image data pointer. For the specific structure, see the previous section.
Cvmat HDR, * matrix = 0; // the two parameters are related to reading from the matrix.
Int depth = 8;

Cv_funcname ("cvloadimage"); // It is mainly used to handle errors. You can skip this step.

_ Begin __; // is a macro: defined as {, so this line can also have no

Cvsize size; // struct: You can see the following definition:
Int iscolor; // how to obtain the color variable.
Int CN;
// Check whether the file is empty. There are two considerations: NULL pointer and empty content.
If (! Filename | strlen (filename) = 0)
Cv_error (cv_stsnullptr, "null FILENAME ");
// G_filters is a static variable in the loadsave. cpp file because it can be duplicated.
// Obtain some images after secondary reading, but the interface for reading images remains unchanged.
// Then, you can stop repeating the construction.
// Reader points to the reader where the image is read.
Reader = g_filters.findreader (filename );
If (! Reader) // if not found.
Exit;
// Read the file header. Get some basic information.
If (! Reader-> readheader ())
Exit;
// Set the size.
Size. width = reader-> getwidth ();
Size. Height = reader-> getheight ();
// If flags is-1, the number of channels of the file itself will be used for reading.
If (flags =-1)
Iscolor = reader-> iscolor ();
Else
{
// The value of each macro can be viewed later:
// 3 channels
// (Cv_load_image_color ),
// Single Channel
// (Cv_load_image_grayscale ),
// Remain unchanged
// (Cv_load_image_anycolor)
// Specify whether or not the input image is converted to 8 bits per pixel for each color channel, or remain unchanged as the input image.
// 8-bit unsigned, 16-bit unsigned, 32-bit signed or 32-bit floating point type.
// Cv_load_image_anydepth
// If the input contains a conflicting sign, a smaller number is used.
// Cv_load_image_color | cv_load_image_anycolor = cv_load_image_color
// Cv_load_image_anycolor and cv_load_image_unchanged are equivalent.
// Cv_load_image_anycolor can be used with cv_load_image_anydepth,
// Therefore, cv_load_image_unchanged is no longer used.
// Load the most authentic image. Select cv_load_image_anydepth | cv_load_image_anycolor.
If (flags & cv_load_image_color )! = 0 |
(Flags & cv_load_image_anycolor )! = 0 & reader-> iscolor ()))
Iscolor = 1;
Else
Iscolor = 0;

If (flags & cv_load_image_anydepth )! = 0)
{
Reader-> usenativedepth (true );
Depth = reader-> getdepth ();
}
}
// Check whether a three-color or grayscale image is used.
CN = iscolor? 3: 1;

If (load_as_matrix) // if it is read from the matrix
{
Int type; // set the read data to see if it is a floating point number.
If (Reader-> isfloat () & depth! = 8)
Type = cv_32f;
Else // if the number of each one is small, it is obtained in multiples of 8.
Type = (depth <= 8 )? Cv_8u: (depth <= 16 )? Cv_16u: cv_32s;
// Create a matrix. Note that cv_call
Cv_call (matrix = cvcreatemat (size. Height, size. Width, cv_maketype (type, CN )));
}
Else
{
Int type; // set whether to read the floating point.
If (Reader-> isfloat () & depth! = 8)
Type = ipl_depth_32f;
Else // return the number multiple of 8.
Type = (depth <= 8 )? Ipl_depth_8u: (depth <= 16 )? Ipl_depth_16u: ipl_depth_32s;
// Create an image.
Cv_call (image = cvcreateimage (size, type, CN ));
// Set the data matrix. Although the returned value is not in the form of a matrix,
// Its internal structure should be a matrix.
Matrix = cvgetmat (image, & HDR );
}
// Read the data into the matrix. If the read fails, the data will be released.
If (! Reader-> readdata (matrix-> data. PTR, matrix-> step, iscolor ))
{
If (load_as_matrix)
Cvreleasemat (& matrix );
Else
Cvreleaseimage (& image );
Exit;
}

_ End __;

Delete reader; // Delete is used. It seems that the previous reader is copied.
// Check the status.
If (cvgeterrstatus () <0)
{
If (load_as_matrix)
Cvreleasemat (& matrix );
Else
Cvreleaseimage (& image );
}
// Return different values based on different situations.
Return load_as_matrix? (Void *) matrix: (void *) image;
}
 

Here is the big speech to read this function.

In fact, there should be a lot of details, so here we will start from scratch.

The first data structure encountered was grfmtreader;

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.