What is the most generic way of assigning an element from one source matrix to a destination matrix in OpenCV? I always find myself coding something like this, which is not exactly elegant:
cv::Mat source; // CV_32FC3
cv::Mat dest; // CV_32FC3
// ...
switch( source.channels() )
{
case 1:
{
dest.at<float>( y, x ) = source.at<float>( j, i );
break;
}
case 2:
{
dest.at<cv::Vec2f>( y, x ) = source.at<cv::Vec2f>( j, i );
break;
}
case 3:
{
dest.at<cv::Vec3f>( y, x ) = source.at<cv::Vec3f>( j, i );
break;
}
case 4:
{
dest.at<cv::Vec4f>( y, x ) = source.at<cv::Vec4f>( j, i );
break;
}
}
I am wondering what the recommended way for this would be, there must be some generic one-liner for this, right?
Bonus points for a solution working across different data types (e.g. assigning n-channel float elements to n-channel double or short elements)
dest(cv::Rect(x,y,1,1)) = source(cv::Rect(i,j,1,1))Probably a lot of overhead if its in a loop, but then you might as well make the ROI larger instead. UseconvertToinstead of the assignment if you need to change type. Or switch to using explicitly typed Mats (likeMat3f) so if you make a templated function like on of the answers suggests, it can determine the parameters automagically.remap, in my current scenario it's indeed the best solution. But I was also thinking of other cases.