Ask Your Question
0

How to make the function can process different type image

asked 2018-04-02 01:38:41 -0600

yode gravatar image

updated 2018-04-02 01:43:55 -0600

Cross post here


I have build two functions with different name to drop the specfiy lines from difference Mat object, this is the code:

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

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

Then I can use it in my main() function like

int main()
{
    Mat mat_uchar = (Mat_<uchar>(5, 4) << 5, 6, 0, 4, 0, 1, 9, 9, 100, 3, 5, 8, 200, 33, 1, 4, 8, 88, 23, 6);
    Mat new_mat_uchar = drop_rows_uchar(mat_uchar, {2,4});

    Mat mat_int = (Mat_<int>(5, 4) << 5, 6, 0, 4, 0, 1, 9, 9, 100, 3, 5, 8, 200, 33, 1, 4, 8, 88, 23, 6);
    Mat new_mat_int = drop_rows_int(mat_int, { 2,4 });

        return 0;
}

Yes, I made it. but as I know, the Mat can have 7 kinds of depth, such as CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F and CV_64F, So I have to build 7 functions with different name to do such thing?? Can anyone tell me how to use one function to implement it??

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2018-04-02 03:12:17 -0600

berak gravatar image

updated 2018-04-02 03:13:22 -0600

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.depth) {
      case CV_8U: drop_rows_uchar<uchar>(mat,v); break; 
      case CV_32S: drop_rows_uchar<int>(mat,v); break;
      ... etc. 
}
edit flag offensive delete link more

Comments

1

Glad to see your help again. Thanks very much.

yode gravatar imageyode ( 2018-04-02 03:16:18 -0600 )edit

Could you tell me how to set a default value for my this template function?

yode gravatar imageyode ( 2018-04-02 05:02:17 -0600 )edit

please explain: "default value" ?

(and please - no images of text)

berak gravatar imageberak ( 2018-04-02 05:10:02 -0600 )edit

Sorry, my last comment misses some information. I don't know why some comment always will fail sometimes. I mean I want to set a default value for the parameter s to be Drop_Rows in template function drop_lines. I have not post code but image because it is tooo long for a comment I think. :)

yode gravatar imageyode ( 2018-04-02 05:24:14 -0600 )edit

what about:

template<typename T>
Mat drop_rows_uchar(const Mat &mat, const vector<int> &v, Drops s=DROP_ROWS) {

??

berak gravatar imageberak ( 2018-04-02 05:36:56 -0600 )edit

Funny. It works..

yode gravatar imageyode ( 2018-04-02 06:29:54 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-04-02 01:38:41 -0600

Seen: 398 times

Last updated: Apr 02 '18