0

I have an input binary (black and white) image containing two concentric ellipses. The background and the internal ellipse are black while the external ellipse is white. My goal is to change all pixels of the internal ellipse from black to white.

This is a simple code snippet to generate two concentric ellipses:

import cv2
import numpy as np

# costants
min_height, max_height = 15, 60 
min_width, max_width = 15, 60 
min_rot, max_rot = 0, 360
center = (max_width*2, max_height*2) 

# empty image
image = np.zeros((max_width*4, max_height*4), dtype=np.uint8)

# randomly selecting angle, horizontal and vertical compression
width = int(np.random.uniform(min_width, max_width))
height = int(np.random.uniform(min_height, max_height))
angle = np.random.uniform(min_rot, max_rot)

# draw external ellipse
image = cv2.ellipse(image, center, (width, height), angle, 0, 360, color=255, thickness=-1)

# draw internal ellipse
image = cv2.ellipse(image, center, (width//2, height//2), angle, 0, 360, color=0, thickness=-1)

cv2.imshow('concentric ellipses', image)
cv2.waitKey(0)

My goal is to change all pixels of the internal ellipse from black to white. Like this: example

A Naive approach could be a loop for on each image but this seems very slow to me:

def fill_image(img):
    import numpy as np
    
    num_rows = img.shape[0]


    for row in range(num_rows):

        # get col index for white pixels (external ellipse)
        white_pixels = np.where(img[row, :] == 255)[0]

        # assign white pixels
        start_px, end_px = min(white_pixels), max(white_pixels)
        img[row, start_px:end_px] = 255

return img

Which is the most pythonic and efficient way to do that?

0 Answers0