Ask Your Question
0

Create a Mat from coordinates stored in another Mat

asked 2018-11-13 08:19:16 -0600

toolelucid gravatar image

updated 2018-11-13 18:47:25 -0600

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

edit retag flag offensive close merge delete

Comments

just curious, -- what's the use-case of it ?

berak gravatar imageberak ( 2018-11-13 08:30:34 -0600 )edit

Itś modified

toolelucid gravatar imagetoolelucid ( 2018-11-13 08:30:48 -0600 )edit

can you add / make up some demo data, so folks can reproduce it here?

berak gravatar imageberak ( 2018-11-13 08:37:06 -0600 )edit

I edit the question, hope it helps

toolelucid gravatar imagetoolelucid ( 2018-11-13 09:03:35 -0600 )edit

êditing always helps ;)

berak gravatar imageberak ( 2018-11-13 09:11:49 -0600 )edit

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 ?

berak gravatar imageberak ( 2018-11-13 09:20:25 -0600 )edit
1

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)

toolelucid gravatar imagetoolelucid ( 2018-11-13 09:23:57 -0600 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2018-11-13 09:37:13 -0600

berak gravatar image

updated 2018-11-13 09:46:38 -0600

there might be no (vectorized) counterpart for what you're doing in the c++ api, but assuming your contour is a vector<Point> you could easily iterate over it:

Mat pixels; // initially empty
for (size_t i=0; i<contour.size(); i++) {
      Vec3b p = img.at<Vec3b>(contour[i]); // bgr image
      //uchar p = img.at<uchar>(contour[i]); // grayscale version
      pixels.push_back(p);
}

leaving you with a Mat with contour.size() rows and a single column

edit flag offensive delete link more

Comments

Thank you , let me check ireally apreciate it

toolelucid gravatar imagetoolelucid ( 2018-11-13 11:20:19 -0600 )edit

what you're trying is basically "sampling pixels under a contour", no ?

berak gravatar imageberak ( 2018-11-13 11:23:36 -0600 )edit

I have the following Error:

error: no matching function for call to 'cv::Mat::at (std::vector<cv::point_<int>>&)'

toolelucid gravatar imagetoolelucid ( 2018-11-13 11:31:05 -0600 )edit

correct, the contours I extract from an image, and those that I want to sample are from a different one, but I want the same points

toolelucid gravatar imagetoolelucid ( 2018-11-13 11:32:54 -0600 )edit

findContours() returns a vector<vector<Point>> (a vector of contours), this is for a single vector<Point> contour (please read the answer, again)

berak gravatar imageberak ( 2018-11-13 11:35:35 -0600 )edit

What if the point are stored like this ? vector<vector<cv::point>> pointList;

toolelucid gravatar imagetoolelucid ( 2018-11-13 11:42:12 -0600 )edit

you have to iterate over those, first, and maybe use contours[j].size() and contours[j][i]

berak gravatar imageberak ( 2018-11-13 11:44:41 -0600 )edit

Yep, itś suposse that i'm copying the vector<vector<point>> in the following

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
    uchar p = image.at<uchar>(test[i]); // grayscale version
    pixels.push_back(p);
    }
toolelucid gravatar imagetoolelucid ( 2018-11-13 11:44:50 -0600 )edit

let's close this. get a good night of sleep, and it'll be all clear tomorrow;)

berak gravatar imageberak ( 2018-11-13 11:48:00 -0600 )edit

Question Tools

Stats

Asked: 2018-11-13 08:19:16 -0600

Seen: 613 times

Last updated: Nov 13 '18