<p>I read the pixels of PNG files via <code>libpng</code> and creates an array of alpha channels (showing visible pixels for a monochromatic image).</p>
<p>int pixels[height][width];</p>
<pre><code>for( int i=0; i&lt;height; i++){
    for( int j=0; j&lt;width; j++){
        if(pixels[i][j]&gt;0){
            double x = (double)j;
            double y = (double)i;
        }
    }
}
</code></pre>
<p>This creates steps at curvatures. Anti-aliasing uses lighter pixels to visually create smoothness at the edges. Assuming the image has only black pixels, and the alpha channel is used for anti-aliasing only, I decided to move the points (x,y) according to the alpha value</p>
<pre><code>for( int i=0; i&lt;height; i++){
    for( int j=0; j&lt;width; j++){
        if(pixels[i][j]&gt;0){ // visible pixels
            double x = (double)j;
            double y = (double)i;

            if(pixels[i][j]&lt;255){ // anti-aliasing partly transparent pixels
                if(pixels[i][j-1]==0){  // left edge
                x+=(double)(255-pixels[i][j])/255 // move the point to the right
            }
            else if(pixels[i][j]&lt;255){ 
                if(pixels[i][j+1]==0){  // right edge
                x-=(double)(255-pixels[i][j])/255 // move the point to the left
            }
            // and the same for the vertical edges
        }
    }
}
</code></pre>
<p>This massively improved the edge smoothness, but I believe the logic I followed is totally wrong, as it cannot account for the curves. For example,</p>
<p><a href="https://i.sstatic.net/Kb7N2.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Kb7N2.png" alt="enter image description here" /></a></p>
<p>Programs like <code>potrace</code> do this job by transforming edges to paths, but here I just want to re-arrange the pixels points to form smooth edges (imagine we are creating the edges by dots instead of cubic pixels) rather than creating bezier curves.</p>
<p>What is the practical way to capture points at the edge of PNG pixels with smoothness?</p>

CC BY-SA 4.0
2022-02-24T19:20:20.087Z
It's a little hard to understand what kind of output you want this algorithm to have. I don't quite see what you're doing with the "move the point to the left or right" business. Maybe you could create a mock-up diagram showing what you would like the output to look like for the antialiased curve image you posted? Also, you might look into the Marching Squares algorithm, as it sounds related to what you're doing.
Nathan Reed
CC BY-SA 4.0
2022-02-24T19:28:12.463Z
@NathanReed I just want to restore the edge through a series of points. Each pixel gives the coordinates of its bottom-left corner. If we draw a circle for each pixel, we will have stairs-stepping. To have a smooth line, we have to move the points according to the anti-aliasing. I asked a follow-up question after some modification https://stackoverflow.com/questions/71251060/reversing-xiaolin-wus-anti-aliasing-algorithm-for-finding-the-edges it can better clarify the case.
Googlebot
<algorithm><computational-geometry><image-processing><antialiasing><pixels>
Algorithm for transforming PNG anti-aliasing to smooth paths
Googlebot
# Algorithm for transforming PNG anti-aliasing to smooth paths

(Asked by: username_0 on 2022-02-23)

I read the pixels of PNG files via `libpng` and creates an array of alpha channels (showing visible pixels for a monochromatic image).

int pixels[height][width];

```
for( int i=0; i<height; i++){
    for( int j=0; j<width; j++){
        if(pixels[i][j]>0){
            double x = (double)j;
            double y = (double)i;
        }
    }
}

```

This creates steps at curvatures. Anti-aliasing uses lighter pixels to visually create smoothness at the edges. Assuming the image has only black pixels, and the alpha channel is used for anti-aliasing only, I decided to move the points (x,y) according to the alpha value

```
for( int i=0; i<height; i++){
    for( int j=0; j<width; j++){
        if(pixels[i][j]>0){ // visible pixels
            double x = (double)j;
            double y = (double)i;

            if(pixels[i][j]<255){ // anti-aliasing partly transparent pixels
                if(pixels[i][j-1]==0){  // left edge
                x+=(double)(255-pixels[i][j])/255 // move the point to the right
            }
            else if(pixels[i][j]<255){ 
                if(pixels[i][j+1]==0){  // right edge
                x-=(double)(255-pixels[i][j])/255 // move the point to the left
            }
            // and the same for the vertical edges
        }
    }
}

```

This massively improved the edge smoothness, but I believe the logic I followed is totally wrong, as it cannot account for the curves. For example,

[![enter image description here](https://i.sstatic.net/Kb7N2.png)](https://i.sstatic.net/Kb7N2.png)

Programs like `potrace` do this job by transforming edges to paths, but here I just want to re-arrange the pixels points to form smooth edges (imagine we are creating the edges by dots instead of cubic pixels) rather than creating bezier curves.

What is the practical way to capture points at the edge of PNG pixels with smoothness?

**Tags:** algorithm, computational-geometry, image-processing, antialiasing, pixels

> **Comments:**
> **username_1**: It's a little hard to understand what kind of output you want this algorithm to have. I don't quite see what you're doing with the "move the point to the left or right" business. Maybe you could create a mock-up diagram showing what you would like the output to look like for the antialiased curve image you posted? Also, you might look into the Marching Squares algorithm, as it sounds related to what you're doing.  
> **username_0**: @username_1 I just want to restore the edge through a series of points. Each pixel gives the coordinates of its bottom-left corner. If we draw a circle for each pixel, we will have stairs-stepping. To have a smooth line, we have to move the points according to the anti-aliasing. I asked a follow-up question after some modification https://stackoverflow.com/questions/71251060/reversing-xiaolin-wus-anti-aliasing-algorithm-for-finding-the-edges it can better clarify the case.  

---

## 0 answers

