#๐Ÿ”’ Fast fill rectangles in cv2 or np

6 messages ยท Page 1 of 1 (latest)

fickle geode
#

I want to use find a path in an image. The issue is that my "player" requires more than 1 pixel of space to fit through. So I figured out, that I can make obstacles bigger. If my player requires about 15 pixels to move through, I can replace an wall pixel with rectangle of size 30 (15 on left side, 15 on right side). But the issue is that following code take about 8s to run.

img = cv2.imread("playground/radar2.png", cv2.IMREAD_GRAYSCALE)
output = np.ones(img.shape, dtype=np.uint8) * 255
for y in range(img.shape[0]):
    for x in range(img.shape[1]):
        if img[y, x] == 255:
            cv2.rectangle(output, (x-15, y-15), (x+15, y+15), 255, -1)

I created second version using numpy, but it still takes about 100 ms.

img = cv2.imread("playground/radar2.png", cv2.IMREAD_GRAYSCALE)
output = np.ones(img.shape, dtype=np.uint8) * 255
positions = np.where(img == 255)
for x, y in zip(positions[1], positions[0]):
    size = 15
    output[y-size:y+size, x-size:x+size] = 0    

Is there a function in np or cv2 which I'm missing?

fallen idolBOT
#

@fickle geode

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

jovial trellis
fickle geode
#

okay thants, that is probably the best solution ๐Ÿ™‚

fallen idolBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.