Ask Your Question

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

asked 2019-02-18 01:09:23 -0500

updated 2019-02-18 03:22:02 -0500

Suppose there is black background with a gray object.

image description

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

image description

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 flag offensive 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.

berak gravatar imageberak ( 2019-02-18 02:19:08 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-02-19 03:22:17 -0500

The task is solved using convexHull() function.

image description

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];
convexHull(contours_dots, conv_hull);
edit flag offensive delete link more


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

LBerger gravatar imageLBerger ( 2019-02-19 03:53:11 -0500 )edit

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.

ya_ocv_user gravatar imageya_ocv_user ( 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

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

Question Tools

1 follower


Asked: 2019-02-18 01:09:23 -0500

Seen: 94 times

Last updated: Feb 19