OpenCV Centroid of Irregular Shape - image-processing

how do you get the centroid of an irregular shape using OpenCV?

I'd recommend looking at the cv::Moments (C++) or cvMoments (C) function.
This StackOverflow thread gives some example code for a problem very similar to yours.
This post goes into some of the theory related to finding object center-points.

What do you mean by centroid?
If it's the center of mass, you can compute the average of the coordinates of the points that are inside your shape. But the center of mass can be outside the shape, for "irregular" (non-convex) shapes.
If you want the point inside the shape that is the further away to any of the contour point, you can have a look at distTransform function.

Related

why the curve have been into a straight line in image [duplicate]

There a is an ellipse on the picture,just as following.
I have got the points of the contour by using opencv. But you can see the pictrue,because the resolution is low, there is a straight line on the contour.How can i fit it into curve like the blue line?
One Of the method to solve your problem is to vectorize your shape (moving from simple intensity space to vectors space).
I am not aware of the state-of-art in this field. However, from school information, I can suggest this solution.
Bezier curves, you can try to model your shape using simple bezier curve.This is not a hard operation you can google for dozen of them. Then, you can resizing it as much as you want after that you may render it to simple image.
Be aware that you may also Splines instead of Bezier.
Another method would be more simple but less efficient. Since you mentioned OpenCV, you can apply the cv::fitEllipse on the points. Be aware that this will return a RotatedRect which contains the ellipse. You can infer your ellipse simply like this:
Center = Center of RotatedRect.
Longest Radius = The Line which pass from the center and intersect with the two small sides of the RotatedRect.
Smallest Radius = The Line which pass from the center and intersect with the two long sides of the RotatedRect.
After you got your Ellipse Parameters, You can resize it as you want then just repaint it in the size you want using cv::ellipse.
I know that this is a pseudo answer. However, I think every thing is easy to apply. If you faced any problem implementing it, just give me a comment.

Defining a 3D scene from a photo of a circle

Given a photo containing a circle, for example this photo of a fountain:
is it possible to define the 3D position and rotation of the fountain in relation to the camera?
I realise we have to define the scale, so lets say the fountain is 2m wide (the diameter of the circle consisting of the inner rim of the fountain is 2m).
So assuming the circle is a perfect circle, and defining the diameter to 2m, is it possible to determine how the circle and the camera relate spatially? I dont know any camera matrix or anything, the only information i have is the picture.
I specifically want to determine the 3D coordinates of a given pixel on the rim of the fountain.
What would be the math and/or OpenCV code to do this?
Circle with perspective is an ellipse. So you basicly you need an ellipse detector.
This algorithm should work:
Detect all ellipses in the given image.
Filter ellipses that you think they are not a circles in origin. (This is not possible using just 1 Camera so you have to depend on previous knowledge. Something like that you knows that you are taking a photo for a circle).
mmm I stopped typing here and bring a paper&pen and started figuring how to estimate the Homography and it is not that easy! you should deal with the circle a special case of an ellipse and then try to construct a linear system of equations. However, I made quick googling :
https://www.researchgate.net/publication/265212988_Homography_estimation_using_one_ellipse_correspondence_and_minimal_additional_information
http://www.macs.hw.ac.uk/bmvc2006/papers/306.pdf
Seems very interesting topic, I am going to spare sometimes on it later!

OpenCV circle distortion detection

OpenCV has capapabilities to compensate for distortion in patterns, such as a this board, for example:
Every example I ever saw for this process does it with grids or squares. I would like to know if something similar exists for a single circle. My practical case is that I detect an ellipse, and I need to calculate the angle between the plane of this ellipse and the projection plane where the ellipse is projected as a circle. I managed to achieve that in my own code, but I would like to know if there is something built into the library to that purpose.
Use the ellipse axes to your advantage
I don't know of any "circular projection" as you name it, but I'm thinking that you can rephrase your problem into having the solution already.
Images make any answer SO cool.
Forget the ellipse, take the axes
A circle can be thought of as 2 vectors with unit norm defining a plane.
The projected circle's axes you estimate are the projection of the unit referential into the 3D plane
Then for projecting back and forth is just an affair of applying the transformation described by the estimated axes vectors

centroid ellipse MSER OPENCV

I am working on an image registration method and I would like to work with region based feature detectors. As representative and because it is already implemented in opencv, i thought of MSER.
I know how to detect the MSER regions.MSER detector gives the MSER regions inside of a vector of points, a contour.I would like to retrieve the centroid of these contours. I could fit a ellipse on them, but then I don't as well how could I retrieve the centroid of these ellipses.
Does someone know if there is an already implemented function that could take care of this task? Or do i have to develop an algorithm?
The reason is that I would like to perform the point correspondence using this centroid points as interesting points.
Thanks
Iván
The centroid of the region can be computed by calculating the mean of all the x values and the mean of all the y values. The resulting (meanX, meanY) point is the region's centroid.

Calculating object position from image processing

I am looking for an efficient way to calculate the position of an oject on a surface based on an image taken from a certain perspective.
Let me explain a little further.
There is an object on a rectangular flat surface.
I have a picture taken of this setup with the camera positioned at one of the corners of the surface area at a rather low angle.
On the picture I will thus see a somewhat distorted, diamond-shaped view of the surface area and somewhere on it the object.
Through some image processing I do have the coordinates of the object on the picture but now have to calculate the actual position of the object on the surface.
So I do know that the center of the object is at the pixel-coordinates (x/y) on the picture and I know the coordinates of the 4 reference points that represent the corners of the area.
How can I now calculate the "real world" position of the object most efficiently (x and y coordinates on the surface)?
Any input is highly appreciated since I have worked so hard on this I can't even think straight anymore.
Best regards,
Tom
You have to find a perspective transformation.
Here you may find an explanation and code in Matlab
HTH!
How good is your linear algebra? A perspective transformation can be described by a homography matrix. You can estimate that matrix using the four corner points, invert it and the calculate the world coordinates of every pixel in your image.
Or you can just let OpenCV do that for you.

Resources