Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Thank you for your help! I have written a function to "extract a mat which have address in a mat from another mat":

cv::Mat extract(cv::Mat SRC, cv::Mat IDX) { cv::Mat src = SRC.clone(); cv::Mat idx = IDX.clone(); cv::Mat dst = cv::Mat(idx.size(), src.type()); src = src.t(); idx.convertTo(idx, 4); for (int i = 0; i < idx.total(); i++) { // dst.at<>(i) = src.at<>(idx.at<int>(i)); switch (src.type()) { case 0: dst.at<uchar>(i) = src.at<uchar>(idx.at<int>(i)); break; case 1: dst.at<char>(i) = src.at<char>(idx.at<int>(i)); break; case 2: dst.at<ushort>(i) = src.at<ushort>(idx.at<int>(i)); break; case 3: dst.at<short>(i) = src.at<short>(idx.at<int>(i)); break; case 4: dst.at<int>(i) = src.at<int>(idx.at<int>(i)); break; case 5: dst.at<float>(i) = src.at<float>(idx.at<int>(i)); break; default: dst.at<double>(i) = src.at<double>(idx.at<int>(i)); break; } } return dst; }

and call it in main function:

cv::Mat src = (cv::Mat_<double>(3, 3) << 4, 9, 8, 10, 3, 7, 5, 8, 4);
cv::Mat idx = (cv::Mat_<int>(2, 2) << 1, 2, 3, 4);

cv::Mat dst = extract(src, idx);
cout << "src = " << endl << " " << src << endl;
cout << "idx = " << endl << " " << idx << endl;
cout << "dst = " << endl << " " << dst << endl;

and result is similar to your program but i don't know my function had Optimized??? Can you please improve my function? Thank a lot !!!!

Thank you for your help! I have written a function to "extract a mat which have address in a mat from another mat":

cv::Mat extract(cv::Mat SRC, cv::Mat IDX)
{
    cv::Mat src = SRC.clone();
    cv::Mat idx = IDX.clone();
    cv::Mat dst = cv::Mat(idx.size(), src.type());
    src = src.t();
    idx.convertTo(idx, 4);
    for (int i = 0; i < idx.total(); i++)
    {
    //  dst.at<>(i) = src.at<>(idx.at<int>(i));
        switch (src.type())
            {
            case 0:
                dst.at<uchar>(i) = src.at<uchar>(idx.at<int>(i));
                break;
            case 1:
                dst.at<char>(i) = src.at<char>(idx.at<int>(i));
                break;
            case 2:
                dst.at<ushort>(i) = src.at<ushort>(idx.at<int>(i));
                break;
            case 3:
                dst.at<short>(i) = src.at<short>(idx.at<int>(i));
                break;
            case 4:
                dst.at<int>(i) = src.at<int>(idx.at<int>(i));
                break;
            case 5:
                dst.at<float>(i) = src.at<float>(idx.at<int>(i));
                break;
            default:
                dst.at<double>(i) = src.at<double>(idx.at<int>(i));
                break;
            }
    }
    return dst;
}

}

and call it in main function:

cv::Mat src = (cv::Mat_<double>(3, 3) << 4, 9, 8, 10, 3, 7, 5, 8, 4);
cv::Mat idx = (cv::Mat_<int>(2, 2) << 1, 2, 3, 4);

cv::Mat dst = extract(src, idx);
cout << "src = " << endl << " " << src << endl;
cout << "idx = " << endl << " " << idx << endl;
cout << "dst = " << endl << " " << dst << endl;

and result is similar to your program but i don't know my function had Optimized??? Can you please improve my function? Thank a lot !!!!