Danny Dan
Danny Dan

Reputation: 25

Image Masking pixel divition

This is the code for masking an image (see).

Why does the mask need to be divided by 255? Isn't img0 = mask * img a pixelwise multiplication, so in the middle all the values will be kept (if multiplying by 1 and not divide by 255)

img = np.array(Image.open('input.jpg'))
ones = np.ones((img.shape[0] // 2, img.shape[1] // 2, 3))
zeros = np.zeros(((img.shape[0] // 4, img.shape[1] // 4, 3)))
zeros_mid = np.zeros(((img.shape[0] // 2, img.shape[1] // 4, 3)))
up = np.concatenate((zeros, zeros, zeros, zeros), axis=1)
middle = np.concatenate((zeros_mid, ones, zeros_mid), axis=1)
down = np.concatenate((zeros, zeros, zeros, zeros), axis=1)
mask = np.concatenate((up, middle, down), axis=0)
mask = mask / 255
img0 = mask * img
fig = plt.figure(figsize=(10, 10))
fig.add_subplot(1, 2, 1)
plt.imshow(img)
fig.add_subplot(1, 2, 2)
plt.imshow(img0)

Upvotes: 1

Views: 32

Answers (0)

Related Questions