import numpy as np import cv2
def Reduice(img): Result=[] for i in range(1,H-1): for ii in range(1,W-1): pix = [list(img[i-1][ii]), list(img[i+1][ii]), list(img[i][ii-1]), list(img[i][ii+1]), list(img[i-1][ii-1]), list(img[i+1][ii-1]), list(img[i-1][ii+1]), list(img[i+1][ii+1]), ] if pix ==[[0,0,0,], [0,0,0,], [0,0,0,], [0,0,0,], [0,0,0,], [0,0,0,], [0,0,0,], [0,0,0,]]: Result +=[[i,ii]] return Result
img = cv2.imread('111.png') H = len(img) W = len(img[0])
for i in range(5): Result = Reduice(img) img2 = np.array(img) img2[img2!=254]=254 for i in Result: img2[i[0],i[1]]=img[i[0],i[1]] img =np.array(img2)
cv2.imwrite('5.png',img)
Num = 5 for i in range(5): Num +=1 Result = Reduice(img) img2 = np.array(img) img2[img2!=254]=254 for i in Result: img2[i[0],i[1]]=img[i[0],i[1]] img =np.array(img2) cv2.imwrite(str(Num)+'.png',img)
def View(img): while(True): cv2.imshow('image',img) if cv2.waitKey(1) & 0xFF == ord('q'): cv2.destroyAllWindows() break
|