我有一个二进制512x512像素图像,我想将其划分为4 x 4像素的块,并计算块中黑色像素的数量。如果块中黑色像素的总和是偶数,则为相应的块指定值0.否则,该值为1.我该怎么做?
答案 0 :(得分:0)
您可以尝试将图片加载为numpy数组,然后为x维度和y维度创建两个循环,偏移量为4.这是我的建议:
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