Ask Your Question
0

OpenCV InputArray to std::vector<type>

asked 2017-07-18 12:49:43 -0600

g3org3 gravatar image

I have a function which takes in an cv::InputArray _inputArray, and I do a check for _inputArray.isVector(). Now, what I would like to do is directly use the cv::InputArray _inputArray as a vector, but it doesn't seem like there is overloaded functionality for this.

What is the correct way to go about doing this?

edit retag flag offensive close merge delete

Comments

Do you want to use an existing function or write a new fucntion ?

LBerger gravatar imageLBerger ( 2017-07-18 13:33:35 -0600 )edit

@LBerger Obviously, I would like to use an existing one.

g3org3 gravatar imageg3org3 ( 2017-07-18 17:02:53 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2017-07-19 03:44:50 -0600

LBerger gravatar image

updated 2017-07-19 03:51:14 -0600

May be you can try something like that :

bool MatVector(InputArray v)
{
    if (!v.isVector())
        return false;
    int i= v.type();
    if (v.type() == CV_32S && v.isContinuous())
    {
        Mat x= v.getMat();
        cout<<"Mat = "<<x<<endl;
        vector<int> myVector;
        myVector.assign(x.begin<int>(), x.end<int>());
        myVector[4]=245;
        for (int i=0;i<myVector.size();i++)
            cout<<myVector[i]<<"\t";
        cout << "Mat = " << x << endl;
        cout<<endl;
        return true;
    }
    return false;
}

int main(int argc, char **argv)
{

    Mat a = (Mat_<int>(4, 2) << 0, 1, 10, 11, 20, 21, 30, 31);
    vector<int> va = { 0, 1, 10, 11, 20, 21, 30, 31 };
    vector<double> vb = { 0, 1, 10, 11, 20, 21, 30, 31 };
    if (MatVector(a))
        cout << "vector \n";
    else
        cout << "It is not a vector \n";
    if (MatVector(va))
    {
        cout << "vector \n";
        for (int i = 0; i<va.size(); i++)
            cout << va[i] << "\t";

    }
    if (MatVector(vb))
    {
        cout << "vector \n";
        for (int i = 0; i<vb.size(); i++)
            cout << vb[i] << "\t";
   }
    else
        cout << "It is not a vector of int \n";
edit flag offensive delete link more
0

answered 2017-07-18 15:55:46 -0600

KjMag gravatar image

There's no way of doing that directly unless it's vector<Mat> - the interface of InputArray doesn't provide such functionality. If you really need to treat it as a vector, call getMat() or getVectorMat() and transform the resulting Mat object to a vector.

If it is a vector<Mat>, then you can use _InputArray::getMat(idx) to get idx-th Mat of the vector, so you can e.g. iterate that way through the vector elements.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-07-18 12:49:43 -0600

Seen: 1,259 times

Last updated: Jul 19 '17