Home > Software engineering >  Using cv2 to create a circular graphic
Using cv2 to create a circular graphic

Time:01-15

My code is intended to build a shape with a random assortment of pixels inside. This is my code at the moment.

def make_patch(width, height):
A = random.random((10,5,3))
A = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
A([[0, 0, 1, 0, 0],
   [1, 1, 1, 1, 1],
   [1, 1, 1, 1, 1],
   [1, 1, 1, 1, 1],
   [0, 0, 1, 0, 0]], dtype=uint8)

return A
A = make_patch(5,5)
imshow(A)
show()

Getting the error

NameError: name 'uint8' is not defined

CodePudding user response:

Graphic sprites, textures (or patches as you might call them) are inherently rectangular shapes for efficiency reasons.

If you want to make them appear like a circle (or any other shape) you need to apply a mask (also called stencil in some contexts like rendering API's), or set the alpha channel (transparency) to 0.

If you want to know which pixels lay inside a circle, you can use the algebraic property of the distance to the center being less than the radius (or compare the square of the distance with the square of the radius for efficiency).

CodePudding user response:

>>> cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
array([[0, 0, 1, 0, 0],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [0, 0, 1, 0, 0]], dtype=uint8)
  •  Tags:  
  • Related