[ C++: How can I create dynamic template type ]
I have the following piece of code, which I created for changing the intensity of a pixel in an OpenCV image (Cv::Mat class).
As you can see, I'm looping in both cases, but with different template type.
The 'transfer' function can be overloaded.
My question is, therefore, how can I create dynamic template type so that it looks better ..
Mat mat = _mat.clone() ;
int channels = mat.channels();
switch(channels)
{
case 1:
for (int i=0; i<mat.rows; i++)
{
for (int j=0; j<mat.cols; j++)
{
uchar src = mat.at<uchar>(i,j);
uchar dst = mat.at<uchar>(i,j);
t.transfer(src, dst);
}
}
break;
case 3:
for (int i=0; i<mat.rows; i++)
{
for (int j=0; j<mat.cols; j++)
{
Vec3b src = mat.at<Vec3b>(i,j);
Vec3b dst = mat.at<Vec3b>(i,j);
t.transfer(src, dst);
}
}
break;
}
return mat ;
Answer 1
How about something like this:
template <typename U, typename T>
void transfer_mat(Mat & mat, T & t)
{
for (int i = 0, r = mat.rows; i != r; ++j)
{
for (int j = 0, c = mat.cols; j != c; ++j)
{
U src = mat.at<U>(i, j);
U dst = mat.at<U>(i, j);
t.transfer(src, dst);
}
}
}
Then you can say:
switch(channels)
{
case 1:
transfer_mat<uchar>(mat, t);
break;
case 2:
transfer_mat<Vec3b>(mat, t);
break;
}
Answer 2
It's unclear as to the scope of your code (looks like a member function), and what type t
is, but this should get you started:
template<typename AtT>
Mat& transfer_impl(Mat& mat, T& t)
{
for (int i = 0; i < mat.rows; ++i)
for (int j = 0; j < mat.cols; ++j)
t.transfer(mat.at<AtT>(i, j), mat.at<AtT>(i, j));
return mat;
};
Mat transfer(Mat const& _mat, T& t)
{
Mat mat = _mat.clone();
switch (mat.channels())
{
case 1: return transfer_impl<uchar>(mat, t);
case 3: return transfer_impl<Vec3b>(mat, t);
default: throw std::runtime_error(/*...*/);
}
}
Answer 3
I'm writing a template function like this :
template<typename T> cv::Mat exercice02_avec_at(const cv::Mat& image, const int BVR[3])
{
// Création d'un clone
Mat imageC = image.clone();
// Recherche du nombre de lignes
int nl = imageC.rows;
// Recherche du nombre d'éléments par ligne
int nc = imageC.cols;
// On boucle dans les éléments de la matrice avec méthode .at<Vec3b>()[]
for(int i = 0 ; i < nl ; i++)
{
for(int j = 0; j < nc ; j++)
{
imageC.at<T>(i,j)[0] = BVR[0];
imageC.at<T>(i,j)[1] = BVR[1];
imageC.at<T>(i,j)[2] = BVR[2];
}
}
return imageC;
}
In my main :
Mat uneImage8bits(10,10, CV_8UC3, Scalar(255,255,255)); // rose Scalar(197,17,217)
int BVR[3] = {197,17,217};
Mat imageModif01 = exercice02_avec_at<Vec3b>(uneImage8bits,BVR);
But I get this error when I build :
main.obj : error LNK2019: unresolved external symbol "class cv::Mat __cdecl exercice02_avec_at >
I need your help
#c++ #templace #OpenCV