I would like to access the elements of a matrix of unknown type:
for(int ii = 0; ii < origCols; ii++)
{
colIdx.at(0,ii) = ii+1; // mak
There is no native Opencv solution to your problem, and it is a frequent pain with this library. There are three possible solutions:
Create a "smart" iterator class that will know how to access matrix data based on the matrix depth. Something like:
class PtrMat
{
PtrMat(cv::Mat& mat, int row)
{
if(mat.depth() == CV_32F) { _ptr = new PtrFloat(mat, row); }
else if(mat.depth() == CV_8U) { _ptr = new PtrUchar(mat, row); }
...
}
Ptr* _ptr
};
class Ptr
{
virtual void set(const float& val)=0;
};
class PtrFloat: public Ptr
{
PtrFloat(const cv::Mat& mat, int row){ _val = mat.ptr(row); }
void set(const float& val) { _val = val; }
float* _val;
}
class PtrUchar: public Ptr
{
PtrUchar(const cv::Mat& mat, int row){ _val = mat.ptr(row); }
void set(const float& val) { _val = val; }
uchar* _val;
}
Of course, with the third solution you end up with lots of duplicated code. Float casting can also slow down your loops. No solution is perfect in this case.