0

input is a list of observations, every observation is fixed size set of elipses (every elipse is represented by 7 parameters).

output is a list of images, one image for one observation, we are basically putting elipses from observation to completely white image. If few elipses overlap then we are putting mean value of rgb values.

n, m = size of image in pixels, image is represented as (n, m, 3) numpy array (3 because of RGB coding)
N = number of elipses in every individual observation

xx, yy = np.mgrid[:n, :m]

def elipses_population_to_img_population(elipses_population):
        population_size = elipses_population.shape[0]
        img_population = np.empty((population_size, n, m, 3))
        for j in range(population_size):
            imarray = np.empty((N, n, m, 3))
            imarray.fill(np.nan)
            for i in range(N):
                x = elipses_population[j, i, 0]
                y = elipses_population[j, i, 1]
                R = elipses_population[j, i, 2]
                G = elipses_population[j, i, 3]
                B = elipses_population[j, i, 4]
                a = elipses_population[j, i, 5]
                b = elipses_population[j, i, 6]
                xx_centered = xx - x
                yy_centered = yy - y
                elipse = (xx_centered / a)**2 + (yy_centered / b)**2 < 1
                imarray[i, elipse, :] = np.array([R, G, B])
            means_img = np.nanmean(imarray, axis=0)
            means_img = np.nan_to_num(means_img, nan=255)
            img_population[j, :, :, :] = means_img
        return img_population

Code is working correctly, but i am looking for optimization advices. I am running it many times in my code so every small improve would be helpful.

Vizik
  • 1
  • 2
  • 1
    I think this question fits more [Code Review](https://codereview.stackexchange.com/) website. Be sure to check their [help center](https://codereview.stackexchange.com/help) for more information. – Pawel Kam Jan 22 '23 at 23:50
  • The algorithm is clearly not efficient (an not practically usable) : it runs in `O(m² n²)` (that is `O(n**4)` for squared image with a relatively large `n`). It looks like you should read https://en.wikipedia.org/wiki/Nearest_neighbor_search . – Jérôme Richard Jan 23 '23 at 09:06
  • @PawelKam we're [a bit more lenient](https://stackoverflow.com/a/52605202/4427777) in the `numpy` tag as so much best practice for the package is about speeding up calculation. – Daniel F Jan 23 '23 at 09:52
  • @JérômeRichard It actually runs O(population_size * N * m * n) with relatively small N (let's say it's O(n**3)). – Vizik Jan 24 '23 at 13:21
  • @JérômeRichard I already knew what KNN problem but i have no idea how to use this knowledge here. For every ,,j" and every ,,i" i want to get all points from the plane which are inside elipse. So the number of these points may vary. – Vizik Jan 24 '23 at 13:29

0 Answers0