Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

that's what c++ templates are for:

template<typename T>
Mat drop_rows_uchar(Mat mat, vector<int> v) {
    Mat mat_new = Mat::zeros(mat.rows - v.size(), mat.cols, mat.type()); // <-- !
    for (int i = 0, j = 0; i < mat.rows; i++) {
        if (find(v.begin(), v.end(), i) != v.end())
        {
            continue;
        }
        else
        {
            T*pmat = mat.ptr<T>(i); // <-- !
            T*pmat_new = mat_new.ptr<T>(j); // <-- !
            for (int w = 0; w < mat.cols; w++) {
                pmat_new[w] = pmat[w];
            }
            j++;
        }
    }
    return mat_new;
}

but you probably still need a switch block somewhere:

switch(mat.type) {
      case CV_8U: drop_rows_uchar<uchar>(mat,v); break; 
      case CV_32S: drop_rows_uchar<int>(mat,v); break;
      ... etc. 
}

that's what c++ templates are for:

template<typename T>
Mat drop_rows_uchar(Mat mat, vector<int> v) {
    Mat mat_new = Mat::zeros(mat.rows - v.size(), mat.cols, mat.type()); // <-- !
    for (int i = 0, j = 0; i < mat.rows; i++) {
        if (find(v.begin(), v.end(), i) != v.end())
        {
            continue;
        }
        else
        {
            T*pmat = mat.ptr<T>(i); // <-- !
            T*pmat_new = mat_new.ptr<T>(j); // <-- !
            for (int w = 0; w < mat.cols; w++) {
                pmat_new[w] = pmat[w];
            }
            j++;
        }
    }
    return mat_new;
}

but you probably still need a switch block somewhere:

switch(mat.type) switch(mat.depth) {
      case CV_8U: drop_rows_uchar<uchar>(mat,v); break; 
      case CV_32S: drop_rows_uchar<int>(mat,v); break;
      ... etc. 
}