Detect geometric object on video stream and reconstruct its contours

I'm trying to detect a plastic object on video stream using OpenCV 2.4.9 on C++. The object has six angles and its contour looks like : .
For each frame I'm doing some kind of segmentation and edges detection. After these operations i got some binary image containing corrupted contours of the object and some noise from background.
For example: or

I need somehow to detect my object here and restore contours. Could you advise some methods? I need a fast method since I want to run this program on android phone.

I know the proportions of my object. And the camera is always approximately at normal angle to the object's surface.
Sometimes when the contour is not corrupted much i can find the correct bounding box, but in other cases I can't.
I think that I need to use somehow the information about object's geometry here. I will appreciate any help!

UPD :
If I have found a partial contour of the object, is it possible to fit My shape somehow inside found contour to obtain missing lines?


Given the shape is a fairly regular polygon, have you tried running Hough Lines and computing intersections to find the vertices ?

Even if you can't get all the vertices, you should be able to reconstruct the missing ones if you get 4 of 6.

The following uses Hough Probabilistic Lines to identify likely line segments. Setting the minimum line length can avoid a certain amount of noise.

I then use kmeans on the end points of all discovered line segments to identify 6 likely vertices.

Results were OK for the one image I tested on but you may need to do some additional outlier removal if you have a lot of other artefacts in your inout images

Mat image = imread(image_name);

// Convert to grey scale    
Mat grey;
cvtColor(image, grey, CV_RGB2GRAY);

// Invert colour scheme
grey = 255 - grey;

// Find Hough probabilistic lines
vector<Vec4i> lines;
double rho( 1 );
double theta( M_PI / 180.0 );
int thresh( 10 );
double minLineLength(20.0);
double maxLineGap( 5.0);
HoughLinesP(grey, lines, rho, theta, thresh, minLineLength, maxLineGap);

// Store end points of these segments as vertices
vector<Point2f> vertices;
for( int i=0; i<lines.size(); i++ ) {
    float x1 = lines[i][0];
    float y1 = lines[i][1];
    float x2 = lines[i][2];
    float y2 = lines[i][3];
    vertices.push_back(Point2f(x1, y1) );
    vertices.push_back(Point2f( x2, y2) );
}

// Run kMeans on line ends to find 6 centres which we assume are verts
TermCriteria criteria(TermCriteria::EPS+TermCriteria::COUNT, 500, 1.0);
int attempts(20);
Mat centres, labels;
kmeans(vertices, 6, labels, criteria, attempts, KMEANS_PP_CENTERS, centres );

// Plot them on the RGB image
for( int i=0; i<6; i++ ) {
    Point2f v1 = Point2f( vertices[i].x-2, vertices[i].y-2 );
    Point2f v2 = Point2f( vertices[i].x-2, vertices[i].y+2 );
    Point2f v3 = Point2f( vertices[i].x+2, vertices[i].y-2 );
    Point2f v4 = Point2f( vertices[i].x+2, vertices[i].y+2 );
    line(image, v1, v4, Scalar(255,255,0));
    line(image, v2, v3, Scalar(255,255,0));
}

imshow( "Verts", image );
cout << centres << endl;

You can see that the topmost two points are practically co-located. I'm guessing this is because there's a couple of missing vertices. Still, the others are pretty well placed and you may be able to recover the missing ones through the shape's symmetry.

链接地址: http://www.djcxy.com/p/58728.html

上一篇: 如何裁剪凸面缺陷?

下一篇: 检测视频流上的几何对象并重建其轮廓