File Exchange

## Decimate Polygon

version 1.2 (27.7 KB) by

Simplify a 2D closed contour/polygon to within user-defined boundary offset tolerance

Updated

Editor's Note: This file was selected as MATLAB Central Pick of the Week

Have you ever worked with 2D boundary shape representations and noticed that the same shape can be represented with reasonable accuracy by a much smaller number of points? Did you ever need to reduce the complexity of a 2D shape while retaining as many salient features as possible? If so then DecimatePoly is the function you have been looking for.
In the attached .ZIP folder you will find the primary, self-contained function titled DecimatePoly. Additionally, in the Demos folder you will find three files demonstrating the capability and utility of this function.

DecimatePoly_demo1: Demonstrates how to use DecimatePoly to simplify a 2D contour extracted from a binary image. Three binary images are included as examples. One of the them is shown in the screenshot. This is the only demo that requires Image Processing Toolbox.

DecimatePoly_demo2: Demonstrates the effectiveness of DecimatePoly in simplifying complex shapes while retaining geometrically and visually salient features.

DecimatePoly_demo3: Demonstrates how DecimatePoly can be used to improve the run-time of an in-polygon test at the cost of minor misclassification error.

Erik Husby

### Erik Husby (view profile)

A Python implementation of this function may be found here:
https://github.com/husby036/DecimatePoly

Besides the small issue I've mentioned in my previous comments (which has been corrected in the translation), this is a direct translation from MATLAB to Python.

Erik Husby

### Erik Husby (view profile)

EDIT to my previous comment:

"Here, `t` is an N-by-1 array. `t<0` is only true when all elements of `t` are less than *zero*, so negative `t` values may pass. Similarly, `t` values greater than one may pass."

Erik Husby

### Erik Husby (view profile)

On lines 79 and 80 of DecimatePoly.m, I believe the following statements don't work as intended:

if t<0, t(t<0)=0; end %#ok<*BDSCI>
if t>1, t(t>1)=1; end

Here, `t` is an N-by-1 array. `t<0` is only true when all elements of `t` are less than one, so negative `t` values may pass. Similarly, `t` values greater than one may pass.

From the context of the two statements and the fact that we're calculating distances, I believe the author means to "clip" the values of `t` to the range (0, 1). If this is true, the conditionals should be taken out. Those two lines would be replaced as:

t(t<0)=0;
t(t>1)=1;

If I'm correct that this is an error, lines 175 and 176 display the same flawed logic. However, `t` is a 1-by-2 array within the `RecomputeErrors` function, so the statements are not erroneous but only highly redundant.

Georgios Gkantzounis

### Georgios Gkantzounis (view profile)

Very useful function. I would enjoy an option on deciding if you could actually save output statistics on variables instead of directly printing them (useful when I use it inside other routines).

Mattias Karlsson

### Mattias Karlsson (view profile)

Great function. It's nice that it is self contained too.

Catarina

### Catarina (view profile)

Hi Anton,
Your code was all I needed to solve problems in my codes due to too many useless points. Thanks a lot!

Bjorn Gustavsson

Andrii

Anton Semechko

### Anton Semechko (view profile)

http://www.insight-journal.org/browse/publication/829

Andrii

### Andrii (view profile)

It is for building extraction from aerial images. After segmentation process boundaries are zigzag-shaped with many intrusions. I would like to simplify polygons but to maintain their essential shape. It means buildings should have orthogonal angles and small extrusions should be cut off. So I am looking for some implementation algorithm in MatLab. Like this: http://img812.imageshack.us/img812/3284/exampleus.png

Anton Semechko

### Anton Semechko (view profile)

@ Andrii, what is it you want to achieve by orthogonalizing a set of input points? Could you clarify what this operation means or perhaps provide a reference?

Andrii

### Andrii (view profile)

Hi Anton. I have tried your function.
It works very well.
which can simplify and after orthogonalize input set of points?

 11 Feb 2018 1.2 Modified code according to suggestions made by Georgios Gkantzounis and Erik Husby 23 Jan 2012 1.1 updated description
##### MATLAB Release
MATLAB 8.5 (R2015a)