Ask Your Question

Revision history [back]

Create a Mat from coordinates stored in another Mat

So, basically in python y have the following img[np.array(coor[:, 1]), np.array(coor[:, 0])]

How can i do the same in C++?

coor is already a Mx2 Mat, img is a completely different Mat, i have been triying for a week or more and can't find a solution

click to hide/show revision 2
None

updated 2018-11-13 08:21:15 -0600

berak gravatar image

Create a Mat from coordinates stored in another Mat

So, basically in python y have the following

img[np.array(coor[:, 1]), np.array(coor[:, 0])]

0])]

How can i do the same in C++?

coor is already a Mx2 Mat, img is a completely different Mat, i have been triying for a week or more and can't find a solution solution

Create a Mat from coordinates stored in another Mat

So, basically in python y have the following

img[np.array(coor[:, 1]), np.array(coor[:, 0])]

How can i do the same in C++?

coor is already a Mx2 Mat, img is a completely different Mat, i have been triying for a week or more and can't find a solution

M is the size of my Mat so imagine 216 rows per 2 column which is the size of coor, basically iḿ triying to store the contours obtained from findcontours and retreive the same contours from a different mat.

this is the whole code in python

coor = cnts[ii].reshape((len(cnts[ii]), 2)) #cnts[ii] is the current contour

after that coor is employed to acces a compeltely different image in the coor coordinates as it follows

out = img[np.array(coor[:, 1]), np.array(coor[:, 0])]

it returns a 1D array with the gray values in the stored coordinates i cánt do the same in C++

First of all i have the contours in C++

    vector<Point> cnts;//Retrieve the vector nested in the contours

Mat reshap = Mat (cntst); //cant reshape in a vector, so cast to Mat

Mat coor = reshap.reshape(1);// Until here have the same size that coor, and the same values

And then i dont know how to produce out equivalent in C++

i have img as Mat img = imread("my file",0);

Create a Mat from coordinates stored in another Mat

So, basically in python y have the following

img[np.array(coor[:, 1]), np.array(coor[:, 0])]

How can i do the same in C++?

coor is already a Mx2 Mat, img is a completely different Mat, i have been triying for a week or more and can't find a solution

M is the size Ok, so basically are the coordinates of my Mat so imagine 216 rows per 2 column which is the size a set of coor, basically iḿ triying to store the contours obtained from findcontours and retreive the same contours from a different mat.

this is the whole code in python

resized as it follows:

 coor = cnts[ii].reshape((len(cnts[ii]), 2)) #cnts[ii] cnts[ii].reshape((len(cnts), 2))#cnts is the current contour

contours

in C++ i have the folloing for the contours

    vector<Point>cntst;//Retrieve the vector nested in the contours

and then due to lack of reshape as vector i move it to Mat

    Mat reshap = Mat (cnts);

    Mat coor = reshap.reshape(1);

Until this point it has the same size in C++ than in python, after that coor is employed to acces a compeltely different image in the coor coordinates as it follows

out = img[np.array(coor[:, 1]), np.array(coor[:, 0])]

it returns a 1D array with the gray values in the stored coordinates i cánt do the same in C++

First of all i have the contours in C++

    vector<Point> cnts;//Retrieve the vector nested in the contours

Mat reshap = Mat (cntst); //cant reshape in a vector, so cast to Mat

Mat coor = reshap.reshape(1);// Until here have the same size that coor, and the same values

And then i dont know how to produce out equivalent in C++

i have img as Mat img = imread("my file",0);

generate the output from here

img[np.array(coor[:, 1]), np.array(coor[:, 0])]

Create a Mat from coordinates stored in another Mat

So, basically in python y have the following

img[np.array(coor[:, 1]), np.array(coor[:, 0])]

How can i do the same in C++?

coor is already a Mx2 Mat, img is a completely different Mat, i have been triying for a week or more and can't find a solution

Ok, so basically are the coordinates of a set of contours resized as it follows:

    coor = cnts[ii].reshape((len(cnts), 2))#cnts is the contours

in C++ i have the folloing for the contours

 vector<Point>cntst;//Retrieve vector<Point>cnts;//Retrieve the vector nested in the contours

and then due to lack of reshape as vector i move it to Mat

    Mat reshap = Mat (cnts);

    Mat coor = reshap.reshape(1);

Until this point it has the same size in C++ than in python, after that i dont know how to generate the output from here

img[np.array(coor[:, 1]), np.array(coor[:, 0])]

Create a Mat from coordinates stored in another Mat

So, basically in python y have the following

img[np.array(coor[:, 1]), np.array(coor[:, 0])]

How can i do the same in C++?

coor is already a Mx2 Mat, img is a completely different Mat, i have been triying for a week or more and can't find a solution

Ok, so basically are the coordinates of a set of contours resized as it follows:

    coor = cnts[ii].reshape((len(cnts), cnts.reshape((len(cnts), 2))#cnts is the contours

in C++ i have the folloing for the contours

    vector<Point>cnts;//Retrieve the vector nested in the contours

and then due to lack of reshape as vector i move it to Mat

    Mat reshap = Mat (cnts);

    Mat coor = reshap.reshape(1);

Until this point it has the same size in C++ than in python, after that i dont know how to generate the output from here

img[np.array(coor[:, 1]), np.array(coor[:, 0])]

Create a Mat from coordinates stored in another Mat

So, basically in python y have the following

img[np.array(coor[:, 1]), np.array(coor[:, 0])]

How can i do the same in C++?

coor is already a Mx2 Mat, img is a completely different Mat, i have been triying for a week or more and can't find a solution

Ok, so basically are the coordinates of a set of contours resized as it follows:

    coor = cnts.reshape((len(cnts), 2))#cnts is the contours

in C++ i have the folloing for the contours

    vector<Point>cnts;//Retrieve the vector nested in the contours

and then due to lack of reshape as vector i move it to Mat

    Mat reshap = Mat (cnts);

    Mat coor = reshap.reshape(1);

Until this point it has the same size in C++ than in python, and the same data (i'm using a simple image test) after that i dont know how to generate the output from here

img[np.array(coor[:, 1]), np.array(coor[:, 0])]

On OpenCV C++

Create a Mat from coordinates stored in another Mat

So, basically in python y have the following

img[np.array(coor[:, 1]), np.array(coor[:, 0])]

How can i do the same in C++?

coor is already a Mx2 Mat, img is a completely different Mat, i have been triying for a week or more and can't find a solution

Ok, so basically are the coordinates of a set of contours resized as it follows:

    coor = cnts.reshape((len(cnts), 2))#cnts is the contours

in C++ i have the folloing for the contours

    vector<Point>cnts;//Retrieve the vector nested in the contours

and then due to lack of reshape as vector i move it to Mat

    Mat reshap = Mat (cnts);

    Mat coor = reshap.reshape(1);

Until this point it has the same size in C++ than in python, and the same data (i'm using a simple image test) after that i dont know how to generate the output from here

img[np.array(coor[:, 1]), np.array(coor[:, 0])]

On OpenCV C++

*EDIT: Think is working only modify to

Mat pixels; // initially empty vector<point> test;//Retrieve the vector nested in the contours std::copy(pointList[ii].begin(),pointList[ii].end(),back_inserter(test));//Copy the vector the current n-th contour [ii] and then your code

for (size_t i=0; i<test.size(); i++) {

// Vec3b p = image.at<vec3b>(test); // bgr image Scalar p = image.at<uchar>(test[i]); // grayscale version pixels.push_back(p); }

then access to the required value as p.val[0];

Need to check with my other images but at this time think is working

Create a Mat from coordinates stored in another Mat

So, basically in python y have the following

img[np.array(coor[:, 1]), np.array(coor[:, 0])]

How can i do the same in C++?

coor is already a Mx2 Mat, img is a completely different Mat, i have been triying for a week or more and can't find a solution

Ok, so basically are the coordinates of a set of contours resized as it follows:

    coor = cnts.reshape((len(cnts), 2))#cnts is the contours

in C++ i have the folloing for the contours

    vector<Point>cnts;//Retrieve the vector nested in the contours

and then due to lack of reshape as vector i move it to Mat

    Mat reshap = Mat (cnts);

    Mat coor = reshap.reshape(1);

Until this point it has the same size in C++ than in python, and the same data (i'm using a simple image test) after that i dont know how to generate the output from here

img[np.array(coor[:, 1]), np.array(coor[:, 0])]

On OpenCV C++

*EDIT: Think is working only modify to

Mat pixels; // initially empty
vector<point> vector<Point> test;//Retrieve the vector nested in the contours
std::copy(pointList[ii].begin(),pointList[ii].end(),back_inserter(test));//Copy the vector the current n-th contour [ii]  and then your code 

 for (size_t i=0; i<test.size(); i++) {

// Vec3b p = image.at<vec3b>(test); image.at<Vec3b>(test); // bgr image Scalar p p = image.at<uchar>(test[i]); // grayscale version version//Modify here for gray scale pixels.push_back(p); }

}

then access to the required value as p.val[0];

Need to check with my other images but at this time think is working