0

I have a very irregular shape in the form of vectors for x and y coordinates that I want to scale down. But I want the scaled-down shape to stay within the boundaries of the original shape. I thought just doing x_scaled = s * x and y_scaled = s * y would do the trick but this is what I get when I do that :-

enter image description here

As you can see the scaled-down shape crosses the boundaries of the original at multiple points. I would like for it to be completely contained inside the original shape. What I want would be something similar to this (not accurate, just a representation I tried making in photopea) -

enter image description here

The representation I made is not very good but I hope it helps get my point across. I just want the scaled-down shapeto be fully inside. And I am not sure how to do that. The language I am using is MATLAB but if I can just understand the logic I should be able to implement it.

Yuki.kuroshita
  • 759
  • 9
  • 29
  • This is not a simple problem. You are asking for an inward offset polygon, not a scaling. You can find this question asked [here][1] [1]: https://stackoverflow.com/questions/1109536/an-algorithm-for-inflating-deflating-offsetting-buffering-polygons – Mikael Öhman Aug 11 '23 at 12:17
  • You cannot do this with scaling and translation alone. Offsetting would get you a polygon that is inside the original one, but it would have a different shape, and could even be cut into multiple polygons. I like using the Clipper library in C++ for this. Someone has already linked this library to MATLAB: https://www.mathworks.com/matlabcentral/fileexchange/61329-new-polygon-clipping-and-offsetting – Cris Luengo Aug 11 '23 at 12:48
  • 1
    [`imerode`](https://www.mathworks.com/help/images/ref/imerode.html) on the interior of the shape would probably work. – beaker Aug 11 '23 at 15:35
  • I did it with imerode in the end – Yuki.kuroshita Aug 15 '23 at 05:03

1 Answers1

0

1. Use bwmorph to capture 1-pixel depth surface of the shape to shrink, with for instance

A2 = bwmorph(A1,'remove');

A has to be [0 1] single layer figure, not an RGB picture.

2. Get the coordinates [x1 y1] or linear index n1 to all white pixels in A2

[x1 y1] are coordinates for all surface pixels.

3. Remove [x1 y1] pixels in A1

4. Repeat to reach desired depth

Eventually, if 'pealing' deep enough the obtained figure reaches the same result as using command bwskel that skeletonizes input figures.

John BG
  • 690
  • 4
  • 12