简体   繁体   中英

OpenCV - Ptr syntax and class definition / declaration - confusion?

I'm attempting to understand the OpenCV implementation of the Generalized Hough Transform based on Ballard 1981 . I'm reading through generalized_hough.cpp . At this line is where the confusion is beginning:

namespace
{
    class GeneralizedHoughBallardImpl : public GeneralizedHoughBallard, private GeneralizedHoughBase
    {

Clearly this is defining a namespace, then a class named GeneralizedHoughBallardImpl, which I gather is short for Generalized Hough Ballard Implementation. The ":" character clearly means "inherits from" as usual, and the "private GeneralizedHoughBase" is clear since that class is declared and its functions are defined earlier on in the same file:

class GeneralizedHoughBase
    {
    protected:
        GeneralizedHoughBase();
        virtual ~GeneralizedHoughBase() {}

        void setTemplateImpl(InputArray templ, Point templCenter);
        void setTemplateImpl(InputArray edges, InputArray dx, InputArray dy, Point templCenter);
(much more code for this class omitted) . . .

Now where I'm lost, I can find where "GeneralizedHoughBallard" is declared or defined. In the same file (generalized_hough.cpp) there is this code:

Ptr<GeneralizedHoughBallard> cv::createGeneralizedHoughBallard()
{
    return makePtr<GeneralizedHoughBallardImpl>();
}

If I'm understanding this correctly, this is specifying that "Ptr GeneralizedHoughBallard" is the return type of function createGeneralizedHoughBallard(), but is not in any way defining class GeneralizedHoughBallard. Moreover, if I search the entire OpenCV repo for "GeneralizedHoughBallard" , I'm finding 11 instances of the text GeneralizedHoughBallard in 7 different files, but they all seem to be uses similar to the above, rather than declarations or definitions.

What am I missing here? Is there something special about the Ptr syntax in OpenCV that a class does not have to be defined/declared? Is GeneralizedHoughBallard defined/declared somewhere that I'm not finding or not noticing? If anybody could provide some clarification it would be greatly appreciated.

So, to review what you have listed above, there is the GeneralizedHoughBallardImpl class, which is defined within an anonymous class*.

Then, in the same file, there is the createGeneralizedHoughBallard function, defined in the cv namespace. This function creates an instance of GeneralizedHoughBallardImpl using the templated makePtr function, then C++ type-casts the pointer instance to GeneralizedHoughBallard, since GeneralizedHoughBallardImpl derives from GeneralizedHoughBallard.

The createGeneralizedHoughBallard function is declared as CV_EXPORTS Ptr createGeneralizedHoughBallard() in 2 different header files (at the moment). So, since createGeneralizedHoughBallard is declared in a header file that defines the GeneralizedHoughBallard class, the function will compile without issue.

Note, the GeneralizedHoughBallard class is defined in https://github.com/opencv/opencv/blob/e93aa158cf39b88836797a6e74b06c7dbe8199b7/modules/imgproc/include/opencv2/imgproc.hpp

*Specifying items within an anonymous class is a good way to make sure the items declared within the file do not clash with potentially same named items in other files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM