# This Python code uses OpenCV to create a circular disk at the centre of an # image. The radius of the disk is the minimum of width and height of image. #-------------- ----------------- ---------------- ----------------- ---------- # This code uses NumPy to change the red-green-blue channels of the image. import numpy as np import cv2 image = 'image.png' out = 'maskedImg.png' # Read image as coloured channels: w x h x 3 uint8 img = cv2.imread(image) # Get height and width of the image, define centre and radius of the disk h = img.shape[0] #Note shape dimension is in [h, w] format w = img.shape[1] xc = h//2 yc = w//2 rad = min(xc, yc) # Get colour channels, RGB of the image. Note OpenCV stores image in BGR format b_chan = img[:, :, 0] g_chan = img[:, :, 1] r_chan = img[:, :, 2] # Create column vectors to the size of height and width of the image x = np.arange(0, h) #Rank '1' array y = np.arange(0, w) #Rank '1' array u = np.array(x).reshape(-1, 1) #x[np.newaxis, :] - make column vector v = np.array(y).reshape(1, -1) #y[:, np.newaxis] - make column vector # Identifypixels outside the disk, ** operator works on each element of vector mask = ((u[:]-xc)**2 + (v[:]-yc)**2) > rad**2 # Set the pixels of all 3 channels outside disk to white (255, 255, 255) b_chan[mask] = 255 g_chan[mask] = 255 r_chan[mask] = 255 # Merge the RGB channels into a desired image result = cv2.merge([b_chan, g_chan, r_chan]) # Save the output image cv2.imwrite(out, result)