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//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
just curious, -- what's the use-case of it ?
Itś modified
can you add / make up some demo data, so folks can reproduce it here?
I edit the question, hope it helps
êditing always helps ;)
so, you have a list of y and x coords, and accessing img with those, you have a list of pixels. and then ? what is it good for ?
the img is basically the gradient magnitude of the original image that is employed for the obtention of the contours, and then for result of let's say:
meanT=img[np.array(coor[:, 1]), np.array(coor[:, 0])]
finalMean=np.mean(meanT)