First time here? Check out the FAQ!

Ask Your Question
0

OpenCV InputArray to std::vector<type>

asked Jul 18 '17

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?

Preview: (hide)

Comments

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

LBerger gravatar imageLBerger (Jul 18 '17)edit

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

g3org3 gravatar imageg3org3 (Jul 18 '17)edit

2 answers

Sort by » oldest newest most voted
0

answered Jul 19 '17

LBerger gravatar image

updated Jul 19 '17

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";
Preview: (hide)
0

answered Jul 18 '17

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.

Preview: (hide)

Question Tools

1 follower

Stats

Asked: Jul 18 '17

Seen: 1,373 times

Last updated: Jul 19 '17