Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to make the function can process different type image

I have build two function 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??

How to make the function can process different type image

Cross post here


I have build two function 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??

How to make the function can process different type image

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 function to implement it??