Hello everyone, I am relatively new in OpenCV (Already checked some tutorials and I am learning on some additional programs how it works) and I know some things, however I have an assignment to work with in opencv (spin the bottle game) which goes somehow a little out of the scope of what I know.

Based on the direction that a bottle (Yes, a common bottle like a beer bottle) points in a 2d plane (a photo of a bottle) I need to create an opencv method to draw a line where the bottle points at. I heard something about Houghlines and I am currently working on that, however if you have some advices or know about other Opencv library functions that I can use, then I would be very thankful to you.

Pd: I have already decomposed the image into its edges (in case this can help to give any idea)

edit retag close merge delete

Sort by ยป oldest newest most voted

If you can get this clean segmentation of the bottle each time you perform it the following steps are quite easy!

1. Your image is binary and thus contains a cloud of edge points indicating your bottle.
2. Now put all the points that have the edge pixel value into a vector of points.
3. Apply PCA on the point cloud and look for the eigenvalues and principle components.
4. This will result in the direction that the bottle is pointing to and allow you to draw a line!

More info on PCA in OpenCV can be seen in this example.

more

1

The drawback of this approach is that you only get the direction without a sign, so another step is needed. I'd propose to use the angle of the main Axis and to rotate the image so that you have an image of the bottle upright (or upside down). You then check the topmost and lowest columns and compute the horizontal variance of the white pixels. The variance at the bottom of the image will be much larger and you are able to tell in which direction your bottle is pointing.

( 2015-04-20 04:45:04 -0500 )edit
1

Indeed, getting the orientation is indeed the second part of the problem :D But I guess that getting the overal direction is already quite challenging for TS.

( 2015-04-20 04:53:48 -0500 )edit
1

If you have the direction and the midpoint of the contour, you can split the contour into 2 parts (on the "left" and "right") relative to the midpoint and in the found direction, and compute the area of the 2 contours. The one that has the smallest area is the top of the bottle, so that is the pointing direction of your bottle. Maybe it is not the best, but is an idea of continuation.

( 2015-04-20 07:24:20 -0500 )edit

Actually it is a quite good solution which will work just fine here :)

( 2015-04-20 08:25:10 -0500 )edit
1

Hey, thanks for all of the ideas. You are awesome guys. I will keep asking here because this is my first approach, later I will have to cope with several objects (identify them) and also to use several multicolored backgrounds and so on. But for now it's really good.

( 2015-04-24 05:29:04 -0500 )edit

Official site

GitHub

Wiki

Documentation