1

I am attempting to produce a python implementation of a certain superpixel algorithm. To do so, I would need a variable sk which stores the coordinates of each pixel in different superpixels.

To be more specific, my current approach is sk = np.empty([N, N, 2], dtype=int), in which N is the number of total pixels; the first dimension refers to a specific superpixel label; the second dimension refers to the index of a specific pixel; the last "2" is for x and y coordinates for that pixel. So sk[3, 2] = [10, 10] means that the 2nd pixel in superpixel 3 is the pixel at (10, 10). I am also using numba for enhancing numpy array calculations, so speed is a major concern here.

As you can see, the above sk would take around 7TB if I were to do so for a 1080p image. However, I am currently using Mac, and OS doesn't allocate memory for sk until I try to access it. And thanks to paging, only the pages associated with sections I wish to access is affected by my modifications. I am wondering if numpy array is the best data structure I could use for this, or if perhaps there are better alternatives out there.

My current approach:

sk = np.empty([N, N, 2], dtype=int)
inside_index = [0]  # inside_index[x] is the number of pixels in superpixel x

I have tried scipy.sparse.csr_matrix and scipy.sparse.lil_matrix. They're fixed 2-dimensional, so I attempted to store coordinates for a single superpixel in one row like this: [x1 y1 x2 y2 x3 y3...]. Issues are, the first one is created for matrix calculations, so its inefficient when it comes to changing the sparity of it (adding stuff in); the second one is not meant for accessing the data within, so getting a specific value from sk is once again inefficient. Lists are simply too slow for other complex, vector-based calculations.

Chasname
  • 11
  • 2
  • Presumable the number `n` of superpixels should be much smaller than the total number `N` of pixels, and the maximum number `k` of pixels per superpixel should also be much smaller than `N`, so the array should only have size `(k, n, 2)`, not `(N, N, 2)`. – Stef Jul 04 '23 at 11:20
  • 1
    @Stef Thanks for your response. This is indeed one of my concerns. Since I am not using methods that would give an fixed shape/size/number to each superpixel like SLIC, I cannot detect beforehand 'k' and 'n'. Instead, my method is to iterate through every single pixel once, and the initial clustering stage may result in 100 - 1000 times more superpixel than the intended 'k'. Then, those will be merged in the merging stage to meet the requirements (which is why setting the dimensions for 'sk' is such an issue). – Chasname Jul 05 '23 at 02:01
  • Is it a mixed case of reading/writing data, or is it sequential (first the creation and afterwards only reading data from it)? – max9111 Jul 05 '23 at 18:19
  • @max9111 There are multiple instances of reading, writing, merging and deleting throughout the program (coordinates are merged together in the merging section). – Chasname Jul 06 '23 at 02:00

0 Answers0