我有一个二进制图像,我想分成4 x 4像素的块,并计算一个块中黑色像素的数量.如果块中黑色像素的总和是偶数,则为相应的块指定值0.否则,该值为1.之后,将其保存/写入txt文件,以便我可以看到结果.
我已尝试使用代码,但卡住了
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
Run Code Online (Sandbox Code Playgroud)