Use type to specify operation rather than using enum

asked 2013-09-19 04:44:04 -0500

stereomatching gravatar image

updated 2013-10-02 20:19:53 -0500

In openCV2, enum always use to specify what kind of operation we want to perform like

cv::threshold(input, output, 128, 255, cv::THRESH_BINARY);
cv::threshold(input, output, 128, 255, cv::THRESH_BINARY_INV);
cv::threshold(input, output, 128, 255, cv::THRESH_TOZERO);

Is it possible to provide the api like

cv::threshold(input, output, 128, 255, cv::THR_BINARY);
cv::threshold(input, output, 128, 255, cv::THR_BINARY_INV);
cv::threshold(input, output, 128, 255, cv::THR_TOZERO);

cv::THR_BINARY, cv::THR_BINARY_INV, cv::THR_TOZERO are empty classes. This way, we could find out the type is valid or not and determine which operation should take at compile time

Would this kind of api violate the coding standard of openCV?

Edit : @berak, no problem.I would paste the pseudo codes at here

class BGR2RGB{}; 
class BGR2GRAY{};
....//and so on

//declaration of cv::cvtColor
cv::cvtColor(cv::Mat &input, cv::Mat &output, cv::BGR2RGB method);
cv::cvtColor(cv::Mat &input, cv::Mat &output, cv::BGR2GRAY method);

//call it like this way
cv::cvtColor(cv::Mat &input, cv::Mat &output, CV_BGR2GRAY);
cv::cvtColor(cv::Mat &input, cv::Mat &output, CV_BGR2RGB);
//oops, compile time error since there are nothing like CV_BGR2RGBAA    
cv::cvtColor(cv::Mat &input, cv::Mat &output, CV_BGR2RGBAA);

other example


cv::threshold(cv::Mat &input, cv::Mat &output, double thresh, double maxValue, 
cv::BINARY_THRESH method, bool otsu = false);

//the flag(otsu) could use type to replace too

The idea are come from the source codes of stl, use type and overload to do compile time if..else, this is faster because the choice are made at compile time, and safer since the compiler would complain when you provide invalid type.You could check about the implementation of std::copy.With the keywords of constexpr, the results could be better(maybe), but openCV do not accept c++11 yet.

edit retag flag offensive close merge delete


just out of curiosity, can you give an example how you would code that ?

berak gravatar imageberak ( 2013-09-19 05:09:52 -0500 )edit