jeudi 10 décembre 2020

'draw' a random rhombus (diamond) on a numpy array (testing harris corner detection)

I'm trying to create a random test for a "harris_corner_detector" function implementation (VERY GENERALLY AND SLIGHTLY INCORRECTLY: a function that finds corners in an image) In the test, I want to create random simple shapes in a binary numpy matrix (it's easy to know the coordinates of their corners) (e.g rectangles, triangles, rhombus (diamond) etc...) and check if the harris implementation finds the correct corners.

I already implemented a function that randomly 'draws' an axis parallel rectangle, but i can't find an efficient way to do so when it comes to shapes that are not parallel to the axes.

To create a random rectangle, I randomly choose a starting point and an ending point on both axes and I change the value of all of the cells within those bounds like so:

getting the random coords:

    def _get_random_coords(self, start, end):
        x_start, y_start = np.random.randint(start, end, 2)
        x_end = np.random.randint(x_start + 7, end + 20)
        y_end = np.random.randint(y_start + 7, end + 20)
        return (x_start, x_end, y_start, y_end)

drawing the random rectangle (values are 255 for the background and 0 for the shape):

mat = np.ones((1024, 1024)) * 255
mat[x_start: x_end, y_start: y_end] = np.zeros((x_end - x_start, y_end - y_start))

but when it comes to drawing a diamond shape efficiently I'm at a loss. All I can think about is to run a loop that creates the diamond like so:

    def _get_rhombus(self, size):
        rhombus = []
        for i in range(size):
            rhombus.append(np.zeros(i+1))
        for i in range(size - 1, 0, -1):
            rhombus.append(np.zeros(i))
        return np.array(rhombus)

and then another loop to add it to the larger matrix. But this method is highly inefficient when it comes to testing (as I'll draw hundreds of them, some of them might be huge).

Any better ideas out there? Alternatively - is there a better way to test this?

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire