Python OpenCV - Reset image and look for new circles - python

I want to detect circles of a shooting target. So the target might have circles on there already, but I am only looking for the NEW bullet holes.
What I was thinking is to start with the target, sort of know where the circles are with the target not being shot. Then once you have that, its almost like greenscreening the current circles so they will not be detected again. From there I would then to pickup only the bullet holes.
How would this be done ? And is this the best approach?

Related

Adjusting perspective of a video with OpenCV so that predicted ball trajectory matches reality? (Python)

So, I have been working on a cricket ball tracking project and have managed to do ball detection and tracking fairly successfully. The next step is to predict the future trajectory, using polynomial regression.
Below is an illustration of the stage I am at. The blue dots represent the last few ball locations before the ball reaches the batsman.
Now, if I were to apply regression on these points to predict the future path (i.e. where the ball would go if it is not intercepted), I might get something like this:
The issue is, if I were to carry this red line on, it would appear to pass above the stumps and carry on due to the perspective from which the video is taken when in reality, the ball would actually go on to hit the stumps and not carry on past the batsman. However, this is the only viewing angle I have.
(For reference, the stumps are the three sticks at either end of a cricket pitch:
In the image on which I've plotted ball positions, the batsman is partially obscuring the stumps.)
I am unsure on how to fix this issue however.
Is it possible to apply some kind of perspective transform to the video frames so that the viewing angle is improved, and the predicted trajectory might more closely match reality?
Alternatively, is it possible to compute the height of the ball above the ground? Then I can compare this to the height of the stumps (known, both in pixels in the video frames, and in real life). I also know the (x,y) coordinates of a single point when the ball is in contact with the ground (height = 0).
Lastly, I believe I can also calculate approximate 3D depths - i.e. at any point, how far along the pitch is the ball - if this may help. I haven't tried to implement this, however, because I wasn't sure how depths would fix my problem.
Any suggestions would be much appreciated because this is the last hurdle on a project I've spent a lot of hours on!

How can I make a rectangle in pygame be able to check for other rectangles around it?

As part of my current pygame project (I am very new to pygame), I have created multiple rectangles that move at random around the screen. As one of the features of this game, I want to make it so if one rectangle is close enough to another one, it moves towards it. Another feature I want to add is that rectangles cannot collide with each other, but I don't want to just do the regular
if rectangle1.colliderect(rectangle2):
rectange.x -= 10 # That is, it's previous position
because it will make the animations look odd.
The main way I can see to solve these problems is to use some sort of function that could check if a rectangle.x - 30 is another rectangle (or something similar), but I am unaware of one that exists.
I have attempted to look through google, but I haven't found anything as all the posts are different problems that aren't quite the same.
Thank you for any responses!
Use inflate to create a rectangle that is larger than the original one and surrounds it. Use this rectangle to find other rectangles in range:
test_rect = rect1.inflate(dist, dist)
if test_rect.colliderect(rect2):
# [...]

Does anybody have suggestions for how you would tackle this problem in OpenCV?

I am trying to find the Australian Aboriginal flag in a collection of historic artwork.
Aboriginal Flag: https://i.imgur.com/sGsnLkv.png
Example Artworks: https://imgur.com/a/t6RisPx
I am new to OpenCV but have been programming for a long time.
I am confident I can code the solution but I can't seem to figure out what an easier solution would look like.
Should I;
Gray scale, find contours, find circles that exist inside rectangles where the area of the circle is a certain percentage area of the rectangle?
Find red, yellow, black shapes that have close proximity?
Gray scale, find circles that exist in rectangles. Then check if the circle is yellow in the original?
Anyone have any cool ideas on how to make this easy?
I plan on running this over 2.8 million images ahaha
My original reason is here -> https://news.ycombinator.com/item?id=24187794

How do I determine correct wall pieces in a random 2D dungeon I made?

I've made a random 2D tile-based dungeon generator in Python (and PyGame), which is a basic 2D array of wall and floor tiles, and it works great, filled with rooms and corridors. But now I want to go around the rooms and corridors with nicer wall sprites (top-left corner, top-right corner, etc).
EDIT: To expand a bit without going into the whole process, an empty 2D array is created (as the level map) consisting of 0 values (empty). Rooms of random size are "placed" at random locations within the 2D array, and linked by corridors. Rooms and corridors are given a value of 1 (floor). It's easy to just outline the rooms and corridors with a single square wall sprite, but I want to have dynamic walls that have shape to them (corners, ends, that sort of thing).
I have tried a few different ways - generating the dungeon then checking edges and replacing wall parts, or creating the wall parts around each room as it's generated, but there's always problems where the corridors cross into rooms, and the order in which the wall parts are determined.
I've searched Google, Roguebasin, etc but I can't find anything about this. Every random generator I find just uses 2 simple sprites (floor and wall). I don't want my "walls" to look generic. I also wanted to steer clear of prefab rooms because I want it to be completely random.
I've been stuck on this for 2 weeks. I'm making my own engine in Python, and there are lots of examples of random dungeon generation, but I want to go a step further and make the dungeon look great with nice corners, horizontal and vertical walls, etc.
Do I incorporate the wall parts into the generation? Or do I go over the generated map afterwards with an algorithm for choosing the correct parts, or is there something else I've not thought of? Or am I punching too high, should I stick with a basic roguelike setup?
Here's a mock-up of how I want it to look (or similar). As you can see, I'm not a stranger to pixel art and sprites, but I want the computer to be able to randomly generate the rooms and corridors, AND populate the correct wall parts:
I found a video on Unity's Tile Rule feature, and setting up rules for tiles so that the tile reacts to the tiles around it.
Here is a link to the video I watched that gave me the idea:
Tilemap: Rule Tile
I have successfully now coded tile rules into my game, and it works really well - the map is randomly generated with rooms connected by corridors, and then works around the floor tiles, assigning the correct wall part based on my rules.
Hopefully this will serve as a signpost for any devs doing the same kind of work.
Here is a screenshot of a randomly-generated level with the walls now auto-generated:
And here are the rules I designed using Excel:
Ticks on a black background mean a wall is required in that space, a blue background means a floor is required in that space. I'm not sure if there is a quicker or better way of doing this, but it's worked for me.
I don't exactly understand you but I can say you need to define which "Tile" going to pair with which tile, like you don't want to pair ground tile with wall tile.
if you make that logic work with code you can make corner pieces pair with normal wall
or door pieces pair with walls.
and there is a lot of videos you can make a map an AI trained by a grayscale image that has understood your wall, ground, door placement and generates maps like your style.

Getting outline of a graph as a polygon in PyGame

I have a feeling that this is impossible (or at least very complicated), but I currently have a generated graph that looks something like this (excuse my terrible paint skills):
Now, I'd like to be able to create a polygon of the outline, I have the coordnates of all the nodes, but not the intersections. The best I can manage so far is the Gift Wrapping algorithm, which gives more of a rough outline of the polygon than anything else.
Does anyone have any ideas as to how I could go about this?
(I'm currently using PyGame)
You're going to want to figure out where intersections happen and make new nodes there.
Then you want to find an edge that's on the outer polygon. I suggest running a random ray in from infinity until it strikes an edge.
Then imagine yourself walking along that edge, keeping your left hand on the boundary and your right hand outside. Start walking.
When you hit a node, you turn, such that you don't cross any edge. That is, you begin to traverse the next edge in counterclockwise order. (A simple implementation of this would be to sort them by direction, using atan2().)
It's all basic high school algebra and trigonometry, but it might be a little rough if this is your first time programming anything of this nature. You'll learn a lot, though.

Categories