I have to find the exact centroid of multiple rectangles. The coordinates of each rectangle are as follows:
coord = (0.294792, 0.474537, 0.0989583, 0.347222) ## (xcenter, ycenter, width, height)
I have around 200 rectangles, how can I compute the centroid of them?
I already tried to implement it, but the code did not work well.
My code:
for i in range(len(xCenter)):
center = np.array((xCenter[i]+(Width[i]/2), yCenter[i]+(Height[i]/2)))
This is a somewhat vague question, but if you mean the centroid of all rectangles by area, then each center of a rectangle is weighted by the area of the rectangle. Think of it as the all the mass of the rectangle being compressed into the center, and then having to take the centroid of several weighted points. The formula for that would be the sum of 1 through n (assuming rectangles are numbered 1 to n) of Area(Rec(i)) * vec(center(i)) all divided by the total mass of the system (the sum of all the areas). If you are referring to the centroid of the area in general, ignoring rectangle overlap, that is a little more tricky. One thing you could do is for each rectangle, check it against all other rectangles, and if a pair of rectangles overlap, split them up into a set of non-overlapping rectangles and put them back into the set of rectangles. Once all rectangles are non-overlapping, find the centroid by mass.
Related
I have an image which I want to extract the pixels of the specific part of that. This part is a quarter circle, and my desire is to obtain the pixels of that.
I have the coordinates of the center and points which the lines connected to the circle. How is it possible to extract one quarter and ignore other parts?
The disk has the equation (X-Xc)²+(Y-Yc)²≤R².
The half planes meeting at the center have equations c(X-Xc) + s(Y-Yc)≥0 where c and s are the cosine and sine of the angle.
Hence, scan the image (or just the bounding box of the circle) and consider the pixels (X, Y) such that the three constraints are satisfied.
I need the approximate radii of the following ellipse.
The bottom/top and left/right radii should be the same nevertheless need to be checked. Which means 4 radii should be the result of my code. I did the following in paint, the green circle should give me the top radius and red the left (the right and bottom one aren't drawn here).
The idea I'm working on is to crop the image (left/right/top/bottom side) and approximate circles to the cropped images. With the cv2.findContours-feature some white pixels get recognized as highlighted here.
Is there a way to approximate my drawn red circle from above with these given coordinates? The problems I've seen on the internet are all with a given center point or angle which I don't have. Is there a cv2 function that draws circles with only some given coordinates or something similar?
Use this function : cv2.fitEllipse(points) and pass contour points -Ziri
Yes this did the trick. I got the radii after your function with:
(x, y), radius = cv2.minEnclosingCircle(i)
I am working in OpenCV/Python and I came up with this problem. I have used cv2.minAreaRect() to get the Bounding Box surrounding a set of points. Is there any other function/generic algorithm that can give me the largest rectangle inscribed within a polygon (set of points)? I have a set of points of the polygon and the function should be able to return 4 points of the largest rectangle inscribing the input points.
Here is an example of a similar kind of problem
Thanks. Any help is highly appreciated.
I can provide you with those set of conditions which can lead you to your desired result but can't provide you with the code at present because it's very time taking for me. so you have to code for yourself in the mean time.
here are the conditions to follow.
Filter all the coordinates of the polygon for all 4 set of coordinates which satisfy these below mentioned condition for coordinates: [(a,b),(c,d),(e,f),(g,h)]
1.(a-c)=(e-g) as opposite sides should be equal
2.(b-f)=(d-h) as opposite sides should be equal
3.(d-f)^2+(c-e)^ = (b-h)^2+(a-g)^2 as diagonals should be equal
if these conditions are satisfied you will get all the set of four
coordinates which are rectangles.After that
4.Filter out all the coordinates received by checking if any polygonal
coordinate is falling inside the rectangle, that's easy.
now you are left with all the possible inbound rectangles, now all you have
to do is
list all the areas possible
max out the list for maximum area.
I'm working on a project to find defective parts on a metal ring.I successfully find defective parts on the surface but I cannot detect the protrusions on the inner surface of the metal ring.
I thought to determine the error using the distance between the inner and outer surface, but I don't know how I can calculate the distance between the two contour.
sucsess, frame = capture.read()
kernel = np.ones((1,1),np.uint8)
blur = cv2.bilateralFilter(frame,6,140,160)
threshold = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,21,16)
closing = cv2.morphologyEx(threshold,cv2.MORPH_CLOSE,kernel)
erosion = cv2.erode(closing,kernel,iterations =0)
contours, hierarchy = cv2.findContours(erosion,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
area = cv2.contourArea(cnt)
if area > 72000 and area < 80000:
cv2.drawContours(frame,cnt,-1,(0,0,0),3)
for cnt2 in contours:
area2 = cv2.contourArea(cnt2)
if area2 > 30 and area2 < 200:
cv2.drawContours(frame,cnt2,-1,(0,0,0),3)
cv2.imshow("frame",frame)
cv2.imshow("Erosion",erosion)
cv2.waitKey(0)
This is my code. First image is the object I am looking, second image is the output of the erosion.
enter image description here
enter image description here
My main problem is I am not able to detect any protrusion inside inner radius.
Any suggestion and help are welcome.
I thought to determine the error using the distance between the inner and outer surface, but I don't know how I can calculate the distance between the two contour.
One method is to take both contours and calculate the centroid, giving you a nominal centre point. Then from this point, cast rays out through 360 degrees, and compute the points of intersection with the inner and outer contours. (Closest point by Euclidean distance for example.) Then you have two corresponding radii on both the inner and outer surface, so you can subtract inner from outer to get the ring thickness. Compute this all the way around, using an angle increment proportional to the accuracy you need. The standard deviation of the thickness all the way around is a measure of protrusions (lower numbers are better!).
My main problem is I am not able to detect any protrusion inside inner radius.
If you are only interested in the inner radius, another way is to take the extracted contour from the inner surface, and again compute the centroid to find a nominal reference point. Take the average distance from this centre to each point on the contour, and that gives you the ideal fitted circle. Compute the distance from this ideal circle to each closest point on the actual contour and that gives you a measure of the perturbations.
This problem is in 3D space.
There is a rectangle, defined by 4 vertices. We rotate it around one of its sides.
There is a triangle, defined by 3 vertices.
After a full 360 degree rotation, will the rectangle ever intersect/touch the triangle?
If so, what is the angle of rotation at which intersection first occurs? And what is the point of this first intersection?
After thinking about this for a while, it seems like there are 3 main cases:
triangle vertex touches rectangle surface
triangle surface touches rectangle vertex
triangle edge touches rectangle edge
And there are 2 unlikely cases where the two are perpendicular when the intersect:
rectangle edge hits triangle surface
rectangle surface hits triangle edge
However identifying these cases hasn't really gotten me closer to a solution. I'm hoping someone can point me in the right direction for how to solve this problem. I want to solve it fast for a small number of rectangles x a large number of triangles.
Context: the larger problem I'm trying to solve is I want to wrap a rectangle around a closed polygonal mesh. I wish to do this step by step by rotating the rectangle until it intersects, then rotating the remaining rectangle around the intersection point, etc.
When you rotate a rectangle around one of its sides, you get a cylinder. Intersect each of the lines with the cylinder. The position of the intersection points gives you the rotation angles. Since this doesn't catch the case where the triangle is completely contained within the cylinder, test whether the vertices' distance to the cylinder's axis is smaller than the cylinder's radius, too.
Say your rectangle has the vertices A to D. You want to rotate around the side AB. The radius of your cylinder is then r = |AD|.
First, transform the coordinates so that the rectangle is placed with the side that you want to rotate about along the z axis and the adjacent side along the x axis.
A′ = {M} · A = {0, 0, 0}
B′ = {M} · B = {0, 0, |AB|}
C′ = {M} · C = {r, 0, 0}
Apply the same transformation {M} to the vertices of the triangle.
Now find the intersections of all three sides of the triangle with the cylinder. Because the cylinder is aligned to the z axis, the problem can be separated into two subproblems: (1) Find any intersections with the top and bottom surfaces a z == 0 and z == |AB|. (2) Find the intersections with the "coat" of the cylinder; this is the intersection of a line with a circle in the xy plane.
You can then calculate the rotation angles with the tangent function of the y and x coordinates of these points as atan2(y, x).
If you need the coordinates of the intersection points in the original coordinates, don't forget to undo the transformation.