Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all,
I was writing a code with MorphologyEx() function of OpenCV, when i mistakenly gave value 0 to 'Morphology Operator' parameter of the function.

As can be seen in the function specs [^], the Operator value ranges from 2-6 for Opening, Closing, Gradient, Top, Bottom hat operations respectively.

The function should ve given some error with Operator value 0, but it worked fine, and the results i received were like a stronger Closing filter. This result is favourable for my project, but i want to know what filter it is. The function also gave some result with operator parameter set to 1.

If someone knows what the operator values 0 and 1 does, please let me know.
Posted

1 solution

It is never a good idea to use direct codes where enums provided by the framework...
If you check the source code (OpenCV is on GitHub!) you will see this:
C++
switch( op )
    {
    case MORPH_ERODE:
        // ...
        break;
    case MORPH_DILATE:
        // ...
        break;
    case MORPH_OPEN:
        break;
    case CV_MOP_CLOSE:
        // ...
        break;
    case CV_MOP_GRADIENT:
        // ...
        break;
    case CV_MOP_TOPHAT:
        // ...
        break;
    case CV_MOP_BLACKHAT:
        // ...
        break;
    default:
        CV_Error( CV_StsBadArg, "unknown morphological operation" );
}

And if you search for the enum behind you will see this:
C++
enum MorphTypes{
    MORPH_ERODE    = 0, //!< see cv::erode
    MORPH_DILATE   = 1, //!< see cv::dilate
    MORPH_OPEN     = 2, //!< an opening operation
                        //!< \f[\texttt{dst} = \mathrm{open} ( \texttt{src} , \texttt{element} )= \mathrm{dilate} ( \mathrm{erode} ( \texttt{src} , \texttt{element} ))\f]
    MORPH_CLOSE    = 3, //!< a closing operation
                        //!< \f[\texttt{dst} = \mathrm{close} ( \texttt{src} , \texttt{element} )= \mathrm{erode} ( \mathrm{dilate} ( \texttt{src} , \texttt{element} ))\f]
    MORPH_GRADIENT = 4, //!< a morphological gradient
                        //!< \f[\texttt{dst} = \mathrm{morph\_grad} ( \texttt{src} , \texttt{element} )= \mathrm{dilate} ( \texttt{src} , \texttt{element} )- \mathrm{erode} ( \texttt{src} , \texttt{element} )\f]
    MORPH_TOPHAT   = 5, //!< "top hat"
                        //!< \f[\texttt{dst} = \mathrm{tophat} ( \texttt{src} , \texttt{element} )= \texttt{src} - \mathrm{open} ( \texttt{src} , \texttt{element} )\f]
    MORPH_BLACKHAT = 6  //!< "black hat"
                        //!< \f[\texttt{dst} = \mathrm{blackhat} ( \texttt{src} , \texttt{element} )= \mathrm{close} ( \texttt{src} , \texttt{element} )- \texttt{src}\f]
};
 
Share this answer
 
Comments
Abhishrek 25-Oct-15 6:18am    
Thanks a lot for the quick response. Its a big help.

I understand importance of enum instead of int values, but since I am using a trackbar to switch between morphology operations, so its convenient to directly map the int trackbar variable as morphology operation (instead of switch case ing Enum Values)

So, morphologyex() function does support 0 and 1 as operation parameter which are Erode and Dilate !!!
(hmm, i wonder why its not stated in the function reference guide or in the tutorials)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900