I have a binary image that I want to divide into 4 x 4 pixels of blocks and counts the number the number of black colour pixel in a block. If the sum of black colour pixel in a block is even, the corresponding block is assigned a value of 0. Otherwise, the value is 1. After that, save/write it into txt file so I can see the result.
I have tried with the code but got stuck
import matplotlib.pyplot as plt
import numpy as np
image = plt.imread('myplot1.png')
image = np.array(image)
image = image[:,:,1] #if RGB
print(image.shape)
for x in np.arange(0,image.shape[0]):
for y in np.arange(image.shape[1]):
if x+4 < image.shape[0] and y+4 < image.shape[1]:
sum = np.sum(image[x:x+4,y:y+4])
if sum > 4:
image[x:x + 4, y:y + 4] = 1
elif sum < 4:
image[x:x + 4, y:y + 4] = 0