Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9139689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:25:13+00:00 2026-06-17T09:25:13+00:00

I want to transform a 2d numpy array into a polygon. Performance is very

  • 0

I want to transform a 2d numpy array into a polygon. Performance is very important for me, but I want to avoid making a C extension.
A binary outline image can be made with erosion. Then I found this. It was too slow and didn’t cope with the spikes created by erosion sometimes.
A spike:

000100
000100
000100
111011

My first try:

mat = mat.copy() != 0
mat = mat - scipy.ndimage.binary_erosion(mat)

vertices = np.argwhere(mat)
minx = vertices.min(axis=0)[0]
maxx = vertices.max(axis=0)[0]

vertices_sorted = {}
for x in xrange(minx - 1, maxx + 2):
    vertices_sorted[x] = []

for vertex in vertices:
    vertices_sorted[vertex[0]].append(vertex[1])

vertex_loop = [(minx, vertices_sorted[minx][0])]
while True:
    x, y = vertex_loop[-1]
    for column, row in ((x, y + 1), (x, y - 1), 
    (x + 1, y), (x + 1, y + 1), (x + 1, y - 1),
    (x - 1, y), (x - 1, y + 1), (x - 1, y - 1)):
        if row in vertices_sorted[column]:
            vertices_sorted[column].remove(row)
            vertex_loop.append((column, row))
            break
    else:
        vertex_loop.pop()

    if vertex_loop[-1] == vertex_loop[0]:
        break
return vertex_loop[:-1]

It works most of the time, but it isn’t fast enough.
My second code works only rarely, but I havent fixed it, because it is multiple times slower than the first one:

mat = mat.copy() != 0
mat = mat - scipy.ndimage.binary_erosion(mat)

xs, ys = np.nonzero(mat)
ys = np.ma.array(ys)

vertex_loop = [(xs[0], ys[0])]
ys[0] = np.ma.masked
while True:
    x, y = vertex_loop[-1]
    start = np.searchsorted(xs, x-1, side="left")
    end = np.searchsorted(xs, x+1, side="right")

    for i in xrange(start, end):
        if ys[i] == y or ys[i] == y + 1 or ys[i] == y - 1:
            vertex_loop.append((xs[i], ys[i]))
            ys[i] = np.ma.masked
            break
    else:
        if np.all(ys.mask):
            break
        else:
            vertex_loop.pop()
return vertex_loop

How can I improve the speed further?

EDIT: It seems that numpy masked arrays are extremely slow. This implementation is almost exactly as fast as the first one:

#import time
#t1 = time.time()
mat = mat.copy() != 0
mat = mat - scipy.ndimage.binary_erosion(mat)

xs, ys = np.nonzero(mat)
#t2 = time.time()
minx = xs[0]
maxx = xs[-1]

# Ketju pakosti käy läpi kaikki rivit minx:n ja maxx:n välissä, sillä se ON KETJU
xlist = range(minx - 1, maxx + 2)
# starts ja ends ovat dictit jotka kertovat missä slicessä x == key
tmp = np.searchsorted(xs, xlist, side="left")
starts = dict(zip(xlist, tmp))
tmp = np.searchsorted(xs, xlist, side="right")
ends = dict(zip(xlist, tmp))

unused = np.ones(len(xs), dtype=np.bool)
#t3 = time.time()
vertex_loop = [(xs[0], ys[0])]
unused[0] = 0
count = 0
while True:
    count += 1
    x, y = vertex_loop[-1]
    for i in xrange(starts[x - 1], ends[x + 1]):
        row = ys[i]
        if unused[i] and (row == y or row == y + 1 or row == y - 1):
            vertex_loop.append((xs[i], row))
            unused[i] = 0
            break
    else:
        if abs(x - xs[0]) <= 1 and abs(y - ys[0]) <= 1:
            break
        else:
            vertex_loop.pop()
#t4 = time.time()
#print abs(t1-t2)*1000, abs(t2-t3)*1000, abs(t3-t4)*1000
return vertex_loop

I wonder if there is an easy way to do this with scipy that I’ve failed to stumble upon.

EDIT2: In pygame there is a mask object that does just what I need in 0.025 ms while my solution requires 35 ms and find_contours that I found on the internet somewhere does it in 4-5 ms. I’m going to modify the source code for pygame.mask.outline to use a numpy array and post it here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:25:14+00:00Added an answer on June 17, 2026 at 9:25 am

    Here it is: an extremely fast way of getting the outline of a binary numpy array.

    outline.py:

    from scipy.weave import inline, converters
    
    _code = open("outline.c", "r").read()
    
    def outline(data, every):
        width, height = data.shape
        return inline(_code, ['data', 'width', 'height', 'every'], type_converters=converters.blitz)
    

    outline.c:

    /*
    Modifioitu pygame.mask.Mask.outline
    Input: data, width, height, every
    */
    
    PyObject *plist, *value;
    int x, y, e, firstx, firsty, secx, secy, currx, curry, nextx, nexty, n;
    int a[14], b[14];
    a[0] = a[1] = a[7] = a[8] = a[9] = b[1] = b[2] = b[3] = b[9] = b[10] = b[11]= 1;
    a[2] = a[6] = a[10] = b[4] = b[0] = b[12] = b[8] = 0;
    a[3] = a[4] = a[5] = a[11] = a[12] = a[13] = b[5] = b[6] = b[7] = b[13] = -1;
    
    plist = NULL;
    plist = PyList_New (0);
    /*if (!plist) En ymmärrä mihin tätä tarvii
        return NULL;*/
    
    every = 1;
    n = firstx = firsty = secx = x = 0;
    
    /*if(!PyArg_ParseTuple(args, "|i", &every)) {
        return NULL;
    }
    
     by copying to a new, larger mask, we avoid having to check if we are at
       a border pixel every time.  
    bitmask_draw(m, c, 1, 1); */
    
    e = every;
    
    /* find the first set pixel in the mask */
    for (y = 1; y < height-1; y++) {
        for (x = 1; x < width-1; x++) {
            if (data(x, y)) {
                 firstx = x;
                 firsty = y;
                 value = Py_BuildValue("(ii)", x-1, y-1);
                 PyList_Append(plist, value);
                 Py_DECREF(value);
                 break;
            }
        }
        if (data(x, y))
            break;
    }
    
    
    
    /* covers the mask having zero pixels or only the final pixel
    Pikseleitä on ainakin kymmenen
    if ((x == width-1) && (y == height-1)) {
        return plist;
    }        */
    
    /* check just the first pixel for neighbors */
    for (n = 0;n < 8;n++) {
        if (data(x+a[n], y+b[n])) {
            currx = secx = x+a[n];
            curry = secy = y+b[n];
            e--;
            if (!e) {
                e = every;
                value = Py_BuildValue("(ii)", secx-1, secy-1);
                PyList_Append(plist, value);
                Py_DECREF(value);
            }
            break;
        }
    }       
    
    /* if there are no neighbors, return
    Pikseleitä on ainakin kymmenen
    if (!secx) {
        return plist;
    }*/
    
    /* the outline tracing loop */
    for (;;) {
        /* look around the pixel, it has to have a neighbor */
        for (n = (n + 6) & 7;;n++) {
            if (data(currx+a[n], curry+b[n])) {
                nextx = currx+a[n];
                nexty = curry+b[n];
                e--;
                if (!e) {
                    e = every;
                    if ((curry == firsty && currx == firstx) && (secx == nextx && secy == nexty)) {
                        break;
                    }
                    value = Py_BuildValue("(ii)", nextx-1, nexty-1);
                    PyList_Append(plist, value);
                    Py_DECREF(value);
                }
                break;
            }
        }
        /* if we are back at the first pixel, and the next one will be the
           second one we visited, we are done */
        if ((curry == firsty && currx == firstx) && (secx == nextx && secy == nexty)) {
            break;
        }
    
        curry = nexty;
        currx = nextx;
    }
    
    return_val = plist;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got an array of channels that I want to transform into a single
I want to transform the 'values' array created by xml_parse_into_struct() into a bunch of
I'm trying to transform each element of a numpy array into an array itself
i have an image (saved as numpy-array) and i want to transform it with
I want to transform a List[Option[T]] into a Option[List[T]] . The signature type of
I don't want to transform the ENTIRE context. I'm making a game with Quartz,
This is my Xml file.i want to transform this xml file into another customized
I want to transform list to a string, but it contains integers. It works
i want to transform some xml into HTML that has the following format: <TR><TD>
Hey ,Guys ,I have a question. I want to transform the array. [[1, [-1,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.