Author: Zhu Jincan
Source: http://www.cnblogs.com/clever101
Note that the dynamic array I mentioned here is not the new array, but the STD: vector and carray containers in STL. In the beginning, we thought that Using STD: vector is nothing more than saving the effort of dynamic memory. Avoiding manual opening and releasing of memory is one aspect. In fact, you will gradually find the advantage of using STD: vector. Today I found a benefit.
Today, I modified an image match written by someone else.AlgorithmThe so-called image matching is to find the same places in the two images. In my algorithm, it is to find the matching result points. OthersCodeIt is roughly like this:
Code /* !
\ Brief matched vertex Information Structure
*/
Struct Pointinfo
{
Long X;
Long Y;
Long Geo_x;
Long Geo_y;
Pointinfo ()
{
X = 0 ;
Y = 0 ;
Geo_x = 0 ;
Geo_y = 0 ;
}
};
/*!
\ Brief matches two images and returns an array of matching result points.
\ Param pleftimg left image file handle
\ Param prightimg right image file handle
\ Param lvaildnum
\ Return match result point Array
*/
Pointinfo * Imagematch (File * Pleftimg, file * Prightimg, Long & Lvaildnum)
{
Long Imgwidth = 0 ;
Long Imgheight = 0 ;
// Retrieve the width and height of the left image without writing specific code
// Opening up an array of the entire image size
Pointinfo * Presultpt = New Pointinfo [imgwidth * Imgheight];
// Starts Image Matching. No specific code is written.
Return Presultpt;
}
To be honest, I don't like the design of such functions. Why? Because I think it is best to follow this principle to avoid Memory leakage: the external release of the memory function applied for by the function, and the internal application of the function has the Function Memory released, if the above function is designed, users will often forget to releasePresultpt Memory, And the release is also uncomfortable. At first, I want to change it to this design:
Code /* !
\ Brief matches two images and returns an array of matching result points.
\ Param pleftimg left image file handle
\ Param prightimg right image file handle
\ Param presultpt match point Array
\ Param initptnum
\ Param lvaildnum
\ Return none
*/
Void Imagematch (File * Pleftimg, file * Prightimg, pointinfo * Presultpt, Long Initptnum, Long & Lvaildnum)
{
// Save specific implementation code
}
Code for calling this function:
Code // Call Code
// Open left and right images
File * Pleftimg;
File * Prightimg;
Long Imgwidth = 0 ;
Long Imgheight = 0 ;
// Retrieve the width and height of the left image without writing specific code
Long Initptnum = Imgwidth * Imgheight;
Pointinfo * Presultpt = New Pointinfo [initptnum];
Long Lvaildnum = 0 ;
Imagematch (pleftimg, prightimg, presultpt, initptnum, lvaildnum );
The above Code conforms to the external release of the Memory Function Applied for outside the function. The internal application of the function has the principle of releasing the Function Memory, but it still gives a strange feeling, first, we need to find a matching point. First, we need to open a large array of the entire image (to meet the needs of finding a matching point, and we are worried that it is not enough). However, matching points usually only occupy a small part of the image; second, the number of input function parameters increases. In addition to the input matching point array pointer, the number of input parameters also increases the difficulty of using the array. For example, the user may be confused.Long InitptnumMeaning of this Parameter.
Finally, what is the ideal design? You only need to enter the file handle and dynamic array of the two images, and then return the array. So I thought of the following design:
Code /* !
\ Brief matches two images
\ Param pleftimg left image file handle
\ Param prightimg right image file handle
\ Param vecpt input and match point Array
\ Return none
*/
VoidImagematch (File*Pleftimg, file*Prightimg, STD: Vector<Pointinfo> &Vecpt)
{
}
This design removes the need to open up and release memory, and you no longer need to open up a large block of memory to deal with an uncertain situation. Third, it is very natural for others to use it.From this point of view, the appropriate use of the dynamic array structure can make our design more reasonable.