After looking at some documents, I donβt think that there is OpenCV's own way to do this without avoiding branching.
If you're just worried about cleaner code, you can try the templated approach, if you don't mind templates:
template <typename T> void dostuff(cv::Mat& colIdx, int origCols) { for(int ii = 0; ii < origCols; ii++) { colIdx.at<T>(0,ii) = (T)(ii+1); // make one based index } } void dostuff_poly(cv::Mat& colIdx, int origCols) { switch(colIdx.type()) { case CV_8UC1: dostuff<char>(colIdx, origCols); break; case CV_32FC1: dostuff<float>(colIdx, origCols); break; case CV_64FC1: dostuff<double>(colIdx, origCols); break; // and so on default: } }
In this example, the code is quite small, so the templates do not seem to be a bad choice and provide you with the polymorphism you need without writing a bunch of redundant code.
Perhaps some of these tutorials will give you a better idea:
OpenCV Docs: Module Tutorials
OpenCV Docs: How to Scan Images
source share