1 | initial version |
I could offer you a generic for_each loop, you could design you own generic algorithms by your own needs.
/**
*@brief apply stl like for_each algorithm on a channel
*
* @param T : the type of the channel(ex, uchar, float, double and so on)
* @param func : Unary function that accepts an element in the range as argument
*
*@return :
* return func
*/
template<typename T, typename UnaryFunc, typename Mat>
inline UnaryFunc for_each_channels(Mat &&input, UnaryFunc func)
{
int rows = input.rows;
int cols = input.cols;
if(input.isContinuous()){
cols = input.total() * input.channels();
rows = 1;
}
for(int row = 0; row != rows; ++row){
auto input_ptr = input.template ptr<T>(row);
for(int col = 0; col != cols; ++col){
func(input_ptr[col]);
}
}
return func;
}
There are still rooms to speed up the algorithms, like vectorize?(I don't know how to do it yet) Multithread, although I can develop it with std::thread, but don't know how to make a thread pool, without a decent thread pool the parallel for_each algorithm is not ready yet.
I use && to make sure it could accept cv::Mat& and cv::Mat const&. This link explain the principle of the algorithm with details(only principle) generic algorithm
2 | No.2 Revision |
I could offer you a generic for_each loop, you could design you own generic algorithms by your own needs.
/**
*@brief apply stl like for_each algorithm on a channel
*
* @param T : the type of the channel(ex, uchar, float, double and so on)
* @param func : Unary function that accepts an element in the range as argument
*
*@return :
* return func
*/
template<typename T, typename UnaryFunc, typename Mat>
inline UnaryFunc for_each_channels(Mat &&input, UnaryFunc func)
{
int rows = input.rows;
int cols = input.cols;
if(input.isContinuous()){
cols = input.total() * input.channels();
rows = 1;
}
for(int row = 0; row != rows; ++row){
auto input_ptr = input.template ptr<T>(row);
for(int col = 0; col != cols; ++col){
func(input_ptr[col]);
}
}
return func;
}
you could do your transform like this
for_each_channels<T>(input, [](T &data){ data = std::tanh(data); })
with the helps of c++11 lambda(c++14 will become even better), stl like algorithms become much more easier to use.
There are still rooms to speed up the algorithms, like vectorize?(I don't know how to do it yet)
Multithread, although I can develop it with std::thread, but don't know how to make a thread pool, without a decent thread pool the parallel for_each algorithm is not ready yet.yet.Although other language do not support template, I think this kind of generic algorithm could speed up the development speed of the openCV source and make the life of the c++ programmers easier.
I use && to make sure it could accept cv::Mat& and cv::Mat const&. This link explain the principle of the algorithm with details(only principle) generic algorithm