Drawing a rotating rectangle around shapely polygon - python

i have polygon coordinates and i have plotted a polygon using PolygonPatch. Next up, using moments i have center coordinates (x,y) , height and width(X,Y) and rotation angle (theta). using these parameters i want draw the bounding rectangle around my polygon . i can draw these rotated rectangle using opencv
rect = cv2.minAreaRect(cnt)
box = cv2.cv.BoxPoints(rect)
box = np.int0(box)
cv2.drawContours(im,[box],0,(0,0,255),2)
but this only works when i am working on images not on shapley polygon patch. Is there a way i can draw the rotated rectangle around ploygonpatch without converting them to images.

Related

draw arc with different arguments

I'm using Pygame to draw some shapes on the screen.
The problem is, I get those shapes from a different application, which exports a txt with the parameters of the arc, like this:
ARC1:
start_x=,
start_y=,
center_x=,
center_y=,
end_x=,
end=y=,
radius=,
start_angle=,
end_angle=
These are the parameters pygame needs to draw an arc:
surface (Surface) -- surface to draw on
color (Color or int or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A])
rect (Rect) -- rectangle to indicate the position and dimensions of the ellipse which the arc will be based on, the ellipse will be centered inside the rectangle
start_angle (float) -- start angle of the arc in radians
stop_angle (float) -- stop angle of the arc in radians
This function asks a rect, is there a way to calculate the rect with the parameters I have (from the txt) and keep its original shape and position?
The rect argument of pygame.draw.arc specifies the bounding rectangle of the circle the arc is on:
rect -- rectangle to indicate the position and dimensions of the ellipse which the arc will be based on, the ellipse will be centered inside the rectangle
The bounding rectangle of a circle with a given center and radius is:
bounding_rect = pygame.Rect(center_x-radius, center_y-radius, radius*2, radius*2)

How to rotate bounding box x,y,w,h format rectangle for a given rotation angle say -90 degrees in an image using python?

I have got a rectangle with x_90,y_90,w_90,h_90 = 2212,642,30,90 respectively. 90 degree rotated image height,width = 2339,1654
Now I need to rotate based on the orientation angle detected. In this case, it is 90 degrees hence, it needs to be rotated by -90 degrees. This should result in the rotated point as x_0,y_0,w_0,h_0 = 642,95,90,30 respectively in the image. 0 degree rotated image or original image height,width = 1654,2339 .
I Am unable to rotate since I don't know how to do it in this case since the origin of the image starts from the top-left corner of the page

How to rotate coordinates taking into account that OpenCV adds black borders to the image when rotating with WarpAffine?

I have this image:
and I have the coordinates of the white circle's center. I have this other image:
, they're the same image but the second one is rotated 4° using OpenCV's WarpAffine. What I want to do is calculate the white circle's center coordinates in the 4° image, taking into account that OpenCV's WarpAffine adds black borders to the image when rotating.
You can multiply the coordinates of the circle on the transformation matrix from WarpAffine: (x* y* 1) = TrMat * (x y 1)T
Theory is here. In Python: numpy.dot

Pygame circle collision?

I am using pygame to make a simple game. I am having issues with circle collisions. I am getting the following error:
"AttributeError: 'pygame.Rect' object has no attribute 'rect'"
Here is the particular code I am having issues with below:
if pygame.sprite.collide_circle(hero_circle, enemy_circle):
gameover()
Use pygame.mask to create a collision mesh for your objects and use the mesh to do collision detections.
In more detail:
Create an image file for both of your circles and set the bg color to something you will not use anywhere else.
Set that color to "transparent" in your image editor.
Import the images.
Create a mesh for them with pygame.mask and set it to make transparent pixels non-collidable.
Use the generated mask as your collision detection mesh.
PROFIT
(Technically this is just doing collision detection of a circle shaped area on a rectangle, but who cares!)
pygame.draw.rect()
draw a rectangle shape
rect(Surface, color, Rect, width=0) -> Rect
Draws a rectangular shape on the Surface. The given Rect is the area of the rectangle. The width argument is the thickness to draw the outer edge. If width is zero then the rectangle will be filled.
Keep in mind the Surface.fill() method works just as well for drawing filled rectangles. In fact the Surface.fill() can be hardware accelerated on some platforms with both software and hardware display modes.
The best way I've found to check circle collision detection is to calculate the distance between the center points of two circles. If the distance is less than the sum of the two circle's radii, then you've collided.
Just like how gmk said it but if your are using circles instead of rectangles, you should use this pygame function :
pygame.draw.circle(surface, color, center_point, radius, width)
This draws a circle on your surface (which would go in the surface area). Clearly the color requires a list of numbers (RGB anyone?). Your center_point decides the location of your circle since it will be the location of the center of your circle. The radius will need a number to set the radius of the circle (using the number like 25 will set your radius at 25 pixels/diameter at 50 pixels). the width section is optional as it sets the thickness of the perimeter of your circle (having 0 will have none at all). If you are not using circles, you should change your title... But anyways, I hope this helps you!

OpenCV, area between two curves

I work with OpenCV library in Python.
The question is how to select in separate roi the area across two curves?
Curves are defined by two quadric polynoms.
I want to find count of black pixels at the area restricted between curve 1 and curve 2
You can create mask by drawing ellipse, but you should have the following data from your equation,
center – Center of the ellipse (here I used centre of image).
axes – Half of the size of the ellipse main axes (here I used image size/2 and image size/4 respectively for both curve).
angle – Ellipse rotation angle in degrees, (here I used 0)
startAngle – Starting angle of the elliptic arc in degrees. (here I used 0)
endAngle – Ending angle of the elliptic arc in degrees.(here I used -180)
If you got the above data for both curve, you can simply draw ellipse with thickness=CV_FILLED like,
First draw largest ellipse with color=255.
Now draw second ellipse with color = 0.
See an example,
Mat src(480,640,CV_8UC3,Scalar(0,0,0));
ellipse(src,Point(src.cols/2,src.rows/2), Size (src.cols/2,src.rows/2), 0, 0,-180,Scalar(0,0,255), -1,8, 0);
ellipse(src,Point(src.cols/2,src.rows/2), Size (src.cols/4,src.rows/4), 0, 0,-180,Scalar(0,0,0), -1,8, 0);
Draw it on a single channel image, if you want to use it as mask.
Edit:-
To find the area, draw above to single channel image with color=255.
Then use countNonZero to get white pixel count.

Categories

Resources