Ask Your Question

Revision history [back]

You can use remap. I copy x diagonal in dst :

    Mat x = (Mat_<uchar>(3, 3) << 1, 2, 3, 4, 5, 6, 7, 8, 9);
    Mat map(1, 3, CV_32FC2);
        for (int j = 0; j<map.cols; j++)
                map.at<Point2f>(0, j) = Point2f(j, j);
       Mat dst;
    remap(x, dst, map, Mat(), INTER_NEAREST);
    cout << x << endl;
    cout << dst << endl;

results are

x=[  1,   2,   3;
   4,   5,   6;
   7,   8,   9]
dst=[  1,   5,   9]