# How to locate an object on the image? [closed]

Suppose there is black background with a gray object.

What is the best way to determine a minimal upright rectangle which contains it? For simplicity, let's think that there is no noise or other objects around, but the object itself may be fractured. The simplest approach of findContours(), boundingRect() is not workable. The standard findContours() renders the following output

The object remains broken because of the large gaps in its outer edges. In fact, what I need is yet another contour which embraces all of the existing.

edit retag reopen merge delete

### Closed for the following reason the question is answered, right answer was accepted by ya_ocv_user close date 2019-02-19 03:31:59.550702

The simplest approach of findContours(), boundingRect() is not workable.

you probably need to explain, why it is so, and show resp. code.

( 2019-02-18 02:19:08 -0500 )edit

Sort by ยป oldest newest most voted

The task is solved using convexHull() function.

vector<vector<Point> > contours_poly(contours.size());
vector<Point> contours_dots;
vector<Point> conv_hull;
Point dot;

for (size_t i = 0; i < contours.size(); i++)
{
approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);
}
for (size_t i = 0; i < contours.size(); i++)
{
for (size_t j = 0; j < contours[i].size(); j++)
{
dot = contours[i][j];
contours_dots.push_back(dot);
}
}
convexHull(contours_dots, conv_hull);
contours_poly.push_back(conv_hull);

more

you closed question but who said that "the question is answered, right answer was accepted" except you?

( 2019-02-19 03:53:11 -0500 )edit
1

I said :) Usually the author of a question closes it then he is satisfied. This particular question is closed, but not this topic. I am going to ask more about this.

( 2019-02-19 07:18:52 -0500 )edit

Sorry I forgot to read who is author question. As you are the author you are free to answer and close your question. Sorry again

( 2019-02-19 08:29:13 -0500 )edit

Official site

GitHub

Wiki

Documentation