python 2d packing irregular shapes one by one - python

the context:
an artpiece where i iteratively fit irregular blobs into a square and paint the outcome with my drawing robot.
the problem:
I have a bunch of irregular shapes that
can resize between a maxsize and a minsize
can rotate
are irregular in shape
and i need to place them, one by one, in a rectangular so i minimize the empty space.
Special problem constraints:
i need to place them, one by one, and do the computation only for the next item, i cannot use packing algos that know the next items, in plural, since that information does not exist.
.
My question:
What is the name of this problem ?
Can you give some directions on how to approach this, relevant papers etc?
my question is related to
Packing An Irregular Polygon with Different Sized Circles

Related

Irregular Polygon Sub-division?

I have a problem in which I have an irregular polygon shaped container which needs to be sub-divided such that it contains certain smaller rectangles of specific dimensions. I have previously tried addressing this problem using a bin packing approach which seems to be too time consuming. As a result I wanted to try out sub-division based approaches where the algorithm ensures that the sub-division contains certain smaller rectangles of specific dimensions while the remaining space can be considered as empty space. Any suggestions on fast algorithms for the generation of these sub-divisions preferably using the shapely library?
The irregular polygon container would look some thing like the diagram below. The edges of the container will always be right angles and it is known that items to be packed can all fit in to the given container with additional remaining free space. The constraints are the same as regular bing packing where the items should not overlap and all items should be completely within the container. The items can be rotated (preferably at right angles).

Divide a 2D depth image into non-overlapping rectangular areas of similar values

I am trying to segment a 2D depth image into non-overlapping rectangular areas of similar values as shown in the example:
In this example, the depth image is segmented into four rectangles: 4by3by6 yellow, 2by4by15 green, 4by2by8 orange, and 2by2by3 blue. Note that there is a min length and width limit to the rectangles, in this case, 2, so that the lower right corner is segmented into one area instead of two.
This is a simple example, the depth map can be a lot more complex than this. This is quite a challenging problem I don't expect it to be solved to optimality, but any solution more optimized than a grid of fixed resolution is appreciated.
I imagine this to be solved by some RANSAC process, similar to plane fitting, but I haven't figured out exactly how to do that. The problem also resembles image segmentation, where each segment is rectangular in shape and does not overlap.
Idea of an algorithm to solve this (not optimally ofc):
examine vertical neighbours for each element that could be grouped. Rule of grouping needs to be determined by you after some "trial and error". Single-element groups are also possible. Every element must be added to one and only one group.
iterate through groups and check whether horizontal neighbours of those are "close enough", if yes, then merge groups.
iterate through groups and check whether vertical neighbours of those are "close enough", if yes, then merge groups.
Continue steps 2 and 3 and finish when there was no single group merging during those
Finally, remove those groups that do not fulfill width/height requirements

How can I fit a perfect square onto a ndarray matrix containing the edges of a fuzzy image of an imperfect square?

Context: Microscopy.
I'm trying to stitch together small images (can refer to as sub-images or tiles) of a fuzzy grid to make a larger "mosaic" or "atlas" of the overall grid, such that these sub-images are lined up perfectly. The orientation is random overall, but consistent from tile to tile, and the four sides of a tile typically cut through several squares in the grid. I've used scikit-image to get the edges of each square separately as an Nx2 matrix (ndarray). What I think would be great is if I could fit a proper square (I do mean the kind with four sides and four right angles -- not a number multiplied by itself in case that's unclear) over each edge/silhouette of a square (I do have unrelated edges detected that pick up noise so need to get rid of those), but a) not sure how to do that and b) the squares themselves have noise--for example a piece of sample or other contaminant falling right on the edge of a square distorts the shape a bit.
In the images above, I'm showing one such sub-image/tile (I have others which line up as neighbors to this, with slight (~1%) overlap. I figure if I have the geometric centers of all of the squares, I'll have no trouble finding the correct dimensions for creating the larger mosaic by average distance between neighboring squares in the grid is consistent. Please feel free to berate me mercilessly if I'm going about this poorly, or ask any follow-up questions if I failed to mention anything relevant.
Also maybe worth mentioning: The script I'm attempting to write needs to do this in an automated, fast (no human interaction) way behind-the-scenes, and for other grid images not just the one you see here, and that's important because other images will likely be fuzzier, as this is what's called a "bare" grid, but others will have "nanowires" (imagine this but the black part is almost furry/hairy).
Thanks in advance!!

Spatstat: Given a list of 2-d points, how to connect them into a polygon, and further make it the study region?

Please allow me to start the question with a simplest taskļ¼šIf I have four points which are vertices of a rectangle, stored in a 4x2 matrix, how can I turn this into a rectangular window? (Please do not use any special command specific to drawing rectangles as the rectangle is raised just to represent a general class of regular geometrical object)
To make things more complicated, suppose I have a nx2 matrix, how can I connect all of the n points so that it becomes a polygon? Note the object is not necessarily convex. I think the main difficulty is that, how can R know which point should be connected with which?
The reason I am asking is that I was doing some image processing on a fish, and I managed to get the body line of the fish by finding the contour with opencv in python, and output it as a nx2 csv file. When I read the csv file into R and tried to use the SpatialPolygnos in the sp package to turn this into a polygon, some very unexpected behavior happened; there seems to be a break somewhere in the middle that the polygon got cut in half, i.e. the boundary of the polygon was not connected. Is there anyway I can fix this problem?
Thank you.
Edit: Someone kindly pointed out that this is possibly a duplicate of another question: drawing polygons in R. However the solution to that question relies on the shape being drawn is convex and hence it makes sense to order by angels; However here the shape is not necessarily convex and it will not work.
Do you want it to be a spatstat study region (of class owin) since you have the spatstat tag on there? In that case you can just use owin(poly=x) where x is your nx2 matrix (after loading the spatstat library of course). The rows in this matrix should contain the vertices of the polygon in the order that you want them connected (that's how R knows which point to connect with which). See help(owin) for more details.

python: sorting two lists of polygons for intersections

I have two big lists of polygons.
Using python, I want to take each polygon in list 1, and find the results of its geometric intersection with the polygons in list 2 (I'm using shapely to do this).
So for polygon i in list 1, there may be several polygons in list 2 that would intersect with it.
The problem is that both lists are big, and if I simply nest two loops and run the intersection command for every
possible pair of polygons, it takes a really long time. I'm not sure if preceding the intersection with a boolean test would speed this up significantly (e.g. if intersects: return intersection).
What would be a good way for me to sort or organize these two lists of polygons in order to make the intersections
more efficient? Is there a sorting algorithm that would be appropriate to this situation, and which I could make with python?
I am relatively new to programming, and have no background in discrete mathematics, so if you know an existing algorithm
that I should use, (which I assume exist for these kinds of situations), please link to or give some explanation that could assist me in actually
implementing it in python.
Also, if there's a better StackExchange site for this question, let me know. I feel like it kind of bridges general python programming, gis, and geometry, so I wasn't really sure.
Quadtrees are often used for the purpose of narrowing down the sets of polygons that need to be checked against each other - two polygons only need to be checked against each other if they both occupy at least one of the same regions in the quadtree. How deep you make your quadtree (in the case of polygons, as opposed to points) is up to you.
Even just dividing your space up to smaller constant-size areas would speed up the intersection detection (if your polygons are small and sparse enough). You make a grid and mark each polygon to belong to some cells in the grid. And then find cells that have more than one polygon in them and make the intersection calculations for those polygons only. This optimization is the easiest to code, but the most ineffective. The second easiest and more effective way would be quadtrees. Then there are BSP tres, KD trees, and BVH trees that are probably the most effective, but the hardest to code.
Edit:
Another optimization would be the following: find out the left-most and the right-most vertices of each polygon and put them in a list. Sort the list and then loop it somehow from left to right and easily find polygons whose bounding boxes' x coordinates overlap, and then make the intersection calculations for those polygons.

Categories