图片修补指的是将一幅被破坏的图片还原成本来的面目。下面这个实例尝试了先将图片污染,再修补的做法(大规模的破坏没有尝试)。有兴趣的同学可以尝试一下其他的破坏尺寸,或者尝试随机破坏。
# 对于图片修补,关键是找到破坏的像素点,并记录其坐标
import cv2
import numpy as np
img = cv2.imread('OpenCV\\image0.JPG',1)
# 对原图像进行破坏,这里是在图中画十字
# 先是竖着画
for i in range(200,300):
# 复制三次,以表示占用三个像素
img[i,200] = (255,255,255)
img[i,200-1] = (255,255,255)
img[i,200+1] = (255,255,255)
# 再横着画
for i in range(150,250):
img[250,i] = (255,255,255)
img[250-1,i] = (255,255,255)
img[250+1,i] = (255,255,255)
cv2.imwrite('OpenCV\\damage.jpg',img)
# 读取已破坏图片
img01 = cv2.imread('OpenCV\\damage.jpg',1)
imgInfo = img01.shape
height = imgInfo[0]
width = imgInfo[1]
paint = np.zeros((height,width,1),np.uint8)
# 描绘出图片中的破坏部分
for i in range(200,300):
paint[i,200] = 255
paint[i,200-1] = 255
paint[i,200+1] = 255
for i in range(150,250):
paint[250,i] = 255
paint[250-1,i] = 255
paint[250+1,i] = 255
# 调用inpaint()方法修补图像
imgDst = cv2.inpaint(img, paint, 3, cv2.INPAINT_TELEA)
cv2.imshow('damage',img)
cv2.imshow('paint',paint)
cv2.imshow('imgDst',imgDst)
cv2.waitKey(0)
cv2.destroyAllWindows()