maximum square inside a contour
Hi friends, Someone knows how can I find a maximum square inside a contour, giving as parameter the centroid point? I need whole square area fall inside the contour. Any advice? Thanks in advance, Jaime
Hi Jaime, I don't have a good solution to your problem, which seems a tricky one from a theoretical point of view https://en.wikipedia.org/wiki/Inscribed_square_problem (however we can be relieved since a solution always exists for a polygon :-) However, if you have a 2D contour (are you in 2D?), then the square can be parametrized by an angle and a radius (if you know its radius). A brute-force search would do, since it's easy to find an upper boundary for the radius (take the bounding box of scipy.ndimage). You just need a function that tells you whether one edge of a square lies within the contour, maybe you can do this by rasterizing the edge using skimage.draw? Only small bits here, if I have better ideas I'll get back to you. Cheers, Emma On Mon, Apr 25, 2016 at 01:30:21PM -0700, Jaime Lopez Carvajal wrote:
Hi friends,
Someone knows how can I find a maximum square inside a contour, giving as parameter the centroid point? I need whole square area fall inside the contour. Any advice?
Thanks in advance, Jaime
I meant "parametrized by an angle and a radius, if you know its centroid". In the image you sent, the edges of the square are parallel to the edges of the images. Are you only considering such squares, or do you also allow a rotation of the square? Best, Emma On Mon, Apr 25, 2016 at 11:46:14PM +0200, Emmanuelle Gouillart wrote:
Hi Jaime,
I don't have a good solution to your problem, which seems a tricky one from a theoretical point of view https://en.wikipedia.org/wiki/Inscribed_square_problem (however we can be relieved since a solution always exists for a polygon :-)
However, if you have a 2D contour (are you in 2D?), then the square can be parametrized by an angle and a radius (if you know its radius). A brute-force search would do, since it's easy to find an upper boundary for the radius (take the bounding box of scipy.ndimage). You just need a function that tells you whether one edge of a square lies within the contour, maybe you can do this by rasterizing the edge using skimage.draw?
Only small bits here, if I have better ideas I'll get back to you.
Cheers, Emma
On Mon, Apr 25, 2016 at 01:30:21PM -0700, Jaime Lopez Carvajal wrote:
Hi friends,
Someone knows how can I find a maximum square inside a contour, giving as parameter the centroid point? I need whole square area fall inside the contour. Any advice?
Thanks in advance, Jaime
Hi Emma, Yeah, I know the square's centroid, and I am working in 2D. I need my square fall inside the contour's region, and also the square must be not rotated, just like the green square in the image. Thanks, Jaime On Tuesday, April 26, 2016 at 2:39:34 AM UTC-4, Emmanuelle Gouillart wrote:
I meant "parametrized by an angle and a radius, if you know its centroid".
In the image you sent, the edges of the square are parallel to the edges of the images. Are you only considering such squares, or do you also allow a rotation of the square?
Best, Emma
On Mon, Apr 25, 2016 at 11:46:14PM +0200, Emmanuelle Gouillart wrote:
Hi Jaime,
I don't have a good solution to your problem, which seems a tricky one from a theoretical point of view https://en.wikipedia.org/wiki/Inscribed_square_problem (however we can be relieved since a solution always exists for a polygon :-)
However, if you have a 2D contour (are you in 2D?), then the square can be parametrized by an angle and a radius (if you know its radius). A brute-force search would do, since it's easy to find an upper boundary for the radius (take the bounding box of scipy.ndimage). You just need a function that tells you whether one edge of a square lies within the contour, maybe you can do this by rasterizing the edge using skimage.draw?
Only small bits here, if I have better ideas I'll get back to you.
Cheers, Emma
On Mon, Apr 25, 2016 at 01:30:21PM -0700, Jaime Lopez Carvajal wrote:
Hi friends,
Someone knows how can I find a maximum square inside a contour, giving as parameter the centroid point? I need whole square area fall inside the contour. Any advice?
Thanks in advance, Jaime
How about turning problem into an optimisation problem ? You have the green square, could be considered as initial maximum square dimensions. I believe it can be done by varying the position of the square along a path that maximises the square dimensions while all px values remain at 0 (ubyte image). The path can be chosen depending on the polygon properties, if it is a platonic solid you use mass centre or the positions along the symmetry axis (example above). Else, a more expensive approach: pick random positions (pos) and keep same size. Check if all px==0: increase size; else: continue to next pos. I think you should also look at the flood fill algorithm, could also be helpful. Hope this helps. Cheers, Georges On Monday, 25 April 2016 13:30:21 UTC-7, Jaime Lopez Carvajal wrote:
Hi friends,
Someone knows how can I find a maximum square inside a contour, giving as parameter the centroid point? I need whole square area fall inside the contour. Any advice?
Thanks in advance, Jaime
Hi Georges, I really don't have the green square, it is what I want to get. I just put it there for illustration purpouses. But you give an interesting point of view. I will start from centroid, checking in every step if every square's corner is inside the region (as you said, 0 value) it they are inside, increase their coordinates respectively, if not, take coordinates from the previous step to build the square. I will give it a try, and will say how it works, Thanks, Jaime On Thursday, April 28, 2016 at 12:46:42 PM UTC-4, GeorgesVis wrote:
How about turning problem into an optimisation problem ? You have the green square, could be considered as initial maximum square dimensions.
I believe it can be done by varying the position of the square along a path that maximises the square dimensions while all px values remain at 0 (ubyte image). The path can be chosen depending on the polygon properties, if it is a platonic solid you use mass centre or the positions along the symmetry axis (example above).
Else, a more expensive approach: pick random positions (pos) and keep same size. Check if all px==0: increase size; else: continue to next pos. I think you should also look at the flood fill algorithm, could also be helpful.
Hope this helps. Cheers, Georges
On Monday, 25 April 2016 13:30:21 UTC-7, Jaime Lopez Carvajal wrote:
Hi friends,
Someone knows how can I find a maximum square inside a contour, giving as parameter the centroid point? I need whole square area fall inside the contour. Any advice?
Thanks in advance, Jaime
If I understand it correctly -- here's one approach using morphological operators. 1. Take centroid as seed point (new image). 2. Dilate it with 3x3 structuring element. 3. Check if union of seed image and contour image has created new pixels (i,e to check if square touched the contour) 4. If not, repeat step 1-2-3 till 3 is met. That should give you largest possible square? I used similar approach for flooding problem and maze solving. https://www.youtube.com/watch?v=jhL8uELbVIM Regards, Pratap On Thursday, April 28, 2016 at 11:01:59 PM UTC+5:30, Jaime Lopez Carvajal wrote:
Hi Georges,
I really don't have the green square, it is what I want to get. I just put it there for illustration purpouses.
But you give an interesting point of view. I will start from centroid, checking in every step if every square's corner is inside the region (as you said, 0 value) it they are inside, increase their coordinates respectively, if not, take coordinates from the previous step to build the square.
I will give it a try, and will say how it works,
Thanks, Jaime
On Thursday, April 28, 2016 at 12:46:42 PM UTC-4, GeorgesVis wrote:
How about turning problem into an optimisation problem ? You have the green square, could be considered as initial maximum square dimensions.
I believe it can be done by varying the position of the square along a path that maximises the square dimensions while all px values remain at 0 (ubyte image). The path can be chosen depending on the polygon properties, if it is a platonic solid you use mass centre or the positions along the symmetry axis (example above).
Else, a more expensive approach: pick random positions (pos) and keep same size. Check if all px==0: increase size; else: continue to next pos. I think you should also look at the flood fill algorithm, could also be helpful.
Hope this helps. Cheers, Georges
On Monday, 25 April 2016 13:30:21 UTC-7, Jaime Lopez Carvajal wrote:
Hi friends,
Someone knows how can I find a maximum square inside a contour, giving as parameter the centroid point? I need whole square area fall inside the contour. Any advice?
Thanks in advance, Jaime
participants (4)
-
Emmanuelle Gouillart
-
GeorgesVis
-
Jaime Lopez Carvajal
-
Pratap Vardhan