Ask Your Question
0

Finding index of an image

asked 2013-12-11 03:14:56 -0500

ati gravatar image

I could find contours of an object in an image by using findContours.

and by using this I could crop the object contours.

Dist.copyTo(crop, mask);

now, I need to find the index of crop Image in Dist image.

to do that I converted the Mat into the float to pointer and I called it cropped!

and then I tried this:

for(int j=0; j < (Dist.rows * Dist.cols); j++){
            if(cropped[j] != 0 ){
                row = (j / width) + ((Dist.rows));
        col = (j % width) + (Dist.cols);

            }
            Index.push_back(std::make_pair(row,col));
            }

But it does not give me any correct answer...

edit retag flag offensive close merge delete

Comments

1

not sure about what you're trying to achieve. What do you mean when you say you want to find the index of the crop image? In the code you wrote it seems that you are going through each pixel of the image and if it isn't black you push the coordinates in the Index vector. In this way you'll obtain a vector with the coordinates of all the nonblack pixels in cropped. Is that what you want? If it is I think that you are pushing back the wrong coordinates. You should calculate row and col like this: row = (j/Dist.rows) + (j%Dist.rows); col = j%Dist.cols;

Raul Andrew gravatar imageRaul Andrew ( 2013-12-11 03:54:24 -0500 )edit

@Raul: Thanks for the answer. This is what I have done until now: I used ROI to just have the object in the image. then by using the findcontour I could find contours of the object. after that I used crop to have access to the contours values.Now I want to get access to index of this values. BUT, I want their indexes in the main image(before ROI). I want to know what is the index of a specific contour in the Image before ROI!

ati gravatar imageati ( 2013-12-11 04:11:36 -0500 )edit
2

Ok, now I got it. If using a ROI before finding the contours is necessary, you can add to row and col the origin of the ROI as an offset: row = (j/Dist.rows) + (j%Dist.rows) + roiOrigin.y; col = j%Dist.cols + roiOrigin.x;

Raul Andrew gravatar imageRaul Andrew ( 2013-12-11 05:52:32 -0500 )edit

Thank you.

ati gravatar imageati ( 2013-12-11 07:02:55 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2013-12-11 07:15:24 -0500

Raul Andrew gravatar image

Ati, I'm writing the solution as an answer so you can accept it for other users to see it.

You have to add the origin of the ROI as an offset:

row = (j/Dist.rows) + (j%Dist.rows) + roiOrigin.y;

col = j%Dist.cols + roiOrigin.x;

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-12-11 03:14:56 -0500

Seen: 427 times

Last updated: Dec 11 '13