Author: Zhu Jincan
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 am modifying an image matching algorithm written by someone else. Image matching is used to find the same places in the two images. In my algorithm, I find the matching result points. The Code of others is roughly like this:
View plaincopy to clipboardprint?
/*!
Structure of rief matching Point Information
*/
Struct PointInfo
{
Long x;
Long y;
Long Geo_x;
Long Geo_y;
PointInfo ()
{
X = 0;
Y = 0;
Geo_x = 0;
Geo_y = 0;
}
};
/*!
Rief matches two images and returns an array of matching result points.
Param pLeftImg left image file handle
Param pRightImg right image file handle
Number of matching result points returned by param lVaildNum
Eturn match result point Array
*/
PointInfo * ImageMatch (FILE * pLeftImg, FILE * pRightImg, long & lVaildNum)
{
Long Imgwidth = 0;
Long ImgHeight = 0;
// Obtain the width and height of the left image without writing the specific code
// Open up an array of the entire image size
PointInfo * pResultPt = new PointInfo [Imgwidth * ImgHeight];
// Start image matching without writing specific code
Return pResultPt;
}
/*!
Structure of rief matching Point Information
*/
Struct PointInfo
{
Long x;
Long y;
Long Geo_x;
Long Geo_y;
PointInfo ()
{
X = 0;
Y = 0;
Geo_x = 0;
Geo_y = 0;
}
};
/*!
Rief matches two images and returns an array of matching result points.
Param pLeftImg left image file handle
Param pRightImg right image file handle
Number of matching result points returned by param lVaildNum
Eturn match result point Array
*/
PointInfo * ImageMatch (FILE * pLeftImg, FILE * pRightImg, long & lVaildNum)
{
Long Imgwidth = 0;
Long ImgHeight = 0;
// Obtain the width and height of the left image without writing the specific code
// Open up an array of the entire image size
PointInfo * pResultPt = new PointInfo [Imgwidth * ImgHeight];
// Start image matching without writing specific code
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, the user will often forget to release the pResultPt memory, and this release is also uncomfortable. At first, I want to change it to this design:
View plaincopy to clipboardprint?
/*!
Rief 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
Number of matched point arrays input by param InitPtNum
Number of matching result points returned by param lVaildNum
Eturn none
*/
Void ImageMatch (FILE * pLeftImg, FILE * pRightImg, PointInfo * pResultPt, long InitPtNum, long & lVaildNum)
{
// Save the specific implementation code
}
/*!
Rief 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
Number of matched point arrays input by param InitPtNum
Number of matching result points returned by param lVaildNum
Eturn none
*/
Void ImageMatch (FILE * pLeftImg, FILE * pRightImg, PointInfo * pResultPt, long InitPtNum, long & lVaildNum)
{
// Save the specific implementation code
}
Code for calling this function:
View plaincopy to clipboardprint?
// Call the code
// Open the left and right images
FILE * pLeftImg;
FILE * pRightImg;
Long Imgwidth = 0;
Long ImgHeight = 0;
// Obtain the width and height of the left image without writing the specific code
Long InitPtNum = Imgwidth * ImgHeight;
PointInfo * pResultPt = new PointInfo [InitPtNum];
Long lVaildNum = 0;
ImageMatch (pLeftImg, pRightImg, pResultPt, InitPtNum, lVaildNum );
// Call the code
// Open the left and right images
FILE * pLeftImg;
FILE * pRightImg;
Long Imgwidth = 0;
Long ImgHeight = 0;
// Obtain the width and height of the left image without writing the 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 increases, for example, you may be confused about the meaning of the long InitPtNum 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:
View plaincopy to clipboardprint?
/*!
Rief matches two images
Param pLeftImg left image file handle
Param pRightImg right image file handle
Param VecPt input and matching point Array
Eturn none
*/
Void ImageMatch (FILE * pLeftImg, FILE * pRightImg, std: vector <PointInfo> & VecPt)
{
}
/*!
Rief matches two images
Param pLeftImg left image file handle
Param pRightImg right image file handle
Param VecPt input and matching point Array
Eturn none
*/
Void ImageMatch (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.